|
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
|
|
|
Bogdan Timofte
authored
5 days ago
|
67
|
PrototypeStoreResetPolicy.requestResetOnNextLaunch(
|
|
|
68
|
defaults: defaults,
|
|
|
69
|
appSupportURL: temporaryDirectory
|
|
|
70
|
)
|
|
Bogdan Timofte
authored
a week ago
|
71
|
|
|
|
72
|
XCTAssertTrue(PrototypeStoreResetPolicy.isResetScheduled(defaults: defaults))
|
|
|
73
|
}
|
|
Bogdan Timofte
authored
6 days ago
|
74
|
|
|
|
75
|
func testManualResetDeletesStoresEvenWhenGenerationIsCurrent() throws {
|
|
|
76
|
defaults.set(PrototypeStoreResetPolicy.currentGeneration, forKey: PrototypeStoreResetPolicy.defaultsKey)
|
|
|
77
|
let archiveURL = temporaryDirectory.appending(path: "HealthProbeArchive.sqlite")
|
|
|
78
|
let cacheWALURL = temporaryDirectory.appending(path: "HealthProbeCache.sqlite-wal")
|
|
Bogdan Timofte
authored
5 days ago
|
79
|
let archiveJournalURL = temporaryDirectory.appending(path: "HealthProbeArchive.sqlite-journal")
|
|
Bogdan Timofte
authored
6 days ago
|
80
|
try Data("archive".utf8).write(to: archiveURL)
|
|
|
81
|
try Data("cache".utf8).write(to: cacheWALURL)
|
|
Bogdan Timofte
authored
5 days ago
|
82
|
try Data("journal".utf8).write(to: archiveJournalURL)
|
|
Bogdan Timofte
authored
6 days ago
|
83
|
|
|
Bogdan Timofte
authored
5 days ago
|
84
|
PrototypeStoreResetPolicy.requestResetOnNextLaunch(
|
|
|
85
|
defaults: defaults,
|
|
|
86
|
appSupportURL: temporaryDirectory
|
|
|
87
|
)
|
|
Bogdan Timofte
authored
6 days ago
|
88
|
|
|
|
89
|
let result = try PrototypeStoreResetPolicy.applyIfNeeded(
|
|
|
90
|
appSupportURL: temporaryDirectory,
|
|
|
91
|
defaults: defaults
|
|
|
92
|
)
|
|
|
93
|
|
|
|
94
|
XCTAssertTrue(result.didReset)
|
|
|
95
|
XCTAssertEqual(Set(result.removedURLs.map(\.lastPathComponent)), Set([
|
|
|
96
|
"HealthProbeArchive.sqlite",
|
|
Bogdan Timofte
authored
5 days ago
|
97
|
"HealthProbeArchive.sqlite-journal",
|
|
Bogdan Timofte
authored
6 days ago
|
98
|
"HealthProbeCache.sqlite-wal"
|
|
|
99
|
]))
|
|
|
100
|
XCTAssertFalse(FileManager.default.fileExists(atPath: archiveURL.path))
|
|
|
101
|
XCTAssertFalse(FileManager.default.fileExists(atPath: cacheWALURL.path))
|
|
Bogdan Timofte
authored
5 days ago
|
102
|
XCTAssertFalse(FileManager.default.fileExists(atPath: archiveJournalURL.path))
|
|
Bogdan Timofte
authored
6 days ago
|
103
|
XCTAssertFalse(PrototypeStoreResetPolicy.isResetScheduled(defaults: defaults))
|
|
|
104
|
}
|
|
|
105
|
|
|
|
106
|
func testManualResetFlagSurvivesWithoutChangingGenerationKey() throws {
|
|
|
107
|
defaults.set(PrototypeStoreResetPolicy.currentGeneration, forKey: PrototypeStoreResetPolicy.defaultsKey)
|
|
|
108
|
|
|
Bogdan Timofte
authored
5 days ago
|
109
|
PrototypeStoreResetPolicy.requestResetOnNextLaunch(
|
|
|
110
|
defaults: defaults,
|
|
|
111
|
appSupportURL: temporaryDirectory
|
|
|
112
|
)
|
|
Bogdan Timofte
authored
6 days ago
|
113
|
|
|
|
114
|
XCTAssertEqual(defaults.integer(forKey: PrototypeStoreResetPolicy.defaultsKey), PrototypeStoreResetPolicy.currentGeneration)
|
|
|
115
|
XCTAssertTrue(defaults.bool(forKey: PrototypeStoreResetPolicy.manualResetDefaultsKey))
|
|
|
116
|
}
|
|
Bogdan Timofte
authored
5 days ago
|
117
|
|
|
|
118
|
func testRequestResetOnNextLaunchWritesDiskMarker() throws {
|
|
|
119
|
defaults.set(PrototypeStoreResetPolicy.currentGeneration, forKey: PrototypeStoreResetPolicy.defaultsKey)
|
|
|
120
|
|
|
|
121
|
PrototypeStoreResetPolicy.requestResetOnNextLaunch(
|
|
|
122
|
defaults: defaults,
|
|
|
123
|
appSupportURL: temporaryDirectory
|
|
|
124
|
)
|
|
|
125
|
|
|
|
126
|
XCTAssertTrue(PrototypeStoreResetPolicy.isResetScheduled(
|
|
|
127
|
defaults: defaults,
|
|
|
128
|
appSupportURL: temporaryDirectory
|
|
|
129
|
))
|
|
|
130
|
XCTAssertTrue(FileManager.default.fileExists(
|
|
|
131
|
atPath: temporaryDirectory
|
|
|
132
|
.appending(path: PrototypeStoreResetPolicy.manualResetMarkerFileName)
|
|
|
133
|
.path
|
|
|
134
|
))
|
|
|
135
|
}
|
|
|
136
|
|
|
|
137
|
func testDiskMarkerTriggersResetWhenDefaultsFlagIsMissing() throws {
|
|
|
138
|
defaults.set(PrototypeStoreResetPolicy.currentGeneration, forKey: PrototypeStoreResetPolicy.defaultsKey)
|
|
|
139
|
defaults.set(false, forKey: PrototypeStoreResetPolicy.manualResetDefaultsKey)
|
|
|
140
|
let archiveURL = temporaryDirectory.appending(path: "HealthProbeArchive.sqlite")
|
|
|
141
|
let markerURL = temporaryDirectory.appending(path: PrototypeStoreResetPolicy.manualResetMarkerFileName)
|
|
|
142
|
try Data("archive".utf8).write(to: archiveURL)
|
|
|
143
|
try Data("pending".utf8).write(to: markerURL)
|
|
|
144
|
|
|
|
145
|
let result = try PrototypeStoreResetPolicy.applyIfNeeded(
|
|
|
146
|
appSupportURL: temporaryDirectory,
|
|
|
147
|
defaults: defaults
|
|
|
148
|
)
|
|
|
149
|
|
|
|
150
|
XCTAssertTrue(result.didReset)
|
|
|
151
|
XCTAssertEqual(result.removedURLs.map(\.lastPathComponent), ["HealthProbeArchive.sqlite"])
|
|
|
152
|
XCTAssertFalse(FileManager.default.fileExists(atPath: archiveURL.path))
|
|
|
153
|
XCTAssertFalse(FileManager.default.fileExists(atPath: markerURL.path))
|
|
|
154
|
XCTAssertFalse(PrototypeStoreResetPolicy.isResetScheduled(
|
|
|
155
|
defaults: defaults,
|
|
|
156
|
appSupportURL: temporaryDirectory
|
|
|
157
|
))
|
|
|
158
|
}
|
|
Bogdan Timofte
authored
2 weeks ago
|
159
|
}
|