Tests/OrgAppTests/WidgetDataTests.swift

d85099bc95bf1766aee990d94671dacda0c0686e
orgstar/Tests/OrgAppTests/WidgetDataTests.swift history · blame · raw

92 lines · 3600 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}
50
51struct PropertyInheritanceTests {
52    static let file = """
53        #+PROPERTY: STYLE habit
54        * TODO Stretch
55        SCHEDULED: <2026-10-05 Mon .+1d>
56        * Work
57        :PROPERTIES:
58        :TEAM: ops
59        :END:
60        ** TODO Deploy
61        """
62
63    static func source(_ inheritance: String) -> AgendaSource {
64        var defaults = OrgSettings.default
65        defaults.propertyInheritance = .init(inheritance)
66        return AgendaSource(path: "/notes/habits.org", text: file, defaults: defaults)
67    }
68
69    /// `org-is-habit-p` reads STYLE with selective inheritance: a `#+PROPERTY` line counts only
70    /// when `org-use-property-inheritance` covers STYLE, as Emacs 31 does.
71    @Test func fileStyleMakesHabitsWhenStyleInherits() {
72        #expect(Self.source("nil").habits.isEmpty)
73        #expect(Self.source("STYLE").habits.map(\.title) == ["Stretch"])
74        #expect(Self.source("t").habits.map(\.title) == ["Stretch"])
75        #expect(Self.source("TEAM").habits.isEmpty)
76    }
77
78    @Test func matchesSeeInheritedProperties() {
79        func titles(_ inheritance: String) -> [String] {
80            Agenda.tagsMatch([Self.source(inheritance)], match: "TEAM=\"ops\"").map(\.text)
81        }
82        #expect(titles("nil").count == 1)
83        #expect(titles("TEAM").count == 2)
84    }
85
86    @Test func readsTheConfigValue() {
87        #expect(SemanticSettings.PropertyInheritance("") == .none)
88        #expect(SemanticSettings.PropertyInheritance("nil") == .none)
89        #expect(SemanticSettings.PropertyInheritance("t") == .all)
90        #expect(SemanticSettings.PropertyInheritance("style  Team") == .only(["STYLE", "TEAM"]))
91    }
92}