import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { test } from 'vitest';
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const promptsDir = join(__dirname, "..", "prompts");

test("execute-task prompt requires reading existing artifacts before write", () => {
	const prompt = readFileSync(join(promptsDir, "execute-task.md"), "utf-8");

	assert.match(
		prompt,
		/Before any `Write` that creates an artifact or output file, check whether that path already exists\./,
		"execute-task prompt should require an existence check before creating artifacts",
	);
	assert.match(
		prompt,
		/If it does, read it first and decide whether the work is already done, should be extended, or truly needs replacement\./,
		"execute-task prompt should require reading existing artifacts before replacement",
	);
});

test("execute-task prompt requires durable verification artifacts and cleanup", () => {
	const prompt = readFileSync(join(promptsDir, "execute-task.md"), "utf-8");

	assert.match(
		prompt,
		/Keep verification artifacts disciplined/,
		"execute-task prompt should have a general verification artifact discipline section",
	);
	assert.match(
		prompt,
		/delete it before completion or promote it into the durable artifact named by the task plan/,
		"execute-task prompt should require temporary helpers to be removed or promoted",
	);
	assert.match(
		prompt,
		/Do not leave duplicate sources of truth/,
		"execute-task prompt should forbid duplicated canonical data/artifacts",
	);
	assert.match(
		prompt,
		/Do not satisfy verification with an ad-hoc helper when the task asks for a durable harness, command, test, or report/,
		"execute-task prompt should require the durable planned verifier to own repeatable checks",
	);
	assert.match(
		prompt,
		/inspect `git status --short`/,
		"execute-task prompt should require a pre-completion status scan for unintended files",
	);
});

test("execute-task prompt requires local API verification before unknown-symbol repairs", () => {
	const prompt = readFileSync(join(promptsDir, "execute-task.md"), "utf-8");

	assert.match(
		prompt,
		/unknown method, function, type, import, or package member/,
		"execute-task prompt should identify unknown-symbol compile failures",
	);
	assert.match(
		prompt,
		/go doc/,
		"execute-task prompt should require language-native API verification",
	);
	assert.match(
		prompt,
		/Do not invent adjacent method names/,
		"execute-task prompt should forbid guessed replacement APIs",
	);
	assert.match(
		prompt,
		/rerun the narrow failing command immediately/,
		"execute-task prompt should force tight compile-repair loops",
	);
});

test("execute-task prompt forbids unbounded interactive test harnesses", () => {
	const prompt = readFileSync(join(promptsDir, "execute-task.md"), "utf-8");

	assert.match(
		prompt,
		/Automated tests must not enter a real interactive TUI/,
		"execute-task prompt should reject real interactive TUI loops in automation",
	);
	assert.match(
		prompt,
		/controlled input\/output and a bounded timeout/,
		"execute-task prompt should require controlled IO and timeouts for interactive paths",
	);
	assert.match(
		prompt,
		/treat it as a test-harness bug/,
		"execute-task prompt should classify repeated noisy output as a harness bug",
	);
});

test("guided resume prompt checks for pre-existing artifacts", () => {
	const prompt = readFileSync(
		join(promptsDir, "guided-resume-task.md"),
		"utf-8",
	);

	assert.match(
		prompt,
		/Before you create any expected artifact or output file, check whether it already exists and read it first/i,
		"guided resume prompt should guard pre-existing artifacts",
	);
});
