import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
  inspectWorktreeHygiene,
  parseArgs,
  parsePorcelain,
} from "./worktree-hygiene.mjs";

function createGitStub(statusOutput) {
  return (args) => {
    if (args[0] === "rev-parse" && args[1] === "--show-toplevel") {
      return "/tmp/capakraken\n";
    }
    if (args[0] === "rev-parse" && args[1] === "--abbrev-ref") {
      return "main\n";
    }
    if (args[0] === "status" && args[1] === "--short") {
      return statusOutput;
    }

    throw new Error(`Unexpected git args: ${args.join(" ")}`);
  };
}

describe("worktree hygiene", () => {
  it("fails when a scope value is missing", () => {
    assert.throws(() => parseArgs(["--scope"]), /--scope requires a path value\./);
  });

  it("fails on unknown arguments", () => {
    assert.throws(() => parseArgs(["--wat"]), /Unknown argument: --wat/);
  });

  it("normalizes rename targets in porcelain output", () => {
    assert.deepEqual(parsePorcelain("R  docs/old.md -> docs/new.md\n"), [
      {
        xy: "R ",
        path: "docs/new.md",
      },
    ]);
  });

  it("returns exit code 1 when fail-on-dirty is enabled", () => {
    const result = inspectWorktreeHygiene(
      ["--fail-on-dirty"],
      createGitStub(" M scripts/worktree-hygiene.mjs\n?? scripts/worktree-hygiene.test.mjs\n"),
    );

    assert.equal(result.exitCode, 1);
    assert.match(result.output, /Dirty entries: 2/);
    assert.equal(result.status?.totals.untracked, 1);
  });

  it("returns exit code 2 when dirty files escape the declared scope", () => {
    const result = inspectWorktreeHygiene(
      ["--scope", "docs/", "--fail-outside-scope"],
      createGitStub(" M docs/guide.md\n M scripts/worktree-hygiene.mjs\n"),
    );

    assert.equal(result.exitCode, 2);
    assert.equal(result.entries.outOfScope.length, 1);
    assert.match(result.output, /outside-scope changes detected\./);
  });

  it("keeps fail-outside-scope authoritative even when allow-outside-scope is set", () => {
    const result = inspectWorktreeHygiene(
      ["--scope", ".\\docs", "--allow-outside-scope", "--fail-outside-scope", "--json"],
      createGitStub(" M docs/guide.md\n M scripts/worktree-hygiene.mjs\n"),
    );

    assert.equal(result.exitCode, 2);

    const parsed = JSON.parse(result.output);
    assert.equal(parsed.scopes[0], "docs");
    assert.equal(parsed.inScope, 1);
    assert.equal(parsed.outOfScope, 1);
  });

  it("tolerates outside-scope files when explicitly allowed and emits json", () => {
    const result = inspectWorktreeHygiene(
      ["--scope", "docs/", "--allow-outside-scope", "--json"],
      createGitStub("R  docs/old.md -> docs/new.md\n?? scripts/worktree-hygiene.test.mjs\n"),
    );

    assert.equal(result.exitCode, 0);

    const parsed = JSON.parse(result.output);
    assert.equal(parsed.inScope, 1);
    assert.equal(parsed.outOfScope, 1);
    assert.deepEqual(parsed.entries.inScope, [{ xy: "R ", path: "docs/new.md" }]);
    assert.deepEqual(parsed.entries.outOfScope, [{ xy: "??", path: "scripts/worktree-hygiene.test.mjs" }]);
  });
});
