#!/usr/bin/env node
import { spawnSync } from "child_process";
import path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(__dirname, "..");

const steps = [
  ["validate-matrix", ["node", "scripts/validate-matrix.mjs"]],
  ["validate-registry", ["node", "scripts/validate-registry.mjs"]],
  ["manifest-schema", ["node", "tests/manifest-schema.test.mjs"]],
  ["contract-snapshot", ["node", "tests/contract-snapshot.test.mjs"]],
  ["scaffold-policy", ["node", "tests/scaffold-policy.test.mjs"]],
  ["event-log", ["node", "tests/event-log.test.mjs"]],
  ["faz6", ["node", "tests/faz6.test.mjs"]],
  ["not-installed", ["node", "tests/not-installed.test.mjs"]],
  ["init-integration", ["node", "tests/init-integration.test.mjs"]],
  ["ide-sync", ["node", "tests/ide-sync.test.mjs"]],
  ["uninstall", ["node", "tests/uninstall.test.mjs"]],
  ["upgrade", ["node", "tests/upgrade.test.mjs"]],
  ["local-adapters", ["node", "tests/local-adapters.test.mjs"]],
  ["explanation", ["node", "tests/explanation.test.mjs"]],
  ["workspace-boundary", ["node", "tests/workspace-boundary.test.mjs"]],
  ["skill-disable", ["node", "tests/skill-disable.test.mjs"]],
  ["recover", ["node", "tests/recover.test.mjs"]],
  ["consult-status", ["node", "tests/consult-status.test.mjs"]],
  ["ui-server", ["node", "tests/ui-server.test.mjs"]],
  ["ui-wizard-e2e", ["node", "tests/ui-wizard.e2e.mjs"]],
  ["pipeline", ["node", "tests/pipeline.test.mjs"]],
  ["bundle-minimal", ["node", "tests/bundle-minimal.test.mjs"]],
  ["route-smoke", ["node", "bin/capint.js", "route", "i18n çeviri"]],
  ["pipeline-smoke", ["node", "bin/capint.js", "pipeline", "i18n çeviri", "--force"]],
  ["doctor-smoke", ["node", "bin/capint.js", "doctor"]],
  ["audit-smoke", ["node", "bin/capint.js", "audit", "--json"]],
  ["consult-smoke", ["node", "bin/capint.js", "consult", "debug login", "--json"]],
  ["status-smoke", ["node", "bin/capint.js", "status", "--json"]]
];

let failed = 0;
for (const [name, cmd] of steps) {
  const r = spawnSync(cmd[0], cmd.slice(1), { cwd: root, encoding: "utf-8" });
  if (r.status === 0) {
    console.log(`ok ${name}`);
  } else {
    console.error(`fail ${name}`);
    if (r.stdout) process.stdout.write(r.stdout);
    if (r.stderr) process.stderr.write(r.stderr);
    failed++;
  }
}

process.exit(failed > 0 ? 1 : 0);
