import { resolve } from "node:path";
import { spawnSync } from "node:child_process";
import test from "node:test";
import assert from "node:assert/strict";

const cliPath = resolve(process.cwd(), "bin/antigravity-superpowers.js");

function runCli(args) {
  return spawnSync(process.execPath, [cliPath, ...args], {
    encoding: "utf8",
  });
}

test("--help shows usage information", () => {
  const result = runCli(["--help"]);
  assert.equal(result.status, 0);
  assert.match(result.stdout, /Usage:/);
  assert.match(result.stdout, /Commands:/);
  assert.match(result.stdout, /init/);
  assert.match(result.stdout, /validate/);
  assert.match(result.stdout, /list/);
});

test("-h shows usage information", () => {
  const result = runCli(["-h"]);
  assert.equal(result.status, 0);
  assert.match(result.stdout, /Usage:/);
});

test("no arguments shows help", () => {
  const result = runCli([]);
  assert.equal(result.status, 0);
  assert.match(result.stdout, /Usage:/);
});

test("--version shows version number", () => {
  const result = runCli(["--version"]);
  assert.equal(result.status, 0);
  assert.match(result.stdout.trim(), /^\d+\.\d+\.\d+$/);
});

test("-v shows version number", () => {
  const result = runCli(["-v"]);
  assert.equal(result.status, 0);
  assert.match(result.stdout.trim(), /^\d+\.\d+\.\d+$/);
});

test("unknown command returns exit code 1", () => {
  const result = runCli(["foobar"]);
  assert.equal(result.status, 1);
  assert.match(result.stderr, /Unknown command: foobar/);
});

test("list shows available skills", () => {
  const result = runCli(["list"]);
  assert.equal(result.status, 0);
  assert.match(result.stdout, /13 skills available/);
  assert.match(result.stdout, /brainstorming/);
  assert.match(result.stdout, /test-driven-development/);
  assert.match(result.stdout, /single-flow-task-execution/);
});
