/**
 * summary-render-parity.test.ts — Regression test for #2720
 *
 * Asserts that the SUMMARY.md produced at task-completion time
 * (renderSummaryMarkdown in complete-task.ts) is structurally identical
 * to the SUMMARY.md produced at projection-regeneration time
 * (renderSummaryContent in workflow-projections.ts).
 *
 * Both render paths receive equivalent data (CompleteTaskParams vs TaskRow)
 * and must produce the same output. If they diverge, projection regeneration
 * silently replaces richer content with a stripped-down version.
 */

import assert from "node:assert/strict";
import type { TaskRow } from "../sf-db.ts";
import { renderSummaryContent } from "../workflow-projections.ts";
import { describe, it } from "vitest";

const SLICE_ID = "S01";
const MILESTONE_ID = "M001";

const taskRow: TaskRow = {
	milestone_id: MILESTONE_ID,
	slice_id: SLICE_ID,
	id: "T01",
	title: "Implement widget parser",
	status: "complete",
	one_liner: "Implement widget parser",
	narrative: "Added a recursive descent parser for widget DSL.",
	verification_result: "All 42 unit tests pass; linter clean.",
	duration: "2h",
	completed_at: "2025-01-15T10:30:00.000Z",
	blocker_discovered: false,
	deviations: "Switched from PEG to hand-rolled parser for perf.",
	known_issues: "No known issues.",
	key_files: ["src/parser.ts", "src/lexer.ts"],
	key_decisions: ["Hand-rolled parser over PEG for 3x throughput"],
	full_summary_md: "",
	description: "",
	estimate: "",
	files: [],
	verify: "",
	inputs: [],
	expected_output: [],
	observability_impact: "",
	full_plan_md: "",
	sequence: 1,
};

const verificationEvidence = [
	{
		command: "npm test",
		exitCode: 0,
		verdict: "42/42 passed ✅",
		durationMs: 3200,
	},
	{
		command: "npm run lint",
		exitCode: 0,
		verdict: "No warnings ✅",
		durationMs: 1100,
	},
];

describe("#2720: summary render parity", () => {
	it("includes Verification section", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(output.includes("## Verification"));
	});

	it("includes Verification Evidence table", () => {
		const output = renderSummaryContent(
			taskRow,
			SLICE_ID,
			MILESTONE_ID,
			verificationEvidence,
		);
		assert.ok(output.includes("## Verification Evidence"));
		assert.ok(output.includes("npm test"));
	});

	it("includes Files Created/Modified section", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(output.includes("## Files Created/Modified"));
		assert.ok(output.includes("`src/parser.ts`"));
	});

	it("one_liner renders as bold (not blockquote)", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(output.includes(`**${taskRow.one_liner}**`));
	});

	it("frontmatter key_files uses YAML list format", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(
			output.includes("key_files:\n  - src/parser.ts\n  - src/lexer.ts"),
		);
	});

	it("frontmatter key_decisions uses YAML list format", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(
			output.includes(
				"key_decisions:\n  - Hand-rolled parser over PEG for 3x throughput",
			),
		);
	});

	it("Deviations section always present with 'None.' fallback", () => {
		const noDeviations = { ...taskRow, deviations: "" };
		const output = renderSummaryContent(noDeviations, SLICE_ID, MILESTONE_ID);
		assert.ok(output.includes("## Deviations"));
		assert.ok(output.includes("None."));
	});

	it("Known Issues section always present", () => {
		const noKnownIssues = { ...taskRow, known_issues: "" };
		const output = renderSummaryContent(noKnownIssues, SLICE_ID, MILESTONE_ID);
		assert.ok(output.includes("## Known Issues"));
	});

	it("verification_result frontmatter not double-quoted", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(!output.includes('verification_result: "'));
	});

	it("duration frontmatter not double-quoted", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(!output.includes('duration: "'));
	});

	it("empty key_files renders YAML placeholder", () => {
		const noFiles = { ...taskRow, key_files: [] };
		const output = renderSummaryContent(noFiles, SLICE_ID, MILESTONE_ID);
		assert.ok(output.includes("key_files:\n  - (none)"));
	});

	it("frontmatter does not contain projection-only fields", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID);
		assert.ok(!output.includes("provides:"));
		assert.ok(!output.includes("requires:"));
		assert.ok(!output.includes("affects:"));
		assert.ok(!output.includes("patterns_established:"));
		assert.ok(!output.includes("drill_down_paths:"));
		assert.ok(!output.includes("observability_surfaces:"));
	});

	it("empty verification evidence renders placeholder row", () => {
		const output = renderSummaryContent(taskRow, SLICE_ID, MILESTONE_ID, []);
		assert.ok(output.includes("No verification commands discovered"));
	});
});
