LocalData/Tests/LocalDataTests/StorageCatalogTests.swift
Matt Bruce 905cabc542 Update Audit, Helpers, Services and tests, docs
Summary:
- Sources: update Audit, Helpers, Services
- Tests: update tests for StorageCatalogTests.swift
- Docs: add docs for Design, Migration, Testing

Stats:
- 10 files changed, 181 insertions(+), 1 deletion(-)
2026-01-18 13:43:09 -06:00

120 lines
3.9 KiB
Swift

import Foundation
import Testing
@testable import LocalData
// MARK: - Test Keys
private struct TestCatalogKey: StorageKey {
typealias Value = String
let name: String
let domain: StorageDomain = .userDefaults(suite: nil)
let security: SecurityPolicy = .none
let serializer: Serializer<String> = .json
let owner: String = "CatalogTests"
let description: String
let availability: PlatformAvailability = .all
let syncPolicy: SyncPolicy = .never
init(name: String, description: String = "Test key") {
self.name = name
self.description = description
}
}
// MARK: - Test Catalogs
private struct ValidCatalog: StorageKeyCatalog {
static var allKeys: [AnyStorageKey] {
[
.key(TestCatalogKey(name: "valid.key1", description: "First test key")),
.key(TestCatalogKey(name: "valid.key2", description: "Second test key"))
]
}
}
private struct DuplicateNameCatalog: StorageKeyCatalog {
static var allKeys: [AnyStorageKey] {
[
.key(TestCatalogKey(name: "duplicate.name", description: "First instance")),
.key(TestCatalogKey(name: "duplicate.name", description: "Second instance"))
]
}
}
private struct EmptyCatalog: StorageKeyCatalog {
static var allKeys: [AnyStorageKey] { [] }
}
private struct MissingDescriptionCatalog: StorageKeyCatalog {
static var allKeys: [AnyStorageKey] {
[
.key(TestCatalogKey(name: "missing.desc", description: " "))
]
}
}
// MARK: - Tests
struct StorageCatalogTests {
@Test func auditReportContainsAllKeys() {
let items = StorageAuditReport.items(for: ValidCatalog.self)
#expect(items.count == 2)
#expect(items[0].name == "valid.key1")
#expect(items[1].name == "valid.key2")
}
@Test func auditReportRendersText() {
let report = StorageAuditReport.renderText(for: ValidCatalog.self)
#expect(report.contains("valid.key1"))
#expect(report.contains("valid.key2"))
#expect(report.contains("First test key"))
#expect(report.contains("Second test key"))
}
@Test func descriptorCapturesKeyMetadata() {
let key = TestCatalogKey(name: "metadata.test", description: "Metadata test key")
let anyKey = AnyStorageKey.key(key)
let descriptor = anyKey.descriptor
#expect(descriptor.name == "metadata.test")
#expect(descriptor.owner == "CatalogTests")
#expect(descriptor.description == "Metadata test key")
#expect(descriptor.valueType == "String")
}
@Test func emptyReportForEmptyCatalog() {
let items = StorageAuditReport.items(for: EmptyCatalog.self)
#expect(items.isEmpty)
let report = StorageAuditReport.renderText(for: EmptyCatalog.self)
#expect(report.isEmpty)
}
@Test func catalogRegistrationDetectsDuplicates() async {
// Attempting to register a catalog with duplicate key names should throw
await #expect(throws: StorageError.self) {
try await StorageRouter.shared.registerCatalog(DuplicateNameCatalog.self)
}
}
@Test func catalogRegistrationDetectsMissingDescriptions() async {
// Attempting to register a catalog with missing descriptions should throw
await #expect(throws: StorageError.self) {
try await StorageRouter.shared.registerCatalog(MissingDescriptionCatalog.self)
}
}
@Test func migrateAllRegisteredKeysInvokesMigrationOnKeys() async throws {
// This test verifies that migrateAllRegisteredKeys calling logic works.
// We'll use the shared StorageRouter and register a clean catalog first.
try await StorageRouter.shared.registerCatalog(ValidCatalog.self)
// No error should occur
try await StorageRouter.shared.migrateAllRegisteredKeys()
}
}