Tests/OrgAppTests/WidgetDataTests.swift
49 lines · 1949 bytes
1import Foundation
2import OrgCore
3import OrgPresentation
4import Testing
5@testable import OrgApp
6
7struct WidgetDataTests {
8 static let file = """
9 * TODO Stretch
10 SCHEDULED: <2026-10-05 Mon .+1d>
11 :PROPERTIES:
12 :STYLE: habit
13 :END:
14 :LOGBOOK:
15 - State "DONE" from "TODO" [2026-10-04 Sun 08:00]
16 :END:
17 * TODO Water plants
18 SCHEDULED: <2026-10-05 Mon>
19 * DONE Old habit
20 SCHEDULED: <2026-10-05 Mon .+1d>
21 :PROPERTIES:
22 :STYLE: habit
23 :END:
24 """
25
26 @Test func habitsOfTheFilesWithTheirGraphsAndColors() throws {
27 let today = Days.absolute(year: 2026, month: 10, day: 5)
28 let source = AgendaSource(path: "/notes/life.org", text: Self.file, defaults: .default)
29 let habits = source.habits.map { HabitSnapshot.Entry(title: $0.title, path: source.path, offset: $0.offset, habit: $0.habit) }
30 let snapshot = HabitSnapshot(habits: habits, theme: .default)
31 #expect(snapshot.habits.map(\.title) == ["Stretch"])
32 let habit = try #require(snapshot.habits.first)
33 #expect(habit.link == .entry(path: "/notes/life.org", offset: 0))
34 #expect(habit.habit.graph(today: today).contains { $0.glyph == "*" })
35 #expect(Set(snapshot.colors.keys) == ["habit-clear", "habit-ready", "habit-alert", "habit-overdue"])
36 }
37
38 @Test func writesOnlyWhatChanged() throws {
39 let folder = try Folder()
40 let file = folder.url.appendingPathComponent("Widgets/clock.json")
41 var clock = ClockSnapshot(running: "Write", startedAt: Date(timeIntervalSince1970: 1_000_000), last: "Read")
42 #expect(try clock.write(to: file))
43 #expect(ClockSnapshot.read(from: file) == clock)
44 #expect(try !clock.write(to: file))
45 clock.running = nil
46 #expect(try clock.write(to: file))
47 #expect(ClockSnapshot.read(from: file)?.running == nil)
48 }
49}