import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import path from "node:path"; import { test } from "node:test"; import { fileURLToPath } from "node:url"; import { COMMANDS_FILE, generateCommandsMarkdown, mdCell, opsActions, opsExamples, opsParagraphs, RECIPES_FILE, renderCommandsMarkdown, splitUsage, unknownCommandsIn, unknownRecipeCommands, } from "./cli-docs"; const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", ".."); const OPS_USAGE = [ "Usage: pnpm ops [--json ''] [--wait]", " pnpm ops list", "", "Actions: sync, build-site, deploy-site, lane", "", "--wait follows the job's log.", "", 'build-site and deploy-site take "siteId" or', ' "siteIds" (a list).', "", 'deploy-site ships a built bundle: {"siteId"}. See `a | b`.', "", "Env: WORKER_TOKEN", ].join("\n"); test("splitUsage splits at the first double space; a usage with none is all text", () => { assert.deepEqual(splitUsage(" [--force] build it now"), { args: " [--force]", text: "build it now", }); assert.deepEqual(splitUsage("rebuild the index"), { args: "", text: "rebuild the index" }); }); test("mdCell escapes markup outside code and only the pipe inside it", () => { assert.equal(mdCell("a | *b* _c_ [x]"), "a <id> \\| \\*b\\* \\_c\\_ \\[x\\]"); assert.equal(mdCell("run `x | y ` then"), "run `x \\| y ` then"); }); test("opsActions reads the Actions line, and refuses a usage without one", () => { assert.deepEqual(opsActions(OPS_USAGE), ["sync", "build-site", "deploy-site", "lane"]); assert.throws(() => opsActions("Usage: nothing"), /no `Actions:` line/); }); test("opsParagraphs attributes a paragraph to every action it opens with", () => { const ps = opsParagraphs(OPS_USAGE, opsActions(OPS_USAGE)); assert.deepEqual( ps.map((p) => p.actions), [[], [], ["build-site", "deploy-site"], ["deploy-site"], []], ); assert.ok(!ps.some((p) => p.text.startsWith("Actions: "))); }); test("opsExamples takes header lines of known actions only, padding collapsed", () => { const source = [ "// pnpm ops sync --json '{\"slug\":\"x\"}' --wait", "// pnpm ops get channel x", "// pnpm ops lane", "// pnpm ops sync --json '{\"slug\":\"y\"}'", "const x = 1; // pnpm ops sync not a header line", ].join("\n"); const ex = opsExamples(source, ["sync", "lane"]); assert.deepEqual(ex.get("sync"), [ "pnpm ops sync --json '{\"slug\":\"x\"}' --wait", "pnpm ops sync --json '{\"slug\":\"y\"}'", ]); assert.deepEqual(ex.get("lane"), ["pnpm ops lane"]); assert.equal(ex.has("get"), false); }); test("the rendered reference lists every command and every action exactly once as a row head", () => { const md = renderCommandsMarkdown( [ { path: ["index"], usage: "rebuild the LMDB transcript index" }, { path: ["publish", "build"], usage: " [--force] build a site" }, { path: ["mcp"], usage: "[--local ] start the MCP server", passthrough: true }, ], OPS_USAGE, new Map([["deploy-site", ["pnpm ops deploy-site --json '{}'"]]]), ); assert.match(md, /^# Command reference\n\n