Add protocol test closures for secure writes, aliases, help and cli docs

This commit is contained in:
gitea committed 2026-10-18 11:16:07 +08:00
1 parent 97c59fae14
commit 9e7275f09b
4 files changed
+260 -1

No files matched your search

+77
View File
@@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test";
import { readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { parseArgs } from "./parser.js";
import { KNOWN_AREAS, UPDATE_ALIASES } from "./runner.js";
/**
@@ -67,3 +68,79 @@ describe("documentation references live commands", () => {
expect(stale).toEqual({});
});
});
/**
* Usage-level closure for the generated cli.md block.
*
* `verify:cli-md` proves the block byte-matches what HELP_REGISTRY renders,
* and parser.test.ts proves registry usage flags are registered — but nothing
* fed the rendered lines back through the real parser. That is exactly the
* hole the 2026-10-17 review found in the opposite direction: 22 parser tests
* green while nine documented flags were rejected at runtime. Rendering is
* code too, so the doc reader's copy-paste path gets its own parse.
*/
const CLI_DOC_PATH = join(REPO_ROOT, "skills/seal-use/references/cli.md");
function generatedUsageLines(markdown: string): string[] {
const block = markdown.match(
/<!-- BEGIN GENERATED CLI COMMANDS -->([\s\S]*?)<!-- END GENERATED CLI COMMANDS -->/
);
if (!block?.[1]) throw new Error(`generated command block missing in ${CLI_DOC_PATH}`);
return block[1].split("\n").filter((line) => line.startsWith("seal-use "));
}
/**
* Turn a documented usage line into argv the way a reader would type it:
* keep one alternative of each `a|b` group, unwrap `[...]` optionality, and
* replace `<value hints>` with a neutral placeholder token. Single quotes are
* respected so `--json '{...}'` payloads survive as one argument.
*/
function usageToArgv(line: string): string[] {
// `--json '{...}'` is a fill-in placeholder in the docs, not literal JSON;
// substitute a valid empty object so the parser sees a well-formed payload.
const hinted = line.slice("seal-use ".length)
.replace(/<[^<>]*>/g, " __HINT__ ")
.replace(/'\{\.\.\.\}'/g, "'{}'");
const unbracketed = hinted.replace(/[[\]]/g, " ");
const tokens: string[] = [];
let current = "";
let quoted = false;
for (const char of unbracketed) {
if (char === "'") {
quoted = !quoted;
continue;
}
if (!quoted && /\s/.test(char)) {
if (current) tokens.push(current);
current = "";
continue;
}
current += char;
}
if (current) tokens.push(current);
return tokens.map((token) => token.split("|")[0]!).filter((token) => token.length > 0);
}
describe("generated cli.md usage lines parse", () => {
test("every documented usage line is accepted by parseArgs", () => {
const lines = generatedUsageLines(readFileSync(CLI_DOC_PATH, "utf8"));
// Format guard: if the markers or the `seal-use ` line convention change,
// the extraction silently yields nothing and this suite would pass while
// checking zero lines.
expect(lines.length).toBeGreaterThan(100);
const rejected: Record<string, string> = {};
for (const line of lines) {
try {
parseArgs(usageToArgv(line));
} catch (error) {
rejected[line] = error instanceof Error ? error.message : String(error);
}
}
expect(rejected).toEqual({});
});
});