Tests/OrgCoreTests/AgendaSkipTests.swift

81 lines · 4041 bytes

3 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// `org-agenda-skip-entry-if` and `org-agenda-skip-subtree-if` against Emacs.
 6struct AgendaSkipTests {
 7    static let today = Days.absolute(year: 2026, month: 10, day: 5)
 8    static let file = """
 9    * TODO Parent :work:
10    SCHEDULED: <2026-10-05 Mon>
11    ** TODO Child one
12    DEADLINE: <2026-10-06 Tue>
13    ** WAIT Child two
14    <2026-10-05 Mon 10:00>
15    Mentions IGNORE here.
16    *** NEXT Grandchild
17    SCHEDULED: <2026-10-04 Sun>
18    * DONE Finished
19    CLOSED: [2026-10-05 Mon 09:00]
20    <2026-10-05 Mon 12:00>
21    * HOLD Plain :home:
22    Some text with a <2026-10-07 Wed> timestamp.
23    * IDEA Range
24    <2026-10-04 Sun>--<2026-10-06 Tue>
25    * TODO Waiting for IGNORE
26    DEADLINE: <2026-10-05 Mon>
27    ** TODO Under ignore
28    SCHEDULED: <2026-10-05 Mon 15:00>
29    * Sexp
30    <%%(diary-float t 1 1)>
31
32    """
33
34    static let conditions = [
35        "'scheduled", "'notscheduled", "'deadline", "'notdeadline", "'timestamp", "'nottimestamp",
36        "'regexp \"IGNORE\"", "'notregexp \"IGNORE\"", "'todo '(\"WAIT\" \"NEXT\")", "'nottodo '(\"TODO\")",
37        "'todo 'done", "'nottodo 'todo", "'todo 'any", "'todo '(\"*\")", "'scheduled 'deadline",
38    ]
39
40    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
41    func skipsLikeOrg() throws {
42        var utc = Calendar(identifier: .gregorian)
43        utc.timeZone = TimeZone(identifier: "UTC")!
44        let now = Date(timeIntervalSince1970: Double((Self.today - 719163) * 86400 + 9 * 3600 + 30 * 60))
45        let defaults = OrgSettings.default.withTodoKeywords(AgendaOracle.keywords)!
46        let files = [("s.org", Self.file)]
47        for subtree in [false, true] {
48            for condition in Self.conditions {
49                let skip = try AgendaSkip.parse(condition, subtree: subtree).get()
50                let fn = "'(org-agenda-skip-\(subtree ? "subtree" : "entry")-if \(condition))"
51                var options = AgendaOptions()
52                options.skip = skip
53                for (form, ours) in [
54                    ("(org-todo-list nil)", { (s: [AgendaSource]) in Agenda.todoList(s, options: options, today: Self.today) }),
55                    ("(org-tags-view nil \"LEVEL>0\")", { Agenda.tagsMatch($0, match: "LEVEL>0", now: now, calendar: utc, options: options) }),
56                    ("(org-search-view nil \"e\")", { Agenda.textSearch($0, string: "e", options: options) }),
57                ] {
58                    try AgendaOracle.compareList(files: files, form: "(let ((org-agenda-skip-function \(fn))) \(form))", today: Self.today, ours)
59                }
60                let form = "(let ((org-agenda-skip-function \(fn)) (org-agenda-start-with-log-mode t)) (org-agenda-list nil \"2026-10-04\" 3))"
61                let emacs = try AgendaOracle.run(files: files, form: form, today: Self.today, hour: 9, minute: 30)
62                var dayOptions = AgendaOptions.terminal
63                dayOptions.skip = skip
64                dayOptions.logMode = true
65                let days = Agenda.list([AgendaSource(path: "s.org", text: Self.file, defaults: defaults)], start: Self.today - 1, days: 3,
66                                       today: Self.today, now: (9, 30), options: dayOptions)
67                #expect(days.count == emacs.count)
68                for (o, e) in zip(days, emacs) {
69                    #expect(AgendaOracle.describe(o.items, today: Self.today) == AgendaOracle.describe(e.items), "\(fn) \(e.header)")
70                }
71            }
72        }
73    }
74
75    @Test func parsesAndWrites() throws {
76        let skip = try AgendaSkip.parse("'scheduled 'regexp \"\\\\<A\\\"B\" 'nottodo '(\"DONE\" \"KILL\") 'todo done", subtree: true).get()
77        #expect(skip.conditions == [.scheduled, .regexp("\\<A\"B"), .notTodo(.list(["DONE", "KILL"])), .todo(.done)])
78        #expect(try AgendaSkip.parse(skip.lisp, subtree: true).get() == skip)
79        #expect(throws: AgendaSkip.Failure.self) { try AgendaSkip.parse("'level 2", subtree: false).get() }
80    }
81}