feat(dashboard): retry status edit before reposting
A transient Discord error (e.g. HTTP 503) on the periodic status-message edit previously caused an immediate repost of a fresh status message. Now the edit is retried up to 2 more times at 15s spacing, and a fresh message is only sent if every attempt fails. Retry logic is extracted into the testable editStatusMessageWithRetry helper (injectable sleep). Added a re-entrancy guard so overlapping interval ticks are skipped while a slow retry runs, preventing double-posts. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
|
||||
import type { UsageRow } from './dashboard-usage-rows.js';
|
||||
import {
|
||||
buildWebUsageRowsForSnapshot,
|
||||
editStatusMessageWithRetry,
|
||||
formatStatusHeader,
|
||||
getDashboardDuplicateCleanupIntervalMs,
|
||||
renderUsageTable,
|
||||
@@ -228,3 +229,86 @@ describe('buildWebUsageRowsForSnapshot', () => {
|
||||
expect(rows.map((row) => row.name)).toEqual(['Codex1']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('editStatusMessageWithRetry', () => {
|
||||
it('succeeds on the first attempt without sleeping', async () => {
|
||||
let calls = 0;
|
||||
let slept = 0;
|
||||
const ok = await editStatusMessageWithRetry({
|
||||
editOnce: async () => {
|
||||
calls++;
|
||||
},
|
||||
maxRetries: 2,
|
||||
retryDelayMs: 15_000,
|
||||
sleep: async () => {
|
||||
slept++;
|
||||
},
|
||||
});
|
||||
expect(ok).toBe(true);
|
||||
expect(calls).toBe(1);
|
||||
expect(slept).toBe(0);
|
||||
});
|
||||
|
||||
it('retries after a failure and succeeds on the second attempt', async () => {
|
||||
let calls = 0;
|
||||
const delays: number[] = [];
|
||||
const ok = await editStatusMessageWithRetry({
|
||||
editOnce: async () => {
|
||||
calls++;
|
||||
if (calls === 1) throw new Error('503');
|
||||
},
|
||||
maxRetries: 2,
|
||||
retryDelayMs: 15_000,
|
||||
sleep: async (ms) => {
|
||||
delays.push(ms);
|
||||
},
|
||||
});
|
||||
expect(ok).toBe(true);
|
||||
expect(calls).toBe(2);
|
||||
expect(delays).toEqual([15_000]); // one 15s wait between the two attempts
|
||||
});
|
||||
|
||||
it('gives up after maxRetries (3 total attempts) and reports each failure', async () => {
|
||||
let calls = 0;
|
||||
const attempts: Array<{ attempt: number; willRetry: boolean }> = [];
|
||||
let slept = 0;
|
||||
const ok = await editStatusMessageWithRetry({
|
||||
editOnce: async () => {
|
||||
calls++;
|
||||
throw new Error('503');
|
||||
},
|
||||
maxRetries: 2,
|
||||
retryDelayMs: 15_000,
|
||||
sleep: async () => {
|
||||
slept++;
|
||||
},
|
||||
onAttemptFailed: (attempt, willRetry) =>
|
||||
attempts.push({ attempt, willRetry }),
|
||||
});
|
||||
expect(ok).toBe(false);
|
||||
expect(calls).toBe(3); // initial + 2 retries
|
||||
expect(slept).toBe(2); // only between attempts, not after the last
|
||||
expect(attempts).toEqual([
|
||||
{ attempt: 1, willRetry: true },
|
||||
{ attempt: 2, willRetry: true },
|
||||
{ attempt: 3, willRetry: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it('uses 15s spacing via the default sleep only when a retry is needed', async () => {
|
||||
// Sanity: with maxRetries 0, a single failure returns false and never sleeps.
|
||||
let slept = 0;
|
||||
const ok = await editStatusMessageWithRetry({
|
||||
editOnce: async () => {
|
||||
throw new Error('nope');
|
||||
},
|
||||
maxRetries: 0,
|
||||
retryDelayMs: 15_000,
|
||||
sleep: async () => {
|
||||
slept++;
|
||||
},
|
||||
});
|
||||
expect(ok).toBe(false);
|
||||
expect(slept).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user