mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 01:46:39 +00:00
* fix(cli)!: accept only the documented kebab-case option spelling REA's argv parser kept incur's second spelling of every option: the camelCase schema key (`--maxRows`) bound like the documented `--max-rows`, although help, completion and manifests list only the kebab-case form. That hidden alias let wrong spellings survive in the guides. The parser now binds only the documented flag. A schema key spelling, including its `--no-` negation, fails as a typed `flag_spelling` failure rendered as PARSE_ERROR that names the documented flag: "Unknown flag: --maxRows. Did you mean --max-rows?". Completion no longer resolves camelCase option names. The sensitive-value scan still treats `--sensitiveValue` as a declaration so its value stays redacted from the refusal. Guides, the inspect-api-contract option description and tests now use the kebab-case flags. BREAKING CHANGE: camelCase option spellings such as `--maxRows` or `--dryRun` are refused; use the kebab-case flag that `--help` lists. * test(cli): exercise invalid dry-run values with the documented flag spelling The invalid dry-run cases in strictPositionals and cliSetup appended --dryRun=false as a later valid override. The parser now refuses that schema key spelling before it validates the dry-run value, so each case failed on flag spelling and never checked that a later override cannot hide an earlier invalid value. The override is now --dry-run=false, and each case asserts the dry-run failure message, so a spelling refusal can no longer satisfy it. The --dryRun=abc case in cliSetup's malformed-value list is removed: it was the --dry-run=abc case under a spelling the parser now refuses before reading the value, and argvParser.test.ts owns that refusal. * test(cli): keep one public case for the flag spelling refusal cliOptionAliases repeated argvParser's three token shapes (bare, inline value, negation) through the full CLI. The parser test owns those shapes and asserts the structured flag_spelling failure with its flag and documented spelling; the CLI keeps one case for the documented PARSE_ERROR projection, whose message is part of the CLI contract. The camel-case sensitive-value case also dropped its trailing --no-<literal> token: the spelling refusal stops parsing before it, so the token no longer exercised anything.
115 lines
4.1 KiB
TypeScript
115 lines
4.1 KiB
TypeScript
import { cliCommand, cliCommands } from "../../../src/cli/commandRegistry.js";
|
|
import { describe, expect, it } from "vitest";
|
|
import { z } from "zod";
|
|
|
|
import { createCli } from "../../../src/cli.js";
|
|
import {
|
|
cliCommandOptionNames,
|
|
createCliInventory,
|
|
} from "../../../scripts/lib/catalog-cli.mjs";
|
|
|
|
type CliInstance = ReturnType<typeof createCli>;
|
|
|
|
const serve = async (
|
|
cli: CliInstance,
|
|
argv: readonly string[],
|
|
): Promise<{ readonly stdout: string; readonly exitCode: number }> => {
|
|
let stdout = "";
|
|
let exitCode = 0;
|
|
await cli.serve([...argv], {
|
|
env: {},
|
|
exit: (code) => {
|
|
exitCode = code;
|
|
},
|
|
stdout: (text) => {
|
|
stdout += text;
|
|
},
|
|
});
|
|
return { stdout, exitCode };
|
|
};
|
|
|
|
const registeredAliases = (
|
|
cli: CliInstance,
|
|
command: string,
|
|
): Record<string, unknown> => {
|
|
const entry = cliCommand(cli, command)?.definition;
|
|
const alias: unknown =
|
|
entry === undefined ? undefined : Reflect.get(entry, "alias");
|
|
return typeof alias === "object" && alias !== null ? { ...alias } : {};
|
|
};
|
|
|
|
describe("CLI option aliases", () => {
|
|
it("rejects an empty primary command inventory", () => {
|
|
expect(() => createCliInventory([])).toThrow(
|
|
"REA exposed no primary CLI commands",
|
|
);
|
|
});
|
|
|
|
it("register only single-character short flags", async () => {
|
|
const cli = createCli({});
|
|
const invalid: string[] = [];
|
|
const undescribed: string[] = [];
|
|
for (const command of createCliInventory(cliCommands(cli)).primary) {
|
|
for (const [option, short] of Object.entries(
|
|
registeredAliases(cli, command),
|
|
))
|
|
if (typeof short !== "string" || short.length !== 1)
|
|
invalid.push(`${command} ${option}: ${String(short)}`);
|
|
// Incur parses `-abc` as stacked one-letter flags, so help must never
|
|
// advertise a multi-letter single-dash spelling.
|
|
const help = await serve(cli, [command, "--help"]);
|
|
expect(help.exitCode, `${command} --help`).toBe(0);
|
|
expect(help.stdout, `${command} --help`).not.toBe("");
|
|
for (const match of help.stdout.matchAll(/(?:^|[\s,])-([a-z][\w-]+)/gmu))
|
|
invalid.push(`${command} help: -${match[1] ?? ""}`);
|
|
const options = /^Options:\n((?: {2}.*\n?)*)/mu.exec(help.stdout)?.[1];
|
|
const entry = cliCommand(cli, command)?.definition;
|
|
expect(entry, `${command} registration`).toBeDefined();
|
|
const schema: unknown = entry && Reflect.get(entry, "options");
|
|
if (schema !== undefined)
|
|
expect(schema, `${command} options schema`).toBeInstanceOf(z.ZodObject);
|
|
if (cliCommandOptionNames(cliCommands(cli), command).length > 0) {
|
|
expect(options, `${command} --help options`).toBeDefined();
|
|
expect(options, `${command} --help option entries`).toMatch(/^ --/mu);
|
|
}
|
|
for (const line of options?.split("\n") ?? [])
|
|
if (/^ {2}--\S+(?: <[^>]+>)?\s*$/u.test(line))
|
|
undescribed.push(`${command} ${line.trim()}`);
|
|
}
|
|
expect(invalid).toEqual([]);
|
|
expect(undescribed).toEqual([]);
|
|
});
|
|
|
|
it("accepts kebab-case long options without registered aliases", async () => {
|
|
const cli = createCli({});
|
|
const result = await serve(cli, [
|
|
"inspect-plist",
|
|
"/nonexistent/REA.app",
|
|
"--relative-path",
|
|
"Contents/Info.plist",
|
|
"--json",
|
|
]);
|
|
expect(result.stdout).not.toContain("Unknown flag");
|
|
expect(result.stdout).toContain("/nonexistent/REA.app");
|
|
});
|
|
|
|
it("refuses the schema key spelling with the documented flag", async () => {
|
|
// argvParser.test.ts covers each token shape (bare, inline value,
|
|
// negation) against the structured flag_spelling failure; this case
|
|
// checks the public CLI projection documented in getting-started/cli.md.
|
|
const result = await serve(createCli({}), [
|
|
"inspect-plist",
|
|
"/nonexistent/REA.app",
|
|
"--relativePath",
|
|
"Contents/Info.plist",
|
|
"--json",
|
|
]);
|
|
expect(result.exitCode).toBe(1);
|
|
expect(JSON.parse(result.stdout)).toEqual({
|
|
code: "PARSE_ERROR",
|
|
message: "Unknown flag: --relativePath. Did you mean --relative-path?",
|
|
retryable: false,
|
|
});
|
|
});
|
|
});
|