Tests/OrgCoreTests/ClockTableTests.swift

9abba9cfc504b344b7d267978a97f1c925a1d5dd
orgstar/Tests/OrgCoreTests/ClockTableTests.swift history · blame · raw

88 lines · 4393 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct ClockTableTests {
 6    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
 7
 8    static let body = """
 9        * Project Alpha :work:
10        :PROPERTIES:
11        :Effort:   3:00
12        :END:
13        ** TODO Design the [[https://x.org][interface]] [1/2]
14        :LOGBOOK:
15        CLOCK: [2026-09-07 Mon 09:00]--[2026-09-07 Mon 11:30] =>  2:30
16        CLOCK: [2026-09-09 Wed 23:00]--[2026-09-10 Thu 01:00] =>  2:00
17        :END:
18        *** A very long heading title that goes on and on beyond the narrowing width
19        CLOCK: [2026-09-10 Thu 08:00]--[2026-09-10 Thu 08:45] =>  0:45
20        ** Write code
21        CLOCK: [2026-08-30 Sun 10:00]--[2026-08-30 Sun 12:00] =>  2:00
22        CLOCK: => 1:15
23        * COMMENT Beta
24        CLOCK: [2026-09-08 Tue 14:00]--[2026-09-09 Wed 16:00] => 26:00
25        * Untouched
26
27        """
28
29    static let params = [
30        ":scope file :maxlevel 2", ":maxlevel 3 :emphasize t", ":maxlevel 1", ":block thisweek", ":block lastweek",
31        ":block today :maxlevel 3", ":block 2026-09", ":tstart \"<2026-09-08>\" :tend \"<2026-09-10>\"",
32        ":link t :maxlevel 3", ":narrow 20! :maxlevel 3", ":tags t :level t :maxlevel 3", ":properties (\"Effort\") :formula %",
33        ":indent nil :maxlevel 3", ":block yesterday", ":block 2026-W36 :maxlevel 3", ":fileskip0 t :block 2025",
34    ]
35
36    static func ours(_ text: String, caret: Int, command: any OrgCommand, answers: [String: String] = [:]) -> String {
37        var utc = Calendar(identifier: .gregorian)
38        utc.timeZone = TimeZone(identifier: "UTC")!
39        let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret],
40                                  now: Date(timeIntervalSince1970: 1_789_000_000), calendar: utc, answers: answers)
41        switch command.run(in: context) {
42        case .commit(let result):
43            var new = text
44            for edit in result.edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { new = edit.apply(to: new) }
45            return new
46        case .failed(let message): return "failed: \(message)"
47        default: return "other"
48        }
49    }
50
51    @Test(.enabled(if: oracle))
52    func updatesLikeOrgUpdateDblock() throws {
53        var cases: [EmacsOracle.Case] = []
54        var ours: [String] = []
55        for param in Self.params {
56            let text = "#+BEGIN: clocktable \(param)\nold contents\n#+END:\n" + Self.body
57            cases.append(EmacsOracle.Case(text: text, point: 1, form: frozenClock(1_789_000_000, "(org-update-dblock)")))
58            ours.append(Self.ours(text, caret: 0, command: UpdateDynamicBlock()))
59        }
60        for (i, (mine, theirs)) in zip(ours, try EmacsOracle.run(cases)).enumerated() {
61            #expect(mine == theirs.text, "\(Self.params[i])\nours:\n\(mine)\nemacs:\n\(theirs.text) \(theirs.error)")
62        }
63    }
64
65    @Test(.enabled(if: oracle))
66    func reportsLikeOrgClockReport() throws {
67        let text = "Intro\n" + Self.body
68        let ns = text as NSString
69        let points = [ns.range(of: "Intro").location + 2, ns.range(of: "** Write").location + 3, ns.range(of: "CLOCK: [2026-09-07").location]
70        let cases = points.map { EmacsOracle.Case(text: text, point: EmacsOracle.point($0, in: text), form: frozenClock(1_789_000_000, "(org-clock-report)")) }
71        let ours = points.map { Self.ours(text, caret: $0, command: ClockReportTable()) }
72        for (i, (mine, theirs)) in zip(ours, try EmacsOracle.run(cases)).enumerated() {
73            #expect(mine == theirs.text, "at \(points[i])\nours:\n\(mine)\nemacs:\n\(theirs.text) \(theirs.error)")
74        }
75    }
76
77    @Test(.enabled(if: oracle))
78    func setsEffortLikeOrgSetEffort() throws {
79        let text = "* Task\n* Other\n:PROPERTIES:\n:Effort:   1:00\n:END:\n"
80        let cases = [(0, "0:30"), (8, "2:00")].map { EmacsOracle.Case(text: text, point: $0.0 + 1, form: "(org-set-effort nil \"\($0.1)\")") }
81        let ours = [(0, "0:30"), (8, "2:00")].map { Self.ours(text, caret: $0.0, command: SetEffort(), answers: ["effort": $0.1]) }
82        #expect(ours == (try EmacsOracle.run(cases)).map(\.text))
83    }
84
85    @Test func durations() {
86        #expect(ClockTable.duration(0) == "0:00" && ClockTable.duration(95) == "1:35" && ClockTable.duration(1560) == "1d 2:00")
87    }
88}