```
feat(AI): 集成MNN推理引擎替换MLX作为主AI运行时 - 引入MNN(alibaba) + Arm SME2 + CPU作为主AI运行时,支持A19/iPhone17的 SME2和A17的NEON加速 - 添加MLX Swift作为兜底GPU推理方案,实现双后端切换机制 - 使用单一Qwen3.5-2B多模态模型(1.2GB),替代原有的LLM+VL分离架构 - 实现InferenceEngine.current引擎选择逻辑,真机默认MNN,模拟器回退MLX - 更新AIAgent架构,通过MNNLLMBridge(ObjC++) → MNNBackend进行推理 - 修改队列机制防止并发推理导致OOM,使用信号量闸门控制显存占用 - 更新文档中的技术栈说明、模块边界和周次交付计划 ```
This commit is contained in:
45
康康Tests/MedicationReminderParsingTests.swift
Normal file
45
康康Tests/MedicationReminderParsingTests.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import 康康
|
||||
|
||||
/// 「用药记录」点药设吃药提醒:行拆分与提醒预填的纯函数测试。
|
||||
/// 入口逻辑见 `TimelineEntryDetailView.medicationBody`。
|
||||
struct MedicationReminderParsingTests {
|
||||
|
||||
// MARK: - 行拆分
|
||||
|
||||
@Test func splitsMultipleLinesAndDropsBlanks() {
|
||||
let content = "缬沙坦胶囊 80mg · 一日一次\n\n二甲双胍 0.5g · 一日三次\n "
|
||||
let lines = TimelineEntryDetailView.medicationLines(content)
|
||||
#expect(lines == ["缬沙坦胶囊 80mg · 一日一次", "二甲双胍 0.5g · 一日三次"])
|
||||
}
|
||||
|
||||
@Test func singleLineNoNewline() {
|
||||
#expect(TimelineEntryDetailView.medicationLines("阿司匹林肠溶片 100mg") == ["阿司匹林肠溶片 100mg"])
|
||||
}
|
||||
|
||||
@Test func emptyContentYieldsNoLines() {
|
||||
#expect(TimelineEntryDetailView.medicationLines("\n \n").isEmpty)
|
||||
}
|
||||
|
||||
// MARK: - 提醒预填
|
||||
|
||||
@Test func splitsNameAndUsageOnMiddot() {
|
||||
let f = TimelineEntryDetailView.medicationReminderFields(forLine: "缬沙坦胶囊 80mg · 一日一次")
|
||||
#expect(f.title.contains("缬沙坦胶囊 80mg")) // 标题带药名+规格(可能含「吃药:」前缀)
|
||||
#expect(!f.title.contains("一日一次")) // 用法不进标题
|
||||
#expect(f.note == "一日一次") // 用法进备注
|
||||
}
|
||||
|
||||
@Test func noUsageGivesEmptyNote() {
|
||||
let f = TimelineEntryDetailView.medicationReminderFields(forLine: "阿司匹林 100mg")
|
||||
#expect(f.title.contains("阿司匹林 100mg"))
|
||||
#expect(f.note.isEmpty)
|
||||
}
|
||||
|
||||
@Test func multipleMiddotsKeepEverythingAfterFirstAsUsage() {
|
||||
let f = TimelineEntryDetailView.medicationReminderFields(forLine: "甲药 · 餐后 · 一日两次")
|
||||
#expect(f.title.contains("甲药"))
|
||||
#expect(f.note == "餐后 · 一日两次")
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,23 @@ struct MedicationScanServiceTests {
|
||||
try MedicationScanService.parseMedicationsJSON("识别不出来,抱歉")
|
||||
}
|
||||
}
|
||||
|
||||
/// 英文药名照原样保留(prompt 已要求英文不翻译);解析层只透传,不应被丢弃。
|
||||
@Test func parsesEnglishDrugName() throws {
|
||||
let raw = #"{"medications":[{"name":"Amoxicillin","strength":"500mg","usage":"Take one capsule three times daily"}]}"#
|
||||
let meds = try MedicationScanService.parseMedicationsJSON(raw)
|
||||
#expect(meds.count == 1)
|
||||
#expect(meds[0].name == "Amoxicillin")
|
||||
#expect(meds[0].entryText == "Amoxicillin 500mg · Take one capsule three times daily")
|
||||
}
|
||||
|
||||
/// 通用名 + 商品名合并写法(prompt 约定 "通用名(商品名)")原样透传。
|
||||
@Test func parsesGenericWithBrandName() throws {
|
||||
let raw = #"{"medications":[{"name":"缬沙坦胶囊(代文)","strength":"80mg×7粒","usage":""}]}"#
|
||||
let meds = try MedicationScanService.parseMedicationsJSON(raw)
|
||||
#expect(meds.count == 1)
|
||||
#expect(meds[0].name == "缬沙坦胶囊(代文)")
|
||||
}
|
||||
}
|
||||
|
||||
/// 「用药」日记 → 时间线分类映射(拍药盒入档落库后在「记录」tab 的归类)。
|
||||
@@ -64,7 +81,8 @@ struct MedicationScanServiceTests {
|
||||
struct MedicationTimelineTests {
|
||||
|
||||
private func makeContext() throws -> ModelContext {
|
||||
let schema = Schema([DiaryEntry.self])
|
||||
// DiaryEntry 现关联 Asset(拍药盒原图),schema 必须带上 Asset.self,否则建容器报错。
|
||||
let schema = Schema([DiaryEntry.self, Asset.self])
|
||||
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
|
||||
return ModelContext(try ModelContainer(for: schema, configurations: [config]))
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ struct ModelsSchemaTests {
|
||||
UserProfile.self,
|
||||
MetricReminder.self,
|
||||
CustomMonitorMetric.self,
|
||||
Medication.self,
|
||||
])
|
||||
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
|
||||
return try ModelContainer(for: schema, configurations: [config])
|
||||
@@ -190,4 +191,42 @@ struct ModelsSchemaTests {
|
||||
#expect(fetched.bloodTypeRaw == "A")
|
||||
#expect(fetched.chronicConditions == ["高血压"])
|
||||
}
|
||||
|
||||
@Test func medicationRoundtripAndDetailLine() throws {
|
||||
let container = try makeContainer()
|
||||
let ctx = ModelContext(container)
|
||||
|
||||
let med = Medication(name: "缬沙坦胶囊", strength: "80mg×7粒", usage: "一日一次,一次一粒")
|
||||
ctx.insert(med)
|
||||
try ctx.save()
|
||||
|
||||
let fetched = try #require(try ctx.fetch(FetchDescriptor<Medication>()).first)
|
||||
#expect(fetched.name == "缬沙坦胶囊")
|
||||
#expect(fetched.detailLine == "80mg×7粒 · 一日一次,一次一粒")
|
||||
#expect(fetched.updatedAt == fetched.createdAt)
|
||||
}
|
||||
|
||||
@Test func medicationDetailLineOmitsEmptyParts() {
|
||||
#expect(Medication(name: "维生素C").detailLine == "")
|
||||
#expect(Medication(name: "钙片", strength: "600mg").detailLine == "600mg")
|
||||
}
|
||||
|
||||
@Test func cascadeDeleteMedicationRemovesAssets() throws {
|
||||
let container = try makeContainer()
|
||||
let ctx = ModelContext(container)
|
||||
|
||||
let med = Medication(name: "二甲双胍缓释片", strength: "0.5g×30片")
|
||||
let asset = Asset(relativePath: "med-1.jpg", bytes: 2048)
|
||||
ctx.insert(asset)
|
||||
med.assets.append(asset)
|
||||
ctx.insert(med)
|
||||
try ctx.save()
|
||||
#expect(med.assets.count == 1)
|
||||
|
||||
ctx.delete(med)
|
||||
try ctx.save()
|
||||
|
||||
#expect(try ctx.fetch(FetchDescriptor<Medication>()).isEmpty)
|
||||
#expect(try ctx.fetch(FetchDescriptor<Asset>()).isEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,42 @@ struct TodayRemindersLogicTests {
|
||||
#expect(!CustomReminder(title: "x", frequency: .yearly, dayOfMonth: 29, month: 5).occurs(on: d, calendar: cal))
|
||||
}
|
||||
|
||||
// MARK: - 多选频率(每日/每周/每月 可同时勾选)
|
||||
|
||||
@Test func multiFrequencyOccursOnAnySelected() {
|
||||
// 周一(周二日 = 2)+ 每月15日,两种节奏任一命中即触发。
|
||||
let monday = date(2026, 6, 1) // 2026-06-01 是周一
|
||||
let wdMon = cal.component(.weekday, from: monday)
|
||||
let r = CustomReminder(title: "x")
|
||||
r.frequencies = [.weekly, .monthly]
|
||||
r.weekdays = [wdMon]
|
||||
r.monthlyDays = [15]
|
||||
// 周一但非15号 → 命中(weekly)
|
||||
#expect(r.occurs(on: monday, calendar: cal))
|
||||
// 15号但非周一(2026-06-15 是周一,换 2026-07-15 是周三)→ 命中(monthly)
|
||||
let mid = date(2026, 7, 15)
|
||||
#expect(r.occurs(on: mid, calendar: cal))
|
||||
// 既非周一也非15号 → 不触发
|
||||
#expect(!r.occurs(on: date(2026, 7, 16), calendar: cal))
|
||||
}
|
||||
|
||||
@Test func monthlyMultiDayOccursOnEach() {
|
||||
let r = CustomReminder(title: "x")
|
||||
r.frequencies = [.monthly]
|
||||
r.monthlyDays = [1, 15]
|
||||
#expect(r.occurs(on: date(2026, 6, 1), calendar: cal))
|
||||
#expect(r.occurs(on: date(2026, 6, 15), calendar: cal))
|
||||
#expect(!r.occurs(on: date(2026, 6, 10), calendar: cal))
|
||||
}
|
||||
|
||||
@Test func legacySingleFrequencyStillReadsThroughFrequenciesFallback() {
|
||||
// 旧数据:只设单选 frequency,frequenciesRaw 为空 → frequencies 回退到 [frequency]。
|
||||
let r = CustomReminder(title: "x", frequency: .weekly, dayOfMonth: 1)
|
||||
r.weekdays = [2]
|
||||
#expect(r.frequencies == [.weekly])
|
||||
#expect(r.monthlyDays == [1]) // monthDays 空 → 回退 dayOfMonth
|
||||
}
|
||||
|
||||
// MARK: - MetricReminder
|
||||
|
||||
@Test func metricReminderOccursOnSelectedWeekday() {
|
||||
|
||||
Reference in New Issue
Block a user