| 1 | import { test } from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, rmSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { createHash } from "node:crypto"; |
| 7 | import { writeAgentDocs } from "./docs-sources-lib.mjs"; |
| 8 | |
| 9 | test("generates a missing snapshot and refuses invalid sources without replacing it", () => { |
| 10 | const root = mkdtempSync(join(tmpdir(), "agent-docs-")); |
| 11 | try { |
| 12 | const web = join(root, "web"); |
| 13 | mkdirSync(join(web, "lib"), { recursive: true }); |
| 14 | mkdirSync(join(web, "data")); |
| 15 | const registry = join(web, "lib", "docs-map.ts"); |
| 16 | const snapshot = join(web, "data", "agent-docs.generated.json"); |
| 17 | const content = "# Current route inventory\n"; |
| 18 | writeFileSync(join(root, "routes.md"), content); |
| 19 | const topic = (source) => `{ id: "runtime", slug: "runtime", repoSource: ["${source}"] }`; |
| 20 | writeFileSync(registry, topic("routes.md")); |
| 21 | writeAgentDocs(web, root); |
| 22 | const generated = readFileSync(snapshot, "utf8"); |
| 23 | assert.deepEqual(JSON.parse(generated), [{ id: "runtime", slug: "runtime", sources: [{ |
| 24 | path: "routes.md", content, sha256: createHash("sha256").update(content).digest("hex"), |
| 25 | }] }]); |
| 26 | for (const source of ["missing.md", "../outside.md"]) { |
| 27 | writeFileSync(registry, topic(source)); |
| 28 | assert.throws(() => writeAgentDocs(web, root)); |
| 29 | assert.equal(readFileSync(snapshot, "utf8"), generated); |
| 30 | } |
| 31 | } finally { |
| 32 | rmSync(root, { recursive: true, force: true }); |
| 33 | } |
| 34 | }); |
| 35 |