import Foundationimport Testing@testable import CodexBarCore @Suite(.serialized)struct KeychainCacheStoreTests { struct TestEntry: Codable, Equatable { let value: String let storedAt: Date } @Test func storesAndLoadsEntry() throws { KeychainCacheStore.setTestStoreForTesting(true) defer { KeychainCacheStore.setTestStoreForTesting(false) } let key = KeychainCacheStore.Key(category: "test", identifier: UUID().uuidString) let storedAt = Date(timeIntervalSince1970: 0) let entry = TestEntry(value: "alpha", storedAt: storedAt) KeychainCacheStore.store(key: key, entry: entry) defer { KeychainCacheStore.clear(key: key) } switch KeychainCacheStore.load(key: key, as: TestEntry.self) { case let .found(loaded): #expect(loaded == entry) case .missing, .invalid: #expect(Bool(false), "Expected keychain cache entry") } } @Test func overwritesExistingEntry() throws { KeychainCacheStore.setTestStoreForTesting(true) defer { KeychainCacheStore.setTestStoreForTesting(false) } let key = KeychainCacheStore.Key(category: "test", identifier: UUID().uuidString) let first = TestEntry(value: "first", storedAt: Date(timeIntervalSince1970: 1)) let second = TestEntry(value: "second", storedAt: Date(timeIntervalSince1970: 2)) KeychainCacheStore.store(key: key, entry: first) KeychainCacheStore.store(key: key, entry: second) defer { KeychainCacheStore.clear(key: key) } switch KeychainCacheStore.load(key: key, as: TestEntry.self) { case let .found(loaded): #expect(loaded == second) case .missing, .invalid: #expect(Bool(false), "Expected overwritten keychain cache entry") } } @Test func clearRemovesEntry() throws { KeychainCacheStore.setTestStoreForTesting(true) defer { KeychainCacheStore.setTestStoreForTesting(false) } let key = KeychainCacheStore.Key(category: "test", identifier: UUID().uuidString) let entry = TestEntry(value: "gone", storedAt: Date(timeIntervalSince1970: 0)) KeychainCacheStore.store(key: key, entry: entry) KeychainCacheStore.clear(key: key) switch KeychainCacheStore.load(key: key, as: TestEntry.self) { case .missing: #expect(true) case .found, .invalid: #expect(Bool(false), "Expected keychain cache entry to be cleared") } }}
import Foundation
import Testing
@testable import CodexBarCore
@Suite(.serialized)
struct KeychainCacheStoreTests {
struct TestEntry: Codable, Equatable {
let value: String
let storedAt: Date
}
@Test
func storesAndLoadsEntry() throws {
KeychainCacheStore.setTestStoreForTesting(true)
defer { KeychainCacheStore.setTestStoreForTesting(false) }
let key = KeychainCacheStore.Key(category: "test", identifier: UUID().uuidString)
let storedAt = Date(timeIntervalSince1970: 0)
let entry = TestEntry(value: "alpha", storedAt: storedAt)
KeychainCacheStore.store(key: key, entry: entry)
defer { KeychainCacheStore.clear(key: key) }
switch KeychainCacheStore.load(key: key, as: TestEntry.self) {
case let .found(loaded):
#expect(loaded == entry)
case .missing, .invalid:
#expect(Bool(false), "Expected keychain cache entry")
}
}
@Test
func overwritesExistingEntry() throws {
KeychainCacheStore.setTestStoreForTesting(true)
defer { KeychainCacheStore.setTestStoreForTesting(false) }
let key = KeychainCacheStore.Key(category: "test", identifier: UUID().uuidString)
let first = TestEntry(value: "first", storedAt: Date(timeIntervalSince1970: 1))
let second = TestEntry(value: "second", storedAt: Date(timeIntervalSince1970: 2))
KeychainCacheStore.store(key: key, entry: first)
KeychainCacheStore.store(key: key, entry: second)
defer { KeychainCacheStore.clear(key: key) }
switch KeychainCacheStore.load(key: key, as: TestEntry.self) {
case let .found(loaded):
#expect(loaded == second)
case .missing, .invalid:
#expect(Bool(false), "Expected overwritten keychain cache entry")
}
}
@Test
func clearRemovesEntry() throws {
KeychainCacheStore.setTestStoreForTesting(true)
defer { KeychainCacheStore.setTestStoreForTesting(false) }
let key = KeychainCacheStore.Key(category: "test", identifier: UUID().uuidString)
let entry = TestEntry(value: "gone", storedAt: Date(timeIntervalSince1970: 0))
KeychainCacheStore.store(key: key, entry: entry)
KeychainCacheStore.clear(key: key)
switch KeychainCacheStore.load(key: key, as: TestEntry.self) {
case .missing:
#expect(true)
case .found, .invalid:
#expect(Bool(false), "Expected keychain cache entry to be cleared")
}
}
}