/**
 * perf-db-pool smoke — asserts the explicit Prisma connection-pool config
 * stays wired so a deploy can't silently regress to the cpu-derived default.
 *
 * Why (2026-05-31): Prisma's MySQL pool defaults to `num_physical_cpus*2+1`
 * (5–9 on a small VPS), which the web process can starve under concurrent
 * analytics aggregation + outbox drain + SSE fan-out. The pool size is set via
 * the `connection_limit` URL param. We document it in .env.example and apply a
 * conservative default in src/lib/prisma.ts WITHOUT overriding an operator's
 * explicit value. This smoke guards that wiring statically (offline-safe).
 */
import './_lib';
import { ok, fail } from './_lib';
import { readFileSync } from 'fs';
import path from 'path';
import { resolveDatabaseUrl } from '../../src/lib/database-url';

const ROOT = path.resolve(__dirname, '../..');
const read = (rel: string): string => readFileSync(path.join(ROOT, rel), 'utf8');

function assert(label: string, cond: boolean, detail: string): void {
    if (!cond) fail(label, detail);
    ok(label);
}

function main(): void {
    // 1. .env.example documents connection_limit + the web/worker split so an
    //    operator knows the knob exists and how to size it.
    const envExample = read('.env.example');
    assert('.env.example documents connection_limit', /connection_limit/.test(envExample),
        '.env.example no longer mentions connection_limit — operators lose the pool-sizing guidance.');
    assert('.env.example explains the web vs worker pool split',
        /WEB vs WORKER|web .*connection_limit|worker .*connection_limit/i.test(envExample),
        '.env.example no longer explains the web-vs-worker pool split.');

    // 2. prisma.ts applies the default via resolveDatabaseUrl + datasources
    //    override (not a hardcoded literal that ignores the operator's URL).
    const prismaClient = read('src/lib/prisma.ts');
    assert('prisma.ts imports resolveDatabaseUrl', /resolveDatabaseUrl/.test(prismaClient),
        'src/lib/prisma.ts no longer routes DATABASE_URL through resolveDatabaseUrl.');
    assert('prisma.ts overrides the datasource url', /datasources\s*:\s*\{\s*db\s*:\s*\{\s*url/.test(prismaClient),
        'src/lib/prisma.ts no longer feeds the pool-sized URL into the PrismaClient datasource.');

    // 3. Behaviour contract: default applied when absent, operator value never
    //    overridden. (Mirrors the unit test; cheap to also assert here so the
    //    smoke gate catches a regression even if the unit test is skipped.)
    assert('default applied when connection_limit absent',
        resolveDatabaseUrl('mysql://u:p@h:3306/db', 10) === 'mysql://u:p@h:3306/db?connection_limit=10',
        'resolveDatabaseUrl no longer appends a default connection_limit.');
    assert('operator connection_limit never overridden',
        resolveDatabaseUrl('mysql://u:p@h:3306/db?connection_limit=42', 10) === 'mysql://u:p@h:3306/db?connection_limit=42',
        'resolveDatabaseUrl now overrides an explicit operator connection_limit — it must not.');

    ok('perf-db-pool', 'explicit connection_limit documented + applied defensively without overriding the operator');
}

main();
