Merge pull request #8860 from Anankke/fix/account-usage-auto-refresh

fix(accounts): refetch Gemini usage when the account's quota tier changes
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-11-26 11:45:30 +08:00
commit 4ecec232d8
3 files changed
+77 -2

No files matched your search

@@ -134,6 +134,61 @@ describe('AccountUsageCell', () => {
wrapper.unmount()
})
it('refetches Gemini usage when the account quota tier changes', async () => {
const account = makeAccount({ id: 98210, platform: 'gemini', type: 'oauth', credentials: { oauth_type: 'google_one', tier_id: 'google_ai_pro' } })
getUsage.mockResolvedValue({ gemini_shared_daily: { utilization: 31 } })
const wrapper = mount(AccountUsageCell, { props: { account } })
await flushPromises()
expect(getUsage).toHaveBeenCalledTimes(1)
expect(getUsage).toHaveBeenLastCalledWith(account.id)
await wrapper.setProps({ account: { ...account, credentials: { ...account.credentials, tier_id: 'google_ai_ultra' } } })
await flushPromises()
expect(getUsage).toHaveBeenCalledTimes(2)
expect(getUsage).toHaveBeenLastCalledWith(account.id)
wrapper.unmount()
})
it.each([false, true])('sends one usage request when a relay Gemini base_url becomes the official one (batch %s)', async (batch) => {
const requestBatchedUsage = vi.fn()
const relayAccount = (baseUrl: string) => makeAccount({
id: 98220,
platform: 'gemini',
type: 'apikey',
credentials: { base_url: baseUrl, tier_id: 'aistudio_free' }
})
const wrapper = mount(AccountUsageCell, {
props: { account: relayAccount('https://relay.example'), ...(batch ? { requestBatchedUsage } : {}) }
})
const calls = () => (batch ? requestBatchedUsage.mock.calls.length : getUsage.mock.calls.length)
await flushPromises()
const before = calls()
await wrapper.setProps({ account: relayAccount('https://generativelanguage.googleapis.com') })
await flushPromises()
expect(calls() - before).toBe(1)
if (!batch) expect(getUsage).toHaveBeenLastCalledWith(98220)
wrapper.unmount()
})
it.each([false, true])('sends no usage request when one relay Gemini base_url becomes another (batch %s)', async (batch) => {
const requestBatchedUsage = vi.fn()
const relayAccount = (baseUrl: string) => makeAccount({
id: 98230,
platform: 'gemini',
type: 'apikey',
credentials: { base_url: baseUrl, tier_id: 'aistudio_free' }
})
const wrapper = mount(AccountUsageCell, {
props: { account: relayAccount('https://relay.example'), ...(batch ? { requestBatchedUsage } : {}) }
})
const calls = () => (batch ? requestBatchedUsage.mock.calls.length : getUsage.mock.calls.length)
await flushPromises()
const before = calls()
await wrapper.setProps({ account: relayAccount('https://relay2.example') })
await flushPromises()
expect(calls()).toBe(before)
wrapper.unmount()
})
it.each(['anthropic', 'gemini'] as const)('asks the desktop batch manager to refresh %s without force', async (platform) => {
const account = makeAccount({ id: 98301, platform, extra: {} })
const requestBatchedUsage = vi.fn()
@@ -18,6 +18,17 @@ describe('buildAccountUsageRefreshKey', () => {
expect(buildAccountUsageRefreshKey({ ...base, last_used_at: 'new' })).not.toBe(buildAccountUsageRefreshKey(base))
})
it('tracks the Gemini quota tier and ignores other credential keys', () => {
const base = { id: 1, platform: 'gemini', type: 'oauth', credentials: { oauth_type: 'google_one', tier_id: 'google_ai_pro' } } as any
expect(buildAccountUsageRefreshKey({ ...base, credentials: { ...base.credentials, tier_id: 'google_ai_ultra' } })).not.toBe(buildAccountUsageRefreshKey(base))
expect(buildAccountUsageRefreshKey({ ...base, credentials: { ...base.credentials, oauth_type: 'ai_studio' } })).not.toBe(buildAccountUsageRefreshKey(base))
const legacy = { id: 1, platform: 'gemini', type: 'oauth', credentials: {} } as any
expect(buildAccountUsageRefreshKey({ ...legacy, credentials: { project_id: 'p-1' } })).not.toBe(buildAccountUsageRefreshKey(legacy))
expect(buildAccountUsageRefreshKey({ ...base, credentials: { ...base.credentials, client_id: 'other', expires_at: '999' } })).toBe(buildAccountUsageRefreshKey(base))
const apiKey = { id: 1, platform: 'gemini', type: 'apikey', credentials: { base_url: 'https://relay.example' } } as any
expect(buildAccountUsageRefreshKey({ ...apiKey, credentials: { ...apiKey.credentials, base_url: 'https://generativelanguage.googleapis.com' } })).not.toBe(buildAccountUsageRefreshKey(apiKey))
})
it('retains OpenAI keys and excludes unsupported platforms/types', () => {
const base = { id: 1, platform: 'openai', type: 'oauth', extra: {} } as any
expect(buildAccountUsageRefreshKey(base)).toBe(buildOpenAIUsageRefreshKey(base))
+11 -2
View File
@@ -70,12 +70,13 @@ export const buildGrokUsageRefreshKey = (account: Pick<Account, 'platform' | 'ex
].join('|')
}
export const buildAccountUsageRefreshKey = (account: Pick<Account, 'id' | 'platform' | 'type' | 'updated_at' | 'last_used_at' | 'rate_limit_reset_at' | 'session_window_start' | 'session_window_end' | 'session_window_status' | 'extra'>): string => {
export const buildAccountUsageRefreshKey = (account: Pick<Account, 'id' | 'platform' | 'type' | 'updated_at' | 'last_used_at' | 'rate_limit_reset_at' | 'session_window_start' | 'session_window_end' | 'session_window_status' | 'credentials' | 'extra'>): string => {
if (account.platform === 'openai') return buildOpenAIUsageRefreshKey(account)
const isClaude = account.platform === 'anthropic' && (account.type === 'oauth' || account.type === 'setup-token')
if (!isClaude && account.platform !== 'gemini') return ''
const credentials = account.credentials ?? {}
const extra = account.extra ?? {}
return serializeSnapshotRefreshValue([
account.id,
@@ -92,6 +93,14 @@ export const buildAccountUsageRefreshKey = (account: Pick<Account, 'id' | 'platf
extra.passive_usage_7d_reset ?? null,
extra.passive_usage_7d_oi_utilization ?? null,
extra.passive_usage_7d_oi_reset ?? null
] : [])
] : [
// Gemini quota comes from the canonical tier of (oauth_type, tier_id); a
// legacy project_id alone makes the backend treat the account as code_assist.
// An API key on a third-party base_url has no tier, the official one does.
credentials.oauth_type ?? null,
credentials.project_id ?? null,
credentials.tier_id ?? null,
credentials.base_url ?? null
])
])
}