Tests/OrgAppTests/FolderGrantsTests.swift

81 lines · 3783 bytes

 1import Foundation
 2import Testing
 3@testable import OrgApp
 4
 5@MainActor
 6struct FolderGrantsTests {
 7    func defaults() -> UserDefaults {
 8        let name = "orgstar-folder-grants-\(UUID().uuidString)"
 9        let defaults = UserDefaults(suiteName: name)!
10        defaults.removePersistentDomain(forName: name)
11        return defaults
12    }
13
14    @Test func grantsComeBackAtLaunch() throws {
15        let store = defaults()
16        let folder = try Folder()
17        let grants = FolderGrants(defaults: store, sandboxed: false)
18        #expect(grants.folders.isEmpty)
19        grants.add(folder.url)
20        grants.add(folder.url)
21        #expect(grants.folders == [folder.url])
22        #expect((store.array(forKey: FolderGrants.key) as? [Data])?.count == 1)
23        let relaunched = FolderGrants(defaults: store, sandboxed: false)
24        #expect(relaunched.folders.map { $0.resolvingSymlinksInPath().path } == [folder.url.resolvingSymlinksInPath().path])
25    }
26
27    @Test func aGrantWithoutABookmarkLastsForTheSession() {
28        let store = defaults()
29        let grants = FolderGrants(defaults: store, sandboxed: false)
30        let missing = URL(fileURLWithPath: "/nowhere-\(UUID().uuidString)")
31        #expect(!grants.add(missing))
32        #expect(grants.folders == [missing])
33        #expect(store.array(forKey: FolderGrants.key) == nil)
34    }
35
36    @Test func coverage() {
37        #expect(FolderGrants.covers(URL(fileURLWithPath: "/a"), URL(fileURLWithPath: "/a/b")))
38        #expect(FolderGrants.covers(URL(fileURLWithPath: "/a/b"), URL(fileURLWithPath: "/a/b")))
39        #expect(FolderGrants.covers(URL(fileURLWithPath: "/"), URL(fileURLWithPath: "/a")))
40        #expect(!FolderGrants.covers(URL(fileURLWithPath: "/a/b"), URL(fileURLWithPath: "/a")))
41        #expect(!FolderGrants.covers(URL(fileURLWithPath: "/a/b"), URL(fileURLWithPath: "/a/bc")))
42    }
43
44    @Test func bookmarksThatDontResolveAreKept() {
45        let store = defaults()
46        store.set([Data("not a bookmark".utf8)], forKey: FolderGrants.key)
47        #expect(FolderGrants(defaults: store, sandboxed: false).folders.isEmpty)
48        #expect((store.array(forKey: FolderGrants.key) as? [Data])?.count == 1)
49    }
50
51    @Test func fileBookmarkRoundTrip() throws {
52        let folder = try Folder()
53        let file = try folder.write("a.org", "* a\n")
54        let data = try #require(SecurityBookmark.make(file, sandboxed: false))
55        let resolved = try #require(SecurityBookmark.resolve(data, sandboxed: false))
56        #expect(resolved.url.resolvingSymlinksInPath().path == file.resolvingSymlinksInPath().path)
57    }
58
59    @Test func foldersToGrantSkipReachablePaths() {
60        let reachable = [URL(fileURLWithPath: "/notes"), URL(fileURLWithPath: "/container")]
61        let existing: Set<String> = ["/", "/notes", "/container", "/other", "/other/site"]
62        let folders = WorkspaceModel.foldersToGrant(
63            for: ["/notes/a.org", "/container/x", "/other/setup.org", "/other/inc.org", "", "/other/site/new/out.html"],
64            directories: false, reachable: reachable, exists: existing.contains)
65        #expect(folders.map(\.path) == ["/other", "/other/site"])
66    }
67
68    @Test func onlyFilesThatAreThereAreAskedFor() throws {
69        let folder = try Folder()
70        let file = try folder.write("setup.org", "#+TODO: A\n")
71        #expect(WorkspaceModel.isThere(file.path))
72        #expect(!WorkspaceModel.isThere(folder.url.appendingPathComponent("missing.org").path))
73    }
74
75    @Test func foldersToGrantForFolders() {
76        let folders = WorkspaceModel.foldersToGrant(
77            for: ["/out/public", "/out/missing/deep"], directories: true, reachable: [],
78            exists: ["/", "/out", "/out/public"].contains)
79        #expect(folders.map(\.path) == ["/out/public", "/out"])
80    }
81}