Tests/OrgCoreTests/AgendaSortTests.swift

b4eeefea9b0dbf6952995d9accda575f1b2b5060
orgstar/Tests/OrgCoreTests/AgendaSortTests.swift

106 lines · 5067 bytes

  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5/// `org-agenda-sorting-strategy` against Emacs.
  6struct AgendaSortTests {
  7    static let today = Days.absolute(year: 2026, month: 10, day: 5)
  8
  9    static let files = [
 10        ("a.org", """
 11        #+CATEGORY: alpha
 12        * TODO [#B] write report :work:
 13        SCHEDULED: <2026-10-05 Mon 14:00>
 14        :PROPERTIES:
 15        :Effort: 1:30
 16        :END:
 17        * WAIT [#A] Call Bob :phone:
 18        DEADLINE: <2026-10-07 Wed>
 19        * TODO Buy milk :home:errand:
 20        SCHEDULED: <2026-10-03 Sat>
 21        :PROPERTIES:
 22        :Effort: 0:10
 23        :END:
 24        * HOLD zebra notes
 25        <2026-10-05 Mon 09:00>
 26        [2026-09-01 Tue]
 27        * TODO [#C] apples
 28        DEADLINE: <2026-10-05 Mon 11:00>
 29        :PROPERTIES:
 30        :Effort: 2:00
 31        :END:
 32
 33        """),
 34        ("b.org", """
 35        #+CATEGORY: beta
 36        * IDEA Mango :work:
 37        SCHEDULED: <2026-10-05 Mon>
 38        * TODO [#A] banana
 39        <2026-10-06 Tue 08:00>
 40        [2026-08-15 Sat]
 41        * STRT Kiwi :home:
 42        DEADLINE: <2026-10-04 Sun>
 43        :PROPERTIES:
 44        :Effort: 0:45
 45        :END:
 46        * DONE cherry
 47        SCHEDULED: <2026-10-05 Mon 10:00>
 48
 49        """),
 50    ]
 51
 52    static func lisp(_ strategy: [String]) -> String { "(" + strategy.joined(separator: " ") + ")" }
 53
 54    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 55    func agendaSortsLikeOrg() throws {
 56        let strategies: [[String]] = [
 57            ["time-down", "priority-up", "category-keep"], ["category-up", "alpha-up"], ["category-down", "alpha-down"],
 58            ["effort-up", "tag-up"], ["effort-down", "tag-down"], ["todo-state-up", "alpha-up"], ["todo-state-down", "alpha-up"],
 59            ["deadline-up", "scheduled-down", "alpha-up"], ["timestamp-up", "alpha-up"], ["ts-down", "urgency-up"],
 60            ["category-keep", "alpha-up"], ["habit-up", "priority-down"],
 61        ]
 62        for strategy in strategies {
 63            for late in [true, false] {
 64                let form = "(let ((org-agenda-sorting-strategy '((agenda . \(Self.lisp(strategy))))) (org-agenda-sort-notime-is-late \(late ? "t" : "nil")) (org-agenda-sort-noeffort-is-high \(late ? "t" : "nil"))) (org-agenda-list nil \"2026-10-04\" 3))"
 65                let emacs = try AgendaOracle.run(files: Self.files, form: form, today: Self.today, hour: 9, minute: 30)
 66                let defaults = OrgSettings.default.withTodoKeywords(AgendaOracle.keywords)!
 67                let sources = Self.files.map { AgendaSource(path: $0.0, text: $0.1, defaults: defaults) }
 68                var options = AgendaOptions.terminal
 69                options.sortingStrategy["agenda"] = strategy
 70                options.sortNotimeIsLate = late
 71                options.sortNoeffortIsHigh = late
 72                let ours = Agenda.list(sources, start: Self.today - 1, days: 3, today: Self.today, now: (9, 30), options: options)
 73                #expect(ours.count == emacs.count)
 74                for (o, e) in zip(ours, emacs) {
 75                    #expect(AgendaOracle.describe(o.items, today: Self.today) == AgendaOracle.describe(e.items), "\(strategy) \(late) \(e.header)")
 76                }
 77            }
 78        }
 79    }
 80
 81    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 82    func listsSortLikeOrg() throws {
 83        let strategies: [[String]] = [
 84            ["priority-down", "alpha-up"], ["scheduled-up", "alpha-up"], ["deadline-down", "alpha-up"], ["timestamp-up", "alpha-up"],
 85            ["tsia-up", "alpha-down"], ["ts-up", "alpha-up"], ["todo-state-down", "effort-up"], ["category-keep", "alpha-up"],
 86            ["tag-down", "category-down"], ["effort-down", "alpha-up"],
 87        ]
 88        var utc = Calendar(identifier: .gregorian)
 89        utc.timeZone = TimeZone(identifier: "UTC")!
 90        let now = Date(timeIntervalSince1970: Double((Self.today - 719163) * 86400 + 9 * 3600 + 30 * 60))
 91        for strategy in strategies {
 92            var options = AgendaOptions()
 93            for view in ["todo", "tags", "search"] { options.sortingStrategy[view] = strategy }
 94            let alist = "'((todo . \(Self.lisp(strategy))) (tags . \(Self.lisp(strategy))) (search . \(Self.lisp(strategy))))"
 95            try AgendaOracle.compareList(files: Self.files, form: "(let ((org-agenda-sorting-strategy \(alist))) (org-todo-list nil))", today: Self.today) {
 96                Agenda.todoList($0, options: options, today: Self.today)
 97            }
 98            try AgendaOracle.compareList(files: Self.files, form: "(let ((org-agenda-sorting-strategy \(alist))) (org-tags-view nil \"work|home\"))", today: Self.today) {
 99                Agenda.tagsMatch($0, match: "work|home", now: now, calendar: utc, options: options)
100            }
101            try AgendaOracle.compareList(files: Self.files, form: "(let ((org-agenda-sorting-strategy \(alist))) (org-search-view nil \"a\"))", today: Self.today) {
102                Agenda.textSearch($0, string: "a", options: options)
103            }
104        }
105    }
106}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form