Files
iccery-v2-mac/Tests/ICCeryCoreTests/JSONFileStoreTests.swift
gronodandDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> a30a8fc551 test(m9): complete migration of all remaining test suites to XCTest
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-11 21:29:16 +01:00

78 lines
2.6 KiB
Swift

import Foundation
import XCTest
@testable import ICCeryCore
final class JSONFileStoreTests: XCTestCase {
private func tempURL() -> URL {
FileManager.default.temporaryDirectory
.appendingPathComponent("json-store-\(UUID().uuidString).json")
}
func testMissingFileDefaults() throws {
let store = JSONFileStore<AppSettings>(
fileURL: tempURL(),
corrupt: .throwCorrupt,
defaultValue: { .default }
)
XCTAssertEqual(try store.load(), .default)
}
func testCorruptDefaults() throws {
let url = tempURL()
try "{ not json".write(to: url, atomically: true, encoding: .utf8)
let store = JSONFileStore<AppSettings>(
fileURL: url,
corrupt: .replaceWithDefault,
defaultValue: { .default }
)
XCTAssertEqual(try store.load(), .default)
let kept = try String(contentsOf: url, encoding: .utf8)
XCTAssertEqual(kept, "{ not json")
}
func testCorruptThrows() throws {
let url = tempURL()
try "not json".write(to: url, atomically: true, encoding: .utf8)
let store = JSONFileStore<[Int]>(
fileURL: url,
corrupt: .throwCorrupt,
defaultValue: { [] }
)
XCTAssertThrowsError(try store.load()) { error in XCTAssertTrue(error is DecodingError) }
let kept = try String(contentsOf: url, encoding: .utf8)
XCTAssertEqual(kept, "not json")
}
func testPrettySorted() throws {
let url = tempURL()
let store = JSONFileStore<AppSettings>(
fileURL: url,
corrupt: .replaceWithDefault,
defaultValue: { .default }
)
try store.save(.default)
let text = try String(contentsOf: url, encoding: .utf8)
XCTAssertTrue(text.contains("\n"))
XCTAssertTrue(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 {
XCTFail("missing or out-of-order key \(key)")
return
}
lastIndex = range.upperBound
}
}
}