Tests/OrgCoreTests/OrgProtocolTests.swift
cmc org-roam: aliases, refs, excludes, chooser tags, template choice, capture buffer
ed22b081d6 1 hour ago85 lines · 4910 bytes
5 symbols in this file
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct OrgProtocolTests {
6 static let urls = [
7 "org-protocol://capture?template=b&url=https%3A%2F%2Fexample.com%2Fa%3Fq%3D1&title=A+Page&body=some%20text",
8 "org-protocol://capture?url=https%3A%2F%2Fx.org&title=&body=",
9 "org-protocol:/capture?template=t&title=Just%20a%20title",
10 "org-protocol://capture?template=t&url=file%3A%2F%2F%2Ftmp%2Fa.org&title=%5Bweird%5D%20%C3%A9",
11 "org-protocol://capture:/b/https%3A%2F%2Fexample.com/Title/Body",
12 "org-protocol://capture:/https%3A%2F%2Fexample.com/Title",
13 "org-protocol://capture://t/https:/example.com/x/Title",
14 "org-protocol://store-link?url=https%3A%2F%2Fexample.com&title=Example",
15 "org-protocol://store-link?url=https%3A%2F%2Fexample.com",
16 "org-protocol://store-link:/https%3A%2F%2Fexample.com/Old%20Title",
17 "https://example.com",
18 ]
19
20 func describe(_ request: OrgProtocol.Request?) -> String {
21 switch request {
22 case .capture(let template, let properties)?:
23 return (["capture template=\(template ?? "nil")"] + ["type", "link", "description", "annotation", "initial"].compactMap { key in
24 properties[key].map { "\(key)=\($0)" }
25 }).joined(separator: "\n")
26 case .storeLink(let url, let title)?:
27 return "store url=\(url)\ntitle=\(title ?? "nil")"
28 case .roamRef, .roamNode:
29 return "roam"
30 case nil:
31 return "none"
32 }
33 }
34
35 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
36 func matchesOrgProtocol() throws {
37 let form = """
38 (progn
39 (require 'org-protocol)
40 (let (out (org-stored-links nil))
41 (cl-letf (((symbol-function 'raise-frame) #'ignore)
42 ((symbol-function 'org-capture)
43 (lambda (_ template)
44 (setq out (mapconcat #'identity
45 (cons (format "capture template=%s" (or template "nil"))
46 (delq nil (mapcar (lambda (k)
47 (let ((v (plist-get org-store-link-plist (intern (concat ":" k)))))
48 (and v (format "%s=%s" k v))))
49 '("type" "link" "description" "annotation" "initial"))))
50 "\\n")))))
51 (let ((result (org-protocol-check-filename-for-protocol (buffer-string) nil nil)))
52 (cond (out nil)
53 ((null result)
54 (setq out (format "store url=%s\\ntitle=%s" (car (car org-stored-links)) (or (cadr (car org-stored-links)) "nil"))))
55 (t (setq out "none")))))
56 (erase-buffer)
57 (insert out)))
58 """
59 let results = try EmacsOracle.run(Self.urls.map { EmacsOracle.Case(text: $0, point: 1, form: form) })
60 for (url, result) in zip(Self.urls, results) {
61 let ours = describe(OrgProtocol.parse(url))
62 #expect(ours == result.text, "\(url)\nours: \(ours.debugDescription)\nemacs: \(result.text.debugDescription) \(result.error)")
63 }
64 }
65
66 /// org-roam-protocol's handlers, which read the `?key=value` form.
67 @Test func roamProtocols() {
68 #expect(OrgProtocol.parse("org-protocol://roam-ref?template=r&ref=https%3A%2F%2Fexample.com%2Fa&title=A+page&body=Some+text")
69 == .roamRef(ref: "https://example.com/a", title: "A page", body: "Some text", template: "r"))
70 #expect(OrgProtocol.parse("org-protocol://roam-ref?ref=cite%3Akey") == .roamRef(ref: "cite:key", title: nil, body: nil, template: nil))
71 #expect(OrgProtocol.parse("org-protocol://roam-ref?title=no+ref") == nil)
72 #expect(OrgProtocol.parse("org-protocol://roam-node?node=5f0c-1e2a") == .roamNode(id: "5f0c-1e2a"))
73 }
74
75 @Test func captureURLRoundTrips() {
76 let url = OrgProtocol.captureURL(template: "w", url: "https://example.com/a?b=1&c=2+3", title: "Tea & cake = 100% café", body: "line one\nline+two")
77 #expect(OrgProtocol.parse(url) == .capture(template: "w", properties: [
78 "link": "https://example.com/a?b=1&c=2+3", "type": "https",
79 "description": "Tea & cake = 100% café", "initial": "line one\nline+two",
80 "annotation": "[[https://example.com/a?b=1&c=2+3][Tea & cake = 100% café]]",
81 ]))
82 #expect(OrgProtocol.parse(OrgProtocol.captureURL(template: nil, url: nil, title: "", body: "just text"))
83 == .capture(template: nil, properties: ["description": "", "initial": "just text", "annotation": ""]))
84 }
85}