fix(cli): reject invalid check numeric flags as usage errors (#7673)

* fix(cli): reject invalid check numeric flags as usage errors (#7672)

--samples, --max-issues, --max-transition-samples and --tolerance fell
back to their defaults on any value they could not parse, and parseInt
read 5abc as 5. A bad value now exits 2 with a usage_error that names the
flag and the value. Absent flags keep their defaults.

* fix(cli): drop added comment to satisfy the comment ratchet
This commit is contained in:
bardrop
2026-11-13 22:00:57 +00:00
committed by GitHub
parent 87429691b1
commit 564a597be2
3 changed files with 87 additions and 10 deletions
+2
View File
@@ -235,6 +235,8 @@ describe("agent-mode contract", () => {
["compare a.html b.html --timeout -1", "--timeout must be a positive integer"],
["grade-compare --for frame.png", "Exactly one of --grades or --luts"],
["check --range 5,1", "Invalid --range"],
["check --samples ten", "Invalid --samples"],
["check --tolerance=-1", "Invalid --tolerance"],
["normalize-audio --target music --tolerance=-1", "--tolerance must be"],
["media-treatment --capabilities --all --capability x", "--all or --capability"],
])("`%s` exits 2 with a usage_error envelope", async (args, message) => {
+60
View File
@@ -940,6 +940,66 @@ it("rejects malformed caption-zone specs instead of silently disabling the gate"
});
});
it.each([
["--samples", "0"],
["--samples", "ten"],
["--samples", "5abc"],
["--samples", "2.5"],
["--max-issues", "-5"],
["--max-transition-samples", "abc"],
["--tolerance", "-1"],
["--tolerance", "px"],
["--tolerance", "2px"],
])("rejects %s %s as a usage error instead of using the default", async (flag, value) => {
const { report } = await runScenario(fakeDriver());
const runPipeline = vi.fn(async () => report);
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
const command = createCheckCommand({
resolveProject: () => PROJECT,
runPipeline,
prepareMedia: vi.fn(async () => []),
withMeta: (value) => value,
});
await expect(
runCommand(command, { rawArgs: ["--json", `${flag}=${value}`] }),
).rejects.toMatchObject({ result: { exitCode: 2, kind: "usage_error" } });
expect(runPipeline).not.toHaveBeenCalled();
const output = JSON.parse(String(log.mock.calls[0]?.[0]));
expect(output.error.code).toBe("usage_error");
expect(output.error.message).toContain(`Invalid ${flag}: "${value}"`);
});
it("passes valid numeric check flags through and keeps defaults for absent ones", async () => {
const { report } = await runScenario(fakeDriver());
const runPipeline = vi.fn(async (_project: ProjectDir, _options: CheckOptions) => report);
vi.spyOn(console, "log").mockImplementation(() => undefined);
const command = createCheckCommand({
resolveProject: () => PROJECT,
runPipeline,
prepareMedia: vi.fn(async () => []),
withMeta: (value) => value,
});
await runCommand(command, {
rawArgs: ["--json", "--samples", "3", "--max-transition-samples", "4", "--tolerance", "0.5"],
});
await runCommand(command, { rawArgs: ["--json", "--tolerance", "0"] });
expect(runPipeline.mock.calls[0]?.[1]).toMatchObject({
samples: 3,
maxTransitionSamples: 4,
maxIssues: DEFAULT_CHECK_OPTIONS.maxIssues,
tolerance: 0.5,
});
expect(runPipeline.mock.calls[1]?.[1]).toMatchObject({
samples: DEFAULT_CHECK_OPTIONS.samples,
maxTransitionSamples: undefined,
tolerance: 0,
});
});
it("flags text whose DOM box overlaps the caption band at the default end seek", async () => {
const collectGeometryCandidates = vi.fn(async (time: number) => [
geometryCandidate({
+25 -10
View File
@@ -477,17 +477,17 @@ function checkReview(): ToolContext["checkReview"] {
}
function parseCheckOptions(args: Record<string, unknown>): CheckOptions {
const maxTransitionSamples = positiveInteger(args["max-transition-samples"], 0);
const maxTransitionSamples = positiveInteger(args, "max-transition-samples", 0);
return {
samples: positiveInteger(args.samples, DEFAULT_CHECK_OPTIONS.samples),
samples: positiveInteger(args, "samples", DEFAULT_CHECK_OPTIONS.samples),
range: parseCheckRange(args.range),
components: typeof args.components === "string" ? [args.components] : undefined,
at: parseAt(args.at),
atTransitions: args["at-transitions"] === true,
maxTransitionSamples: maxTransitionSamples > 0 ? maxTransitionSamples : undefined,
maxIssues: positiveInteger(args["max-issues"], DEFAULT_CHECK_OPTIONS.maxIssues),
maxIssues: positiveInteger(args, "max-issues", DEFAULT_CHECK_OPTIONS.maxIssues),
collapseStatic: args["collapse-static"] !== false,
tolerance: nonNegativeNumber(args.tolerance, DEFAULT_CHECK_OPTIONS.tolerance),
tolerance: nonNegativeNumber(args, "tolerance", DEFAULT_CHECK_OPTIONS.tolerance),
timeout: parseTimeoutMs(args.timeout, DEFAULT_CHECK_OPTIONS.timeout, 500),
budget: parseBudget(args.budget),
cache: args.cache !== false,
@@ -718,14 +718,29 @@ function captionZoneError(): Error {
);
}
function positiveInteger(value: unknown, fallback: number): number {
const parsed = parseInt(String(value ?? ""), 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
function positiveInteger(args: Record<string, unknown>, flag: string, fallback: number): number {
const value = args[flag];
if (value === undefined) return fallback;
const parsed = wholeStringNumber(value);
if (!Number.isInteger(parsed) || parsed < 1)
throw new CliUsageError(`Invalid --${flag}: ${JSON.stringify(value)}; use a positive integer`);
return parsed;
}
function nonNegativeNumber(value: unknown, fallback: number): number {
const parsed = parseFloat(String(value ?? ""));
return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback;
function nonNegativeNumber(args: Record<string, unknown>, flag: string, fallback: number): number {
const value = args[flag];
if (value === undefined) return fallback;
const parsed = wholeStringNumber(value);
if (!Number.isFinite(parsed) || parsed < 0)
throw new CliUsageError(
`Invalid --${flag}: ${JSON.stringify(value)}; use a non-negative number`,
);
return parsed;
}
function wholeStringNumber(value: unknown): number {
const text = String(value).trim();
return text === "" ? NaN : Number(text);
}
function browserSkippedLine(report: CheckReport): string {