/**
 * auditor-monitor-engine-roundtrips smoke — guards the 2026-06-01 T2G
 * fix that collapses the per-probe heartbeat queries from 2 to 1.
 *
 * Before T2G:
 *   - One findMany for the retry-interval count (take: retries + 1,
 *     only when status=='down' && retryInterval > 0).
 *   - One findMany for state-eval historyBefore (take: lookback, with
 *     `id: { not: heartbeat.id }` filter).
 *
 * After T2G:
 *   - A single findMany with `take: Math.max(retries+1, lookback+1)`.
 *   - Retry-interval branch slices `[0, retries+1)` of the result.
 *   - State-eval filters out the just-saved row + slices to `lookback`.
 */
import './_lib';
import { ok, fail } from './_lib';
import { readFileSync } from 'fs';
import path from 'path';

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 {
    const engine = read('src/lib/monitor-engine.ts');

    // The merged buffer variable is named recentAll and is fetched ONCE.
    assert('monitor-engine: single recentAll findMany declaration',
        /const recentAll = await prisma\.heartbeat\.findMany\(/.test(engine),
        'monitor-engine no longer has the merged recentAll findMany — roundtrips may have regressed.');

    // Fetch size is bounded by Math.max of the two windows.
    assert('monitor-engine: fetchSize uses Math.max of both windows',
        /Math\.max\(monitor\.retries \+ 1,\s*lookbackForFetch \+ 1\)/.test(engine),
        'monitor-engine fetchSize no longer combines the retry + state-eval windows.');

    // The retry-interval branch slices the first `retries + 1` rows
    // instead of issuing its own findMany.
    assert('monitor-engine: retry-interval branch slices recentAll',
        /recentAll[\s\S]*?\.slice\(0,\s*monitor\.retries \+ 1\)/.test(engine),
        'retry-interval branch no longer slices recentAll — it may have regressed to a separate findMany.');

    // The state-eval branch filters out the just-saved heartbeat and
    // slices to `lookback` — no separate findMany.
    assert('monitor-engine: historyBefore is derived from recentAll',
        /const historyBefore = recentAll[\s\S]*?\.filter[\s\S]*?\.slice\(0,\s*lookback\)/.test(engine),
        'historyBefore no longer comes from the merged recentAll — a second findMany may have crept back in.');

    // Belt + suspenders: there must be NO separate `prisma.heartbeat.findMany`
    // call that filters on `id: { not:`. That was the old historyBefore
    // shape; it should be gone post-T2G.
    assert('monitor-engine: no legacy id-not findMany for historyBefore',
        !/prisma\.heartbeat\.findMany\([\s\S]{0,200}id:\s*\{\s*not:/.test(engine),
        'monitor-engine still has the legacy historyBefore findMany — T2G partially regressed.');

    ok('auditor-monitor-engine-roundtrips', 'per-probe heartbeat queries merged from 2 to 1');
}

main();
