/**
 * plan-slice-evidence.test.mjs — slice planning evidence.
 *
 * Purpose: prove slice planning records structured DB evidence while rendering
 * generated plan projections from the database.
 */
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,
	getSliceAuditTrail,
	getTask,
	insertMilestone,
	insertSlice,
	openDatabase,
} from "../sf-db.js";
import { handlePlanSlice } from "../tools/plan-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-plan-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",
	});
	return dir;
}

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

	const result = await handlePlanSlice(
		{
			milestoneId: "M001",
			sliceId: "S01",
			goal: "Plan with evidence",
			successCriteria: "Plan rows are persisted.",
			proofLevel: "focused",
			integrationClosure: "Plan projection rendered.",
			observabilityImpact: "Evidence row is queryable.",
			planningMeeting: {
				trigger: "test",
				pm: "Plan the slice.",
				researcher: "DB schema exists.",
				partner: "Use existing tools.",
				combatant: "Avoid JSON peer state.",
				architect: "Persist evidence in SQLite.",
				moderator: "Proceed.",
				recommendedRoute: "planning",
				confidenceSummary: "high",
			},
			tasks: [
				{
					taskId: "T01",
					title: "Implement evidence",
					description: "Persist planning evidence.",
					estimate: "small",
					files: ["src/resources/extensions/sf/tools/plan-slice.js"],
					verify: "npm test -- plan-slice",
					inputs: ["DB-backed slice"],
					expectedOutput: ["Evidence row"],
					risk: "high",
					mutationScope: "cross-cutting",
					verification: "integration",
					planApproval: "pending",
					blocksParallel: true,
				},
			],
		},
		project,
	);

	assert.equal(result.error, undefined);
	const trail = getSliceAuditTrail("M001", "S01");
	assert.equal(trail.length, 1);
	assert.equal(trail[0].evidence_type, "plan_slice");
	assert.match(trail[0].content, /Plan with evidence/);
	assert.match(trail[0].content, /Implement evidence/);
	const task = getTask("M001", "S01", "T01");
	assert.equal(task.frontmatter.risk, "high");
	assert.equal(task.frontmatter.mutationScope, "cross-cutting");
	assert.equal(task.frontmatter.verification, "integration");
	assert.equal(task.frontmatter.planApproval, "pending");
	assert.equal(task.frontmatter.blocksParallel, true);
});
