Tests/OrgCoreTests/DenoteTests.swift
194 lines · 13665 bytes
16 symbols in this file
DenoteTestsreadsIdentifiersFromFileNamessplitsSearchOptionsprefersOrgFilesnamesNewFilessignaturesreadsNamesInAnyOrderrenamesFromFrontMatterfileTypestextTypeFrontMatterreadsTextTypeFrontMatterrewritesTextTypeFrontMatterrenamesTextTypesFromFrontMatterlinksInTextNotesfollowsAndInsertsOnlyDenoteLinksInTextNotescontext
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; the
65 // keywords are sorted, and the front matter is rewritten from what was read.
66 #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: text)?.path == "/n/20261119T090503--new-title__alpha_zeta.org")
67 #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: text)?.text == text.replacingOccurrences(of: ":Zeta:alpha:", with: ":alpha:zeta:"))
68 #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: "#+signature: Two Words\n" + text)?.path
69 == "/n/20261119T090503==two=words--new-title__alpha_zeta.org")
70 #expect(Denote.renamed("/n/plain.org", text: text)?.path == "/n/20261119T090503--new-title__alpha_zeta.org")
71 #expect(Denote.renamed("/n/plain.org", text: "#+title: x\n") == nil)
72 #expect(Denote.renamed("/n/20261119T090503--old-name__x.org", text: "No front matter\n") == nil)
73 #expect(Denote.renamed("/n/20261119T090503--old-name__x.org", text: "#+title: Only a title\n")?.path == "/n/20261119T090503--only-a-title.org")
74 #expect(Denote.renamed("/n/20261119T090503--a.pdf", text: "#+title: Not a type\n") == nil)
75 }
76
77 // MARK: - Markdown and text notes
78
79 @Test func fileTypes() {
80 #expect(Denote.FileType(path: "/n/a.org") == .org)
81 #expect(Denote.FileType(path: "/n/a.txt") == .text)
82 #expect(Denote.FileType(path: "/n/a.md", text: "---\ntitle: x\n---\n") == .markdownYAML)
83 #expect(Denote.FileType(path: "/n/a.md", text: "+++\ntitle = \"x\"\n+++\n") == .markdownTOML)
84 #expect(Denote.FileType(path: "/n/a.md", text: "# x\n") == .markdownYAML)
85 #expect(Denote.FileType(path: "/n/a.md", text: "# x\n", preferred: .markdownTOML) == .markdownTOML)
86 #expect(Denote.FileType(path: "/n/a.md", text: "# x\n", preferred: .text) == .markdownYAML)
87 #expect(Denote.FileType(path: "/n/a.markdown") == nil && Denote.FileType(path: "/n/a.pdf") == nil)
88 }
89
90 @Test func textTypeFrontMatter() {
91 var calendar = Self.calendar
92 #expect(Denote.frontMatter(title: "A \"q\" note", date: Self.date, keywords: ["a", "b"], identifier: "20261119T090503", signature: "1a",
93 type: .markdownYAML, calendar: calendar) == """
94 ---
95 title: "A \\"q\\" note"
96 date: 2026-11-19T09:05:03+00:00
97 tags: ["a", "b"]
98 identifier: "20261119T090503"
99 signature: "1a"
100 ---
101
102
103 """)
104 calendar.timeZone = TimeZone(secondsFromGMT: -5 * 3600 - 1800)!
105 #expect(Denote.frontMatter(title: "A", date: Self.date, keywords: [], identifier: "20261119T090503", type: .markdownTOML, calendar: calendar) == """
106 +++
107 title = "A"
108 date = 2026-11-19T03:35:03-05:30
109 tags = []
110 identifier = "20261119T090503"
111 +++
112
113
114 """)
115 #expect(Denote.frontMatter(title: "A", date: Self.date, keywords: ["a", "b"], identifier: "20261119T090503", type: .text, calendar: Self.calendar)
116 == "title: A\ndate: 2026-11-19\ntags: a b\nidentifier: 20261119T090503\n---------------------------\n\n")
117 #expect(Denote.frontMatter(title: "A", date: Self.date, keywords: [], identifier: "20261119T090503", type: .text, calendar: Self.calendar)
118 .contains("\ntags: \n"))
119 }
120
121 @Test func readsTextTypeFrontMatter() {
122 let yaml = "---\ntitle: \"A \\\"q\\\" note\"\ndate: 2026-11-19T10:05:03+01:00\ntags: [\"a\", \"b\"]\nidentifier: \"20261119T090503\"\nsignature: '1a'\n---\n"
123 let front = Denote.frontMatter(of: yaml, type: .markdownYAML)
124 // As Denote reads it: quotes trimmed, backslashes kept.
125 #expect(front.title == #"A \"q\" note"# && front.keywords == ["a", "b"] && front.identifier == "20261119T090503" && front.signature == "1a")
126 #expect(Denote.date(of: yaml, type: .markdownYAML) == Self.date)
127 let toml = "+++\nTitle = \"T\"\ntags = [\"x\",\"y\"]\nidentifier = \"20261119T090503\"\n+++\n"
128 #expect(Denote.frontMatter(of: toml, type: .markdownTOML).title == "T" && Denote.frontMatter(of: toml, type: .markdownTOML).keywords == ["x", "y"])
129 #expect(Denote.frontMatter(of: toml, type: .markdownYAML).title == nil)
130 let text = "title: Plain\ndate: 2026-11-19\ntags: a b\nidentifier: 20261119T090503\n---------------------------\n"
131 #expect(Denote.frontMatter(of: text, type: .text).title == "Plain" && Denote.frontMatter(of: text, type: .text).keywords == ["a", "b"])
132 #expect(Denote.date(of: text, type: .text, calendar: Self.calendar) == Self.calendar.date(from: DateComponents(year: 2026, month: 11, day: 19)))
133 #expect(Denote.frontMatter(ofFile: "/n/x.txt", text: text).title == "Plain")
134 }
135
136 @Test func rewritesTextTypeFrontMatter() {
137 let yaml = "---\ntitle: \"Old\"\ntags: [\"x\"]\nidentifier: \"20261119T090503\"\nsignature: \"zz\"\n---\n\nBody\n"
138 // The key keeps its spacing; a date is added after the title, the signature removed.
139 #expect(Denote.rewritingFrontMatter(yaml, title: "New", keywords: ["a"], signature: "", date: Self.date, identifier: "20261119T090503",
140 type: .markdownYAML, calendar: Self.calendar)
141 == "---\ntitle: \"New\"\ndate: 2026-11-19T09:05:03+00:00\ntags: [\"a\"]\nidentifier: \"20261119T090503\"\n---\n\nBody\n")
142 // No date given: the date line stays and none is added.
143 #expect(Denote.rewritingFrontMatter("title: Old\nidentifier: 1\n---------------------------\n", title: "New", keywords: [], signature: "s",
144 date: nil, identifier: "1", type: .text, calendar: Self.calendar)
145 == "title: New\nidentifier: 1\nsignature: s\n---------------------------\n")
146 // Without front matter, all of it goes first.
147 #expect(Denote.rewritingFrontMatter("# Heading\n", title: "T", keywords: [], signature: "", date: Self.date, identifier: "1",
148 type: .markdownTOML, calendar: Self.calendar).hasPrefix("+++\ntitle = \"T\"\n"))
149 }
150
151 @Test func renamesTextTypesFromFrontMatter() {
152 let toml = "+++\ntitle = 'Toml Title'\ntags = [\"zeta\", \"alpha\"]\nidentifier = \"20261119T090503\"\n+++\n\nBody\n"
153 let renamed = Denote.renamed("/n/20261119T090503--old.md", text: toml)
154 #expect(renamed?.path == "/n/20261119T090503--toml-title__alpha_zeta.md")
155 #expect(renamed?.text == "+++\ntitle = \"Toml Title\"\ntags = [\"alpha\", \"zeta\"]\nidentifier = \"20261119T090503\"\n+++\n\nBody\n")
156 #expect(Denote.renamed("/n/20261119T090503--old.txt", text: "title: Text\ntags: b a\n")?.path == "/n/20261119T090503--text__a_b.txt")
157 #expect(Denote.renamed("/n/20261119T090503--old.md", text: "# No front matter\n") == nil)
158 }
159
160 @Test func linksInTextNotes() {
161 #expect(Denote.linkText("denote:20261119T090503", description: "A note", inFile: "/n/x.md") == "[A note](denote:20261119T090503)")
162 #expect(Denote.linkText("denote:20261119T090503", description: "", inFile: "/n/x.md") == "[[denote:20261119T090503]]")
163 #expect(Denote.linkText("denote:20261119T090503", description: "A note", inFile: "/n/x.txt") == "[[denote:20261119T090503][A note]]")
164 #expect(Denote.linkText("denote:20261119T090503", description: "A note", inFile: "/n/x.org") == "[[denote:20261119T090503][A note]]")
165 #expect(Denote.linkText("id:abc", description: "A", inFile: "/n/x.md") == "[[id:abc][A]]")
166 let text = "See [one](denote:20261119T090501) and [[denote:20261119T090502][two]], [[denote:20261119T090503::#h]], [web](https://x.org).\n"
167 #expect(Denote.links(in: text, markdown: true).map(\.target) == ["denote:20261119T090501", "denote:20261119T090502", "denote:20261119T090503::#h"])
168 #expect(Denote.links(in: text, markdown: false).map(\.target) == ["denote:20261119T090502", "denote:20261119T090503::#h"])
169 #expect(Denote.link(at: 6, in: text, markdown: true)?.path == "20261119T090501")
170 #expect(Denote.link(at: 6, in: text, markdown: false) == nil)
171 #expect(Denote.link(at: 50, in: text, markdown: true)?.type == "denote")
172 #expect(Denote.link(at: 106, in: text, markdown: true) == nil)
173 }
174
175 @Test func followsAndInsertsOnlyDenoteLinksInTextNotes() {
176 var options = EditingOptions.org
177 options.filePath = "/n/20261119T090500--x.md"
178 let text = "[one](denote:20261119T090501) <https://x.org>\n"
179 func context(_ caret: Int) -> EditContext { EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], options: options) }
180 #expect(OpenLinkAtPoint().applies(in: context(2)))
181 #expect(!OpenLinkAtPoint().applies(in: context(34)))
182 if case .external(.openLink(let link)) = OpenLinkAtPoint().run(in: context(2)) { #expect(link.raw == "denote:20261119T090501") } else { Issue.record("not followed") }
183 for path in ["/n/x.md", "/n/x.txt"] {
184 #expect(!OrgOnlyCommands.refuses("org.link.open", markdown: path.hasSuffix(".md"), path: path))
185 #expect(!OrgOnlyCommands.refuses("app.node.insert", markdown: false, path: path))
186 #expect(!OrgOnlyCommands.refuses("app.node.rename", markdown: false, path: path))
187 #expect(!OrgOnlyCommands.refuses("app.node.rename-from-front-matter", markdown: false, path: path))
188 #expect(OrgOnlyCommands.refuses("app.node.sequence-link", markdown: false, path: path))
189 #expect(OrgOnlyCommands.refuses("org.link.insert", markdown: false, path: path))
190 }
191 #expect(OrgOnlyCommands.refuses("org.link.open", markdown: false, path: "/n/x.py"))
192 #expect(OrgOnlyCommands.refuses("app.node.insert", markdown: true, path: "/n/x.markdown"))
193 }
194}