feat(monitor): add UserProfile + MonitorMetric catalog + Indicator.seriesKey
数据层(spec 2026-05-26):
- UserProfile @Model:核心 4 项 + 健康背景 + 用药,SwiftData 单例(loadOrCreate)
- Indicator 加 seriesKey: String?,标识长期指标分组('bp.systolic' 等)
- MonitorMetric enum 8 case:血压(2 field 拆 2 Indicator)/ 空腹+餐后血糖 /
体重 / 体温 / 心率 / SpO2 / 身高
- effectiveRange(for:profile:) 实现 1 条 Profile-aware 规则:
age >= 65 时 bp.systolic 上限 140→150
- KangkangApp schema 加 UserProfile.self
测试 17 个全绿(UserProfile 6 + MonitorMetric 11);schema 烟测扩 2(seriesKey roundtrip + UserProfile persist)。
UI 层 + Timeline 合并下个 commit。
This commit is contained in:
@@ -13,6 +13,7 @@ struct ModelsSchemaTests {
|
||||
Asset.self,
|
||||
ChatTurn.self,
|
||||
Symptom.self,
|
||||
UserProfile.self,
|
||||
])
|
||||
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
|
||||
return try ModelContainer(for: schema, configurations: [config])
|
||||
@@ -108,4 +109,52 @@ struct ModelsSchemaTests {
|
||||
#expect(all.count == 1)
|
||||
#expect(all.first?.referencedIndicatorIDs == ["abc"])
|
||||
}
|
||||
|
||||
@Test func indicatorSeriesKeyRoundtrip() throws {
|
||||
let container = try makeContainer()
|
||||
let ctx = ModelContext(container)
|
||||
|
||||
let bp = Indicator(
|
||||
name: "收缩压",
|
||||
value: "125",
|
||||
unit: "mmHg",
|
||||
range: "90-140",
|
||||
status: .normal,
|
||||
pinned: true,
|
||||
seriesKey: "bp.systolic"
|
||||
)
|
||||
ctx.insert(bp)
|
||||
try ctx.save()
|
||||
|
||||
let fetched = try #require(try ctx.fetch(FetchDescriptor<Indicator>()).first)
|
||||
#expect(fetched.seriesKey == "bp.systolic")
|
||||
#expect(fetched.pinned == true)
|
||||
}
|
||||
|
||||
@Test func indicatorSeriesKeyDefaultsToNil() {
|
||||
let i = Indicator(name: "ALT", value: "32", unit: "U/L", range: "9-50", status: .normal)
|
||||
#expect(i.seriesKey == nil)
|
||||
}
|
||||
|
||||
@Test func userProfileSchemaPersistsAcrossSave() throws {
|
||||
let container = try makeContainer()
|
||||
let ctx = ModelContext(container)
|
||||
|
||||
let p = UserProfile(
|
||||
birthYear: 1985,
|
||||
biologicalSexRaw: "male",
|
||||
heightCM: 175,
|
||||
bloodTypeRaw: "A",
|
||||
chronicConditions: ["高血压"]
|
||||
)
|
||||
ctx.insert(p)
|
||||
try ctx.save()
|
||||
|
||||
let fetched = try #require(try ctx.fetch(FetchDescriptor<UserProfile>()).first)
|
||||
#expect(fetched.birthYear == 1985)
|
||||
#expect(fetched.sex == .male)
|
||||
#expect(fetched.heightCM == 175)
|
||||
#expect(fetched.bloodTypeRaw == "A")
|
||||
#expect(fetched.chronicConditions == ["高血压"])
|
||||
}
|
||||
}
|
||||
|
||||
84
康康Tests/MonitorMetricTests.swift
Normal file
84
康康Tests/MonitorMetricTests.swift
Normal file
@@ -0,0 +1,84 @@
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import 康康
|
||||
|
||||
@MainActor
|
||||
struct MonitorMetricTests {
|
||||
|
||||
@Test func allMetricsHaveAtLeastOneField() {
|
||||
for m in MonitorMetric.allCases {
|
||||
#expect(!m.fields.isEmpty, "metric \(m.rawValue) has no fields")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func bloodPressureHasTwoFields() {
|
||||
let bp = MonitorMetric.bloodPressure
|
||||
#expect(bp.fields.count == 2)
|
||||
#expect(bp.fields[0].seriesKey == "bp.systolic")
|
||||
#expect(bp.fields[1].seriesKey == "bp.diastolic")
|
||||
}
|
||||
|
||||
@Test func statusHighWhenValueAboveRange() {
|
||||
let s = MonitorMetric.status(value: 150, in: 90...140)
|
||||
#expect(s == .high)
|
||||
}
|
||||
|
||||
@Test func statusLowWhenValueBelowRange() {
|
||||
let s = MonitorMetric.status(value: 80, in: 90...140)
|
||||
#expect(s == .low)
|
||||
}
|
||||
|
||||
@Test func statusNormalWhenValueInside() {
|
||||
let s = MonitorMetric.status(value: 120, in: 90...140)
|
||||
#expect(s == .normal)
|
||||
}
|
||||
|
||||
@Test func statusNormalWhenRangeIsNil() {
|
||||
let s = MonitorMetric.status(value: 999, in: nil)
|
||||
#expect(s == .normal)
|
||||
}
|
||||
|
||||
@Test func systolicUpperBoundShiftsForElderly() {
|
||||
let bp = MonitorMetric.bloodPressure
|
||||
let systolic = bp.fields[0]
|
||||
let elderly = UserProfile(birthYear: 1955) // 71 岁
|
||||
let range = bp.effectiveRange(for: systolic, profile: elderly)
|
||||
#expect(range == 90...150)
|
||||
}
|
||||
|
||||
@Test func systolicUpperBoundUnchangedForYoungAdult() {
|
||||
let bp = MonitorMetric.bloodPressure
|
||||
let systolic = bp.fields[0]
|
||||
let young = UserProfile(birthYear: 1990)
|
||||
let range = bp.effectiveRange(for: systolic, profile: young)
|
||||
#expect(range == 90...140)
|
||||
}
|
||||
|
||||
@Test func systolicUpperBoundUnchangedWhenProfileNil() {
|
||||
let bp = MonitorMetric.bloodPressure
|
||||
let systolic = bp.fields[0]
|
||||
let range = bp.effectiveRange(for: systolic, profile: nil)
|
||||
#expect(range == 90...140)
|
||||
}
|
||||
|
||||
@Test func glucoseUnaffectedByAge() {
|
||||
let g = MonitorMetric.fastingGlucose
|
||||
let field = g.fields[0]
|
||||
let elderly = UserProfile(birthYear: 1940)
|
||||
#expect(g.effectiveRange(for: field, profile: elderly) == field.baseRange)
|
||||
}
|
||||
|
||||
@Test func isRangePersonalizedTrueForElderlySystolic() {
|
||||
let bp = MonitorMetric.bloodPressure
|
||||
let systolic = bp.fields[0]
|
||||
let elderly = UserProfile(birthYear: 1955)
|
||||
#expect(bp.isRangePersonalized(for: systolic, profile: elderly) == true)
|
||||
}
|
||||
|
||||
@Test func isRangePersonalizedFalseForYoungProfile() {
|
||||
let bp = MonitorMetric.bloodPressure
|
||||
let systolic = bp.fields[0]
|
||||
let young = UserProfile(birthYear: 1995)
|
||||
#expect(bp.isRangePersonalized(for: systolic, profile: young) == false)
|
||||
}
|
||||
}
|
||||
83
康康Tests/UserProfileTests.swift
Normal file
83
康康Tests/UserProfileTests.swift
Normal file
@@ -0,0 +1,83 @@
|
||||
import Testing
|
||||
import SwiftData
|
||||
import Foundation
|
||||
@testable import 康康
|
||||
|
||||
@MainActor
|
||||
struct UserProfileTests {
|
||||
|
||||
private func makeContext() throws -> ModelContext {
|
||||
let schema = Schema([UserProfile.self])
|
||||
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
|
||||
let container = try ModelContainer(for: schema, configurations: [config])
|
||||
return ModelContext(container)
|
||||
}
|
||||
|
||||
@Test func freshProfileHasNilDemographics() {
|
||||
let p = UserProfile()
|
||||
#expect(p.birthYear == nil)
|
||||
#expect(p.heightCM == nil)
|
||||
#expect(p.biologicalSexRaw == "")
|
||||
#expect(p.bloodTypeRaw == "")
|
||||
#expect(p.allergies.isEmpty)
|
||||
#expect(p.chronicConditions.isEmpty)
|
||||
#expect(p.currentMedications.isEmpty)
|
||||
#expect(p.familyHistory.isEmpty)
|
||||
#expect(p.age == nil)
|
||||
#expect(p.sex == .undisclosed)
|
||||
#expect(p.hasAnyBasics == false)
|
||||
#expect(p.summaryLine == "")
|
||||
}
|
||||
|
||||
@Test func ageComputedFromBirthYear() {
|
||||
let p = UserProfile(birthYear: 1985)
|
||||
let currentYear = Calendar.current.component(.year, from: .now)
|
||||
#expect(p.age == currentYear - 1985)
|
||||
}
|
||||
|
||||
@Test func sexEnumRoundtripsThroughRaw() {
|
||||
let p = UserProfile(biologicalSexRaw: "male")
|
||||
#expect(p.sex == .male)
|
||||
p.sex = .female
|
||||
#expect(p.biologicalSexRaw == "female")
|
||||
p.sex = .undisclosed
|
||||
#expect(p.biologicalSexRaw == "")
|
||||
}
|
||||
|
||||
@Test func summaryLineSkipsEmptyFields() {
|
||||
let p = UserProfile(birthYear: 1990, biologicalSexRaw: "female", heightCM: 162)
|
||||
let line = p.summaryLine
|
||||
#expect(line.contains("岁"))
|
||||
#expect(line.contains("女"))
|
||||
#expect(line.contains("162cm"))
|
||||
#expect(!line.contains("型")) // 血型空,不出现
|
||||
}
|
||||
|
||||
@Test func loadOrCreateReturnsExistingSingleton() throws {
|
||||
let ctx = try makeContext()
|
||||
let first = UserProfileStore.loadOrCreate(in: ctx)
|
||||
first.birthYear = 1990
|
||||
try ctx.save()
|
||||
|
||||
let second = UserProfileStore.loadOrCreate(in: ctx)
|
||||
#expect(second.birthYear == 1990)
|
||||
|
||||
let all = try ctx.fetch(FetchDescriptor<UserProfile>())
|
||||
#expect(all.count == 1)
|
||||
}
|
||||
|
||||
@Test func arrayFieldsRoundtripThroughSwiftData() throws {
|
||||
let ctx = try makeContext()
|
||||
let p = UserProfile(
|
||||
chronicConditions: ["高血压", "糖尿病"],
|
||||
currentMedications: ["缬沙坦 80mg qd", "二甲双胍 500mg bid"]
|
||||
)
|
||||
ctx.insert(p)
|
||||
try ctx.save()
|
||||
|
||||
let fetched = try #require(try ctx.fetch(FetchDescriptor<UserProfile>()).first)
|
||||
#expect(fetched.chronicConditions == ["高血压", "糖尿病"])
|
||||
#expect(fetched.currentMedications.count == 2)
|
||||
#expect(fetched.currentMedications.first == "缬沙坦 80mg qd")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user