fix: show a rolled-over usage window as unknown, not 0%

91203d8 rendered an expired window as 0% unconditionally. If the post-reset
refetch fails (network error, 429) the 0% stayed for as long as fetches
kept failing, the same stale-value bug in the other direction. An expired
window now renders as -1 (the empty "─────" unknown cell) until a fetch
succeeds and brings the new window's real value.

Tests: expired window -> -1 for Claude and Codex rows; refetch failing at
+1 min and +60 min stays unknown, then success shows 3%; 429 shows the
429 indicator; renderer draws -1 as the placeholder cell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-19 23:10:51 +09:00
parent 91203d84fc
commit 97cc670e34
5 changed files with 128 additions and 27 deletions

View File

@@ -150,39 +150,93 @@ 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' },
},
// Production case (2026-10-19): fetched 99% at 22:59:04 KST for a 5h window
// resetting at 23:00:00; the 5-min throttle kept serving 99% until 23:04.
const staleCache = {
'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 freshBody = {
five_hour: { utilization: 3, resets_at: '2026-10-19T19:00:00Z' },
seven_day: { utilization: 27, resets_at: '2026-10-19T20:00:00Z' },
};
function okResponse(body: unknown): Response {
return { ...makeResponse(200), json: async () => body } as Response;
}
async function dashboardRow() {
const { fetchClaudeUsage } = await import('./claude-usage.js');
const { buildClaudeUsageRows } = await import('./dashboard-usage-rows.js');
return buildClaudeUsageRows(await fetchClaudeUsage())[0];
}
// Before the reset: throttled, no API call.
await fetchClaudeUsage();
beforeEach(() => {
vi.useFakeTimers();
readJsonFileMock.mockReturnValue(structuredClone(staleCache));
});
it('skips the 5-minute throttle once when the cached window has reset', async () => {
vi.setSystemTime(new Date('2026-10-19T13:59:30Z'));
const fetchMock = vi.fn(async () => okResponse(freshBody));
vi.stubGlobal('fetch', fetchMock);
// Before the reset: throttled, no API call, cached value still valid.
expect(await dashboardRow()).toMatchObject({ h5pct: 99, d7pct: 27 });
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(await dashboardRow()).toMatchObject({ h5pct: 3, d7pct: 27 });
expect(fetchMock).toHaveBeenCalledTimes(1);
// The throttle applies again after that one refetch.
vi.setSystemTime(new Date('2026-10-19T14:00:40Z'));
await fetchClaudeUsage();
await dashboardRow();
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('shows the expired window as unknown while refetches fail, then the real value', async () => {
const fetchMock = vi
.fn()
.mockRejectedValueOnce(new Error('ECONNRESET'))
.mockRejectedValueOnce(new Error('ECONNRESET'))
.mockResolvedValueOnce(okResponse(freshBody));
vi.stubGlobal('fetch', fetchMock);
// +1 min after reset: refetch fails -> unknown, never 99% or 0%.
vi.setSystemTime(new Date('2026-10-19T14:01:00Z'));
expect(await dashboardRow()).toMatchObject({ h5pct: -1, h5reset: '' });
expect(fetchMock).toHaveBeenCalledTimes(1);
// +60 min: still failing -> still unknown.
vi.setSystemTime(new Date('2026-10-19T15:00:00Z'));
expect(await dashboardRow()).toMatchObject({ h5pct: -1, h5reset: '' });
expect(fetchMock).toHaveBeenCalledTimes(2);
// Next allowed attempt succeeds -> real new-window value.
vi.setSystemTime(new Date('2026-10-19T15:05:30Z'));
expect(await dashboardRow()).toMatchObject({ h5pct: 3, d7pct: 27 });
expect(fetchMock).toHaveBeenCalledTimes(3);
});
it('shows the 429 indicator (not a stale %) when the post-reset refetch is rate limited', async () => {
const fetchMock = vi.fn(async () =>
makeResponse(429, { 'retry-after': '120' }),
);
vi.stubGlobal('fetch', fetchMock);
vi.setSystemTime(new Date('2026-10-19T14:01:00Z'));
expect(await dashboardRow()).toMatchObject({
h5pct: -1,
d7pct: -1,
error: '429',
});
});
});
describe('getCachedClaudeUsageLimitResetAt', () => {