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

import {
	buildAutoBootstrapContext,
	hasMilestones,
} from "../headless-context.js";

test("buildAutoBootstrapContext includes purpose docs and source inventory", () => {
	const root = mkdtempSync(join(tmpdir(), "sf-headless-bootstrap-"));
	mkdirSync(join(root, "docs"), { recursive: true });
	mkdirSync(join(root, "src"), { recursive: true });

	writeFileSync(join(root, "VISION.md"), "# Vision\n\nBuild the runner.\n");
	writeFileSync(join(root, "TODO.md"), "# TODO\n\nWire ACE.\n");
	writeFileSync(join(root, "docs", "architecture.md"), "# Architecture\n");
	writeFileSync(join(root, "src", "main.ts"), "export const main = true;\n");

	const context = buildAutoBootstrapContext(root);

	assert.match(context, /Autonomous Repo Bootstrap/);
	assert.match(context, /purpose, vision, architecture/);
	assert.match(context, /ACE spec-first TDD/);
	assert.match(context, /explorer-style subagents/);
	assert.match(context, /harness-engineering principles/);
	assert.match(context, /## VISION\.md/);
	assert.match(context, /## TODO\.md/);
	assert.match(context, /## docs\/architecture\.md/);
	assert.match(context, /Source File Inventory/);
	assert.match(context, /src\/main\.ts/);
});

test("hasMilestones only reports true when milestone directories exist", () => {
	const root = mkdtempSync(join(tmpdir(), "sf-headless-milestones-"));

	assert.equal(hasMilestones(root), false);

	mkdirSync(join(root, ".sf", "milestones"), { recursive: true });
	assert.equal(hasMilestones(root), false);

	mkdirSync(join(root, ".sf", "milestones", "M001"), { recursive: true });
	assert.equal(hasMilestones(root), true);
});
