71 lines · 2.5 KB · Swift
RawDownload
import Foundationimport Testing@testable import CodexBarCore @Suite(.serialized)struct KeychainCacheStoreTests {    struct TestEntry: Codable, Equatable {        let value: String        let storedAt: Date    }     @Test    func `stores and loads entry`() {        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 `overwrites existing entry`() {        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 `clear removes entry`() {        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")        }    }}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t