Tests/OrgAppTests/FolderGrantsTests.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Tests/OrgAppTests/FolderGrantsTests.swift history · blame · raw

91 lines · 4540 bytes

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