fix(persistence): complete M8 JSON store contracts (#81) #97
@@ -27,10 +27,7 @@ public struct JSONFileStore<T: Codable & Sendable>: Sendable {
|
||||
self.fileURL = fileURL
|
||||
self.corrupt = corrupt
|
||||
self.defaultValue = defaultValue
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
||||
encoder.dateEncodingStrategy = dateEncoding
|
||||
self.encoder = encoder
|
||||
self.encoder = JSONEncoder.icceryPretty(dateEncoding: dateEncoding)
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = dateDecoding
|
||||
self.decoder = decoder
|
||||
@@ -75,10 +72,14 @@ public struct JSONFileStore<T: Codable & Sendable>: Sendable {
|
||||
}
|
||||
|
||||
extension JSONEncoder {
|
||||
/// Pretty-printed, sorted-keys encoder used by preset export.
|
||||
public static func icceryPretty() -> JSONEncoder {
|
||||
/// Shared pretty-printed, sorted-keys encoder used by `JSONFileStore`
|
||||
/// and preset export.
|
||||
static func icceryPretty(
|
||||
dateEncoding: JSONEncoder.DateEncodingStrategy = .deferredToDate
|
||||
) -> JSONEncoder {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
||||
encoder.dateEncodingStrategy = dateEncoding
|
||||
return encoder
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,11 @@ public actor VerificationHistoryStore {
|
||||
}
|
||||
|
||||
/// Removes all history and updates disk.
|
||||
///
|
||||
/// Loads the existing history first and propagates any load error so an
|
||||
/// unparseable file is never overwritten.
|
||||
public func clear() throws {
|
||||
try load()
|
||||
try write([])
|
||||
records = []
|
||||
}
|
||||
|
||||
@@ -9,7 +9,17 @@ struct JSONFileStoreTests {
|
||||
.appendingPathComponent("json-store-\(UUID().uuidString).json")
|
||||
}
|
||||
|
||||
@Test("Corrupt file with replaceWithDefault returns default")
|
||||
@Test("Missing file returns default")
|
||||
func missingFileDefaults() throws {
|
||||
let store = JSONFileStore<AppSettings>(
|
||||
fileURL: tempURL(),
|
||||
corrupt: .throwCorrupt,
|
||||
defaultValue: { .default }
|
||||
)
|
||||
#expect(try store.load() == .default)
|
||||
}
|
||||
|
||||
@Test("Corrupt file with replaceWithDefault returns default and leaves bytes")
|
||||
func corruptDefaults() throws {
|
||||
let url = tempURL()
|
||||
try "{ not json".write(to: url, atomically: true, encoding: .utf8)
|
||||
@@ -19,7 +29,8 @@ struct JSONFileStoreTests {
|
||||
defaultValue: { .default }
|
||||
)
|
||||
#expect(try store.load() == .default)
|
||||
#expect(FileManager.default.fileExists(atPath: url.path))
|
||||
let kept = try String(contentsOf: url, encoding: .utf8)
|
||||
#expect(kept == "{ not json")
|
||||
}
|
||||
|
||||
@Test("Corrupt file with throwCorrupt throws and leaves bytes")
|
||||
@@ -50,6 +61,25 @@ struct JSONFileStoreTests {
|
||||
let text = try String(contentsOf: url, encoding: .utf8)
|
||||
#expect(text.contains("\n"))
|
||||
#expect(text.contains("\"delta_e_good_max\""))
|
||||
// Lexical key sorting: ascending order of top-level keys.
|
||||
let keys = [
|
||||
"ask_before_overwrite_profile",
|
||||
"calibration_stale_days",
|
||||
"custom_presets",
|
||||
"default_install_location",
|
||||
"delta_e_good_max",
|
||||
"delta_e_warning_max",
|
||||
"enable_i1pro2_leds",
|
||||
"open_color_panel_after_install",
|
||||
]
|
||||
var lastIndex = text.startIndex
|
||||
for key in keys {
|
||||
guard let range = text.range(of: "\"\(key)\"", range: lastIndex..<text.endIndex) else {
|
||||
Issue.record("missing or out-of-order key \(key)")
|
||||
return
|
||||
}
|
||||
lastIndex = range.upperBound
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,6 +93,28 @@ struct SettingsStoreTests {
|
||||
#expect(!FileManager.default.fileExists(atPath: url.path))
|
||||
}
|
||||
|
||||
@Test func invalidSaveOverValidFilePreservesBytesAndPostsNothing() throws {
|
||||
let url = tempStoreURL()
|
||||
let store = SettingsStore(fileURL: url)
|
||||
var valid = AppSettings.default
|
||||
valid.deltaEGoodMax = 1.5
|
||||
try store.save(valid)
|
||||
let originalBytes = try Data(contentsOf: url)
|
||||
|
||||
var fired = false
|
||||
let token = NotificationCenter.default.addObserver(
|
||||
forName: SettingsStore.settingsDidChange, object: nil, queue: nil
|
||||
) { _ in fired = true }
|
||||
defer { NotificationCenter.default.removeObserver(token) }
|
||||
|
||||
var invalid = AppSettings.default
|
||||
invalid.deltaEGoodMax = 9.0
|
||||
#expect(throws: SettingsStore.SettingsError.self) { try store.save(invalid) }
|
||||
#expect(try Data(contentsOf: url) == originalBytes)
|
||||
#expect(!fired)
|
||||
#expect(store.load() == valid)
|
||||
}
|
||||
|
||||
@Test func savePostsNotification() async throws {
|
||||
let url = tempStoreURL()
|
||||
let store = SettingsStore(fileURL: url)
|
||||
|
||||
@@ -131,6 +131,57 @@ struct VerificationHistoryStoreTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Clear does not overwrite an unparseable file")
|
||||
func clearPreservesUnparseableFile() async {
|
||||
let fm = FileManager.default
|
||||
let tmp = fm.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
try? fm.createDirectory(at: tmp, withIntermediateDirectories: true)
|
||||
let url = tmp.appendingPathComponent("verification_history.json")
|
||||
|
||||
let badJSON = "not json"
|
||||
try? badJSON.write(to: url, atomically: true, encoding: .utf8)
|
||||
|
||||
let store = VerificationHistoryStore(url: url)
|
||||
do {
|
||||
try await store.clear()
|
||||
Issue.record("clear() should propagate the load error")
|
||||
} catch {
|
||||
let contents = try? String(contentsOf: url, encoding: .utf8)
|
||||
#expect(contents == badJSON)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("ISO-8601 timestamps round-trip through a fresh store")
|
||||
func iso8601RoundTrip() async throws {
|
||||
let fm = FileManager.default
|
||||
let tmp = fm.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
try fm.createDirectory(at: tmp, withIntermediateDirectories: true)
|
||||
let url = tmp.appendingPathComponent("verification_history.json")
|
||||
|
||||
let timestamp = Date(timeIntervalSince1970: 1_700_000_000)
|
||||
let record = VerificationRecord(
|
||||
id: "vr-iso",
|
||||
profileName: "p",
|
||||
printerName: "",
|
||||
avgDE: 1.0,
|
||||
maxDE: 2.0,
|
||||
rmsDE: 1.5,
|
||||
patchCount: 1,
|
||||
status: .good,
|
||||
timestamp: timestamp
|
||||
)
|
||||
let store1 = VerificationHistoryStore(url: url)
|
||||
_ = try await store1.append(record)
|
||||
|
||||
let text = try String(contentsOf: url, encoding: .utf8)
|
||||
#expect(text.contains(ISO8601DateFormatter().string(from: timestamp)))
|
||||
|
||||
let store2 = VerificationHistoryStore(url: url)
|
||||
let loaded = try await store2.load()
|
||||
#expect(loaded.count == 1)
|
||||
#expect(loaded.first?.timestamp == timestamp)
|
||||
}
|
||||
|
||||
@Test("CSV export quoting")
|
||||
func csvQuoting() async throws {
|
||||
let fm = FileManager.default
|
||||
|
||||
@@ -117,6 +117,17 @@ struct WizardStateStoreTests {
|
||||
#expect(WizardStateStore(fileURL: url).load().stage == .generate)
|
||||
}
|
||||
|
||||
@Test func corruptJsonReturnsDefaultAndKeepsBytes() throws {
|
||||
let url = tempURL()
|
||||
try FileManager.default.createDirectory(
|
||||
at: url.deletingLastPathComponent(), withIntermediateDirectories: true
|
||||
)
|
||||
try "not json".write(to: url, atomically: true, encoding: .utf8)
|
||||
#expect(WizardStateStore(fileURL: url).load() == .default)
|
||||
let kept = try String(contentsOf: url, encoding: .utf8)
|
||||
#expect(kept == "not json")
|
||||
}
|
||||
|
||||
@Test func sessionModeCalibrationRoundTrips() throws {
|
||||
var s = WizardState(sessionMode: .calibration)
|
||||
let data = try JSONEncoder().encode(s)
|
||||
|
||||
Reference in New Issue
Block a user