import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from 'vitest';

import { ensureAgenticDocsScaffold } from "../agentic-docs-scaffold.ts";

test("ensureAgenticDocsScaffold creates repo docs and nested AGENTS files without overwriting", () => {
	const dir = mkdtempSync(join(tmpdir(), "sf-agentic-docs-"));
	try {
		writeFileSync(join(dir, "AGENTS.md"), "# Existing\n", "utf-8");

		ensureAgenticDocsScaffold(dir);

		assert.equal(readFileSync(join(dir, "AGENTS.md"), "utf-8"), "# Existing\n");
		assert.match(
			readFileSync(join(dir, "ARCHITECTURE.md"), "utf-8"),
			/short map of the codebase/,
		);
		assert.match(
			readFileSync(join(dir, "src", "AGENTS.md"), "utf-8"),
			/Source Agent Notes/,
		);
		assert.match(
			readFileSync(join(dir, "tests", "AGENTS.md"), "utf-8"),
			/Test Agent Notes/,
		);
		assert.match(
			readFileSync(join(dir, "docs", "AGENTS.md"), "utf-8"),
			/records keeper checklist/,
		);
		assert.match(
			readFileSync(join(dir, "docs", "records", "AGENTS.md"), "utf-8"),
			/Records Agent Notes/,
		);
		assert.match(
			readFileSync(join(dir, "docs", "RECORDS_KEEPER.md"), "utf-8"),
			/Canonical Homes/,
		);
		assert.match(
			readFileSync(join(dir, "docs", "exec-plans", "AGENTS.md"), "utf-8"),
			/Execution Plan Agent Notes/,
		);
		assert.match(
			readFileSync(join(dir, "docs", "QUALITY_SCORE.md"), "utf-8"),
			/Make code legible to agents/,
		);
		assert.match(
			readFileSync(join(dir, ".siftignore"), "utf-8"),
			/\*\*\/node_modules\/\*\*/,
		);
		assert.match(
			readFileSync(join(dir, ".siftignore"), "utf-8"),
			/\.venv\/\*\*/,
		);
	} finally {
		rmSync(dir, { recursive: true, force: true });
	}
});
