Tests/OrgAppTests/RoamNodeTests.swift
cmc org-roam: aliases, refs, excludes, chooser tags, template choice, capture buffer
ed22b081d6 20 hours ago32 lines · 2339 bytes
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}