/**
 * P1-11 smoke test — outbox retry schedule is the extended [5s, 30s, 5m,
 * 30m, 2h] sequence with maxAttempts=5.
 *
 * Run: npx tsx scripts/smoke/p1-11-outbox-backoff.ts
 */
import './_lib';
import { ok, fail } from './_lib';
import {
    RETRY_DELAYS_SECONDS,
    DEFAULT_MAX_ATTEMPTS,
    nextDelaySec,
} from '../../src/lib/notification-system/outbox/retry-policy';

function main(): void {
    if (DEFAULT_MAX_ATTEMPTS !== 5) {
        fail('P1-11 max attempts', `expected 5, got ${DEFAULT_MAX_ATTEMPTS}`);
    }
    ok('DEFAULT_MAX_ATTEMPTS = 5');

    const expected = [5, 30, 5 * 60, 30 * 60, 2 * 60 * 60];
    expected.forEach((d, i) => {
        if (RETRY_DELAYS_SECONDS[i] !== d) {
            fail(`P1-11 delay[${i}]`, `expected ${d}s, got ${RETRY_DELAYS_SECONDS[i]}s`);
        }
    });
    ok('Retry schedule matches [5s, 30s, 5m, 30m, 2h]');

    if (nextDelaySec(1) !== 5) fail('nextDelaySec(1)', `expected 5, got ${nextDelaySec(1)}`);
    if (nextDelaySec(5) !== 2 * 60 * 60) fail('nextDelaySec(5)', `expected 7200, got ${nextDelaySec(5)}`);
    if (nextDelaySec(100) !== 2 * 60 * 60) fail('nextDelaySec(clamp)', `expected 7200, got ${nextDelaySec(100)}`);
    ok('nextDelaySec returns clamped 2h for over-attempts');

    // Total budget across 4 retries (attempts 1..4 produce 4 delays before
    // attempt 5 fires and gives up): 5 + 30 + 300 + 1800 = 2135s ≈ 35 min
    // before the 5th attempt; full window covers ~2h+ if the last attempt
    // hits the longest delay.
    const totalBeforeGiveUp = expected.slice(0, DEFAULT_MAX_ATTEMPTS - 1).reduce((a, b) => a + b, 0);
    if (totalBeforeGiveUp < 30 * 60) {
        fail('P1-11 budget', `total retry window too short: ${totalBeforeGiveUp}s`);
    }
    ok('Retry budget covers >= 30 min before permanent failure');

    ok('P1-11 outbox-backoff', 'extended retry schedule active');
}

main();
