Files
kangkang/康康Tests/TimelineGroupingTests.swift
link2026 6c6a950140 ```
feat: 添加拍药盒功能和语音直达入口

- 实现拍药盒扫描流程,支持本地OCR识别药品信息
- 在日记页面添加拍药盒和记症状的三选一入口
- 优化按钮点击区域,确保符合苹果HIG最小命中区标准
- 添加用药记录到时间线的独立分类显示
- 实现长按+号语音直达功能,支持语音意图分类跳转
- 更新项目配置文件,启用代码分析和死代码剥离选项
- 增加多项本地化字符串支持新功能
```
2026-06-13 09:16:25 +08:00

88 lines
3.3 KiB
Swift

import Testing
import Foundation
@testable import
struct TimelineGroupingTests {
private let now: Date = {
var c = DateComponents()
c.year = 2026; c.month = 5; c.day = 25; c.hour = 12
return Calendar(identifier: .gregorian).date(from: c)!
}()
@Test func timelineKindOrderMatchesRecordFilterChips() {
#expect(TimelineKind.allCases == [.diary, .symptom, .indicator, .medication, .report])
}
@Test func todaySection() {
#expect(TimelineGrouping.section(for: now, now: now) == .today)
}
@Test func yesterdaySection() {
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)!
#expect(TimelineGrouping.section(for: yesterday, now: now) == .yesterday)
}
@Test func thisWeekSection() {
// 5/25 , ( 5/24 zh_CN ; 3 , within-week)
let cal = Calendar(identifier: .gregorian)
let twoDaysAgo = cal.date(byAdding: .day, value: -2, to: now)!
let section = TimelineGrouping.section(for: twoDaysAgo, now: now, calendar: cal)
// 2 .thisWeek .yesterday/.thisMonth .today
#expect(section != .today)
}
@Test func thisMonthSection() {
let earlierThisMonth = Calendar.current.date(byAdding: .day, value: -15, to: now)!
let section = TimelineGrouping.section(for: earlierThisMonth, now: now)
#expect(section == .thisMonth)
}
@Test func yearSection() {
var c = DateComponents()
c.year = 2024; c.month = 3; c.day = 1
let oldDate = Calendar(identifier: .gregorian).date(from: c)!
let section = TimelineGrouping.section(for: oldDate, now: now)
#expect(section == .year(2024))
}
@Test func sectionOrderingTodayFirst() {
let cal = Calendar(identifier: .gregorian)
let yesterday = cal.date(byAdding: .day, value: -1, to: now)!
let lastYear = cal.date(byAdding: .year, value: -1, to: now)!
let entries = [
mockEntry(id: "old", date: lastYear),
mockEntry(id: "yest", date: yesterday),
mockEntry(id: "today", date: now),
]
let grouped = TimelineGrouping.group(entries, now: now, calendar: cal)
let labels = grouped.map { $0.section.label }
#expect(labels.first == "今天")
#expect(labels.last?.contains("") == true)
}
@Test func formatDurationBoundaries() {
#expect(formatDuration(0) == "刚刚")
#expect(formatDuration(30) == "刚刚") // < 1 min
#expect(formatDuration(120) == "2 分钟") // 2 min
#expect(formatDuration(3600) == "1 小时") // 1h
#expect(formatDuration(3600 + 1800) == "1 小时 30 分")
#expect(formatDuration(86400) == "1 天")
#expect(formatDuration(86400 + 3600) == "1 天 1 小时")
}
private func mockEntry(id: String, date: Date) -> TimelineEntry {
TimelineEntry(
id: id,
kind: .diary,
date: date,
title: id,
subtitle: "test",
trailing: nil,
trailingIsAlert: false,
isOngoing: false
)
}
}