diff --git a/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts b/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts index f62285175b..7181f63c91 100644 --- a/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts +++ b/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts @@ -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() diff --git a/frontend/src/utils/__tests__/accountUsageRefresh.spec.ts b/frontend/src/utils/__tests__/accountUsageRefresh.spec.ts index f2d21a4212..1bba33e58e 100644 --- a/frontend/src/utils/__tests__/accountUsageRefresh.spec.ts +++ b/frontend/src/utils/__tests__/accountUsageRefresh.spec.ts @@ -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)) diff --git a/frontend/src/utils/accountUsageRefresh.ts b/frontend/src/utils/accountUsageRefresh.ts index 4d64ee7899..be01b6b415 100644 --- a/frontend/src/utils/accountUsageRefresh.ts +++ b/frontend/src/utils/accountUsageRefresh.ts @@ -70,12 +70,13 @@ export const buildGrokUsageRefreshKey = (account: Pick): string => { +export const buildAccountUsageRefreshKey = (account: Pick): 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