import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { describe, it } from "node:test"; import { fileURLToPath } from "node:url"; const __dirname = fileURLToPath(new URL(".", import.meta.url)); const ROOT = path.resolve(__dirname, "../../"); const AGENTS_PATH = path.resolve(ROOT, "AGENTS.md"); const DOCS_DIR = path.resolve(ROOT, "docs"); // eslint-disable-next-line security/detect-non-literal-fs-filename const readUtf8 = (filePath: string): string => fs.readFileSync(filePath, "utf8"); const getMandatoryRefsFromAgents = (content: string): string[] => { const marker = "## Mandatory References"; const start = content.indexOf(marker); if (start < 0) { return []; } const tail = content.slice(start + marker.length); const nextHeaderIdx = tail.search(/\n##\s+/); const block = nextHeaderIdx >= 0 ? tail.slice(0, nextHeaderIdx) : tail; return block .split("\n") .map((line) => line.trim()) .filter((line) => line.startsWith("- ")) .map((line) => line.replace(/^- /, "").trim()) .map((line) => { const backtickMatch = line.match(/`([^`]+)`/); return backtickMatch?.[1] ?? line; }); }; describe("Process docs linkage in AGENTS.md", () => { it("keeps required process docs listed in Mandatory References", () => { // eslint-disable-next-line security/detect-non-literal-fs-filename assert.ok(fs.existsSync(AGENTS_PATH), "AGENTS.md not found"); const agents = readUtf8(AGENTS_PATH); const refs = new Set(getMandatoryRefsFromAgents(agents)); // eslint-disable-next-line security/detect-non-literal-fs-filename const docsFiles = fs.readdirSync(DOCS_DIR); const autoRequired = docsFiles .filter((name) => /\.(md)$/i.test(name)) .filter((name) => /(TEMPLATE|POLICY)\.md$/i.test(name)) .map((name) => `docs/${name}`); const coreRequired = [ "docs/ARCHITECTURE.md", "docs/CONTRIBUTING.md", "docs/TASK_INTAKE.md", "docs/QUALITY_GATES.md", "docs/TESTING.md", "docs/FAILURE_PLAYBOOK.md", "docs/HANDOFF_TEMPLATE.md", "docs/PUBLIC_API.md", "docs/CHANGE_POLICY.md", "docs/MIGRATION.md", "docs/RELEASE_HANDOFF.md", "CHANGELOG.md", ]; const required = Array.from(new Set([ ...autoRequired, ...coreRequired ])); required.forEach((item) => { assert.ok(refs.has(item), `Missing "${item}" in AGENTS.md -> Mandatory References`); }); }); it("ensures all referenced Mandatory files exist", () => { const refs = getMandatoryRefsFromAgents(readUtf8(AGENTS_PATH)); refs.forEach((relativePath) => { const absolutePath = path.resolve(ROOT, relativePath); // eslint-disable-next-line security/detect-non-literal-fs-filename assert.ok(fs.existsSync(absolutePath), `Mandatory reference path does not exist: "${relativePath}"`); }); }); });