#!/usr/bin/env node
/**
 * PR-D Smoke Test
 * Quick sanity check: does the toolchain work?
 * 
 * Run: node scripts/smoke-test.mjs
 * 
 * Checks (7 items):
 *   1. openclaw CLI executable
 *   2. Baseline test files exist
 *   3. Guard scripts exist
 *   4. Deprecation check script exists
 *   5. package.json scripts available
 *   6. Minimal CLI command runs
 *   7. Version or help info outputs
 */

import { existsSync, readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { execSync } from "node:child_process";
import { fileURLToPath } from "node:url";

const __dirname = join(fileURLToPath(import.meta.url), "..");
const ROOT = join(__dirname, "..");

let PASS = 0;
let FAIL = 0;

function check(label, ok, detail = "") {
  const mark = ok ? "✓" : "✗";
  if (ok) PASS++; else FAIL++;
  console.log(`  [${mark}] ${label}${detail ? ` — ${detail}` : ""}`);
  return ok;
}

console.log("");
console.log("──────────────────────────────────────────");
console.log("  PR-D: Smoke Test");
console.log("──────────────────────────────────────────");
console.log("");

// ─── 1. openclaw CLI executable ──────────────────
let ocVersion = "";
let ocOk = false;
try {
  ocVersion = execSync("openclaw --version 2>&1", {
    encoding: "utf8",
    timeout: 10000,
  }).trim();
  ocOk = ocVersion.length > 0;
} catch {
  ocOk = false;
}
check("openclaw CLI executable", ocOk, ocVersion || "not found");

// ─── 2. Baseline test files exist ──────────────────
const baselineDir = join(ROOT, "test", "baseline");
let baselineCount = 0;
let baselineOk = false;
try {
  if (existsSync(baselineDir)) {
    const files = readdirSync(baselineDir).filter(f => f.startsWith("test-") && f.endsWith(".test.mjs"));
    baselineCount = files.length;
    baselineOk = baselineCount > 0;
  }
} catch {}
check("Baseline test files exist", baselineOk, `${baselineCount} files`);

// ─── 3. Guard scripts exist ────────────────────────
const guardScript = join(ROOT, "scripts", "guard-all.sh");
let guardOk = existsSync(guardScript);
check("Guard scripts exist", guardOk, guardScript);

// ─── 4. Deprecation check script exists ─────────────
const deprecationScript = join(ROOT, "scripts", "check-deprecations.mjs");
let depOk = existsSync(deprecationScript);
check("Deprecation check script exists", depOk, deprecationScript);

// ─── 5. package.json scripts available ─────────────
let pkgOk = false;
let pkgDetail = "";
try {
  const pkg = JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8"));
  const scripts = Object.keys(pkg.scripts || {});
  const required = ["production-check", "smoke", "guard", "check:deprecations", "test:baseline"];
  const missing = required.filter(s => !scripts.includes(s));

  if (missing.length === 0) {
    pkgOk = true;
    pkgDetail = `${scripts.length} scripts, all required present`;
  } else {
    pkgDetail = `missing: ${missing.join(", ")}`;
  }
} catch (e) {
  pkgDetail = `error: ${e.message}`;
}
check("package.json scripts available", pkgOk, pkgDetail);

// ─── 6. Minimal CLI command runs ───────────────────
let cliRunOk = false;
let cliDetail = "";
try {
  const out = execSync("openclaw --help 2>&1", {
    encoding: "utf8",
    timeout: 15000,
  });
  cliRunOk = out.length > 0;
  cliDetail = `${out.length} bytes output`;
} catch (e) {
  cliDetail = `error: ${e.message}`;
}
check("Minimal CLI command runs", cliRunOk, cliDetail);

// ─── 7. Version or help info outputs ────────────────
let versionOk = false;
let versionDetail = "";
try {
  const v = execSync("openclaw --version 2>&1", {
    encoding: "utf8",
    timeout: 10000,
  }).trim();
  versionOk = v.length > 0;
  versionDetail = v;
} catch (e) {
  versionDetail = `error: ${e.message}`;
}
check("Version/help info outputs", versionOk, versionDetail);

// ─── Summary ────────────────────────────────────────
console.log("");
console.log("──────────────────────────────────────────");
const total = PASS + FAIL;
console.log(`  Total: ${total} | Pass: ${PASS} | Fail: ${FAIL}`);
if (FAIL === 0) {
  console.log("  ✓ Smoke test PASSED");
  console.log("──────────────────────────────────────────");
  console.log("");
  process.exit(0);
} else {
  console.log("  ✗ Smoke test FAILED");
  console.log("──────────────────────────────────────────");
  console.log("");
  process.exit(1);
}
