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

+69
View File
@@ -0,0 +1,69 @@
import { describe, expect, test } from "bun:test";
import { AREA_ALIASES, UPDATE_ALIASES, normalizeCliArea } from "./area-aliases.js";
import { KNOWN_AREAS } from "./runner.js";
/**
* Drift guard for the shared alias table.
*
* `read-only.ts` once hand-copied the alias pairs from `runner.ts` and the two
* silently diverged — the exact class of bug the 2026-10-17 architecture
* review called "two sources of truth". The tables now live in one module and
* are imported by dispatch, help, and the read-only gateway; these tests make
* the remaining cross-module contract explicit: an alias may only point at an
* area the dispatcher actually handles, may not chain into another alias, and
* must not shadow a real area name.
*/
describe("area alias integrity", () => {
test("every alias resolves to a dispatchable area", () => {
const dangling = Object.entries(AREA_ALIASES)
.filter(([, target]) => !KNOWN_AREAS.has(target))
.map(([alias, target]) => `${alias} -> ${target}`);
expect(dangling).toEqual([]);
});
test("no alias chains into another alias", () => {
// normalizeCliArea applies one lookup; a chained alias would dispatch the
// raw alias spelling as a command area and fail with "Unknown command".
const chained = Object.values(AREA_ALIASES).filter((target) => target in AREA_ALIASES);
expect(chained).toEqual([]);
});
test("alias spellings never shadow a real area name", () => {
// KNOWN_AREAS is the handler tables plus the alias keys; subtracting the
// alias keys recovers the real dispatch areas. An alias colliding with
// one of them would be dead weight at best and ambiguous at worst.
const realAreas = new Set([...KNOWN_AREAS].filter((area) => !(area in AREA_ALIASES)));
const shadowing = Object.keys(AREA_ALIASES).filter((alias) => realAreas.has(alias));
expect(shadowing).toEqual([]);
});
test("every update spelling normalizes onto the real update area", () => {
for (const spelling of UPDATE_ALIASES) {
expect(normalizeCliArea(spelling)).toBe("update");
}
expect(KNOWN_AREAS.has("update")).toBe(true);
});
test("normalization is idempotent and case-insensitive", () => {
expect(normalizeCliArea(normalizeCliArea("corp"))).toBe("corps");
expect(normalizeCliArea("CORP")).toBe("corps");
expect(normalizeCliArea("Self-Update")).toBe("update");
});
test("every alias and update spelling resolves through KNOWN_AREAS", () => {
// Alias keys must be members outright: help lookup and the read-only
// gateway resolve the raw spelling before any normalization. Update
// spellings are normalized first, so their canonical target is the
// dispatchable name.
for (const alias of Object.keys(AREA_ALIASES)) {
expect(KNOWN_AREAS.has(alias)).toBe(true);
}
for (const spelling of UPDATE_ALIASES) {
expect(KNOWN_AREAS.has(normalizeCliArea(spelling))).toBe(true);
}
});
});
+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({});
});
});
+12 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { extractHelpRequest, renderHelp } from "./help.js";
import { extractHelpRequest, HELP_REGISTRY, renderHelp } from "./help.js";
import { KNOWN_AREAS, runCli } from "./runner.js";
describe("extractHelpRequest", () => {
@@ -210,6 +210,17 @@ describe("help coverage", () => {
}
});
test("every help registry area is dispatchable", () => {
// The forward direction is checked above. The reverse catches a registry
// area that renders help but no longer reaches a handler table — the
// "documented for two releases after the dispatcher dropped it" failure
// mode this suite exists to prevent, applied to `seal-use help` itself.
const dead = HELP_REGISTRY.map((entry) => entry.area)
.filter((area) => !KNOWN_AREAS.has(area));
expect(dead).toEqual([]);
});
test("stop shortcut and server alias resolve to help", () => {
expect(renderHelp(["server"])).toContain("seal-use mcp");
});