HealthProbe / HealthProbeTests / PrototypeStoreResetPolicyTests.swift
Newer Older
104 lines | 4.62kb
Bogdan Timofte authored 2 weeks ago
1
import XCTest
2
@testable import HealthProbe
3

            
4
final class PrototypeStoreResetPolicyTests: XCTestCase {
5
    private var temporaryDirectory: URL!
6
    private var defaults: UserDefaults!
7
    private var defaultsSuiteName: String!
8

            
9
    override func setUpWithError() throws {
10
        temporaryDirectory = FileManager.default.temporaryDirectory
11
            .appending(path: "HealthProbeResetTests-\(UUID().uuidString)", directoryHint: .isDirectory)
12
        try FileManager.default.createDirectory(at: temporaryDirectory, withIntermediateDirectories: true)
13
        defaultsSuiteName = "HealthProbeResetTests-\(UUID().uuidString)"
14
        defaults = UserDefaults(suiteName: defaultsSuiteName)
15
        defaults.removePersistentDomain(forName: defaultsSuiteName)
16
    }
17

            
18
    override func tearDownWithError() throws {
19
        if let temporaryDirectory {
20
            try? FileManager.default.removeItem(at: temporaryDirectory)
21
        }
22
        if let defaults {
23
            defaults.removePersistentDomain(forName: defaultsSuiteName)
24
        }
25
        temporaryDirectory = nil
26
        defaults = nil
27
        defaultsSuiteName = nil
28
    }
29

            
30
    func testApplyIfNeededRemovesPrototypeArchiveAndCacheStoresOnce() throws {
31
        let files = [
32
            "HealthProbeRecords.store",
33
            "HealthProbeRecords.store-wal",
34
            "HealthProbeArchive.sqlite",
35
            "HealthProbeArchive.sqlite-shm",
36
            "HealthProbeCache.sqlite"
37
        ]
38
        for file in files {
39
            try Data("prototype".utf8).write(to: temporaryDirectory.appending(path: file))
40
        }
41
        try Data("settings".utf8).write(to: temporaryDirectory.appending(path: "HealthProbeLocal.store"))
42

            
43
        let first = try PrototypeStoreResetPolicy.applyIfNeeded(
44
            appSupportURL: temporaryDirectory,
45
            defaults: defaults
46
        )
47
        XCTAssertTrue(first.didReset)
48
        XCTAssertEqual(Set(first.removedURLs.map(\.lastPathComponent)), Set(files))
49
        for file in files {
50
            XCTAssertFalse(FileManager.default.fileExists(atPath: temporaryDirectory.appending(path: file).path))
51
        }
52
        XCTAssertTrue(FileManager.default.fileExists(atPath: temporaryDirectory.appending(path: "HealthProbeLocal.store").path))
53

            
54
        let second = try PrototypeStoreResetPolicy.applyIfNeeded(
55
            appSupportURL: temporaryDirectory,
56
            defaults: defaults
57
        )
58
        XCTAssertFalse(second.didReset)
59
        XCTAssertTrue(second.removedURLs.isEmpty)
60
    }
Bogdan Timofte authored a week ago
61

            
62
    func testRequestResetOnNextLaunchMarksResetPending() throws {
63
        defaults.set(PrototypeStoreResetPolicy.currentGeneration, forKey: PrototypeStoreResetPolicy.defaultsKey)
64

            
65
        XCTAssertFalse(PrototypeStoreResetPolicy.isResetScheduled(defaults: defaults))
66

            
67
        PrototypeStoreResetPolicy.requestResetOnNextLaunch(defaults: defaults)
68

            
69
        XCTAssertTrue(PrototypeStoreResetPolicy.isResetScheduled(defaults: defaults))
70
    }
Bogdan Timofte authored 6 days ago
71

            
72
    func testManualResetDeletesStoresEvenWhenGenerationIsCurrent() throws {
73
        defaults.set(PrototypeStoreResetPolicy.currentGeneration, forKey: PrototypeStoreResetPolicy.defaultsKey)
74
        let archiveURL = temporaryDirectory.appending(path: "HealthProbeArchive.sqlite")
75
        let cacheWALURL = temporaryDirectory.appending(path: "HealthProbeCache.sqlite-wal")
76
        try Data("archive".utf8).write(to: archiveURL)
77
        try Data("cache".utf8).write(to: cacheWALURL)
78

            
79
        PrototypeStoreResetPolicy.requestResetOnNextLaunch(defaults: defaults)
80

            
81
        let result = try PrototypeStoreResetPolicy.applyIfNeeded(
82
            appSupportURL: temporaryDirectory,
83
            defaults: defaults
84
        )
85

            
86
        XCTAssertTrue(result.didReset)
87
        XCTAssertEqual(Set(result.removedURLs.map(\.lastPathComponent)), Set([
88
            "HealthProbeArchive.sqlite",
89
            "HealthProbeCache.sqlite-wal"
90
        ]))
91
        XCTAssertFalse(FileManager.default.fileExists(atPath: archiveURL.path))
92
        XCTAssertFalse(FileManager.default.fileExists(atPath: cacheWALURL.path))
93
        XCTAssertFalse(PrototypeStoreResetPolicy.isResetScheduled(defaults: defaults))
94
    }
95

            
96
    func testManualResetFlagSurvivesWithoutChangingGenerationKey() throws {
97
        defaults.set(PrototypeStoreResetPolicy.currentGeneration, forKey: PrototypeStoreResetPolicy.defaultsKey)
98

            
99
        PrototypeStoreResetPolicy.requestResetOnNextLaunch(defaults: defaults)
100

            
101
        XCTAssertEqual(defaults.integer(forKey: PrototypeStoreResetPolicy.defaultsKey), PrototypeStoreResetPolicy.currentGeneration)
102
        XCTAssertTrue(defaults.bool(forKey: PrototypeStoreResetPolicy.manualResetDefaultsKey))
103
    }
Bogdan Timofte authored 2 weeks ago
104
}