import assert from "node:assert/strict";
import { test } from "vitest";
import { extractSliceExecutionExcerpt } from "../summary-helpers.js";

test("extractSliceExecutionExcerpt when content is null returns fallback", () => {
	const result = extractSliceExecutionExcerpt(null, "S01/PLAN.md");
	assert.ok(result.includes("## Slice Plan Excerpt"));
	assert.ok(result.includes("Slice plan not found"));
	assert.ok(result.includes("S01/PLAN.md"));
});

test("extractSliceExecutionExcerpt when content is undefined returns fallback", () => {
	const result = extractSliceExecutionExcerpt(undefined, "S02/PLAN.md");
	assert.ok(result.includes("## Slice Plan Excerpt"));
	assert.ok(result.includes("Slice plan not found"));
	assert.ok(result.includes("S02/PLAN.md"));
});

test("extractSliceExecutionExcerpt extracts goal and demo lines", () => {
	const content = [
		"# Slice Plan",
		"",
		"**Goal:** This slice delivers X",
		"",
		"**Demo:** After this, you can Y",
		"",
		"## Verification",
		"Run npm test",
	].join("\n");

	const result = extractSliceExecutionExcerpt(content, "S01/PLAN.md");
	assert.ok(result.includes("**Goal:** This slice delivers X"));
	assert.ok(result.includes("**Demo:** After this, you can Y"));
	assert.ok(result.includes("Source: `S01/PLAN.md`"));
});

test("extractSliceExecutionExcerpt extracts verification section", () => {
	const content = [
		"# Slice Plan",
		"",
		"**Goal:** Test goal",
		"",
		"## Verification",
		"Run the integration suite",
		"Check exit code is 0",
		"",
		"## Observability / Diagnostics",
		"Metrics at /metrics",
	].join("\n");

	const result = extractSliceExecutionExcerpt(content, "S01/PLAN.md");
	assert.ok(result.includes("### Slice Verification"));
	assert.ok(result.includes("Run the integration suite"));
	assert.ok(result.includes("Check exit code is 0"));
	assert.ok(result.includes("### Slice Observability / Diagnostics"));
	assert.ok(result.includes("Metrics at /metrics"));
});

test("extractSliceExecutionExcerpt omits missing sections gracefully", () => {
	const content = ["# Slice Plan", "", "**Goal:** Minimal slice"].join("\n");

	const result = extractSliceExecutionExcerpt(content, "S01/PLAN.md");
	assert.ok(result.includes("**Goal:** Minimal slice"));
	assert.ok(!result.includes("### Slice Verification"));
	assert.ok(!result.includes("### Slice Observability"));
});
