Tests/OrgWatchDataTests/WatchAgendaTests.swift
57 lines · 3293 bytes
6 symbols in this file
1import Foundation
2import Testing
3@testable import OrgWatchData
4
5struct WatchAgendaTests {
6 static var utc: Calendar {
7 var c = Calendar(identifier: .gregorian)
8 c.timeZone = TimeZone(identifier: "UTC")!
9 return c
10 }
11
12 static func entry(_ id: String, start: Int? = nil, end: Int? = nil, todo: String? = "TODO", done: Bool = false, event: Bool = false) -> WatchAgenda.Entry {
13 let midnight = utc.date(from: DateComponents(year: 2026, month: 10, day: 12))!
14 func at(_ hhmm: Int?) -> Date? { hhmm.map { midnight.addingTimeInterval(Double(($0 / 100) * 3600 + ($0 % 100) * 60)) } }
15 return WatchAgenda.Entry(
16 id: id, title: id, gutter: start == nil ? "Scheduled" : "", category: "work", todo: todo, status: done ? "done" : "normal", isDone: done,
17 isEvent: event, start: at(start), end: at(end), path: "/n/work.org", offset: 10, headingLine: "* \(todo ?? "") \(id)", doneKeyword: "DONE"
18 )
19 }
20
21 static let agenda = WatchAgenda(
22 generated: Date(timeIntervalSince1970: 0), day: utc.date(from: DateComponents(year: 2026, month: 10, day: 12))!,
23 overdue: [entry("late")],
24 rows: [entry("standup", start: 900, end: 915), entry("lunch", start: 1200, todo: nil, event: true), entry("review", start: 1530),
25 entry("done", done: true), entry("plants")],
26 clock: .init(heading: "Review", startedAt: Date(timeIntervalSince1970: 100)), lastClocked: "Review"
27 )
28
29 @Test func upcomingLeavesOutWhatsOver() {
30 let ten = Self.utc.date(from: DateComponents(year: 2026, month: 10, day: 12, hour: 10))!
31 #expect(Self.agenda.upcoming(at: ten, calendar: Self.utc).map(\.id) == ["lunch", "review", "plants", "late"])
32 #expect(Self.agenda.next(at: ten, calendar: Self.utc)?.id == "lunch")
33 #expect(Self.agenda.openCount(at: ten, calendar: Self.utc) == 4)
34 let tomorrow = ten.addingTimeInterval(86400)
35 #expect(Self.agenda.upcoming(at: tomorrow, calendar: Self.utc).isEmpty && Self.agenda.openCount(at: tomorrow, calendar: Self.utc) == 0)
36 }
37
38 @Test func markingDoneHere() {
39 let marked = Self.agenda.markingDone("late")
40 #expect(marked.overdue[0].isDone && marked.overdue[0].todo == "DONE" && !marked.overdue[0].canBeDone)
41 #expect(Self.agenda.overdue[0].done == .done(path: "/n/work.org", offset: 10, headingLine: "* TODO late", keyword: "DONE"))
42 #expect(Self.agenda.rows[1].done == nil)
43 }
44
45 @Test func roundTrips() throws {
46 #expect(WatchAgenda.decode(try Self.agenda.data()) == Self.agenda)
47 for action in [WatchAction.done(path: "/a.org", offset: 3, headingLine: "* TODO x", keyword: "DONE"), .clockOut, .clockInLast, .capture(text: "Buy milk")] {
48 #expect(WatchAction(userInfo: action.userInfo) == action)
49 }
50 #expect(WatchAction(userInfo: [:]) == nil)
51 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-watch-\(UUID().uuidString)")
52 defer { try? FileManager.default.removeItem(at: folder) }
53 let file = folder.appendingPathComponent("watch.json")
54 #expect(try Self.agenda.write(to: file) && !(try Self.agenda.write(to: file)))
55 #expect(WatchAgenda.read(from: file) == Self.agenda)
56 }
57}