113 lines
3.5 KiB
Swift
113 lines
3.5 KiB
Swift
import Foundation
|
|
import Testing
|
|
@testable import LocalData
|
|
|
|
// MARK: - Test Keys
|
|
|
|
private func makeCatalogKey(name: String, description: String = "Test key") -> StorageKey<String> {
|
|
StorageKey(
|
|
name: name,
|
|
domain: .userDefaults(suite: nil),
|
|
security: .none,
|
|
owner: "CatalogTests",
|
|
description: description
|
|
)
|
|
}
|
|
|
|
// MARK: - Test Catalogs
|
|
|
|
private struct ValidCatalog: StorageKeyCatalog {
|
|
var allKeys: [AnyStorageKey] {
|
|
[
|
|
.key(makeCatalogKey(name: "valid.key1", description: "First test key")),
|
|
.key(makeCatalogKey(name: "valid.key2", description: "Second test key"))
|
|
]
|
|
}
|
|
}
|
|
|
|
private struct DuplicateNameCatalog: StorageKeyCatalog {
|
|
var allKeys: [AnyStorageKey] {
|
|
[
|
|
.key(makeCatalogKey(name: "duplicate.name", description: "First instance")),
|
|
.key(makeCatalogKey(name: "duplicate.name", description: "Second instance"))
|
|
]
|
|
}
|
|
}
|
|
|
|
private struct EmptyCatalog: StorageKeyCatalog {
|
|
var allKeys: [AnyStorageKey] { [] }
|
|
}
|
|
|
|
private struct MissingDescriptionCatalog: StorageKeyCatalog {
|
|
var allKeys: [AnyStorageKey] {
|
|
[
|
|
.key(makeCatalogKey(name: "missing.desc", description: " "))
|
|
]
|
|
}
|
|
}
|
|
|
|
// MARK: - Tests
|
|
|
|
@Suite(.serialized)
|
|
struct StorageCatalogTests {
|
|
private let router = StorageRouter(keychain: MockKeychainHelper())
|
|
|
|
@Test func auditReportContainsAllKeys() {
|
|
let items = StorageAuditReport.items(for: ValidCatalog())
|
|
|
|
#expect(items.count == 2)
|
|
#expect(items[0].name == "valid.key1")
|
|
#expect(items[1].name == "valid.key2")
|
|
}
|
|
|
|
@Test func auditReportRendersText() {
|
|
let report = StorageAuditReport.renderText(ValidCatalog())
|
|
|
|
#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 = makeCatalogKey(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())
|
|
#expect(items.isEmpty)
|
|
|
|
let report = StorageAuditReport.renderText(EmptyCatalog())
|
|
#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 router.registerCatalog(DuplicateNameCatalog())
|
|
}
|
|
}
|
|
|
|
@Test func catalogRegistrationDetectsMissingDescriptions() async {
|
|
// Attempting to register a catalog with missing descriptions should throw
|
|
await #expect(throws: StorageError.self) {
|
|
try await router.registerCatalog(MissingDescriptionCatalog())
|
|
}
|
|
}
|
|
|
|
@Test func migrateAllRegisteredKeysInvokesMigrationOnKeys() async throws {
|
|
// This test verifies that migrateAllRegisteredKeys calling logic works.
|
|
try await router.registerCatalog(ValidCatalog())
|
|
|
|
// No error should occur
|
|
try await router.migrateAllRegisteredKeys()
|
|
}
|
|
}
|