From 564a597be2d311916868a79acf24e21f4282fc3c Mon Sep 17 00:00:00 2001 From: bardrop Date: Fri, 13 Nov 2026 22:00:57 +0000 Subject: [PATCH] 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 --- packages/cli/src/cli.agentMode.test.ts | 2 + packages/cli/src/commands/check.test.ts | 60 +++++++++++++++++++++++++ packages/cli/src/commands/check.ts | 35 ++++++++++----- 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/cli.agentMode.test.ts b/packages/cli/src/cli.agentMode.test.ts index 25c832e4ee..38a18073e1 100644 --- a/packages/cli/src/cli.agentMode.test.ts +++ b/packages/cli/src/cli.agentMode.test.ts @@ -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) => { diff --git a/packages/cli/src/commands/check.test.ts b/packages/cli/src/commands/check.test.ts index 01c2c0b26a..afc7e7ee66 100644 --- a/packages/cli/src/commands/check.test.ts +++ b/packages/cli/src/commands/check.test.ts @@ -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({ diff --git a/packages/cli/src/commands/check.ts b/packages/cli/src/commands/check.ts index 3f4cc6eb23..8ee2a67593 100644 --- a/packages/cli/src/commands/check.ts +++ b/packages/cli/src/commands/check.ts @@ -477,17 +477,17 @@ function checkReview(): ToolContext["checkReview"] { } function parseCheckOptions(args: Record): 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, 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, 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 {