fix(grok): honor zero-usage weekly reset

This commit is contained in:
Brennan Benson
2026-07-17 12:53:24 -07:00
parent 2cb5d4e149
commit 632a124829
2 changed files with 31 additions and 7 deletions
+26 -6
View File
@@ -100,6 +100,31 @@ describe('fetchGrokRateLimits', () => {
)
})
it('maps an omitted protobuf percentage as zero for a weekly credits period', async () => {
authState.file = freshAuthJson()
netFetchMock.mockResolvedValueOnce(
jsonResponse({
config: {
currentPeriod: {
type: 'USAGE_PERIOD_TYPE_WEEKLY',
start: '2026-07-17T19:38:56.948570+00:00',
end: '2026-07-24T19:38:56.948570+00:00'
},
billingPeriodStart: '2026-07-17T19:38:56.948570+00:00',
billingPeriodEnd: '2026-07-24T19:38:56.948570+00:00',
isUnifiedBillingUser: true
}
})
)
const result = await fetchGrokRateLimits()
expect(result.status).toBe('ok')
expect(result.weekly?.usedPercent).toBe(0)
expect(result.weekly?.resetsAt).toBe(Date.parse('2026-07-24T19:38:56.948570+00:00'))
expect(result.monthly).toBeUndefined()
expect(netFetchMock).toHaveBeenCalledTimes(1)
})
it('returns unavailable when not signed in even if a token-less auth file exists', async () => {
authState.file = JSON.stringify({})
const result = await fetchGrokRateLimits()
@@ -119,17 +144,12 @@ describe('fetchGrokRateLimits', () => {
expect(result.monthly).toBeUndefined()
})
it('maps monthly included usage for unified-billing accounts without weekly credits', async () => {
it('maps monthly included usage for unified-billing accounts without a weekly period', async () => {
authState.file = freshAuthJson()
netFetchMock
.mockResolvedValueOnce(
jsonResponse({
config: {
currentPeriod: {
type: 'USAGE_PERIOD_TYPE_WEEKLY',
start: '2026-07-10T19:38:56.948570+00:00',
end: '2026-07-17T19:38:56.948570+00:00'
},
isUnifiedBillingUser: true,
subscriptionTier: 'SuperGrok'
}
+5 -1
View File
@@ -82,7 +82,11 @@ function parseResetDescription(isoString: string | undefined): string | null {
}
function mapWeeklyCredits(config: GrokBillingConfig): RateLimitWindow | null {
const usedPercent = config.creditUsagePercent
// Why: Grok's protobuf JSON omits zero-valued percentages; a declared weekly
// period without this field is 0% usage, not an absent weekly allowance.
const usedPercent =
config.creditUsagePercent ??
(config.currentPeriod?.type === 'USAGE_PERIOD_TYPE_WEEKLY' ? 0 : null)
if (typeof usedPercent !== 'number' || !Number.isFinite(usedPercent)) {
return null
}