Tests/OrgAppTests/AppTests.swift
115 lines · 4475 bytes
1import Foundation
2import OrgCore
3import OrgIndex
4import Testing
5@testable import OrgApp
6
7/// A temporary folder that removes itself.
8final class Folder {
9 let url: URL
10
11 init() throws {
12 url = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-\(UUID().uuidString)").standardizedFileURL
13 try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
14 }
15
16 deinit {
17 try? FileManager.default.removeItem(at: url)
18 }
19
20 @discardableResult
21 func write(_ path: String, _ text: String) throws -> URL {
22 let file = url.appendingPathComponent(path)
23 try FileManager.default.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)
24 try Data(text.utf8).write(to: file)
25 return file.standardizedFileURL
26 }
27}
28
29struct FileTreeTests {
30 @Test func foldersFirstThenFiles() {
31 let files = ["b.org", "a/z.org", "a/y.org", "c.sync-conflict-1.org", "a2.org"].map {
32 FileEntry(path: "/r/" + $0, root: "/r", kind: $0.contains("conflict") ? .conflict : .org)
33 }
34 let tree = FileTreeNode.build(root: "/r", files: files)
35 #expect(tree.children?.map(\.name) == ["a", "a2.org", "b.org", "c.sync-conflict-1.org"])
36 #expect(tree.children?[0].children?.map(\.name) == ["y.org", "z.org"])
37 #expect(tree.children?[1].children == nil)
38 }
39
40 @Test func fuzzyMatching() {
41 #expect(FuzzyMatch.score("wn", "work/notes.org") != nil)
42 #expect(FuzzyMatch.score("xyz", "work/notes.org") == nil)
43 let files = ["work/notes.org", "notes.org", "personal/wine.org"].map { FileEntry(path: "/r/" + $0, root: "/r", kind: .org) }
44 #expect(FuzzyMatch.rank("notes", files).map(\.relativePath) == ["notes.org", "work/notes.org"])
45 #expect(FuzzyMatch.rank("", files).count == 3)
46 }
47}
48
49@MainActor
50struct WorkspaceModelTests {
51 @Test func addSearchRemoveAndPersist() async throws {
52 let notes = try Folder()
53 let support = try Folder()
54 try notes.write("a.org", "* Groceries\nbuy apples\n")
55 try notes.write("sub/b.org", "* Taxes\n")
56 try notes.write("c.sync-conflict-1.org", "* Taxes copy\n")
57
58 let model = try WorkspaceModel(directory: support.url)
59 try await model.addRoot(notes.url)
60 #expect(model.files.count == 3)
61 #expect(model.search("apples").map(\.title) == ["Groceries"])
62 #expect(model.quickOpen("bo").map(\.name) == ["b.org"])
63 let root = try #require(model.rootPaths.first)
64 #expect(model.tree(forRoot: root).children?.map(\.name) == ["sub", "a.org", "c.sync-conflict-1.org"])
65
66 let reopened = try WorkspaceModel(directory: support.url)
67 #expect(reopened.roots.count == 1)
68 #expect(reopened.files.count == 3)
69
70 try reopened.removeRoot(path: root)
71 #expect(reopened.roots.isEmpty)
72 #expect(reopened.files.isEmpty)
73 #expect(reopened.search("apples").isEmpty)
74 }
75
76 @Test func changedFilesAreReindexed() async throws {
77 let notes = try Folder()
78 let file = try notes.write("a.org", "* Old\n")
79 let model = try WorkspaceModel(directory: nil)
80 try await model.addRoot(notes.url)
81 try notes.write("a.org", "* New\n")
82 await model.filesChanged([file], rescan: [])
83 #expect(model.search("new").count == 1)
84 #expect(model.changedFile?.path == file.path)
85 }
86}
87
88@MainActor
89struct DocumentSessionTests {
90 @Test func openShowAndReload() throws {
91 let notes = try Folder()
92 let file = try notes.write("a.org", "* One\n* Two\n")
93 let session = DocumentSession()
94 session.open(file.path)
95 #expect(session.outline.map(\.title) == ["One", "Two"])
96
97 let location = HeadingLocation(path: file.path, ordinal: 1, title: "Two", start: 6, contentHash: "stale")
98 session.show(location)
99 #expect(session.revealRequest?.offset == 6)
100
101 try notes.write("a.org", "intro\n* One\n* Two\n")
102 session.fileChanged(file.path)
103 #expect(session.outline.map(\.start) == [6, 12])
104 #expect(session.diskBytes != nil)
105 session.show(location)
106 #expect(session.revealRequest?.offset == 12)
107 }
108
109 @Test func missingFileReportsAnError() {
110 let session = DocumentSession()
111 session.open("/nonexistent/a.org")
112 #expect(session.lastError != nil)
113 #expect(session.document == nil)
114 }
115}