Tests/OrgAppTests/RoamNodeTests.swift
45 lines · 3322 bytes
3 symbols in this file
1import Foundation
2import Testing
3@testable import OrgApp
4
5struct RoamNodeTests {
6 /// `org-roam-node-slugify` from org-roam 2.3, run in Emacs. Titles and slugs go through
7 /// UTF-8 files: Emacs decodes its command line by the locale.
8 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
9 func slugsMatchOrgRoam() throws {
10 let titles = ["Hello World", "Café au lait", " Spaces and__underscores ", "C++ & Rust!", "Привет мир", "日本語のノート", "naïve résumé", "x", "---", "Ångström 2026"]
11 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-slug-\(UUID().uuidString)")
12 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
13 defer { try? FileManager.default.removeItem(at: folder) }
14 let input = folder.appendingPathComponent("titles.txt"), output = folder.appendingPathComponent("slugs.txt")
15 try titles.joined(separator: "\n").write(to: input, atomically: true, encoding: .utf8)
16 let form = """
17 (progn (require 'ucs-normalize) (require 'seq) (require 'subr-x)
18 (let* ((coding-system-for-read 'utf-8) (coding-system-for-write 'utf-8)
19 (slug-trim-chars '(#x300 #x301 #x302 #x303 #x304 #x306 #x307 #x308 #x309 #x30A #x30B #x30C #x31B #x323 #x324 #x325 #x327 #x32D #x32E #x330 #x331))
20 (titles (split-string (with-temp-buffer (insert-file-contents "\(input.path)") (buffer-string)) "\n")))
21 (with-temp-file "\(output.path)"
22 (dolist (title titles)
23 (insert (thread-last title (ucs-normalize-NFD-string) (seq-remove (lambda (char) (memq char slug-trim-chars)))
24 (apply #'string) (ucs-normalize-NFC-string) (replace-regexp-in-string "[^[:alnum:]]" "_")
25 (replace-regexp-in-string "__*" "_") (replace-regexp-in-string "^_" "") (replace-regexp-in-string "_$" "") (downcase))
26 "\n")))))
27 """
28 _ = try DailiesTests.emacs(form)
29 let emacs = try String(contentsOf: output, encoding: .utf8).split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
30 #expect(Array(emacs.prefix(titles.count)) == titles.map(RoamNode.slug))
31 }
32
33 @Test func newNodeFile() throws {
34 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-roam-\(UUID().uuidString)").path
35 defer { try? FileManager.default.removeItem(atPath: folder) }
36 let now = Calendar.current.date(from: DateComponents(year: 2026, month: 10, day: 13, hour: 9, minute: 5, second: 7))!
37 let node = try RoamNode.create(title: "Field Notes", in: folder, now: now, id: { "abc-123" })
38 #expect((node.path as NSString).lastPathComponent == "20261013090507-field_notes.org")
39 #expect(node.id == "abc-123")
40 let text = try String(contentsOfFile: node.path, encoding: .utf8)
41 #expect(text == ":PROPERTIES:\n:ID: abc-123\n:END:\n#+title: Field Notes\n")
42 // The same second and title again: the file is there, so nothing is overwritten.
43 #expect(throws: (any Error).self) { try RoamNode.create(title: "Field Notes", in: folder, now: now, id: { "other" }) }
44 }
45}