// structural gate against the recurrence-class
// silent-fallback-masks-root-cause violation. The four runtime sites that
// previously substituted `9222 + offset` for a missing brand.json.cdpPort
// (paths.ts, admin/mcp/index.ts, vnc.sh, test-laptop-vnc-boot.sh) plus the
// installer-side brand stamp at packages/create-maxy-code/src/index.ts have all
// been swept to loud-fail. This test asserts the three greps from criterion
// 2 of the task brief return zero matches; reintroducing any silent fallback
// fails CI immediately.
//
// Compiles to dist/__tests__/cdp-port-no-silent-fallback.test.js and runs
// via `node --test 'dist/__tests__/*.test.js'` per package.json's test
// script. Greps run via `git grep` rooted at the repo top-level (resolved
// relative to dist/__tests__/'s known offset) so the test is invariant to
// the caller's cwd.
import test from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
const SELF_DIR = fileURLToPath(new URL(".", import.meta.url));
// dist/__tests__/ → repo root is four levels up
// (packages/create-maxy-code/dist/__tests__ → packages/create-maxy-code → packages → repo).
const REPO_ROOT = resolve(SELF_DIR, "..", "..", "..", "..");
function grepReturns(pattern, includes) {
    const args = ["-RnE", pattern];
    for (const inc of includes)
        args.push(`--include=${inc}`);
    args.push("platform/", "packages/");
    // Exclude this test file (it embeds the patterns as string literals,
    // which would otherwise self-match) and exclude the gitignored payload
    // mirror generated by `npm run bundle`.
    args.push("--exclude-dir=payload");
    args.push("--exclude=cdp-port-no-silent-fallback.test.*");
    const out = spawnSync("grep", args, { cwd: REPO_ROOT, encoding: "utf8" });
    // grep exit 1 = no matches (success for this test); exit 0 = matches found
    // (failure); exit 2 = grep error (test infrastructure fault).
    if (out.status !== 0 && out.status !== 1) {
        throw new Error(`grep error (status=${out.status}): ${out.stderr}`);
    }
    return (out.stdout ?? "").trim();
}
test("grep 1: no `9222 + offset` arithmetic in source files", () => {
    const matches = grepReturns("9222\\s*\\+\\s*\\w*offset", ["*.ts", "*.tsx", "*.sh"]);
    assert.equal(matches, "", `silent-fallback regression — '9222 + offset' arithmetic reintroduced:\n${matches}`);
});
test("grep 2: no jq `cdpPort // 9222` fallback in shell or TS", () => {
    const matches = grepReturns("cdpPort\\s*//\\s*9222", ["*.sh", "*.ts"]);
    assert.equal(matches, "", `silent-fallback regression — jq cdpPort // 9222 fallback reintroduced:\n${matches}`);
});
test("grep 3: no `?? 9222` or `|| 9222` nullish-coalesce in TS", () => {
    const matches = grepReturns("\\?\\?\\s*9222|\\|\\|\\s*9222", ["*.ts"]);
    assert.equal(matches, "", `silent-fallback regression — '?? 9222' or '|| 9222' coalesce reintroduced:\n${matches}`);
});
