Tests/OrgCoreTests/TodoLoggingTests.swift
89 lines · 4878 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct TodoLoggingTests {
6 static let texts = [
7 "#+TODO: TODO(t) WAIT(w@/!) | DONE(d!) CANCELED(c@)\n* TODO Task\nBody\n",
8 "* TODO Task\nSCHEDULED: <2026-10-05 Mon>\n:PROPERTIES:\n:X: 1\n:END:\nBody\n",
9 "* TODO Task\n:LOGBOOK:\n- State \"TODO\" from [2026-01-01 Thu 10:00]\n:END:\n",
10 "* DONE Task\nCLOSED: [2026-01-01 Thu 10:00]\n",
11 "#+STARTUP: lognotedone logdrawer\n* TODO Task\n",
12 "* TODO Task\n:PROPERTIES:\n:LOGGING: DONE(!) logrepeat\n:LOG_INTO_DRAWER: NOTES\n:END:\n",
13 "* TODO Task\nSCHEDULED: <2026-10-05 Mon +1w>\n",
14 "* TODO Task\n- item one\n",
15 "* TODO Task\n** Child\n",
16 "* TODO Task",
17 ]
18
19 static let options: [(LogHow?, String?)] = [(nil, nil), (.time, nil), (.time, "LOGBOOK"), (.note, nil), (.note, "LOGBOOK")]
20
21 static let note = "Called back\nsecond line"
22
23 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
24 func logsLikeOrgTodo() throws {
25 let changes: [(any OrgCommand, String)] = [
26 (TodoCycle(), "(org-todo)"),
27 (SetTodo(keyword: "DONE"), "(org-todo \"DONE\")"),
28 (SetTodo(keyword: "TODO"), "(org-todo \"TODO\")"),
29 (SetTodo(keyword: nil), "(org-todo 'none)"),
30 (SetTodo(keyword: "WAIT"), "(org-todo \"WAIT\")"),
31 (SetTodo(keyword: "CANCELED"), "(org-todo \"CANCELED\")"),
32 ]
33 var utc = Calendar(identifier: .gregorian)
34 utc.timeZone = TimeZone(identifier: "UTC")!
35 let now = Date(timeIntervalSince1970: 1_789_000_000)
36 var cases: [EmacsOracle.Case] = []
37 var ours: [String] = []
38 for text in Self.texts {
39 let heading = (text as NSString).range(of: "Task").location
40 for (done, drawer) in Self.options {
41 let options = EditingOptions(logDone: done, logIntoDrawer: drawer, fastTodoSelection: false)
42 for (command, form) in changes {
43 let lisp = """
44 (let ((b (current-buffer)) (org-use-fast-todo-selection nil))
45 ;; As the user's settings, under the file's #+STARTUP.
46 (setq-default org-log-done \(done.map { "'" + $0.rawValue } ?? "nil") org-log-into-drawer \(drawer.map { "\"\($0)\"" } ?? "nil"))
47 (org-mode)
48 \(form)
49 (run-hooks 'post-command-hook)
50 (when (get-buffer "*Org Note*")
51 (with-current-buffer "*Org Note*" (goto-char (point-max)) (insert \(lispString(Self.note))) (org-store-log-note)))
52 (set-buffer b))
53 """
54 cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(heading, in: text), form: frozenClock(1_789_000_000, lisp)))
55 var answers: [String: String] = [:]
56 var result = text
57 while true {
58 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [heading..<heading], now: now, calendar: utc, answers: answers, options: options)
59 switch command.run(in: context) {
60 case .commit(let r):
61 for edit in r.edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { result = edit.apply(to: result) }
62 case .prompt(let prompt) where prompt.key == "note" && answers["note"] == nil:
63 answers["note"] = Self.note
64 continue
65 default: break
66 }
67 break
68 }
69 ours.append(result)
70 }
71 }
72 }
73 let emacs = try EmacsOracle.run(cases)
74 for (i, (mine, theirs)) in zip(ours, emacs).enumerated() {
75 #expect(mine == theirs.text, "\(cases[i].form.split(separator: "\n")[1...2].joined()) on \(cases[i].text.debugDescription)\nours: \(mine.debugDescription)\nemacs: \(theirs.text.debugDescription) \(theirs.error)")
76 }
77 }
78
79 @Test func asksForTheNote() {
80 let text = "#+TODO: TODO(t) WAIT(w@/!) | DONE(d!)\n* TODO Task\n"
81 let heading = (text as NSString).range(of: "Task").location
82 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [heading..<heading])
83 #expect(SetTodo(keyword: "WAIT").run(in: context) == .prompt(Prompt(key: "note", message: "Note for state change from \"TODO\" to \"WAIT\":")))
84 }
85}
86
87func lispString(_ s: String) -> String {
88 "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + "\""
89}