import assert from "node:assert/strict";
import { test } from "vitest";

import { reorderForCaching } from "../prompt-ordering.js";

test("reorderForCaching_when_inlined_slice_summary_has_requirements_advanced_keeps_it_after_mission", () => {
	const prompt = [
		"# Milestone Validation",
		"",
		"## Working Directory",
		"/repo",
		"",
		"## Mission",
		"Dispatch reviewers.",
		"",
		"## Context",
		"Inlined below.",
		"",
		"## Inlined Context",
		"### S01 Summary",
		"# S01",
		"",
		"## Requirements Advanced",
		"- R1",
		"",
		"## Requirements Validated",
		"None.",
	].join("\n");

	const reordered = reorderForCaching(prompt);

	assert.ok(
		reordered.indexOf("## Mission") <
			reordered.indexOf("## Requirements Advanced"),
	);
	assert.ok(
		reordered.indexOf("## Context") <
			reordered.indexOf("## Requirements Advanced"),
	);
});

test("reorderForCaching_when_top_level_requirements_exists_still_hoists_exact_requirements_block", () => {
	const prompt = [
		"# Execute",
		"",
		"## Mission",
		"Do work.",
		"",
		"## Requirements",
		"- R1",
		"",
		"## Verification",
		"Run tests.",
	].join("\n");

	const reordered = reorderForCaching(prompt);

	assert.ok(
		reordered.indexOf("## Requirements") < reordered.indexOf("## Mission"),
	);
});
