import assert from "node:assert/strict";
import { existsSync, readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const root = resolve(__dirname, "../../..");

function literalRegExp(text) {
  return new RegExp(text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
}

function parseTsv(text) {
  const [headerLine, ...lines] = text.trim().split(/\r?\n/);
  const headers = headerLine.split("\t");
  return lines.map((line) => {
    const cells = line.split("\t");
    return Object.fromEntries(headers.map((header, index) => [header, cells[index] ?? ""]));
  });
}

const docText = readFileSync(resolve(root, "docs/host-runtime-boundary-handoff.md"), "utf8");
const fullProcessText = readFileSync(resolve(root, "docs/full-process-boundary-design.md"), "utf8");
const simHandoffText = readFileSync(resolve(root, "docs/sim-configs-coverage-handoff.md"), "utf8");
const inventoryTestText = readFileSync(
  resolve(root, "tests/wasm/node/verify_sim_configs_inventory_wasm.mjs"),
  "utf8",
);
const hostSmokeText = readFileSync(resolve(root, "tests/host/verify_host_smokes.sh"), "utf8");
const readmeText = readFileSync(resolve(root, "README.md"), "utf8");
const trackerText = readFileSync(resolve(root, "../PROJECT_COMPLETION_TRACKER.md"), "utf8");

for (const phrase of [
  "Host/runtime boundary release handoff",
  "runtime-boundary-host-readiness-rollup.tsv",
  "runtime-boundary-promotion-blockers.tsv",
  "runtime-boundary-opt-in-probe-dispatch-rollup.tsv",
  "runtime-boundary-native-evidence-acceptance-gate.tsv",
  "blocked-runtime-promotion-lock.tsv",
  "host_readiness_status=host_blocked_for_all_opt_in_native_probes",
  "missing_host_requirements=halcmd,halrun,linuxcnc,milltask",
  "dispatch_status=dispatch_blocked_for_all_opt_in_native_probes",
  "current_probe_status=skipped_missing_host_runtime",
  "native_evidence_status=pending_until_native_pass",
  "evidence_acceptance_allowed=0",
  "ENABLE_MILLTURN_USER_M_RUNTIME_PROBE=1",
  "ENABLE_TOOL_DB_RUNTIME_PROBE=1",
  "ENABLE_PYTHON_REMAP_RUNTIME_PROBE=1",
  "L4-USER-M-PROCESS",
  "L4-TOOL-DB",
  "L4-PYTHON-REMAP",
  "host_runtime_boundary_docs_node_smoke=ok",
]) {
  assert.match(docText, literalRegExp(phrase));
}

for (const phrase of [
  "runtime-boundary-host-readiness-rollup.tsv",
  "runtime-boundary-opt-in-probe-dispatch-rollup.tsv",
  "runtime-boundary-native-evidence-acceptance-gate.tsv",
]) {
  assert.match(fullProcessText, literalRegExp(phrase));
  assert.match(inventoryTestText, literalRegExp(phrase));
  assert.match(docText, literalRegExp(phrase));
}

assert.match(simHandoffText, /L4-PYTHON-REMAP/);
assert.match(hostSmokeText, /verify_host_runtime_boundary_docs\.sh/);
assert.match(readmeText, /docs\/host-runtime-boundary-handoff\.md/);
assert.match(trackerText, /Host\/runtime boundary/);

const hostRollupPath = resolve(
  root,
  "build/wasm/sim-configs-inventory/runtime-boundary-host-readiness-rollup.tsv",
);
if (existsSync(hostRollupPath)) {
  const [row] = parseTsv(readFileSync(hostRollupPath, "utf8"));
  assert.equal(row.host_readiness_status, "host_blocked_for_all_opt_in_native_probes");
  assert.equal(row.family_count, "3");
  assert.equal(row.host_ready_family_count, "0");
  assert.equal(row.host_blocked_family_count, "3");
  assert.equal(row.missing_host_requirements, "halcmd,halrun,linuxcnc,milltask");
  assert.equal(row.execution_enabled, "0");
  assert.equal(row.promotion_allowed, "0");
}

const dispatchRollupPath = resolve(
  root,
  "build/wasm/sim-configs-inventory/runtime-boundary-opt-in-probe-dispatch-rollup.tsv",
);
if (existsSync(dispatchRollupPath)) {
  const [row] = parseTsv(readFileSync(dispatchRollupPath, "utf8"));
  assert.equal(row.dispatch_status, "dispatch_blocked_for_all_opt_in_native_probes");
  assert.equal(row.dispatch_allowed_count, "0");
  assert.equal(row.dispatch_blocked_count, "3");
  assert.equal(row.execution_enabled, "0");
  assert.equal(row.promotion_allowed, "0");
}

console.log("host_runtime_boundary_docs_node_smoke=ok");
