CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息
Swift appkitcodexcodex-clideveloper-toolsmacosmacos-appmenu-bar-appopenai-codexproductivityswiftswiftuiusage-tracker
import CloudKit
import Foundation
import Testing
struct AggregationPolicyTests {
@Test func tokenUsageIdentityAndCloudEncodingPreserveAlgorithmVersions() throws {
let now = Date()
var turn = TestFixtures.tokenUsageAggregate(at: now)
turn.aggregationVersion = 1
let daily = try #require(TokenUsageSyncRecord.dailyRecords(local: [turn], deviceID: "device", now: now).first)
let record = CKRecord(recordType: TokenUsageSync.recordType, recordID: CKRecord.ID(recordName: daily.id))
try TokenUsageSync.apply(daily, to: record)
#expect(try TokenUsageSync.dailyRecord(from: record)?.aggregationVersion == AggregationVersion.tokenUsage)
let observation = TestFixtures.tokenUsageObservation(at: now, tokenUsage: .zero)
let encoded = try JSONLines.stableEncoder.encode(observation)
let object = try #require(JSONSerialization.jsonObject(with: encoded) as? [String: Any])
#expect(object["turn"] == nil)
#expect(object["rootID"] == nil)
#expect(object["aggregationVersion"] == nil)
#expect(object["observedAt"] != nil)
}
@Test func unsupportedTokenUsageAlgorithmCannotEnterLocalSnapshots() throws {
let first = TestFixtures.tokenUsageAggregate(at: Date())
var unsupported = first
unsupported.aggregationVersion = 999
#expect(throws: StorageCompatibilityError.self) { try TokenUsageSyncRecord.dailyRecords(local: [first, unsupported], deviceID: "device") }
#expect(throws: StorageCompatibilityError.self) {
try JSONLines.decoder.decode(TokenUsageAggregate.self, from: JSONLines.stableEncoder.encode(unsupported))
}
}
@Test func algorithmCompatibilityAcceptsPastAndRejectsFuture() throws {
try AggregationVersion.require(1, current: 2, name: "test")
#expect(throws: StorageCompatibilityError.self) { try AggregationVersion.require(3, current: 2, name: "test") }
#expect(throws: StorageCompatibilityError.self) { try AggregationVersion.require(0, current: 2, name: "test") }
}
}