/**
 * PR-A Baseline Smoke Test
 * Quick end-to-end sanity check before running full test suite.
 * Verifies that essential tools are available and the system is in a testable state.
 */

import { describe, it } from "node:test";
import assert from "node:assert";
import { execSync } from "node:child_process";
import { existsSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";

describe("PR-A Smoke Test", () => {
  it("smoke: node version >= 22", () => {
    const v = process.version;
    const major = parseInt(v.split(".")[0].replace("v", ""));
    console.log("[SMOKE] Node.js version:", v);
    assert.ok(major >= 22, `node >= 22 required, got ${v}`);
  });

  it("smoke: openclaw CLI available", () => {
    const out = execSync("openclaw --version 2>&1", {
      timeout: 10000,
      encoding: "utf8",
    });
    console.log("[SMOKE] openclaw:", out.trim().split("\n")[0]);
    assert.ok(out.includes("OpenClaw"), "openclaw CLI must be available");
  });

  it("smoke: gateway is reachable", () => {
    const out = execSync("openclaw status 2>&1", {
      timeout: 15000,
      encoding: "utf8",
    });
    const isRunning = out.includes("running") || out.includes("active") || out.includes("reachable");
    console.log("[SMOKE] gateway running:", isRunning);
    assert.ok(isRunning, "gateway must be running");
  });

  it("smoke: workspace exists", () => {
    const ws = join(homedir(), ".openclaw", "workspace");
    assert.ok(existsSync(ws), "workspace must exist");
    console.log("[SMOKE] workspace:", ws);
  });

  it("smoke: memory system functional", () => {
    const out = execSync("openclaw memory status --json 2>/dev/null", {
      timeout: 15000,
      encoding: "utf8",
    });
    const status = JSON.parse(out);
    console.log("[SMOKE] memory status: OK");
    assert.ok(Array.isArray(status), "memory status must be an array");
  });

  it("smoke: config loads without error", () => {
    const out = execSync("openclaw config get gateway.port 2>&1", {
      timeout: 10000,
      encoding: "utf8",
    });
    console.log("[SMOKE] gateway port:", out.trim());
    assert.ok(out.length > 0, "config must return value");
  });
});
