Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/rate-limits/grok-fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion src/main/rate-limits/grok-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading