/**
 * Smoke test for the headless complete-slice / skip-slice /
 * complete-milestone commands (#mark-state).
 *
 * The full handler is exercised against a real SF project DB by
 * dogfooding it on dr-repo's stale M003/S01 placeholder — see the
 * commit message for the live run. Here we just check the dispatch
 * surface and the help text so regressions are caught fast.
 */

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { test } from "vitest";

const __dirname = dirname(fileURLToPath(import.meta.url));
const headlessSrc = readFileSync(join(__dirname, "..", "headless.ts"), "utf-8");
const helpSrc = readFileSync(join(__dirname, "..", "help-text.ts"), "utf-8");
const handlerSrc = readFileSync(
	join(__dirname, "..", "headless-mark-state.ts"),
	"utf-8",
);

test("headless.ts dispatches the three new commands to handleMarkState", () => {
	assert.match(
		headlessSrc,
		/options\.command === "complete-slice"/,
		"complete-slice must be dispatched",
	);
	assert.match(
		headlessSrc,
		/options\.command === "skip-slice"/,
		"skip-slice must be dispatched",
	);
	assert.match(
		headlessSrc,
		/options\.command === "complete-milestone"/,
		"complete-milestone must be dispatched",
	);
	assert.match(
		headlessSrc,
		/import\("\.\/headless-mark-state\.js"\)/,
		"headless.ts must import the new handler",
	);
});

test("help text lists the three new commands", () => {
	assert.match(helpSrc, /complete-slice\s+Mark a slice complete/);
	assert.match(helpSrc, /skip-slice\s+Mark a slice skipped/);
	assert.match(helpSrc, /complete-milestone\s+Mark a milestone complete/);
});

test("handler refuses missing positional and bad ref shapes", () => {
	// Spot-checks the documented error paths so a refactor doesn't drop them.
	assert.match(
		handlerSrc,
		/requires a reference \(e\.g\. M010\/S03\)/,
		"missing-ref error must remain user-readable",
	);
	assert.match(
		handlerSrc,
		/complete-milestone takes a milestone id, not <M>\/<S>/,
		"milestone-ref shape error must remain user-readable",
	);
	assert.match(
		handlerSrc,
		/requires <milestone>\/<slice> \(e\.g\. M010\/S03\)/,
		"slice-ref shape error must remain user-readable",
	);
});

test("handler is idempotent on already-closed targets", () => {
	// The handler short-circuits with {ok: true, idempotent: true}
	// rather than failing — important for scripting.
	assert.match(handlerSrc, /idempotent: true/);
	assert.match(handlerSrc, /already \$\{targetStatus\}/);
	assert.match(handlerSrc, /already complete/);
});
