Skip to content

Commit 23ed792

Browse files
Mossakaclaude
andcommitted
test: add parseAgentTimeout tests to fix coverage regression
Extract inline timeout validation into exported parseAgentTimeout() function and add 7 unit tests covering valid integers, zero, negative, non-numeric, empty string, and large values. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0f8d8cc commit 23ed792

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/cli.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command } from 'commander';
2-
import { parseEnvironmentVariables, parseDomains, parseDomainsFile, escapeShellArg, joinShellArgs, parseVolumeMounts, isValidIPv4, isValidIPv6, parseDnsServers, validateAgentImage, isAgentImagePreset, AGENT_IMAGE_PRESETS, processAgentImageOption, processLocalhostKeyword, validateSkipPullWithBuildLocal, validateAllowHostPorts, validateFormat, validateApiProxyConfig, buildRateLimitConfig, validateRateLimitFlags } from './cli';
2+
import { parseEnvironmentVariables, parseDomains, parseDomainsFile, escapeShellArg, joinShellArgs, parseVolumeMounts, isValidIPv4, isValidIPv6, parseDnsServers, validateAgentImage, isAgentImagePreset, AGENT_IMAGE_PRESETS, processAgentImageOption, processLocalhostKeyword, validateSkipPullWithBuildLocal, validateAllowHostPorts, validateFormat, validateApiProxyConfig, buildRateLimitConfig, validateRateLimitFlags, parseAgentTimeout } from './cli';
33
import { redactSecrets } from './redact-secrets';
44
import * as fs from 'fs';
55
import * as path from 'path';
@@ -1539,4 +1539,41 @@ describe('cli', () => {
15391539
expect(result.error).toBeUndefined();
15401540
});
15411541
});
1542+
1543+
describe('parseAgentTimeout', () => {
1544+
it('should parse a valid positive integer', () => {
1545+
const result = parseAgentTimeout('30');
1546+
expect(result).toEqual({ minutes: 30 });
1547+
});
1548+
1549+
it('should parse single minute timeout', () => {
1550+
const result = parseAgentTimeout('1');
1551+
expect(result).toEqual({ minutes: 1 });
1552+
});
1553+
1554+
it('should return error for zero', () => {
1555+
const result = parseAgentTimeout('0');
1556+
expect(result).toEqual({ error: '--agent-timeout must be a positive integer (minutes)' });
1557+
});
1558+
1559+
it('should return error for negative value', () => {
1560+
const result = parseAgentTimeout('-5');
1561+
expect(result).toEqual({ error: '--agent-timeout must be a positive integer (minutes)' });
1562+
});
1563+
1564+
it('should return error for non-numeric string', () => {
1565+
const result = parseAgentTimeout('abc');
1566+
expect(result).toEqual({ error: '--agent-timeout must be a positive integer (minutes)' });
1567+
});
1568+
1569+
it('should return error for empty string', () => {
1570+
const result = parseAgentTimeout('');
1571+
expect(result).toEqual({ error: '--agent-timeout must be a positive integer (minutes)' });
1572+
});
1573+
1574+
it('should parse large timeout values', () => {
1575+
const result = parseAgentTimeout('1440');
1576+
expect(result).toEqual({ minutes: 1440 });
1577+
});
1578+
});
15421579
});

src/cli.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,19 @@ export function validateAllowHostPorts(
422422
return { valid: true };
423423
}
424424

425+
/**
426+
* Parses and validates the --agent-timeout option
427+
* @param value - The raw string value from the CLI option
428+
* @returns The parsed timeout in minutes, or an error
429+
*/
430+
export function parseAgentTimeout(value: string): { minutes: number } | { error: string } {
431+
const timeoutMinutes = parseInt(value, 10);
432+
if (isNaN(timeoutMinutes) || timeoutMinutes <= 0) {
433+
return { error: '--agent-timeout must be a positive integer (minutes)' };
434+
}
435+
return { minutes: timeoutMinutes };
436+
}
437+
425438
/**
426439
* Parses and validates DNS servers from a comma-separated string
427440
* @param input - Comma-separated DNS server string (e.g., "8.8.8.8,1.1.1.1")
@@ -1114,13 +1127,13 @@ program
11141127

11151128
// Parse and validate --agent-timeout
11161129
if (options.agentTimeout !== undefined) {
1117-
const timeoutMinutes = parseInt(options.agentTimeout, 10);
1118-
if (isNaN(timeoutMinutes) || timeoutMinutes <= 0) {
1119-
logger.error('--agent-timeout must be a positive integer (minutes)');
1130+
const result = parseAgentTimeout(options.agentTimeout);
1131+
if ('error' in result) {
1132+
logger.error(result.error);
11201133
process.exit(1);
11211134
}
1122-
config.agentTimeout = timeoutMinutes;
1123-
logger.info(`Agent timeout set to ${timeoutMinutes} minutes`);
1135+
config.agentTimeout = result.minutes;
1136+
logger.info(`Agent timeout set to ${result.minutes} minutes`);
11241137
}
11251138

11261139
// Build rate limit config when API proxy is enabled

0 commit comments

Comments
 (0)