/**
 * P2-8 smoke test — scripts/worker.ts registers unhandledRejection /
 * uncaughtException handlers.
 *
 * Run: npx tsx scripts/smoke/p2-8-worker-resilience.ts
 */
import './_lib';
import { ok, fail } from './_lib';
import { readFileSync } from 'fs';
import path from 'path';

function main(): void {
    const file = path.resolve(__dirname, '../../scripts/worker.ts');
    const src = readFileSync(file, 'utf8');
    if (!/process\.on\(['"]unhandledRejection['"]/.test(src)) {
        fail('P2-8 unhandledRejection', 'no handler registered in scripts/worker.ts');
    }
    ok('worker.ts registers unhandledRejection handler');
    if (!/process\.on\(['"]uncaughtException['"]/.test(src)) {
        fail('P2-8 uncaughtException', 'no handler registered in scripts/worker.ts');
    }
    ok('worker.ts registers uncaughtException handler');
    if (!/continuing/i.test(src)) {
        fail('P2-8 semantics', 'handlers should log and continue, not crash');
    }
    ok('worker handlers log-and-continue (no process.exit)');
    ok('P2-8 worker-resilience', 'worker survives transient promise rejections');
}

main();
