fix: dashboard usage no longer shows a rolled-over window's old value

At 23:00 on 2026-10-19 the Claude 5h window reset, but the dashboard kept
showing "99% reset" until 23:04:34 (real value 3%): the 5-minute fetch
throttle replayed the 22:59:04 snapshot. Codex rows were also formatted
once per 5-min poll, so their % and countdown froze between polls.

- A window whose resets_at has passed renders as 0% with no countdown.
- Claude usage fetch skips the throttle once when a cached window reset
  fell after the last attempt (429 cooldown still honoured).
- Codex rows are rebuilt from cached state at every render.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-19 23:06:16 +09:00
parent d1068f1627
commit 91203d84fc
6 changed files with 139 additions and 11 deletions

View File

@@ -149,6 +149,42 @@ describe('Claude usage 429 Retry-After backoff', () => {
});
});
describe('Claude usage refetch after a window rolls over', () => {
it('skips the 5-minute throttle once when the cached window has reset', async () => {
// Production case (2026-10-19): fetched 99% at 22:59:04 for a 5h window
// resetting at 23:00:00; the throttle kept serving 99% until 23:04.
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-10-19T13:59:30Z'));
readJsonFileMock.mockReturnValue({
'account-0': {
fetchedAt: Date.parse('2026-10-19T13:59:04Z'),
lastAttemptAt: Date.parse('2026-10-19T13:59:04Z'),
usage: {
five_hour: { utilization: 99, resets_at: '2026-10-19T14:00:00Z' },
seven_day: { utilization: 27, resets_at: '2026-10-19T20:00:00Z' },
},
},
});
const fetchMock = vi.fn(async () => makeResponse(200));
vi.stubGlobal('fetch', fetchMock);
const { fetchClaudeUsage } = await import('./claude-usage.js');
// Before the reset: throttled, no API call.
await fetchClaudeUsage();
expect(fetchMock).not.toHaveBeenCalled();
// Just after the reset: fetch the new window despite the throttle.
vi.setSystemTime(new Date('2026-10-19T14:00:10Z'));
await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
// The throttle applies again after that one refetch.
vi.setSystemTime(new Date('2026-10-19T14:00:40Z'));
await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
});
});
describe('getCachedClaudeUsageLimitResetAt', () => {
it('returns the later reset of the exhausted windows', async () => {
readJsonFileMock.mockReturnValue({