diff --git a/src/main/rate-limits/grok-fetcher.test.ts b/src/main/rate-limits/grok-fetcher.test.ts index f816d5c552fc..21d03623748c 100644 --- a/src/main/rate-limits/grok-fetcher.test.ts +++ b/src/main/rate-limits/grok-fetcher.test.ts @@ -100,6 +100,26 @@ describe('fetchGrokRateLimits', () => { ) }) + it('maps omitted proto3 credit usage to zero for a weekly period', async () => { + authState.file = freshAuthJson() + netFetchMock.mockResolvedValueOnce( + jsonResponse({ + config: { + currentPeriod: { + type: 'USAGE_PERIOD_TYPE_WEEKLY', + start: '2026-07-07T18:36:14.268512+00:00', + end: '2026-07-14T18:36:14.268512+00:00' + }, + subscriptionTier: 'SuperGrok' + } + }) + ) + + const result = await fetchGrokRateLimits() + expect(result.status).toBe('ok') + expect(result.weekly?.usedPercent).toBe(0) + }) + it('returns unavailable when not signed in even if a token-less auth file exists', async () => { authState.file = JSON.stringify({}) const result = await fetchGrokRateLimits() diff --git a/src/main/rate-limits/grok-fetcher.ts b/src/main/rate-limits/grok-fetcher.ts index 963a2fb13505..7824dab7d6ad 100644 --- a/src/main/rate-limits/grok-fetcher.ts +++ b/src/main/rate-limits/grok-fetcher.ts @@ -67,7 +67,13 @@ function parseResetDescription(isoString: string | undefined): string | null { } function mapWeeklyCredits(config: GrokBillingConfig): RateLimitWindow | null { - const usedPercent = config.creditUsagePercent + // Why: proto3 JSON omits a zero-valued percentage; a weekly period still + // identifies a valid credit window whose usage is exactly zero. + const usedPercent = + config.creditUsagePercent === undefined && + config.currentPeriod?.type === 'USAGE_PERIOD_TYPE_WEEKLY' + ? 0 + : config.creditUsagePercent if (typeof usedPercent !== 'number' || !Number.isFinite(usedPercent)) { return null }