Tests/Unit/PromptScheduleTests.swift (6131B)
1import Foundation
2import Testing
3
4@testable import Reportage
5
6@Suite("PromptSchedule")
7struct PromptScheduleTests {
8
9 private static let secret: UInt64 = 0x1234_5678_9ABC_DEF0
10
11 private static var calendar: Calendar {
12 var calendar = Calendar(identifier: .gregorian)
13 calendar.timeZone = TimeZone(identifier: "Asia/Tokyo")!
14 return calendar
15 }
16
17
18 private static let day = calendar.date(from: DateComponents(year: 2026, month: 10, day: 14))!
19
20 private static func settings(
21 perDay: Int = 4, window: WakingWindow = WakingWindow(start: 9 * 60, end: 22 * 60),
22 weekend: WakingWindow? = nil, gap: Int = 30
23 ) -> PromptSettings {
24 var settings = PromptSettings()
25 settings.isEnabled = true
26 settings.perDay = perDay
27 settings.window = window
28 settings.weekendWindow = weekend
29 settings.minimumGap = gap
30 return settings
31 }
32
33 private static func times(_ settings: PromptSettings, on day: Date = day, secret: UInt64 = secret) -> [Date] {
34 PromptSchedule.times(on: day, settings: settings, secret: secret, calendar: calendar)
35 }
36
37 private static func at(_ hour: Int, _ minute: Int = 0, on day: Date = day) -> Date {
38 calendar.date(bySettingHour: hour, minute: minute, second: 0, of: day)!
39 }
40
41 @Test("A day's times are the same every time they're worked out")
42 func deterministic() {
43 let settings = Self.settings()
44 #expect(Self.times(settings) == Self.times(settings))
45 }
46
47 @Test("Different days and installs get different times")
48 func varies() {
49 let settings = Self.settings()
50 let nextDay = Self.calendar.date(byAdding: .day, value: 1, to: Self.day)!
51 #expect(Self.times(settings) != Self.times(settings, on: nextDay))
52 #expect(Self.times(settings) != Self.times(settings, secret: 42))
53 }
54
55 @Test("One time falls in each slot of the window, in order, a gap apart", arguments: 0..<50)
56 func stratified(seed: UInt64) {
57 let settings = Self.settings(perDay: 6, gap: 45)
58 let times = Self.times(settings, secret: seed)
59 let start = Self.at(9)
60 let end = Self.at(22)
61 let slot = end.timeIntervalSince(start) / 6
62
63 #expect(times.count == 6)
64 #expect(times == times.sorted())
65 for (index, time) in times.enumerated() {
66 #expect(time >= start.addingTimeInterval(Double(index) * slot))
67 #expect(time < start.addingTimeInterval(Double(index + 1) * slot))
68 #expect(time.timeIntervalSinceReferenceDate.rounded() == time.timeIntervalSinceReferenceDate)
69 }
70 for (earlier, later) in zip(times, times.dropFirst()) {
71 #expect(later.timeIntervalSince(earlier) >= 45 * 60 - 1)
72 }
73 }
74
75 @Test("A gap too large for the window shrinks to fit")
76 func gapTooLarge() {
77 let settings = Self.settings(
78 perDay: 6, window: WakingWindow(start: 9 * 60, end: 12 * 60), gap: 120)
79 let times = Self.times(settings)
80 #expect(times.count == 6)
81 #expect(times.allSatisfy { $0 >= Self.at(9) && $0 < Self.at(12) })
82 for (earlier, later) in zip(times, times.dropFirst()) {
83 #expect(later.timeIntervalSince(earlier) >= 30 * 60 - 1)
84 }
85 }
86
87 @Test("A window can run past midnight")
88 func pastMidnight() {
89 let settings = Self.settings(window: WakingWindow(start: 20 * 60, end: 2 * 60))
90 let times = Self.times(settings)
91 let nextDay = Self.calendar.date(byAdding: .day, value: 1, to: Self.day)!
92 #expect(times.count == 4)
93 #expect(times.allSatisfy { $0 >= Self.at(20) && $0 < Self.at(2, on: nextDay) })
94 }
95
96 @Test("Weekends use their own window")
97 func weekends() {
98 let settings = Self.settings(weekend: WakingWindow(start: 11 * 60, end: 13 * 60))
99 let saturday = Self.calendar.date(byAdding: .day, value: 3, to: Self.day)!
100 #expect(Self.times(settings, on: saturday).allSatisfy {
101 $0 >= Self.at(11, on: saturday) && $0 < Self.at(13, on: saturday)
102 })
103 #expect(Self.times(settings).contains { $0 >= Self.at(13) })
104 }
105
106 @Test("The horizon covers the coming week from now, ending with a reminder")
107 func horizon() {
108 let settings = Self.settings()
109 let now = Self.at(15)
110 let plan = PromptSchedule.horizon(
111 from: now, settings: settings, secret: Self.secret, calendar: Self.calendar)
112
113 let today = Self.times(settings).filter { $0 > now }
114 #expect(plan.count == today.count + 6 * 4)
115 #expect(plan.allSatisfy { $0.date > now })
116 #expect(plan.map(\.date) == plan.map(\.date).sorted())
117 #expect(plan.dropLast().allSatisfy { $0.kind == .prompt })
118 #expect(plan.last?.kind == .reminder)
119 #expect(Set(plan.map(\.identifier)).count == plan.count)
120 }
121
122 @Test("The horizon is capped, leaving room for snoozes")
123 func horizonLimit() {
124 let plan = PromptSchedule.horizon(
125 from: Self.at(0), settings: Self.settings(perDay: 12, gap: 15),
126 secret: Self.secret, calendar: Self.calendar)
127 #expect(plan.count == 50)
128 }
129
130 @Test("Prompts that are off plan nothing")
131 func disabled() {
132 var settings = Self.settings()
133 settings.isEnabled = false
134 #expect(
135 PromptSchedule.horizon(
136 from: Self.at(0), settings: settings, secret: Self.secret, calendar: Self.calendar
137 ).isEmpty)
138 }
139
140 @Test("Identifiers name the kind and the local fire time")
141 func identifiers() {
142 let prompt = PlannedPrompt(
143 kind: .reminder, date: Self.at(9, 5),
144 components: DateComponents(year: 2026, month: 10, day: 14, hour: 9, minute: 5, second: 7))
145 #expect(prompt.identifier == "reminder.20261014-090507")
146 #expect(PromptNotification.kind(ofIdentifier: prompt.identifier) == .reminder)
147 #expect(PromptNotification.kind(ofIdentifier: "snoozed.ABC") == .snoozed)
148 #expect(PromptNotification.kind(ofIdentifier: "something-else") == nil)
149 }
150}