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

import {
	buildUnitPromptMessageContent,
	scopeActiveToolsForRunUnit,
} from "../auto/run-unit.js";

test("buildUnitPromptMessageContent_when_cache_split_present_preserves_join_boundary", () => {
	const content = buildUnitPromptMessageContent("flat", {
		before: "## Working Directory\n/repo",
		after: "## Inlined Task Plan\nDo it.",
	});

	assert.ok(Array.isArray(content));
	assert.deepEqual(content[0], {
		type: "text",
		text: "## Working Directory\n/repo\n",
		cache_control: { type: "ephemeral" },
	});
	assert.deepEqual(content[1], {
		type: "text",
		text: "## Inlined Task Plan\nDo it.",
	});
	assert.equal(
		content.map((part) => part.text).join(""),
		"## Working Directory\n/repo\n## Inlined Task Plan\nDo it.",
	);
});

test("buildUnitPromptMessageContent_when_no_cache_split_returns_flat_prompt", () => {
	assert.equal(buildUnitPromptMessageContent("flat", null), "flat");
});

test("scopeActiveToolsForRunUnit_when_explicit_allowlist_set_returns_only_allowed_tools", () => {
	const scoped = scopeActiveToolsForRunUnit(
		"execute-task",
		[
			"read",
			"bash",
			"edit",
			"checkpoint",
			"subagent",
			"await_subagent",
			"complete_task",
		],
		{ activeToolsAllowlist: ["checkpoint"] },
	);

	assert.deepEqual(scoped, ["checkpoint"]);
});

test("scopeActiveToolsForRunUnit_when_no_explicit_allowlist_keeps_existing_unit_scoping", () => {
	const scoped = scopeActiveToolsForRunUnit("research-slice", [
		"read",
		"sf_plan_task",
		"save_summary",
		"report_issue",
	]);

	assert.deepEqual(scoped, ["read", "save_summary", "report_issue"]);
});
