import { describe, expect, test } from "bun:test"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { HELP_REGISTRY } from "./help.js"; import { parseArgs } from "./parser.js"; /** * Naming charter guard (.agents/rules/cli-protocol.md "Naming Charter"). * * The visible surface (help registry, mirrored into the skill docs) must present * one canonical spelling per concept; legacy spellings keep working as silent * dispatcher/parser aliases but must never appear in documentation. This test * fails when a camelCase flag or a retired verb/flag leaks back onto the * visible surface, or when a canonical flag loses its parser registration. */ const LEGACY_SURFACE_TOKENS = [ "--to-downloads", "--show-secret", "--maxResults", "--maxRuns", "--humanResult", "--manualApprovalStatus", "--startDate", "--endDate", "--includeBridge", "--ruleVersion", "--documentLimit" ]; const VALUE_FLAG_PAIRS: Array<[kebab: string, legacyCamel: string]> = [ ["--start-date", "--startDate"], ["--end-date", "--endDate"], ["--max-results", "--maxResults"], ["--human-result", "--humanResult"], ["--manual-approval-status", "--manualApprovalStatus"], ["--rule-version", "--ruleVersion"], ["--document-limit", "--documentLimit"], ["--context-lines", "--contextLines"], ["--source-document-sn", "--sourceDocumentSN"], ["--source-document-id", "--sourceDocumentId"] ]; const BOOLEAN_FLAG_CANONICAL = ["--downloads", "--include-bridge", "--reveal"]; function collectSurfaceFlags(): string[] { const flags: string[] = []; const scan = (text: string) => { // Strip segments first: values like contain // dash-led uppercase fragments that are not flags. for (const match of text.replace(/<[^>]*>/g, " ").matchAll(/--?[A-Za-z][A-Za-z0-9-]*/g)) { flags.push(match[0]); } }; for (const area of HELP_REGISTRY) { for (const command of area.commands) { for (const usage of command.usage) { scan(usage); } for (const option of command.options ?? []) { scan(option.flag); } } } return flags; } describe("naming charter: visible help surface", () => { test("documents only lowercase kebab-case flags", () => { const offenders = collectSurfaceFlags().filter((flag) => /[A-Z]/.test(flag)); expect(offenders).toEqual([]); }); test("never documents retired flag spellings", () => { const serialized = JSON.stringify(HELP_REGISTRY); const offenders = LEGACY_SURFACE_TOKENS.filter((token) => serialized.includes(token)); expect(offenders).toEqual([]); }); test("never documents the retired bare flow switch", () => { // `--flow` on `approval-runs get` is a silent legacy alias for // `approval-runs hose-flow`, so the visible surface must not name it. // LEGACY_SURFACE_TOKENS matches by substring and cannot express this: // `--flow-id`, `--flow-ids` and `--flow-concurrency` are canonical. const serialized = JSON.stringify(HELP_REGISTRY); const offenders = serialized.match(/--flow(?![A-Za-z0-9-])/g) ?? []; expect(offenders).toEqual([]); }); }); describe("naming charter: parser accepts canonical and legacy spellings", () => { test("kebab canonical and camel legacy value flags both parse", () => { for (const [kebab, legacy] of VALUE_FLAG_PAIRS) { const canonical = parseArgs(["x", kebab, "v"]); const legacyArgs = parseArgs(["x", legacy, "v"]); expect(canonical.options[kebab.slice(2)]).toBe("v"); expect(legacyArgs.options[legacy.slice(2)]).toBe("v"); } }); test("canonical boolean flags parse without a value", () => { for (const flag of BOOLEAN_FLAG_CANONICAL) { const parsed = parseArgs(["x", flag]); expect(parsed.options[flag.slice(2)]).toBe(true); } }); }); describe("naming charter: canonical accept-sets are wired in commands", () => { const read = (path: string) => readFileSync(fileURLToPath(new URL(path, import.meta.url)), "utf8"); test("webhook resource-pack export reads --out and --downloads (no silent ignore)", () => { const source = read("./commands/webhook.ts"); expect(source).toContain("stringOption(args.options.out) ??"); expect(source).toContain("booleanOption(args.options.downloads) ??"); expect(source).toContain("booleanOption(args.options.download) ??"); }); test("webhook secret reveal accepts --reveal alongside legacy --show-secret", () => { const source = read("./parser.ts"); expect(source).toContain("booleanOption(options.reveal)"); expect(source).toContain('booleanOption(options["show-secret"])'); }); test("approval-runs reads canonical --start-date/--end-date everywhere", () => { const source = read("./commands/approval-runs.ts"); const startDateReads = source.match(/args\.options\["start-date"\]/g)?.length ?? 0; const endDateReads = source.match(/args\.options\["end-date"\]/g)?.length ?? 0; expect(startDateReads).toBeGreaterThanOrEqual(3); expect(endDateReads).toBeGreaterThanOrEqual(3); }); });