Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import type { CliCommand } from './registry.js';
import { executeCommand, prepareCommandArgs } from './execution.js';
import { coerceAndValidateArgs, executeCommand, prepareCommandArgs } from './execution.js';
import { ArgumentError, TimeoutError, toEnvelope } from './errors.js';
import { cli, Strategy } from './registry.js';
import { withTimeoutMs } from './runtime.js';
Expand All @@ -12,6 +12,22 @@ import * as capRouting from './capabilityRouting.js';
import * as daemonClient from './browser/daemon-client.js';
import { BrowserCommandError } from './browser/daemon-client.js';

describe('coerceAndValidateArgs', () => {
it('rejects fractional values for integer arguments', () => {
const args = [{ name: 'limit', type: 'int' as const, help: 'Result limit' }];

expect(() => coerceAndValidateArgs(args, { limit: '1.5' })).toThrow(ArgumentError);
});

it('rejects non-finite values for numeric arguments', () => {
const integerArgs = [{ name: 'limit', type: 'int' as const, help: 'Result limit' }];
const numberArgs = [{ name: 'threshold', type: 'number' as const, help: 'Threshold' }];

expect(() => coerceAndValidateArgs(integerArgs, { limit: 'Infinity' })).toThrow(ArgumentError);
expect(() => coerceAndValidateArgs(numberArgs, { threshold: '-Infinity' })).toThrow(ArgumentError);
});
});

describe('executeCommand — non-browser timeout', () => {
it('applies the user --timeout arg as the ceiling for non-browser commands', async () => {
const runWithTimeoutSpy = vi.spyOn(runtime, 'runWithTimeout');
Expand Down
5 changes: 4 additions & 1 deletion src/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ export function coerceAndValidateArgs(cmdArgs: Arg[], kwargs: CommandArgs): Comm
if (val !== undefined && val !== null) {
if (argDef.type === 'int' || argDef.type === 'number') {
const num = Number(val);
if (Number.isNaN(num)) {
if (!Number.isFinite(num)) {
throw new ArgumentError(`Argument "${argDef.name}" must be a valid number. Received: "${val}"`);
}
if (argDef.type === 'int' && !Number.isInteger(num)) {
throw new ArgumentError(`Argument "${argDef.name}" must be a valid integer. Received: "${val}"`);
}
result[argDef.name] = num;
} else if (argDef.type === 'boolean' || argDef.type === 'bool') {
if (typeof val === 'string') {
Expand Down