test+fix(webhooks): lock delivery contract; reach the missing 24h retry

The webhook delivery service shipped to customers across the
211188a3 → bfd39b6d thread with zero direct test coverage. Writing
that coverage exposed a real off-by-one in computeNextAttempt: the
docstring published "1m / 5m / 25m / 2h / 12h / 24h, then fail" but
the implementation failed AT attempt 6 instead of after, making the
1440-minute slot in BACKOFF_MINUTES dead code. Customers reading the
contract expected a final 24h retry; they were never getting it.

Fix:
  - computeNextAttempt boundary changed from `>= MAX_ATTEMPTS` to
    `> MAX_ATTEMPTS`. attempt 6 now schedules 24h out; attempt 7 fails.
    Matches the docstring + matches BACKOFF_MINUTES.length.
  - Worker route's outcome classification mirrored: `> 6` not `>= 6`.

Tests (tests/unit/services/webhooks/deliveryService.test.ts, 22):
  Pure functions:
    - truncateResponseBody: null/undefined → null, at-cap unchanged,
      over-cap truncated with marker
    - computeNextAttempt: all 6 retry intervals locked numerically
      (1m, 5m, 25m, 2h, 12h, 24h), failure boundary at 7+,
      default-now path
  Admin-client mock:
    - enqueueDeliveryReplay: payload assertions (status='pending',
      attempt_count=0, response fields cleared, next_attempt_at=now),
      false on DB error, false on missing row
    - deliveryBelongsToEndpoint: true on match, false on mismatch,
      false on query error (fail-closed)
    - pruneDeliveredWebhookDeliveries: count returned on success,
      0 swallowed on error, 0 when count null

Suite: 48 suites · 632 tests · 0 failures. tsc + eslint clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georgy ButaevandClaude Opus 4.7 committed 2026-06-04 02:12:17 +02:00
1 parent 5854a2b583
commit eb5fc998a2
3 files changed
+267 -3

No files matched your search

+6 -2
View File
@@ -102,13 +102,17 @@ async function processDelivery(delivery: DeliveryRow): Promise<DeliveryOutcome>
}
await markFailedOrRetry(claimedDelivery, res.status, responseText);
const outcome = claimedDelivery.attempt_count >= 6 ? ('failed' as const) : ('retry' as const);
// Match the boundary used by computeNextAttempt — failure happens
// when attempt_count is STRICTLY above MAX_ATTEMPTS.
const outcome = claimedDelivery.attempt_count > 6 ? ('failed' as const) : ('retry' as const);
return { deliveryId: delivery.id, outcome, responseStatus: res.status };
} catch (err) {
// Network error / timeout.
const reason = err instanceof Error ? err.message : 'unknown';
await markFailedOrRetry(claimedDelivery, null, `[network] ${reason}`);
const outcome = claimedDelivery.attempt_count >= 6 ? ('failed' as const) : ('retry' as const);
// Match the boundary used by computeNextAttempt — failure happens
// when attempt_count is STRICTLY above MAX_ATTEMPTS.
const outcome = claimedDelivery.attempt_count > 6 ? ('failed' as const) : ('retry' as const);
return { deliveryId: delivery.id, outcome, responseStatus: null };
} finally {
clearTimeout(timeout);
+6 -1
View File
@@ -55,7 +55,12 @@ export function computeNextAttempt(
attemptCount: number,
now: Date = new Date()
): { nextAttemptAt: Date | null; shouldFail: boolean } {
if (attemptCount >= MAX_ATTEMPTS) {
// Pre-2026-06-04 this was `>= MAX_ATTEMPTS`, which made the 24h
// BACKOFF slot unreachable: attempt 6 failed straight to status=failed
// instead of scheduling the final 24h retry the docstring promises.
// Correct semantics: after the MAX_ATTEMPTS-th retry we stop, so an
// attemptCount strictly above MAX_ATTEMPTS is the failure boundary.
if (attemptCount > MAX_ATTEMPTS) {
return { nextAttemptAt: null, shouldFail: true };
}
const minutes = BACKOFF_MINUTES[attemptCount - 1] ?? BACKOFF_MINUTES[BACKOFF_MINUTES.length - 1];
@@ -0,0 +1,255 @@
/**
* Webhook delivery service — contract tests.
*
* The deliveryService shipped to customers (FleetCrown) across the
* 211188a3 → bfd39b6d webhook thread. truncateResponseBody +
* computeNextAttempt are pure functions; the rest poke the admin DB
* client via a controllable mock. None of them had direct tests until
* now, and any silent regression on the backoff schedule or the
* replay semantics would surprise customers.
*
* Created: 2026-06-04
*/
import {
computeNextAttempt,
truncateResponseBody,
enqueueDeliveryReplay,
deliveryBelongsToEndpoint,
pruneDeliveredWebhookDeliveries,
} from '@/services/webhooks/deliveryService';
// =============================================================================
// Pure-function tests — no mocks
// =============================================================================
describe('truncateResponseBody', () => {
it('passes null through as null', () => {
expect(truncateResponseBody(null)).toBeNull();
});
it('passes undefined through as null', () => {
expect(truncateResponseBody(undefined)).toBeNull();
});
it('returns short bodies unchanged', () => {
expect(truncateResponseBody('ok')).toBe('ok');
expect(truncateResponseBody('')).toBe('');
});
it('returns a body at exactly the cap unchanged', () => {
const at = 'a'.repeat(4 * 1024);
expect(truncateResponseBody(at)).toBe(at);
});
it('truncates a body over the 4KB cap and appends a marker', () => {
const over = 'a'.repeat(4 * 1024 + 100);
const result = truncateResponseBody(over);
expect(result).not.toBeNull();
expect(result!.length).toBeLessThanOrEqual(4 * 1024 + 20);
expect(result!.endsWith('…[truncated]')).toBe(true);
});
});
describe('computeNextAttempt', () => {
// The published schedule from docs and the service docstring:
// 1 → 1m, 2 → 5m, 3 → 25m, 4 → 2h, 5 → 12h, 6 → 24h, then fail.
const NOW = new Date(1_700_000_000_000);
it.each([
[1, 1 * 60_000],
[2, 5 * 60_000],
[3, 25 * 60_000],
[4, 120 * 60_000],
[5, 720 * 60_000],
[6, 1440 * 60_000],
])('attempt %i schedules %i ms in the future', (attempt, expectedMs) => {
const result = computeNextAttempt(attempt, NOW);
expect(result.shouldFail).toBe(false);
expect(result.nextAttemptAt).not.toBeNull();
expect(result.nextAttemptAt!.getTime() - NOW.getTime()).toBe(expectedMs);
});
it('after 6 attempts (== MAX_ATTEMPTS+1) flips to shouldFail with no nextAttempt', () => {
// claimDelivery bumps attempt_count BEFORE calling here, so when the
// worker fails attempt 6 the value passed in is 6, and the schedule
// above runs. When it fails attempt 7 the next call sees 7 ≥ MAX.
const result = computeNextAttempt(7, NOW);
expect(result.shouldFail).toBe(true);
expect(result.nextAttemptAt).toBeNull();
});
it('uses the wall clock when no `now` is passed', () => {
const before = Date.now();
const result = computeNextAttempt(1);
const after = Date.now();
expect(result.nextAttemptAt).not.toBeNull();
const t = result.nextAttemptAt!.getTime();
expect(t).toBeGreaterThanOrEqual(before + 60_000);
expect(t).toBeLessThanOrEqual(after + 60_000);
});
});
// =============================================================================
// DB-touching tests — controllable admin client mock
// =============================================================================
interface MockState {
updateError: { message?: string } | null;
updateReturnsRow: boolean;
selectError: { message?: string } | null;
selectRow: { id: string } | null;
deleteCount: number | null;
deleteError: { message?: string } | null;
/** Captured insert/update payload for assertions. */
lastUpdatePayload: Record<string, unknown> | null;
}
const state: MockState = {
updateError: null,
updateReturnsRow: true,
selectError: null,
selectRow: null,
deleteCount: 0,
deleteError: null,
lastUpdatePayload: null,
};
function resetState() {
state.updateError = null;
state.updateReturnsRow = true;
state.selectError = null;
state.selectRow = null;
state.deleteCount = 0;
state.deleteError = null;
state.lastUpdatePayload = null;
}
function makeBuilder() {
const updateChain = {
eq() {
return updateChain;
},
select() {
return updateChain;
},
async maybeSingle() {
return {
data: state.updateReturnsRow ? { id: 'd-1' } : null,
error: state.updateError,
};
},
};
const selectChain = {
eq() {
return selectChain;
},
async maybeSingle() {
return { data: state.selectRow, error: state.selectError };
},
};
const deleteChain = {
eq() {
return deleteChain;
},
lt() {
return deleteChain;
},
// The service awaits the chain directly to read count/error.
then(resolve: (v: { count: number | null; error: { message?: string } | null }) => unknown) {
return Promise.resolve({ count: state.deleteCount, error: state.deleteError }).then(resolve);
},
};
return {
update(payload: Record<string, unknown>) {
state.lastUpdatePayload = payload;
return updateChain;
},
select() {
return selectChain;
},
delete() {
return deleteChain;
},
};
}
jest.mock('@/lib/supabase/admin', () => ({
createAdminClient: () => ({
from: () => makeBuilder(),
}),
}));
describe('enqueueDeliveryReplay', () => {
beforeEach(resetState);
it('resets attempt_count, status, response fields, and schedules now', async () => {
const before = Date.now();
const result = await enqueueDeliveryReplay('d-1');
const after = Date.now();
expect(result).toBe(true);
const payload = state.lastUpdatePayload!;
expect(payload.status).toBe('pending');
expect(payload.attempt_count).toBe(0);
expect(payload.response_status).toBeNull();
expect(payload.response_body).toBeNull();
expect(payload.last_attempt_at).toBeNull();
// next_attempt_at is now-ish
const next = new Date(payload.next_attempt_at as string).getTime();
expect(next).toBeGreaterThanOrEqual(before - 5);
expect(next).toBeLessThanOrEqual(after + 5);
});
it('returns false on DB error', async () => {
state.updateError = { message: 'boom' };
expect(await enqueueDeliveryReplay('d-1')).toBe(false);
});
it('returns false when the row does not exist (no row returned)', async () => {
state.updateReturnsRow = false;
expect(await enqueueDeliveryReplay('d-1')).toBe(false);
});
});
describe('deliveryBelongsToEndpoint', () => {
beforeEach(resetState);
it('returns true when the row exists for the (delivery, endpoint) pair', async () => {
state.selectRow = { id: 'd-1' };
expect(await deliveryBelongsToEndpoint('d-1', 'e-1')).toBe(true);
});
it('returns false when no row matches', async () => {
state.selectRow = null;
expect(await deliveryBelongsToEndpoint('d-1', 'e-1')).toBe(false);
});
it('returns false on query error (fail-closed)', async () => {
state.selectError = { message: 'boom' };
expect(await deliveryBelongsToEndpoint('d-1', 'e-1')).toBe(false);
});
});
describe('pruneDeliveredWebhookDeliveries', () => {
beforeEach(resetState);
it('returns the deleted count on success', async () => {
state.deleteCount = 7;
expect(await pruneDeliveredWebhookDeliveries()).toBe(7);
});
it('returns 0 on DB error (cron task contract: log + swallow)', async () => {
state.deleteError = { message: 'boom' };
expect(await pruneDeliveredWebhookDeliveries()).toBe(0);
});
it('returns 0 when the count is null', async () => {
state.deleteCount = null;
expect(await pruneDeliveredWebhookDeliveries()).toBe(0);
});
});