Tests/OrgCoreTests/DenoteTests.swift

73 lines · 4426 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct DenoteTests {
 6    @Test func readsIdentifiersFromFileNames() {
 7        #expect(Denote.identifier(ofFile: "/n/20261119T120000--a-note__kw1_kw2.org") == "20261119T120000")
 8        #expect(Denote.identifier(ofFile: "20261119T120000.org") == "20261119T120000")
 9        #expect(Denote.identifier(ofFile: "20261119T120000==1a--title.org") == "20261119T120000")
10        #expect(Denote.identifier(ofFile: "--a-note@@20261119T120000__kw.org") == "20261119T120000")
11        #expect(Denote.identifier(ofFile: "a-note.org") == nil)
12        #expect(Denote.identifier(ofFile: "20261119-diary.org") == nil)
13    }
14
15    @Test func splitsSearchOptions() {
16        #expect(Denote.split("20261119T120000") == ("20261119T120000", nil))
17        #expect(Denote.split("20261119T120000::#h") == ("20261119T120000", "#h"))
18    }
19
20    @Test func prefersOrgFiles() {
21        let paths = ["/n/20261119T120000--a.pdf", "/n/20261119T120000--a.org", "/n/20261119T120001--b.org"]
22        #expect(Denote.file(withIdentifier: "20261119T120000", in: paths) == "/n/20261119T120000--a.org")
23        #expect(Denote.file(withIdentifier: "20261119T120002", in: paths) == nil)
24    }
25
26    /// 2026-11-19 09:05:03, a Thursday, in UTC.
27    static let calendar: Calendar = { var c = Calendar(identifier: .gregorian); c.timeZone = TimeZone(identifier: "UTC")!; return c }()
28    static let date = calendar.date(from: DateComponents(year: 2026, month: 11, day: 19, hour: 9, minute: 5, second: 3))!
29
30    @Test func namesNewFiles() {
31        #expect(Denote.slug("Hello, World: a_test -- Note!") == "hello-world-a-test-note")
32        #expect(Denote.slug("  Café au lait  ") == "café-au-lait")
33        #expect(Denote.keywords(["Emacs", "org-mode", "emacs", "", "two words"]) == ["emacs", "orgmode", "twowords"])
34        let id = Denote.identifier(for: Self.date, calendar: Self.calendar)
35        #expect(id == "20261119T090503")
36        #expect(Denote.fileName(identifier: id, title: "A note", keywords: ["b", "a"]) == "20261119T090503--a-note__b_a.org")
37        #expect(Denote.fileName(identifier: id, signature: "1a", title: "", keywords: []) == "20261119T090503==1a.org")
38        #expect(Denote.frontMatter(title: "A note", date: Self.date, keywords: ["a", "b"], identifier: id, calendar: Self.calendar) == """
39            #+title:      A note
40            #+date:       [2026-11-19 Thu 09:05]
41            #+filetags:   :a:b:
42            #+identifier: 20261119T090503
43
44
45            """)
46    }
47
48    @Test func signatures() {
49        #expect(Denote.signature("Two Words") == "two=words")
50        #expect(Denote.signature(" 1a_b-c==d ") == "1a=bc=d")
51        #expect(Denote.frontMatter(title: "A", date: Self.date, keywords: [], identifier: "20261119T090503", signature: "1a", calendar: Self.calendar)
52            == "#+title:      A\n#+date:       [2026-11-19 Thu 09:05]\n#+filetags:   \n#+identifier: 20261119T090503\n#+signature:  1a\n\n")
53    }
54
55    @Test func readsNamesInAnyOrder() {
56        #expect(Denote.name(ofFile: "/n/20261119T090503==1a--a-note__kw1_kw2.org")
57            == Denote.Name(identifier: "20261119T090503", signature: "1a", title: "a-note", keywords: ["kw1", "kw2"], ext: "org"))
58        #expect(Denote.name(ofFile: "--a-note__kw@@20261119T090503.pdf")
59            == Denote.Name(identifier: "20261119T090503", signature: nil, title: "a-note", keywords: ["kw"], ext: "pdf"))
60    }
61
62    @Test func renamesFromFrontMatter() {
63        let text = "#+title:      New Title\n#+filetags:   :Zeta:alpha:\n#+identifier: 20261119T090503\n\nBody\n"
64        // The signature, like the title and keywords, comes from the front matter.
65        #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: text) == "/n/20261119T090503--new-title__zeta_alpha.org")
66        #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: "#+signature:  Two Words\n" + text)
67            == "/n/20261119T090503==two=words--new-title__zeta_alpha.org")
68        #expect(Denote.renamed("/n/plain.org", text: text) == "/n/20261119T090503--new-title__zeta_alpha.org")
69        #expect(Denote.renamed("/n/plain.org", text: "#+title: x\n") == nil)
70        #expect(Denote.renamed("/n/20261119T090503--old-name__x.org", text: "No front matter\n") == nil)
71        #expect(Denote.renamed("/n/20261119T090503--old-name__x.org", text: "#+title: Only a title\n") == "/n/20261119T090503--only-a-title.org")
72    }
73}