Tests/OrgAppTests/CaptureInboxTests.swift
68 lines · 3552 bytes
6 symbols in this file
1import Foundation
2import OrgCore
3import OrgDocument
4import Testing
5@testable import OrgApp
6
7struct CaptureInboxTests {
8 @Test func keepsLinksOldestFirstUntilRemoved() throws {
9 let folder = try Folder()
10 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
11 #expect(inbox.links() == [])
12 try inbox.add("org-protocol://capture?body=one")
13 try inbox.add("org-protocol://capture?body=two")
14 let links = inbox.links()
15 #expect(links.map(\.text) == ["org-protocol://capture?body=one", "org-protocol://capture?body=two"])
16 // Another instance, as after the app was ended with both queued.
17 #expect(CaptureInbox(folder: inbox.folder).links() == links)
18 inbox.remove(links[0])
19 #expect(inbox.links().map(\.text) == ["org-protocol://capture?body=two"])
20 inbox.remove(links[1])
21 #expect(inbox.links() == [])
22 }
23
24 @Test func keepsALinkItCannotRead() throws {
25 let folder = try Folder()
26 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
27 try inbox.add("org-protocol://capture?body=one")
28 let notText = inbox.folder.appendingPathComponent("0-not-text.link")
29 try Data([0xFF, 0xFE, 0xFD]).write(to: notText)
30 let directory = inbox.folder.appendingPathComponent("1-directory.link")
31 try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
32 #expect(inbox.links().map(\.text) == ["org-protocol://capture?body=one"])
33 #expect(FileManager.default.fileExists(atPath: notText.path))
34 #expect(FileManager.default.fileExists(atPath: directory.path))
35 }
36
37 @Test func keepsTheTemplateList() throws {
38 let folder = try Folder()
39 let inbox = CaptureInbox(folder: folder.url.appendingPathComponent("Captures"))
40 #expect(inbox.templates() == [])
41 let templates = [CaptureInbox.Template(key: "t", name: "Task"), CaptureInbox.Template(key: "w", name: "Web page")]
42 try inbox.setTemplates(templates)
43 #expect(inbox.templates() == templates)
44 }
45}
46
47@MainActor
48struct CaptureWithoutWindowTests {
49 @Test func filesWithTheKeyedTemplateAndDefaultAnswers() async throws {
50 let notes = try Folder()
51 let todo = try notes.write("todo.org", "* Inbox\n")
52 let workspace = try WorkspaceModel(directory: nil)
53 try await workspace.addRoot(notes.url)
54 let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
55 let clock = ClockModel(file: notes.url.appendingPathComponent("clock.json"))
56 let templates = [
57 CaptureTemplates.defaults[0],
58 CaptureTemplate(key: "x", name: "Errand", target: .headline(file: "todo.org", headline: "Inbox"), template: "* TODO %^{What|Buy milk}\n%i"),
59 ]
60 let chosen = session.capture("from the share sheet", templateKey: "x", templates: templates, workspace: workspace, clock: clock, clipboard: "", userName: "")
61 #expect((try? chosen.get())?.key == "x")
62 #expect(try String(contentsOf: todo, encoding: .utf8) == "* Inbox\n** TODO Buy milk\nfrom the share sheet\n")
63
64 let missing = session.capture("x", templateKey: "nope", templates: templates, workspace: workspace, clock: clock, clipboard: "", userName: "")
65 guard case .failure(let error) = missing else { Issue.record("filed with a missing template"); return }
66 #expect(error.message == "No capture template \"nope\"")
67 }
68}