import Testing import Foundation @testable import 康康 /// 主页「今日提醒」筛选逻辑(`occurs(on:)`)的纯函数测试。 /// 用固定 Gregorian 日历构造确定日期,避免依赖 `Date.now` / 本机时区。 struct TodayRemindersLogicTests { private var cal: Calendar { var c = Calendar(identifier: .gregorian) c.timeZone = TimeZone(identifier: "Asia/Shanghai")! return c } private func date(_ y: Int, _ mo: Int, _ d: Int) -> Date { cal.date(from: DateComponents(year: y, month: mo, day: d, hour: 12))! } // MARK: - CustomReminder @Test func dailyOccursEveryDay() { let r = CustomReminder(title: "跑步", frequency: .daily) #expect(r.occurs(on: date(2026, 5, 30), calendar: cal)) #expect(r.occurs(on: date(2026, 1, 1), calendar: cal)) } @Test func disabledNeverOccurs() { let r = CustomReminder(title: "跑步", frequency: .daily, enabled: false) #expect(!r.occurs(on: date(2026, 5, 30), calendar: cal)) } @Test func weeklyOccursOnlyOnSelectedWeekdays() { let d = date(2026, 5, 30) let wd = cal.component(.weekday, from: d) let other = wd == 1 ? 2 : 1 let hit = CustomReminder(title: "x", weekdays: [wd], frequency: .weekly) #expect(hit.occurs(on: d, calendar: cal)) let miss = CustomReminder(title: "x", weekdays: [other], frequency: .weekly) #expect(!miss.occurs(on: d, calendar: cal)) } @Test func monthlyOccursOnlyOnMatchingDay() { let d = date(2026, 5, 30) // 30 日 #expect(CustomReminder(title: "x", frequency: .monthly, dayOfMonth: 30).occurs(on: d, calendar: cal)) #expect(!CustomReminder(title: "x", frequency: .monthly, dayOfMonth: 15).occurs(on: d, calendar: cal)) } @Test func monthlyDay31SkipsShortMonths() { // 4 月只有 30 天:选「31 日」的提醒在 4/30 这天不应触发(无 4/31,该月跳过)。 let apr30 = date(2026, 4, 30) let r = CustomReminder(title: "x", frequency: .monthly, dayOfMonth: 31) #expect(!r.occurs(on: apr30, calendar: cal)) } @Test func yearlyOccursOnlyOnMatchingMonthAndDay() { let d = date(2026, 5, 30) #expect(CustomReminder(title: "x", frequency: .yearly, dayOfMonth: 30, month: 5).occurs(on: d, calendar: cal)) #expect(!CustomReminder(title: "x", frequency: .yearly, dayOfMonth: 30, month: 6).occurs(on: d, calendar: cal)) #expect(!CustomReminder(title: "x", frequency: .yearly, dayOfMonth: 29, month: 5).occurs(on: d, calendar: cal)) } // MARK: - MetricReminder @Test func metricReminderOccursOnSelectedWeekday() { let d = date(2026, 5, 30) let wd = cal.component(.weekday, from: d) let other = wd == 1 ? 2 : 1 #expect(MetricReminder(metricId: "bp", displayName: "血压", weekdays: [wd]).occurs(on: d, calendar: cal)) #expect(!MetricReminder(metricId: "bp2", displayName: "血压", weekdays: [other]).occurs(on: d, calendar: cal)) } @Test func disabledMetricReminderNeverOccurs() { let d = date(2026, 5, 30) let wd = cal.component(.weekday, from: d) let r = MetricReminder(metricId: "bp", displayName: "血压", weekdays: [wd], enabled: false) #expect(!r.occurs(on: d, calendar: cal)) } }