Tests/OrgPresentationTests/AgendaRowsTests.swift
286 lines · 15141 bytes
20 symbols in this file
AgendaRowsTestsdaysrowsdateguttersAndStatusesOnTodaycheckgutterLinesAndSymbolslogRowsKeepTheDurationAndStateguttersOnOtherDaysemptyDaysAreHiddenButTodayStaysoverdueEntriesGroupAboveTodayOldestFirsteventsMergeByTimenowLineGoesAmongTimedRowstimedHabitsGoAmongTimedRowsgridLinesGoBeforeLaterRowsAgendaThemeTestsnewKeysHaveDefaultsInBothAppearanceskeywordClassescolorsResolveOverridesFirstcategoryHuesAreStable
1import Foundation
2import OrgCore
3import Testing
4@testable import OrgPresentation
5
6struct AgendaRowsTests {
7 static let file = """
8 #+CATEGORY: life
9 * TODO [#B] Pay rent [0/2] :home:
10 SCHEDULED: <2026-10-01 Thu>
11 * TODO Ancient chore
12 SCHEDULED: <2025-05-24 Sat>
13 * TODO Send forms
14 DEADLINE: <2026-10-02 Fri>
15 * TODO File taxes
16 DEADLINE: <2026-10-05 Mon>
17 * TODO Renew passport
18 DEADLINE: <2026-10-09 Fri>
19 * TODO Water plants
20 SCHEDULED: <2026-10-05 Mon>
21 * DONE Took out bins
22 SCHEDULED: <2026-10-05 Mon>
23 * Standup
24 SCHEDULED: <2026-10-05 Mon 08:00>
25 * Dentist <2026-10-05 Mon 13:50-14:30>
26 * Birthday <2026-10-05 Mon>
27 * Conference
28 <2026-10-04 Sun>--<2026-10-06 Tue>
29 * TODO Stretch
30 SCHEDULED: <2026-10-04 Sun .+1d>
31 :PROPERTIES:
32 :STYLE: habit
33 :END:
34 * TODO Walk dog
35 SCHEDULED: <2026-10-03 Sat 18:00 .+1d>
36 :PROPERTIES:
37 :STYLE: habit
38 :END:
39 * Far off
40 <2026-10-11 Sun>
41 """
42
43 static let today = Days.absolute(year: 2026, month: 10, day: 5)
44 static let utc: Calendar = {
45 var c = Calendar(identifier: .gregorian)
46 c.timeZone = TimeZone(identifier: "UTC")!
47 return c
48 }()
49
50 static func days(start: Int = today, span: Int = 1) -> [AgendaDay] {
51 let source = AgendaSource(path: "life.org", text: file, defaults: OrgSettings.default.withTodoKeywords("TODO | DONE")!)
52 return Agenda.list([source], start: start, days: span, today: today, now: (10, 0))
53 }
54
55 static func rows(day: Int = today) -> [String: AgendaRow] {
56 var rows: [String: AgendaRow] = [:]
57 for item in days(start: day)[0].items {
58 guard let row = AgendaRow(item: item, day: day, today: today) else { continue }
59 rows[row.title] = rows[row.title] ?? row
60 }
61 return rows
62 }
63
64 static func date(_ day: Int, _ hour: Int, _ minute: Int = 0) -> Date {
65 Date(timeIntervalSince1970: Double((day - 719163) * 86400 + hour * 3600 + minute * 60))
66 }
67
68 @Test func guttersAndStatusesOnToday() {
69 let rows = Self.rows()
70 func check(_ title: String, _ gutter: String, _ status: AgendaRow.Status, _ icon: AgendaRow.Icon?, sourceLocation: SourceLocation = #_sourceLocation) {
71 let row = rows[title]
72 #expect(row?.gutter == gutter, "\(title)", sourceLocation: sourceLocation)
73 #expect(row?.status == status, "\(title)", sourceLocation: sourceLocation)
74 #expect(row?.icon == icon, "\(title)", sourceLocation: sourceLocation)
75 }
76 check("Pay rent", "4d ago", .overdue, .scheduled)
77 check("Ancient chore", "1y 4mo ago", .overdue, .scheduled)
78 check("Send forms", "3d late", .overdue, .deadline)
79 check("File taxes", "Due today", .dueSoon, .deadline)
80 check("Renew passport", "Due in 4d", .dueSoon, .deadline)
81 check("Water plants", "Scheduled", .normal, .scheduled)
82 check("Took out bins", "Scheduled", .done, .scheduled)
83 check("Standup", "08:00", .normal, .scheduled)
84 check("Dentist", "13:50–14:30", .normal, .timestamp)
85 check("Birthday", "All day", .normal, .timestamp)
86 check("Conference", "Day 2/3", .normal, .timestamp)
87 check("Stretch", "1d ago", .overdue, .habit)
88 // An earlier day's time says nothing about today.
89 check("Walk dog", "2d ago", .overdue, .habit)
90 #expect(rows["Walk dog"]?.start == nil)
91 #expect(rows["Dentist"]?.start == 1350 && rows["Dentist"]?.end == 1430)
92 let rent = rows["Pay rent"]!
93 #expect(rent.category == "life" && rent.tags == ["home"] && rent.todo == "TODO" && rent.priority == "B" && rent.progress == "0/2")
94 #expect(!rent.isEvent && rent.item != nil)
95 }
96
97 @Test func gutterLinesAndSymbols() {
98 let rows = Self.rows()
99 #expect(rows["Dentist"]?.gutterLines == ["13:50", "14:30"])
100 #expect(rows["Standup"]?.gutterLines == ["08:00"])
101 #expect(rows["Send forms"]?.gutterLines == ["3d late"])
102 #expect(rows["Send forms"]?.symbol == "exclamationmark.circle.fill")
103 #expect(rows["Pay rent"]?.symbol == "exclamationmark.circle.fill")
104 #expect(rows["Took out bins"]?.symbol == "checkmark.circle.fill")
105 #expect(rows["File taxes"]?.symbol == "flag.fill")
106 #expect(rows["Water plants"]?.symbol == "calendar")
107 #expect(rows["Dentist"]?.symbol == "clock")
108 #expect(rows["Birthday"]?.symbol == "calendar")
109 }
110
111 @Test func logRowsKeepTheDurationAndState() {
112 let text = """
113 * DONE Write report
114 CLOSED: [2026-10-05 Mon 11:00]
115 :LOGBOOK:
116 - State "DONE" from "TODO" [2026-10-05 Mon 11:00]
117 CLOCK: [2026-10-05 Mon 09:00]--[2026-10-05 Mon 10:30] => 1:30
118 :END:
119 """
120 var options = AgendaOptions()
121 options.logMode = true
122 options.logItems = ["closed", "clock", "state"]
123 let source = AgendaSource(path: "w.org", text: text, defaults: OrgSettings.default.withTodoKeywords("TODO | DONE")!)
124 let items = Agenda.list([source], start: Self.today, days: 1, today: Self.today, now: (12, 0), options: options)[0].items
125 let rows = items.compactMap { AgendaRow(item: $0, day: Self.today, today: Self.today) }
126 #expect(rows.first { $0.item?.kind == .clock }?.detail == "1:30")
127 #expect(rows.first { $0.item?.kind == .state }?.detail == "DONE")
128 #expect(rows.first { $0.item?.kind == .closed }?.detail == nil)
129 }
130
131 @Test func guttersOnOtherDays() {
132 let later = Self.rows(day: Self.today + 4)
133 #expect(later["Renew passport"]?.gutter == "Due" && later["Renew passport"]?.status == .normal)
134 let earlier = Self.rows(day: Self.today - 3)
135 #expect(earlier["Send forms"]?.gutter == "Due" && earlier["Send forms"]?.status == .overdue)
136 }
137
138 @Test func emptyDaysAreHiddenButTodayStays() {
139 let days = Self.days(start: Self.today - 1, span: 8)
140 let shown = AgendaRows.groups(days, today: Self.today).map(\.day)
141 // Sunday has the conference and the habit's first day; Wednesday to Saturday only Friday's deadline.
142 #expect(shown == [Self.today - 1, Self.today, Self.today + 1, Self.today + 4, Self.today + 6])
143 #expect(AgendaRows.groups(days, today: Self.today, showAllDates: true).count == 8)
144 let empty = AgendaRows.groups(days, today: Self.today, keep: { _ in false })
145 #expect(empty.map(\.day) == [Self.today] && empty[0].isEmpty && empty[0].isToday)
146 }
147
148 @Test func overdueEntriesGroupAboveTodayOldestFirst() {
149 let today = AgendaRows.groups(Self.days(), today: Self.today)[0]
150 #expect(today.overdue.map(\.title) == ["Ancient chore", "Pay rent", "Send forms", "Walk dog", "Stretch"])
151 #expect(!today.rows.contains { $0.status == .overdue })
152 #expect(Set(today.rows.map(\.id)).count == today.rows.count)
153 }
154
155 @Test func eventsMergeByTime() {
156 let day = Self.today
157 let events = [
158 AgendaEvent(id: "a", title: "Coffee", calendarID: "c1", calendarTitle: "Work", color: RGBA(hex: "#ff0000"), start: Self.date(day, 9), end: Self.date(day, 9, 30), isAllDay: false),
159 AgendaEvent(id: "b", title: "Holiday", calendarID: "c2", calendarTitle: "Home", color: nil, start: Self.date(day, 0), end: Self.date(day + 1, 0), isAllDay: true),
160 AgendaEvent(id: "c", title: "Flight", calendarID: "c1", calendarTitle: "Work", color: nil, start: Self.date(day - 1, 22), end: Self.date(day + 1, 2), isAllDay: false),
161 AgendaEvent(id: "d", title: "Lunch", calendarID: "c1", calendarTitle: "Work", color: nil, start: Self.date(day, 13, 50), end: Self.date(day, 14, 30), isAllDay: false),
162 ]
163 let groups = AgendaRows.groups(Self.days(start: day - 1, span: 3), events: events, today: day, calendar: Self.utc)
164 let today = groups.first { $0.isToday }!
165 #expect(today.rows.prefix(2).map(\.title) == ["Holiday", "Flight"])
166 #expect(today.rows.map(\.gutter).prefix(6) == ["All day", "All day", "08:00", "09:00–09:30", "13:50–14:30", "13:50–14:30"])
167 #expect(today.rows[4].title == "Dentist" && today.rows[5].title == "Lunch")
168 #expect(today.rows[3].isEvent && today.rows[3].category == "Work" && today.rows[3].color == RGBA(hex: "#ff0000") && today.rows[3].icon == .event)
169 let before = groups.first { $0.day == day - 1 }!
170 #expect(before.rows.first { $0.title == "Flight" }?.gutter == "22:00")
171 let after = groups.first { $0.day == day + 1 }!
172 #expect(after.rows.first { $0.title == "Flight" }?.gutter == "Until 02:00")
173 // The all-day event ends at midnight: one day only.
174 #expect(!after.rows.contains { $0.title == "Holiday" })
175 }
176
177 @Test func nowLineGoesAmongTimedRows() {
178 let today = AgendaRows.groups(Self.days(), today: Self.today)[0]
179 let timed = today.rows.indices.filter { today.rows[$0].start != nil }
180 #expect(today.nowIndex(now: 700) == timed[0])
181 #expect(today.nowIndex(now: 800) == timed[1])
182 #expect(today.nowIndex(now: 1000) == timed[1])
183 #expect(today.nowIndex(now: 1500) == timed.last! + 1)
184 #expect(AgendaRows.nowIndex([], now: 900) == nil)
185 let later = AgendaRows.groups(Self.days(start: Self.today + 4), today: Self.today)[0]
186 #expect(later.nowIndex(now: 900) == nil)
187 }
188
189 @Test func timedHabitsGoAmongTimedRows() {
190 let file = """
191 * TODO Lunch
192 SCHEDULED: <2026-10-05 Mon 12:00>
193 * TODO Medication
194 SCHEDULED: <2026-10-05 Mon 20:00>
195 * TODO Bathe
196 SCHEDULED: <2026-10-05 Mon 18:30 .+1d>
197 :PROPERTIES:
198 :STYLE: habit
199 :END:
200 """
201 let source = AgendaSource(path: "life.org", text: file, defaults: OrgSettings.default.withTodoKeywords("TODO | DONE")!)
202 let days = Agenda.list([source], start: Self.today, days: 1, today: Self.today, now: (10, 0))
203 let day = Self.today
204 let events = [
205 AgendaEvent(id: "a", title: "Call", calendarID: "c1", calendarTitle: "Work", color: nil, start: Self.date(day, 19), end: Self.date(day, 19, 30), isAllDay: false),
206 ]
207 let today = AgendaRows.groups(days, events: events, today: day, calendar: Self.utc)[0]
208 #expect(today.rows.map(\.title) == ["Lunch", "Bathe", "Call", "Medication"])
209 #expect(today.nowIndex(now: 1800) == 1)
210 #expect(today.nowIndex(now: 1904) == 3)
211 #expect(today.grid.first { $0.time == 1800 }?.before == 1)
212 }
213
214 @Test func gridLinesGoBeforeLaterRows() {
215 let today = AgendaRows.groups(Self.days(), today: Self.today)[0]
216 #expect(today.grid.map(\.time) == [800, 1000, 1200, 1400, 1600, 1800, 2000])
217 let lines = today.lines(now: 1100)
218 // Each grid line comes before the first timed row after its time, entries at the same
219 // time first, as `time-up urgency-down` orders them; the rest after the last timed row.
220 var last = -1
221 for line in lines {
222 switch line {
223 case .row(let index):
224 if let start = today.rows[index].start { #expect(start >= last) ; last = start }
225 case .grid(let time): #expect(time >= last); last = time
226 case .now: #expect(1100 >= last); last = 1100
227 }
228 }
229 #expect(lines.filter { if case .row = $0 { return true } else { return false } }.count == today.rows.count)
230 #expect(lines.contains(.now))
231 var options = AgendaOptions()
232 options.useTimeGrid = false
233 let source = AgendaSource(path: "life.org", text: Self.file, defaults: OrgSettings.default.withTodoKeywords("TODO | DONE")!)
234 let plain = AgendaRows.groups(Agenda.list([source], start: Self.today, days: 1, today: Self.today, now: (10, 0), options: options), today: Self.today)[0]
235 #expect(plain.grid.isEmpty && plain.nowIndex(now: 1100) == nil)
236 }
237}
238
239struct AgendaThemeTests {
240 @Test func newKeysHaveDefaultsInBothAppearances() {
241 for key in ["todo-next", "todo-waiting", "todo-cancelled", "priority-a", "priority-b", "priority-c", "agenda-overdue", "agenda-due-soon", "agenda-event", "agenda-today"] {
242 #expect(ThemeSpec.default.light[key] != nil && ThemeSpec.default.dark[key] != nil, "\(key)")
243 }
244 }
245
246 @Test func keywordClasses() {
247 #expect(ThemeSpec.TodoClass.of("TODO", isDone: false) == .todo)
248 #expect(ThemeSpec.TodoClass.of("STRT", isDone: false) == .next)
249 #expect(ThemeSpec.TodoClass.of("NEXT", isDone: false) == .next)
250 #expect(ThemeSpec.TodoClass.of("WAIT", isDone: false) == .waiting)
251 #expect(ThemeSpec.TodoClass.of("HOLD", isDone: false) == .waiting)
252 #expect(ThemeSpec.TodoClass.of("DONE", isDone: true) == .done)
253 #expect(ThemeSpec.TodoClass.of("KILL", isDone: true) == .cancelled)
254 #expect(ThemeSpec.TodoClass.of("SKIPPED", isDone: true) == .cancelled)
255 // Only done keywords cancel.
256 #expect(ThemeSpec.TodoClass.of("SKIPPED", isDone: false) == .todo)
257 }
258
259 @Test func colorsResolveOverridesFirst() throws {
260 let (spec, problems) = ThemeSpec.reading(try TOML.parse("""
261 [theme.todo]
262 WAIT = "#010203"
263 [theme.category]
264 work = "#0a0b0c"
265 """))
266 #expect(problems.isEmpty)
267 #expect(spec.todoColor("WAIT", isDone: false, dark: true)?.hex == "#010203")
268 #expect(spec.todoColor("HOLD", isDone: false, dark: true) == ThemeSpec.default.dark["todo-waiting"])
269 #expect(spec.todoColor("KILL", isDone: true, dark: false) == ThemeSpec.default.light["todo-cancelled"])
270 #expect(spec.categoryColor("work", dark: false).hex == "#0a0b0c" && spec.categoryColor("work", dark: true).hex == "#0a0b0c")
271 #expect(spec.priorityColor("A", dark: false) == ThemeSpec.default.light["priority-a"])
272 #expect(spec.priorityColor("D", dark: false) == ThemeSpec.default.light["priority"])
273 #expect(spec.statusColor(.overdue, dark: true) == ThemeSpec.default.dark["agenda-overdue"])
274 let event = AgendaEvent(id: "x", title: "", calendarID: "", calendarTitle: "", color: nil, start: Date(), end: Date(), isAllDay: true)
275 #expect(spec.eventColor(event, dark: false) == ThemeSpec.default.light["agenda-event"])
276 }
277
278 @Test func categoryHuesAreStable() {
279 #expect(ThemeSpec.categoryHue("work") == 264)
280 #expect(ThemeSpec.categoryHue("home") == 206)
281 #expect(ThemeSpec.default.categoryColor("home", dark: false) == ThemeSpec.default.categoryColor("home", dark: false))
282 #expect(ThemeSpec.default.categoryColor("home", dark: false) != ThemeSpec.default.categoryColor("home", dark: true))
283 #expect(RGBA(hue: 0, saturation: 1, lightness: 0.5).hex == "#ff0000")
284 #expect(RGBA(hue: 240, saturation: 1, lightness: 0.5).hex == "#0000ff")
285 }
286}