-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Preserve Claude widget quota on partial refresh #2480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ChenZiHong-Gavin
wants to merge
4
commits into
steipete:main
from
ChenZiHong-Gavin:agent/preserve-claude-widget-quota
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ebcf0a
Preserve Claude widget quota on partial refresh
ChenZiHong-Gavin 7ce6d6a
Preserve Claude widget quota before token refresh
ChenZiHong-Gavin d480079
Merge remote-tracking branch 'origin/main' into agent/preserve-claude…
ChenZiHong-Gavin 9e59018
Scope Claude widget quota to account
ChenZiHong-Gavin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
Tests/CodexBarTests/UsageStoreWidgetSnapshotAccountTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
| import Testing | ||
| @testable import CodexBar | ||
|
|
||
| @MainActor | ||
| struct UsageStoreWidgetSnapshotAccountTests { | ||
| @Test | ||
| func `widget snapshot does not preserve Claude quota after selected account changes`() async throws { | ||
| let suite = "UsageStoreWidgetSnapshotTests-claude-account-change-drops-quota" | ||
| let defaults = try #require(UserDefaults(suiteName: suite)) | ||
| defaults.removePersistentDomain(forName: suite) | ||
|
|
||
| let settings = SettingsStore( | ||
| userDefaults: defaults, | ||
| configStore: testConfigStore(suiteName: suite), | ||
| zaiTokenStore: NoopZaiTokenStore(), | ||
| syntheticTokenStore: NoopSyntheticTokenStore()) | ||
| settings.statusChecksEnabled = false | ||
| settings.addTokenAccount(provider: .claude, label: "Primary", token: "primary-token") | ||
| settings.addTokenAccount(provider: .claude, label: "Secondary", token: "secondary-token") | ||
| settings.setActiveTokenAccountIndex(0, for: .claude) | ||
|
|
||
| let store = UsageStore( | ||
| fetcher: UsageFetcher(environment: [:]), | ||
| browserDetection: BrowserDetection(cacheTTL: 0), | ||
| settings: settings) | ||
| let accounts = settings.tokenAccounts(for: .claude) | ||
| let primaryAccount = try #require(accounts.first) | ||
| let quotaOwnerKey = store.tokenAccountSnapshotCacheKey(provider: .claude, account: primaryAccount) | ||
| let quotaUpdatedAt = Date(timeIntervalSince1970: 1_800_000_000) | ||
| let tokenUpdatedAt = quotaUpdatedAt.addingTimeInterval(60) | ||
| let quota = RateWindow( | ||
| usedPercent: 28, | ||
| windowMinutes: 300, | ||
| resetsAt: quotaUpdatedAt.addingTimeInterval(3600), | ||
| resetDescription: nil) | ||
| store.lastQueuedWidgetSnapshot = WidgetSnapshot( | ||
| entries: [ | ||
| WidgetSnapshot.ProviderEntry( | ||
| provider: .claude, | ||
| updatedAt: quotaUpdatedAt, | ||
| primary: quota, | ||
| secondary: nil, | ||
| tertiary: nil, | ||
| usageRows: [ | ||
| WidgetSnapshot.WidgetUsageRowSnapshot( | ||
| id: "primary", | ||
| title: "Session", | ||
| percentLeft: 72), | ||
| ], | ||
| creditsRemaining: nil, | ||
| codeReviewRemainingPercent: nil, | ||
| tokenUsage: nil, | ||
| dailyUsage: [], | ||
| quotaOwnerKey: quotaOwnerKey), | ||
| ], | ||
| enabledProviders: [.claude], | ||
| generatedAt: quotaUpdatedAt) | ||
|
|
||
| settings.setActiveTokenAccountIndex(1, for: .claude) | ||
| store._setTokenSnapshotForTesting( | ||
| CostUsageTokenSnapshot( | ||
| sessionTokens: 4300, | ||
| sessionCostUSD: 1.50, | ||
| last30DaysTokens: 43000, | ||
| last30DaysCostUSD: 13.50, | ||
| daily: [], | ||
| updatedAt: tokenUpdatedAt), | ||
| provider: .claude) | ||
|
|
||
| var widgetSnapshots: [WidgetSnapshot] = [] | ||
| store._test_widgetSnapshotSaveOverride = { widgetSnapshots.append($0) } | ||
| defer { store._test_widgetSnapshotSaveOverride = nil } | ||
|
|
||
| store.persistWidgetSnapshot(reason: "claude-account-change-drops-quota-test") | ||
| await store.widgetSnapshotPersistTask?.value | ||
|
|
||
| let entry = try #require(widgetSnapshots.last?.entries.first { $0.provider == .claude }) | ||
| #expect(entry.primary == nil) | ||
| #expect(entry.usageRows?.isEmpty == true) | ||
| #expect(entry.quotaOwnerKey == nil) | ||
| #expect(entry.tokenUsage?.sessionTokens == 4300) | ||
| } | ||
|
|
||
| @Test | ||
| func `stacked Claude account success re-enables quota preservation`() async throws { | ||
| let suite = "UsageStoreWidgetSnapshotTests-claude-stacked-success-unblocks-quota" | ||
| let defaults = try #require(UserDefaults(suiteName: suite)) | ||
| defaults.removePersistentDomain(forName: suite) | ||
|
|
||
| let settings = SettingsStore( | ||
| userDefaults: defaults, | ||
| configStore: testConfigStore(suiteName: suite), | ||
| zaiTokenStore: NoopZaiTokenStore(), | ||
| syntheticTokenStore: NoopSyntheticTokenStore()) | ||
| settings.statusChecksEnabled = false | ||
| settings.multiAccountMenuLayout = .stacked | ||
| settings.addTokenAccount(provider: .claude, label: "Primary", token: "primary-token") | ||
| settings.addTokenAccount(provider: .claude, label: "Secondary", token: "secondary-token") | ||
| settings.setActiveTokenAccountIndex(0, for: .claude) | ||
|
|
||
| let store = UsageStore( | ||
| fetcher: UsageFetcher(environment: [:]), | ||
| browserDetection: BrowserDetection(cacheTTL: 0), | ||
| settings: settings) | ||
| let account = try #require(settings.effectiveSelectedTokenAccount(for: .claude)) | ||
| let quotaUpdatedAt = Date(timeIntervalSince1970: 1_800_000_000) | ||
| let tokenUpdatedAt = quotaUpdatedAt.addingTimeInterval(60) | ||
| let quota = RateWindow( | ||
| usedPercent: 28, | ||
| windowMinutes: 300, | ||
| resetsAt: quotaUpdatedAt.addingTimeInterval(3600), | ||
| resetDescription: nil) | ||
| let outcome = ProviderFetchOutcome( | ||
| result: .success(ProviderFetchResult( | ||
| usage: UsageSnapshot( | ||
| primary: quota, | ||
| secondary: nil, | ||
| updatedAt: quotaUpdatedAt), | ||
| credits: nil, | ||
| dashboard: nil, | ||
| sourceLabel: "fixture", | ||
| strategyID: "fixture.api-token", | ||
| strategyKind: .apiToken)), | ||
| attempts: []) | ||
|
|
||
| store.widgetUsagePreservationBlockedProviders.insert(.claude) | ||
| await store.applySelectedOutcome( | ||
| outcome, | ||
| provider: .claude, | ||
| account: account, | ||
| fallbackSnapshot: nil) | ||
| #expect(!store.widgetUsagePreservationBlockedProviders.contains(.claude)) | ||
|
|
||
| var widgetSnapshots: [WidgetSnapshot] = [] | ||
| store._test_widgetSnapshotSaveOverride = { widgetSnapshots.append($0) } | ||
| defer { store._test_widgetSnapshotSaveOverride = nil } | ||
|
|
||
| store.persistWidgetSnapshot(reason: "claude-stacked-success-primes-quota-test") | ||
| await store.widgetSnapshotPersistTask?.value | ||
| let freshEntry = try #require(widgetSnapshots.last?.entries.first { $0.provider == .claude }) | ||
| #expect(freshEntry.primary == quota) | ||
| #expect(freshEntry.quotaOwnerKey != nil) | ||
|
|
||
| store.snapshots.removeValue(forKey: .claude) | ||
| store._setTokenSnapshotForTesting( | ||
| CostUsageTokenSnapshot( | ||
| sessionTokens: 4300, | ||
| sessionCostUSD: 1.50, | ||
| last30DaysTokens: 43000, | ||
| last30DaysCostUSD: 13.50, | ||
| daily: [], | ||
| updatedAt: tokenUpdatedAt), | ||
| provider: .claude) | ||
| store.persistWidgetSnapshot(reason: "claude-stacked-success-preserves-quota-test") | ||
| await store.widgetSnapshotPersistTask?.value | ||
|
|
||
| let preservedEntry = try #require(widgetSnapshots.last?.entries.first { $0.provider == .claude }) | ||
| #expect(preservedEntry.primary == quota) | ||
| #expect(preservedEntry.quotaOwnerKey == freshEntry.quotaOwnerKey) | ||
| #expect(preservedEntry.tokenUsage?.sessionTokens == 4300) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.