Tests/OrgCoreTests/TimestampCommandTests.swift

d4c76b8503d7069cc3c2404db3cafc46defe1a9a
orgstar/Tests/OrgCoreTests/TimestampCommandTests.swift history · blame · raw

183 lines · 11144 bytes

  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5struct TimestampCommandTests {
  6    static let now = ReadDateTests.now
  7    static let calendar = ReadDateTests.utc
  8
  9    func run(_ command: any OrgCommand, _ text: String, caret: Int, answer: String? = nil) -> (text: String, caret: Int, failure: String?) {
 10        let context = EditContext(
 11            revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret],
 12            now: Self.now, calendar: Self.calendar, answers: answer.map { ["date": $0] } ?? [:]
 13        )
 14        switch command.run(in: context) {
 15        case .commit(let result):
 16            let new = result.edits.sorted { $0.range.lowerBound > $1.range.lowerBound }.reduce(text) { $1.apply(to: $0) }
 17            return (new, result.selection?.first?.lowerBound ?? caret, nil)
 18        case .failed(let message): return (text, caret, message)
 19        case .prompt(let prompt): return (text, caret, "prompt \(prompt.key)")
 20        case .external(let request): return (text, caret, "external \(request)")
 21        }
 22    }
 23
 24    @Test func examples() {
 25        #expect(run(InsertTimestamp(), "x\n", caret: 1, answer: "fri").text == "x<2026-09-11 Fri>\n")
 26        #expect(run(SetPlanning(deadline: false), "* a\n", caret: 0, answer: "+1").text == "* a\nSCHEDULED: <2026-09-11 Fri>\n")
 27        #expect(run(ShiftTimestampDay(later: true), "<2026-09-10 Thu>\n", caret: 5).text == "<2026-09-11 Fri>\n")
 28        #expect(run(ShiftTimestamp(up: true), "<2026-09-10 Thu 10:07>\n", caret: 20).text == "<2026-09-10 Thu 10:10>\n")
 29    }
 30
 31    @Test func promptSaysWhetherTheStampIsInactive() {
 32        for inactive in [false, true] {
 33            let context = EditContext(revision: 0, text: "x\n", tree: OrgParser.parse("x\n"), selection: [1..<1], now: Self.now, calendar: Self.calendar)
 34            guard case .prompt(let prompt) = InsertTimestamp(inactive: inactive).run(in: context) else {
 35                Issue.record("no prompt")
 36                continue
 37            }
 38            #expect(prompt.date?.inactive == inactive)
 39        }
 40    }
 41
 42    static let texts = [
 43        "* a\n",
 44        "* a\nbody <2026-09-01 Tue> text\n",
 45        "* a\nSCHEDULED: <2026-09-01 Tue +1w>\n",
 46        "* a\nDEADLINE: <2026-09-20 Sun> SCHEDULED: <2026-09-01 Tue 10:00-11:30>\n",
 47        "* a\nCLOSED: [2026-09-02 Wed 09:00] SCHEDULED: <2026-09-01 Tue>\n",
 48        "* a\n[2026-12-31 Thu 23:58] <2026-01-31 Sat .+2m -3d>\n",
 49        "text <2026-02-28 Sat 07:03>\n",
 50    ]
 51    static let answers = ["", "+1", "fri 9:00", "2027-01-15", "10:00-11:00", "++2d"]
 52
 53    static let commands: [(any OrgCommand, String)] = [
 54        (InsertTimestamp(), "(org-timestamp nil)"),
 55        (InsertTimestamp(inactive: true), "(org-timestamp nil t)"),
 56        (SetPlanning(deadline: false), "(org-schedule nil)"),
 57        (SetPlanning(deadline: true), "(org-deadline nil)"),
 58        (RemovePlanning(deadline: false), "(org-schedule '(4))"),
 59        (ShiftTimestamp(up: true), "(org-timestamp-up)"),
 60        (ShiftTimestamp(up: false), "(org-timestamp-down)"),
 61        (ShiftTimestampDay(later: true), "(org-timestamp-up-day)"),
 62        (ShiftTimestampDay(later: false), "(org-timestamp-down-day)"),
 63    ]
 64
 65    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 66    func timestampCommandsMatchEmacs() throws {
 67        try #require(EmacsOracle.isAvailable)
 68        var cases: [EmacsOracle.Case] = []
 69        var ours: [(label: String, text: String, caret: Int, failed: Bool)] = []
 70        for text in Self.texts {
 71            let tree = OrgParser.parse(text)
 72            for caret in EmacsOracle.positions(text) {
 73                for (command, form) in Self.commands {
 74                    let prompts = command is InsertTimestamp || command is SetPlanning
 75                    if command is RemovePlanning || command is SetPlanning, entryHeading(at: caret, in: tree) == nil { continue }
 76                    if command is ShiftTimestamp || command is ShiftTimestampDay, EmacsBuffer(text, point: caret).atTimestamp() == nil { continue }
 77                    for answer in prompts ? Self.answers : [""] {
 78                        let escaped = answer.replacingOccurrences(of: "\"", with: "\\\"")
 79                        let wrapped = frozenClock(1_789_000_000, """
 80                            (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "\(escaped)")))
 81                              (let ((org-read-date-popup-calendar nil)) \(form)))
 82                            """)
 83                        cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: wrapped))
 84                        let result = run(command, text, caret: caret, answer: prompts ? answer : nil)
 85                        ours.append(("\(command.id) \(answer.debugDescription) at \(caret) in \(text.debugDescription)", result.text, result.caret, result.failure != nil))
 86                    }
 87                }
 88            }
 89        }
 90        var mismatches = 0
 91        for (mine, emacs) in zip(ours, try EmacsOracle.run(cases)) {
 92            let failed = !emacs.error.isEmpty
 93            let caret = EmacsOracle.offset(emacs.point, in: emacs.text)
 94            guard mine.failed != failed || (!failed && (mine.text != emacs.text || mine.caret != caret)) else { continue }
 95            mismatches += 1
 96            if mismatches <= 12 {
 97                Issue.record("\(mine.label)\n  ours:  \(mine.failed ? "failed" : "\(mine.text.debugDescription) @\(mine.caret)")\n  emacs: \(failed ? emacs.error : "\(emacs.text.debugDescription) @\(caret)")")
 98            }
 99        }
100        #expect(mismatches == 0, "\(mismatches) of \(ours.count) differ")
101    }
102
103    /// Shifting a timestamp on a CLOCK line rewrites the duration (`org-clock-update-time-maybe`).
104    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
105    func clockShiftsMatchEmacs() throws {
106        let text = "* a\n:LOGBOOK:\nCLOCK: [2026-09-01 Tue 10:00]--[2026-09-01 Tue 11:30] =>  1:30\n  CLOCK: [2026-09-02 Wed 23:58]--[2026-09-03 Thu 00:02]\nCLOCK: [2026-09-04 Fri 09:00]\n:END:\n"
107        let commands: [(any OrgCommand, String)] = [
108            (ShiftTimestamp(up: true), "(org-timestamp-up)"), (ShiftTimestamp(up: false), "(org-timestamp-down)"),
109            (ShiftTimestampDay(later: true), "(org-timestamp-up-day)"), (ShiftTimestampDay(later: false), "(org-timestamp-down-day)"),
110        ]
111        var cases: [EmacsOracle.Case] = []
112        var ours: [(label: String, text: String, caret: Int, failed: Bool)] = []
113        for caret in EmacsOracle.positions(text) where EmacsBuffer(text, point: caret).atTimestamp() != nil {
114            for (command, form) in commands {
115                cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: frozenClock(1_789_000_000, form)))
116                let result = run(command, text, caret: caret)
117                ours.append(("\(command.id) at \(caret)", result.text, result.caret, result.failure != nil))
118            }
119        }
120        var mismatches = 0
121        for (mine, emacs) in zip(ours, try EmacsOracle.run(cases)) {
122            let failed = !emacs.error.isEmpty
123            let caret = EmacsOracle.offset(emacs.point, in: emacs.text)
124            guard mine.failed != failed || (!failed && (mine.text != emacs.text || mine.caret != caret)) else { continue }
125            mismatches += 1
126            if mismatches <= 12 {
127                Issue.record("\(mine.label)\n  ours:  \(mine.failed ? "failed" : "\(mine.text.debugDescription) @\(mine.caret)")\n  emacs: \(failed ? emacs.error : "\(emacs.text.debugDescription) @\(caret)")")
128            }
129        }
130        #expect(mismatches == 0, "\(mismatches) of \(ours.count) differ")
131        #expect(run(ShiftTimestamp(up: true), text, caret: (text as NSString).range(of: "11:30").location + 4).text.contains("11:35] =>  1:35\n"))
132    }
133
134    static let repeating = [
135        "* TODO a\nSCHEDULED: <2026-09-01 Tue +1w>\n",
136        "* TODO a\nDEADLINE: <2026-09-01 Tue .+1d>\n",
137        "* TODO a\nSCHEDULED: <2026-08-01 Sat ++1m>\nbody\n",
138        "* TODO a :t:\n<2026-09-08 Tue 10:00 +2d>\n",
139        "* TODO a\nDEADLINE: <2026-09-05 Sat +1w> SCHEDULED: <2026-09-01 Tue>\n",
140        "* TODO a\nSCHEDULED: <2026-09-01 Tue +1w>\n:PROPERTIES:\n:LAST_REPEAT: [2026-09-01 Tue 08:00]\n:END:\n- State \"DONE\"       from \"TODO\"       [2026-09-01 Tue 08:00]\nnotes\n",
141        "#+TODO: TODO STRT | DONE\n* STRT a\nSCHEDULED: <2026-09-01 Tue +1d>\n",
142        "* P [0/1]\n** TODO a\nSCHEDULED: <2026-09-01 Tue +1w>\n* Q\n",
143        "* TODO no repeat\nSCHEDULED: <2026-09-01 Tue>\n",
144        "* TODO hourly\nSCHEDULED: <2026-09-10 Thu 00:00 +1h>\n",
145        "* TODO from now\nSCHEDULED: <2026-09-08 Tue 10:00 .+2h>\n",
146        "* TODO from now\n<2026-09-10 Thu 03:15-04:00 .+1h>\n",
147        "* TODO catch up\nSCHEDULED: <2026-09-09 Wed 23:00 ++2h>\n",
148    ]
149
150    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
151    func repeatsMatchEmacs() throws {
152        try #require(EmacsOracle.isAvailable)
153        var cases: [EmacsOracle.Case] = []
154        var ours: [(label: String, text: String, caret: Int)] = []
155        for text in Self.repeating {
156            let ns = text as NSString
157            let heading = ns.range(of: "\n\\*|^\\*", options: .regularExpression)
158            let start = heading.location == 0 ? 0 : heading.location + 1
159            let lineEnd = ns.range(of: "\n", range: NSRange(location: start, length: ns.length - start)).location
160            for caret in [start, start + 2, lineEnd, min(lineEnd + 3, ns.length)] {
161                cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: frozenClock(1_789_000_000, "(progn (org-todo) (when org-log-setup (org-add-log-note)))")))
162                let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], now: Self.now, calendar: Self.calendar)
163                guard case .commit(let result) = TodoCycle().run(in: context) else {
164                    ours.append(("failed at \(caret)", "", -1))
165                    continue
166                }
167                let new = result.edits.sorted { $0.range.lowerBound > $1.range.lowerBound }.reduce(text) { $1.apply(to: $0) }
168                ours.append(("at \(caret) in \(text.debugDescription)", new, result.selection?.first?.lowerBound ?? caret))
169            }
170        }
171        var mismatches = 0
172        for (mine, emacs) in zip(ours, try EmacsOracle.run(cases)) {
173            let caret = EmacsOracle.offset(emacs.point, in: emacs.text)
174            guard !emacs.error.isEmpty || mine.text != emacs.text || mine.caret != caret else { continue }
175            mismatches += 1
176            if mismatches <= 10 {
177                let (a, b) = excerpts(mine.text, emacs.text)
178                Issue.record("\(mine.label)\n  ours:  \(a.debugDescription) @\(mine.caret)\n  emacs: \(emacs.error.isEmpty ? "\(b.debugDescription) @\(caret)" : emacs.error)")
179            }
180        }
181        #expect(mismatches == 0, "\(mismatches) of \(ours.count) differ")
182    }
183}