import assert from "node:assert/strict";
import { describe, it } from 'vitest';
import { composeTaskWithParentTrace } from "../index.js";

describe("composeTaskWithParentTrace", () => {
	it("returns task unchanged when parentTrace is undefined", () => {
		const task = "do the thing";
		assert.equal(composeTaskWithParentTrace(task, undefined), task);
	});

	it("returns task unchanged when parentTrace is an empty string", () => {
		const task = "do the thing";
		assert.equal(composeTaskWithParentTrace(task, ""), task);
	});

	it("returns task unchanged when parentTrace is whitespace-only", () => {
		const task = "do the thing";
		assert.equal(composeTaskWithParentTrace(task, "   \n\t  "), task);
	});

	it("wraps trace content in <parent_trace> tags when content is provided", () => {
		const task = "verify the parent's claim";
		const trace = "ran `npm test`\nexit 0";
		const result = composeTaskWithParentTrace(task, trace);
		assert.ok(
			result.includes("<parent_trace>"),
			"should include opening tag",
		);
		assert.ok(
			result.includes("</parent_trace>"),
			"should include closing tag",
		);
		assert.ok(result.includes(trace), "should include trimmed trace content");
	});

	it("places the original task after the closing </parent_trace> tag", () => {
		const task = "verify the parent's claim";
		const trace = "ran `npm test`\nexit 0";
		const result = composeTaskWithParentTrace(task, trace);
		const closeIdx = result.indexOf("</parent_trace>");
		const taskIdx = result.indexOf(task);
		assert.ok(closeIdx >= 0, "closing tag must be present");
		assert.ok(taskIdx >= 0, "task must be present");
		assert.ok(
			taskIdx > closeIdx,
			"task must appear after the closing </parent_trace> tag",
		);
	});

	it("trims whitespace from the parentTrace content before injection", () => {
		const task = "audit";
		const trace = "the actual trace line";
		const padded = `\n\n  ${trace}  \n\n`;
		const result = composeTaskWithParentTrace(task, padded);
		assert.ok(
			result.includes(trace),
			"trimmed trace content should appear in result",
		);
		assert.ok(
			!result.includes(padded),
			"untrimmed padded trace should not appear verbatim",
		);
	});

	it("includes verifier instruction phrases for hedge words and tool errors", () => {
		const task = "audit";
		const trace = "some trace";
		const result = composeTaskWithParentTrace(task, trace);
		assert.ok(
			result.includes("hedge words"),
			"should mention hedge words in verifier instructions",
		);
		assert.ok(
			result.includes("tool errors"),
			"should mention tool errors in verifier instructions",
		);
	});
});
