Summary: - Sources: update Helpers, Protocols, Services - Tests: update tests for EncryptionHelperTests.swift, KeychainHelperTests.swift, LocalDataTests.swift (+2 more) Stats: - 9 files changed, 205 insertions(+), 103 deletions(-)
121 lines
3.8 KiB
Swift
121 lines
3.8 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
|
|
|
|
@Suite(.serialized)
|
|
struct StorageCatalogTests {
|
|
private let router = StorageRouter(keychain: MockKeychainHelper())
|
|
|
|
@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 router.registerCatalog(DuplicateNameCatalog.self)
|
|
}
|
|
}
|
|
|
|
@Test func catalogRegistrationDetectsMissingDescriptions() async {
|
|
// Attempting to register a catalog with missing descriptions should throw
|
|
await #expect(throws: StorageError.self) {
|
|
try await router.registerCatalog(MissingDescriptionCatalog.self)
|
|
}
|
|
}
|
|
|
|
@Test func migrateAllRegisteredKeysInvokesMigrationOnKeys() async throws {
|
|
// This test verifies that migrateAllRegisteredKeys calling logic works.
|
|
try await router.registerCatalog(ValidCatalog.self)
|
|
|
|
// No error should occur
|
|
try await router.migrateAllRegisteredKeys()
|
|
}
|
|
}
|