Tests/OrgAppTests/ConfigFolderTests.swift

99 lines · 4543 bytes

 1import Foundation
 2import OrgCore
 3import Testing
 4@testable import OrgApp
 5
 6@MainActor
 7struct ConfigFolderTests {
 8    func defaults() -> UserDefaults {
 9        let name = "orgstar-config-folder-\(UUID().uuidString)"
10        let defaults = UserDefaults(suiteName: name)!
11        defaults.removePersistentDomain(forName: name)
12        return defaults
13    }
14
15    func folder() throws -> URL {
16        let url = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-config-folder-\(UUID().uuidString)")
17        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
18        return url
19    }
20
21    @Test func chosenFolderResolves() throws {
22        let store = defaults()
23        let url = try folder()
24        defer { try? FileManager.default.removeItem(at: url) }
25        #expect(ConfigFolder.resolve(defaults: store) == nil)
26        ConfigFolder.set(url, defaults: store)
27        #expect(store.data(forKey: ConfigFolder.bookmarkKey) != nil)
28        let resolved = try #require(ConfigFolder.resolve(defaults: store))
29        #expect(resolved.resolvingSymlinksInPath().path == url.resolvingSymlinksInPath().path)
30        ConfigFolder.clear(defaults: store)
31        #expect(ConfigFolder.resolve(defaults: store) == nil)
32    }
33
34    @Test func aFolderWithoutABookmarkIsNotKept() {
35        let store = defaults()
36        #expect(!ConfigFolder.set(URL(fileURLWithPath: "/nowhere-\(UUID().uuidString)"), defaults: store))
37        #expect(store.data(forKey: ConfigFolder.bookmarkKey) == nil)
38    }
39
40    /// iOS keeps its folder under the key it always used.
41    @Test func bookmarkKeyIsUnchanged() {
42        #expect(ConfigFolder.bookmarkKey == "configFolderBookmark")
43    }
44
45    @Test func suggestedIsInTheRealHome() {
46        #expect(ConfigFolder.suggested.path == OrgPaths.home + "/.config/orgstar")
47    }
48
49    @Test func movingAppliesTheNewFolder() throws {
50        let first = try folder()
51        let second = try folder()
52        defer {
53            try? FileManager.default.removeItem(at: first)
54            try? FileManager.default.removeItem(at: second)
55        }
56        try "fill-column = 64\n".write(to: first.appendingPathComponent("config.toml"), atomically: true, encoding: .utf8)
57        try "fill-column = 72\n".write(to: second.appendingPathComponent("config.toml"), atomically: true, encoding: .utf8)
58        let store = defaults()
59        let sync = ConfigSync(url: first.appendingPathComponent("config.toml"), defaults: store)
60        sync.start(watch: false)
61        #expect(store.integer(forKey: "fillColumn") == 64)
62        sync.move(to: second.appendingPathComponent("config.toml"))
63        #expect(store.integer(forKey: "fillColumn") == 72)
64        #expect(sync.changedFile?.name == "keymap.toml")
65        // Changes made in the app go into the new file only.
66        store.set(80, forKey: "fillColumn")
67        sync.writeBack()
68        #expect(try String(contentsOf: second.appendingPathComponent("config.toml"), encoding: .utf8).contains("fill-column = 80"))
69        let old = try String(contentsOf: first.appendingPathComponent("config.toml"), encoding: .utf8)
70        #expect(old.contains("fill-column = 64") && !old.contains("fill-column = 80"))
71    }
72
73    @Test func movingToAnEmptyFolderWritesTheCurrentSettings() throws {
74        let first = try folder()
75        let second = try folder()
76        defer {
77            try? FileManager.default.removeItem(at: first)
78            try? FileManager.default.removeItem(at: second)
79        }
80        try "fill-column = 64\n".write(to: first.appendingPathComponent("config.toml"), atomically: true, encoding: .utf8)
81        let store = defaults()
82        let sync = ConfigSync(url: first.appendingPathComponent("config.toml"), defaults: store)
83        sync.start(watch: false)
84        sync.move(to: second.appendingPathComponent("config.toml"))
85        #expect(try String(contentsOf: second.appendingPathComponent("config.toml"), encoding: .utf8).contains("fill-column = 64"))
86    }
87
88    @Test func movingBeforeStartDoesNothing() throws {
89        let first = try folder()
90        let second = try folder()
91        defer {
92            try? FileManager.default.removeItem(at: first)
93            try? FileManager.default.removeItem(at: second)
94        }
95        let sync = ConfigSync(url: first.appendingPathComponent("config.toml"), defaults: defaults())
96        sync.move(to: second.appendingPathComponent("config.toml"))
97        #expect(!FileManager.default.fileExists(atPath: second.appendingPathComponent("config.toml").path))
98    }
99}