Files
reverse-engineering-anything/tests/boundary/cli/argvParser.test.ts
morluto c4fa5b475c fix(cli)!: accept only the documented kebab-case option spelling (#14204)
* 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.
2026-11-23 19:36:23 -05:00

160 lines
4.8 KiB
TypeScript

import { z } from "zod";
import { describe, expect, it } from "vitest";
import { parseCliArgv } from "../../../src/cli/argvParser.js";
const schemas = {
args: z.object({ path: z.string(), rest: z.array(z.string()).optional() }),
options: z.object({
blockSize: z.number().default(4096),
global: z.boolean().default(true),
yes: z.boolean().optional(),
column: z.array(z.string()).optional(),
mode: z.enum(["a", "b"]).optional(),
}),
alias: { yes: "y", mode: "m" },
};
const bound = (
argv: readonly string[],
selected: Parameters<typeof parseCliArgv>[1] = schemas,
) => {
const result = parseCliArgv(argv, selected);
if (!result.ok) throw new Error(result.error.message);
return result.value;
};
const failure = (argv: readonly string[]) => {
const result = parseCliArgv(argv, schemas);
if (result.ok) throw new Error("parse succeeded");
return result.error;
};
describe("parseCliArgv", () => {
it("binds kebab-case spellings, values and negations", () => {
expect(
bound([
"file",
"--block-size",
"256",
"--no-global",
"--column=a",
"--column",
"b",
"x",
"y",
]),
).toEqual({
args: { path: "file", rest: ["x", "y"] },
options: { blockSize: 256, global: false, column: ["a", "b"] },
});
expect(bound(["file", "--block-size=512", "--global"]).options).toEqual({
blockSize: 512,
global: true,
});
});
it.each([
[["file", "--blockSize", "512"], "--blockSize", "--block-size"],
[["file", "--blockSize=512"], "--blockSize", "--block-size"],
[["file", "--no-blockSize"], "--no-blockSize", "--no-block-size"],
])(
"refuses the schema key spelling %j and names the documented flag",
(argv, flag, documented) => {
expect(failure(argv)).toEqual({
kind: "flag_spelling",
message: `Unknown flag: ${flag}. Did you mean ${documented}?`,
flag,
documented,
});
},
);
it("stacks short aliases when all but the last are booleans", () => {
expect(bound(["file", "-ym", "a"]).options).toMatchObject({
yes: true,
mode: "a",
});
expect(failure(["file", "-my"])).toEqual({
kind: "parse",
message: "Non-boolean flag -m must be last in a stacked alias",
});
});
it.each([
[["file", "--nope"], "Unknown flag: --nope"],
[["file", "--nope=1"], "Unknown flag: --nope"],
[["file", "--no-nope"], "Unknown flag: --no-nope"],
[["file", "--no-blockSize=1"], "Unknown flag: --no-blockSize=1"],
[["file", "-z"], "Unknown flag: -z"],
[["file", "--"], "Unknown flag: --"],
[["file", "--block-size"], "Missing value for flag: --block-size"],
[["file", "-m"], "Missing value for flag: -m"],
])("refuses %j with the message REA prints", (argv, message) => {
expect(failure(argv)).toEqual({ kind: "parse", message });
});
it("reports schema failures as field errors", () => {
// Positionals are validated first, so the missing path is reported alone.
expect(failure(["--block-size", "abc", "--mode", "c"])).toMatchObject({
kind: "validation",
message: "Invalid input: expected string, received undefined",
fieldErrors: [
{
code: "invalid_type",
missing: true,
path: "path",
expected: "string",
received: "",
message: "Invalid input: expected string, received undefined",
},
],
});
// Captured from incur 0.6.1's Parser.parse with the same schema and argv.
expect(
failure(["file", "--block-size", "abc", "--mode", "c"]),
).toMatchObject({
kind: "validation",
message:
'Invalid input: expected number, received NaN; Invalid option: expected one of "a"|"b"',
fieldErrors: [
{
code: "invalid_type",
missing: false,
path: "blockSize",
expected: "number",
received: "NaN",
message: "Invalid input: expected number, received NaN",
},
{
code: "invalid_value",
missing: false,
path: "mode",
expected: "",
received: "",
message: 'Invalid option: expected one of "a"|"b"',
},
],
});
});
it("adds one per occurrence of a count option, stacked or spelled out", () => {
const counted = {
options: z.object({
verbosity: z.number().default(0).meta({ count: true }),
yes: z.boolean().optional(),
}),
alias: { verbosity: "v", yes: "y" },
};
expect(bound(["-vyv", "--verbosity"], counted).options).toEqual({
verbosity: 3,
yes: true,
});
});
it("reads boolean text as true only for 'true'", () => {
expect(bound(["file", "--yes=maybe"]).options.yes).toBe(false);
expect(bound(["file", "--yes=true"]).options.yes).toBe(true);
});
});