Tests/OrgAppTests/CaptureInboxTests.swift
49 lines · 2446 bytes
1import Foundation
2import OrgCore
3import OrgDocument
4import Testing
5@testable import OrgApp
6
7struct CaptureInboxTests {
8 @Test func takesLinksOldestFirstOnce() throws {
9 let folder = try Folder()
10 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
11 #expect(inbox.take() == [])
12 try inbox.add("org-protocol://capture?body=one")
13 try inbox.add("org-protocol://capture?body=two")
14 #expect(inbox.take() == ["org-protocol://capture?body=one", "org-protocol://capture?body=two"])
15 #expect(inbox.take() == [])
16 }
17
18 @Test func keepsTheTemplateList() throws {
19 let folder = try Folder()
20 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
21 #expect(inbox.templates() == [])
22 let templates = [CaptureInbox.Template(key: "t", name: "Task"), CaptureInbox.Template(key: "w", name: "Web page")]
23 try inbox.setTemplates(templates)
24 #expect(inbox.templates() == templates)
25 }
26}
27
28@MainActor
29struct CaptureWithoutWindowTests {
30 @Test func filesWithTheKeyedTemplateAndDefaultAnswers() async throws {
31 let notes = try Folder()
32 let todo = try notes.write("todo.org", "* Inbox\n")
33 let workspace = try WorkspaceModel(directory: nil)
34 try await workspace.addRoot(notes.url)
35 let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
36 let clock = ClockModel(file: notes.url.appendingPathComponent("clock.json"))
37 let templates = [
38 CaptureTemplates.defaults[0],
39 CaptureTemplate(key: "x", name: "Errand", target: .headline(file: "todo.org", headline: "Inbox"), template: "* TODO %^{What|Buy milk}\n%i"),
40 ]
41 let chosen = session.capture("from the share sheet", templateKey: "x", templates: templates, workspace: workspace, clock: clock, clipboard: "", userName: "")
42 #expect((try? chosen.get())?.key == "x")
43 #expect(try String(contentsOf: todo, encoding: .utf8) == "* Inbox\n** TODO Buy milk\nfrom the share sheet\n")
44
45 let missing = session.capture("x", templateKey: "nope", templates: templates, workspace: workspace, clock: clock, clipboard: "", userName: "")
46 guard case .failure(let error) = missing else { Issue.record("filed with a missing template"); return }
47 #expect(error.message == "No capture template \"nope\"")
48 }
49}