/**
 * complete-slice-evidence.test.mjs — slice completion evidence.
 *
 * Purpose: prove slice completion records structured DB evidence in addition
 * to generated SUMMARY/UAT markdown projections.
 */
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, test } from "vitest";
import {
	closeDatabase,
	getSlice,
	getSliceAuditTrail,
	insertMilestone,
	insertSlice,
	insertTask,
	openDatabase,
} from "../sf-db.js";
import { handleCompleteSlice } from "../tools/complete-slice.js";

const tmpDirs = [];

afterEach(() => {
	closeDatabase();
	while (tmpDirs.length > 0) {
		const dir = tmpDirs.pop();
		if (dir) rmSync(dir, { recursive: true, force: true });
	}
});

function makeProject() {
	const dir = mkdtempSync(join(tmpdir(), "sf-complete-slice-evidence-"));
	tmpDirs.push(dir);
	mkdirSync(join(dir, ".sf", "milestones", "M001", "slices", "S01"), {
		recursive: true,
	});
	openDatabase(join(dir, ".sf", "sf.db"));
	insertMilestone({ id: "M001", title: "Evidence", status: "active" });
	insertSlice({
		milestoneId: "M001",
		id: "S01",
		title: "Slice",
		status: "pending",
	});
	insertTask({
		milestoneId: "M001",
		sliceId: "S01",
		id: "T01",
		title: "Task",
		status: "complete",
	});
	return dir;
}

test("handleCompleteSlice_when_successful_records_completion_summary_evidence", async () => {
	const project = makeProject();

	const result = await handleCompleteSlice(
		{
			milestoneId: "M001",
			sliceId: "S01",
			sliceTitle: "Slice",
			verification: "Verification passed.",
			uatContent: "UAT passed.",
			oneLiner: "Slice finished with evidence",
			narrative: "All tasks are closed.",
			successCriteriaResults: "Passed.",
			verificationProof: "Focused test passed.",
			keyDecisions: ["Keep slice evidence in DB"],
			keyFiles: ["src/resources/extensions/sf/tools/complete-slice.js"],
		},
		project,
	);

	assert.equal(result.error, undefined);
	assert.equal(getSlice("M001", "S01").status, "complete");
	const trail = getSliceAuditTrail("M001", "S01");
	assert.equal(trail.length, 1);
	assert.equal(trail[0].evidence_type, "completion_summary");
	assert.match(trail[0].content, /Slice finished with evidence/);
	assert.match(trail[0].content, /Keep slice evidence in DB/);
});
