/**
 * auto-prompts-complete-slice.test.mjs - complete-slice prompt contracts.
 *
 * Purpose: prove slice closeout receives structured task state so autonomous
 * agents verify and close completed slices instead of rediscovering task status.
 */
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, test } from "vitest";
import { buildCompleteSlicePrompt } from "../auto-prompts.js";
import {
	closeDatabase,
	insertMilestone,
	insertSlice,
	insertTask,
	openDatabase,
} from "../sf-db.js";

let tempDirs = [];

function makeProject() {
	const dir = mkdtempSync(join(tmpdir(), "sf-complete-slice-prompt-"));
	tempDirs.push(dir);
	mkdirSync(join(dir, ".sf", "milestones", "M900", "slices", "S01", "tasks"), {
		recursive: true,
	});
	return dir;
}

afterEach(() => {
	closeDatabase();
	for (const dir of tempDirs) {
		rmSync(dir, { recursive: true, force: true });
	}
	tempDirs = [];
});

describe("complete-slice prompt", () => {
	test("complete_slice_prompt_includes_db_task_ledger_for_closeout", async () => {
		const base = makeProject();
		openDatabase(join(base, ".sf", "sf.db"));
		insertMilestone({
			id: "M900",
			title: "Closeout prompt",
			status: "active",
			planning: {
				vision: "Close completed slices without state rediscovery.",
				successCriteria: ["Completed task rows drive slice closeout."],
			},
		});
		insertSlice({
			milestoneId: "M900",
			id: "S01",
			title: "Already built docs",
			status: "active",
			risk: "low",
			depends: [],
			demo: "Docs are present and verified.",
			sequence: 1,
		});
		insertTask({
			milestoneId: "M900",
			sliceId: "S01",
			id: "T01",
			title: "Write docs",
			status: "done",
			oneLiner: "Schedule docs exist.",
			verificationResult: "node dist/loader.js schedule --help",
			verificationStatus: "passed",
			keyFiles: ["docs/specs/sf-schedule.md"],
			fullSummaryMd: "Docs written and help verified.",
			sequence: 1,
		});
		writeFileSync(
			join(base, ".sf", "milestones", "M900", "M900-ROADMAP.md"),
			"# M900: Closeout prompt\n\n## S01: Already built docs\n",
		);
		writeFileSync(
			join(base, ".sf", "milestones", "M900", "slices", "S01", "S01-PLAN.md"),
			"# S01: Already built docs\n\n## Verification\n\n- schedule help passes\n",
		);
		writeFileSync(
			join(
				base,
				".sf",
				"milestones",
				"M900",
				"slices",
				"S01",
				"tasks",
				"T01-SUMMARY.md",
			),
			"# T01 Summary\n\nSchedule docs exist.\n",
		);

		const prompt = await buildCompleteSlicePrompt(
			"M900",
			"Closeout prompt",
			"S01",
			"Already built docs",
			base,
			"minimal",
		);

		expect(prompt).toContain("## Slice Closeout Control");
		expect(prompt).toContain(
			"| T01 | done | .sf/milestones/M900/slices/S01/tasks/T01-SUMMARY.md | Schedule docs exist. | passed | docs/specs/sf-schedule.md |",
		);
		expect(prompt).toContain(
			"verify the slice-level contract once and call `complete_slice`",
		);
		expect(prompt).toContain("Do not reopen planning");
	});
});
