|
| 1 | +import CodexBarCore |
| 2 | +import Foundation |
| 3 | +import Testing |
| 4 | +@testable import CodexBar |
| 5 | + |
| 6 | +@Suite(.serialized) |
| 7 | +struct CodexBarConfigMigratorTests { |
| 8 | + @Test |
| 9 | + func `legacy secret migration completion flag skips repeated scans`() throws { |
| 10 | + let suite = "CodexBarConfigMigratorTests-skip-\(UUID().uuidString)" |
| 11 | + let defaults = try #require(UserDefaults(suiteName: suite)) |
| 12 | + defaults.removePersistentDomain(forName: suite) |
| 13 | + defer { defaults.removePersistentDomain(forName: suite) } |
| 14 | + |
| 15 | + let secrets = CountingLegacySecretStore() |
| 16 | + let accountStore = CountingTokenAccountStore() |
| 17 | + let stores = Self.legacyStores(secrets: secrets, accountStore: accountStore) |
| 18 | + let configStore = testConfigStore(suiteName: suite) |
| 19 | + |
| 20 | + _ = CodexBarConfigMigrator.loadOrMigrate(configStore: configStore, userDefaults: defaults, stores: stores) |
| 21 | + |
| 22 | + let firstSecretLoads = secrets.loadCount |
| 23 | + let firstAccountLoads = accountStore.loadCount |
| 24 | + #expect(firstSecretLoads > 0) |
| 25 | + #expect(firstAccountLoads == 1) |
| 26 | + #expect(defaults.bool(forKey: Self.legacyMigrationCompletedKey) == true) |
| 27 | + |
| 28 | + _ = CodexBarConfigMigrator.loadOrMigrate(configStore: configStore, userDefaults: defaults, stores: stores) |
| 29 | + |
| 30 | + #expect(secrets.loadCount == firstSecretLoads) |
| 31 | + #expect(accountStore.loadCount == firstAccountLoads) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + func `legacy migration completion waits for successful cleanup`() throws { |
| 36 | + let suite = "CodexBarConfigMigratorTests-cleanup-failure-\(UUID().uuidString)" |
| 37 | + let defaults = try #require(UserDefaults(suiteName: suite)) |
| 38 | + defaults.removePersistentDomain(forName: suite) |
| 39 | + defer { defaults.removePersistentDomain(forName: suite) } |
| 40 | + |
| 41 | + let secrets = CountingLegacySecretStore(token: "legacy-token", throwOnStore: true) |
| 42 | + let accountStore = CountingTokenAccountStore() |
| 43 | + let stores = Self.legacyStores(secrets: secrets, accountStore: accountStore) |
| 44 | + let configStore = testConfigStore(suiteName: suite) |
| 45 | + |
| 46 | + _ = CodexBarConfigMigrator.loadOrMigrate(configStore: configStore, userDefaults: defaults, stores: stores) |
| 47 | + |
| 48 | + let firstSecretLoads = secrets.loadCount |
| 49 | + #expect(firstSecretLoads > 0) |
| 50 | + #expect(secrets.clearAttempts > 0) |
| 51 | + #expect(defaults.bool(forKey: Self.legacyMigrationCompletedKey) == false) |
| 52 | + |
| 53 | + secrets.throwOnStore = false |
| 54 | + _ = CodexBarConfigMigrator.loadOrMigrate(configStore: configStore, userDefaults: defaults, stores: stores) |
| 55 | + |
| 56 | + #expect(secrets.loadCount > firstSecretLoads) |
| 57 | + #expect(defaults.bool(forKey: Self.legacyMigrationCompletedKey) == true) |
| 58 | + } |
| 59 | + |
| 60 | + private static let legacyMigrationCompletedKey = "codexbar.legacySecretsMigrationCompleted" |
| 61 | + |
| 62 | + private static func legacyStores( |
| 63 | + secrets: CountingLegacySecretStore, |
| 64 | + accountStore: CountingTokenAccountStore) -> CodexBarConfigMigrator.LegacyStores |
| 65 | + { |
| 66 | + CodexBarConfigMigrator.LegacyStores( |
| 67 | + zaiTokenStore: secrets, |
| 68 | + syntheticTokenStore: secrets, |
| 69 | + codexCookieStore: secrets, |
| 70 | + claudeCookieStore: secrets, |
| 71 | + cursorCookieStore: secrets, |
| 72 | + opencodeCookieStore: secrets, |
| 73 | + factoryCookieStore: secrets, |
| 74 | + minimaxCookieStore: secrets, |
| 75 | + minimaxAPITokenStore: secrets, |
| 76 | + kimiTokenStore: secrets, |
| 77 | + kimiK2TokenStore: secrets, |
| 78 | + augmentCookieStore: secrets, |
| 79 | + ampCookieStore: secrets, |
| 80 | + copilotTokenStore: secrets, |
| 81 | + tokenAccountStore: accountStore) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +private final class CountingLegacySecretStore: ZaiTokenStoring, SyntheticTokenStoring, CookieHeaderStoring, |
| 86 | + MiniMaxCookieStoring, MiniMaxAPITokenStoring, KimiTokenStoring, KimiK2TokenStoring, CopilotTokenStoring, |
| 87 | + @unchecked Sendable |
| 88 | +{ |
| 89 | + private let lock = NSLock() |
| 90 | + private var token: String? |
| 91 | + var throwOnStore: Bool |
| 92 | + private(set) var loadCount = 0 |
| 93 | + private(set) var clearAttempts = 0 |
| 94 | + |
| 95 | + init(token: String? = nil, throwOnStore: Bool = false) { |
| 96 | + self.token = token |
| 97 | + self.throwOnStore = throwOnStore |
| 98 | + } |
| 99 | + |
| 100 | + func loadToken() throws -> String? { |
| 101 | + self.lock.lock() |
| 102 | + defer { self.lock.unlock() } |
| 103 | + self.loadCount += 1 |
| 104 | + return self.token |
| 105 | + } |
| 106 | + |
| 107 | + func storeToken(_ token: String?) throws { |
| 108 | + try self.store(token) |
| 109 | + } |
| 110 | + |
| 111 | + func loadCookieHeader() throws -> String? { |
| 112 | + self.lock.lock() |
| 113 | + defer { self.lock.unlock() } |
| 114 | + self.loadCount += 1 |
| 115 | + return self.token |
| 116 | + } |
| 117 | + |
| 118 | + func storeCookieHeader(_ header: String?) throws { |
| 119 | + try self.store(header) |
| 120 | + } |
| 121 | + |
| 122 | + private func store(_ value: String?) throws { |
| 123 | + self.lock.lock() |
| 124 | + defer { self.lock.unlock() } |
| 125 | + self.clearAttempts += value == nil ? 1 : 0 |
| 126 | + if self.throwOnStore { |
| 127 | + throw TestStoreError.storeFailed |
| 128 | + } |
| 129 | + self.token = value |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +private final class CountingTokenAccountStore: ProviderTokenAccountStoring, @unchecked Sendable { |
| 134 | + private let lock = NSLock() |
| 135 | + private(set) var loadCount = 0 |
| 136 | + |
| 137 | + func loadAccounts() throws -> [UsageProvider: ProviderTokenAccountData] { |
| 138 | + self.lock.lock() |
| 139 | + defer { self.lock.unlock() } |
| 140 | + self.loadCount += 1 |
| 141 | + return [:] |
| 142 | + } |
| 143 | + |
| 144 | + func storeAccounts(_: [UsageProvider: ProviderTokenAccountData]) throws {} |
| 145 | + |
| 146 | + func ensureFileExists() throws -> URL { |
| 147 | + FileManager.default.temporaryDirectory.appendingPathComponent("codexbar-empty-accounts.json") |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +private enum TestStoreError: Error { |
| 152 | + case storeFailed |
| 153 | +} |
0 commit comments