23f7624596
ADR-166 MCP Bridge Security Lock / Static-source security lock (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / Compose default binds loopback + Mongo has auth (push) Failing after 2s
CodeQL Advanced / Analyze (rust) (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / plugin-agent-federation bindHost default (push) Failing after 1s
ADR-166 MCP Bridge Security Lock / Runtime behavior — 401 + terminal gate + fail-closed (push) Failing after 4s
business-pods-smoke / smoke (push) Failing after 1s
all-plugins-smoke / smoke-all (push) Failing after 2s
CI/CD Pipeline / Security & Code Quality (push) Failing after 1s
CI/CD Pipeline / Test Suite (ubuntu-latest) (push) Failing after 1s
CI/CD Pipeline / Build & Package (macos-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (ubuntu-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (windows-latest) (push) Has been skipped
CI/CD Pipeline / Documentation & Examples (push) Failing after 1s
Clone Tracker (14-day rolling) / Snapshot clones for ruflo ecosystem (push) Failing after 1s
CodeQL Advanced / Analyze (actions) (push) Failing after 1s
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 1s
federation-peer-rust / stable-noop (push) Failing after 1s
metaharness-ci / score (push) Failing after 1s
metaharness-ci / router-compat (push) Failing after 0s
metaharness-ci / similarity-tests (push) Failing after 0s
no-agentbbs-smoke / smoke-without-agentbbs (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (windows-latest) (push) Has been skipped
codex-integration-audit / Codex integration audit (push) Failing after 1s
helpers-manifest-guard / guard (push) Failing after 1s
🔗 Cross-Agent Integration Tests / 🤝 Agent Coordination Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🧠 Memory Sharing Integration (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🛡️ Fault Tolerance Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / ⚡ Performance Integration Tests (push) Has been skipped
metaharness-ci / mcp-scan (push) Failing after 1s
metaharness-ci / eject-dryrun (push) Failing after 1s
metaharness-ci / metaharness-real-data (push) Failing after 0s
no-cli-optdep-bloat-2561 / guard (push) Failing after 1s
no-metaharness-smoke / smoke-without-metaharness (push) Failing after 1s
no-phantom-agentic-flow-subpath / guard (push) Failing after 1s
🔄 Automated Rollback Manager / 🚨 Failure Detection (push) Failing after 1s
V3 CI/CD Pipeline / Plugin hooks smoke / ubuntu-latest / Node 22 (push) Failing after 1s
V3 CI/CD Pipeline / ruflo-graph-intelligence build + test smoke (#2044, ADR-123) (push) Failing after 1s
CVE Audit Gate / Audit root (critical-blocking) (push) Failing after 2s
cost-tracker-smoke / smoke (push) Failing after 3s
oia-audit-weekly / audit (push) Failing after 2s
ruflo-agent-smoke / ruflo-agent structural smoke (push) Failing after 1s
📊 Status Badges Update / 📊 Update Status Badges (push) Failing after 1s
V3 CI/CD Pipeline / Static regression guards (#2267 YAML + (push) Failing after 1s
V3 CI/CD Pipeline / Test V3 Packages (push) Failing after 0s
V3 CI/CD Pipeline / agent_execute provider routing smoke (#2042) (push) Failing after 0s
CVE Audit Gate / Audit v3 (critical-blocking) (push) Failing after 1s
federation-peer-rust / stable-native (push) Failing after 2s
🔗 Cross-Agent Integration Tests / 🚀 Integration Test Setup (push) Failing after 2s
neural-trader-smoke / runtime-smoke (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (macos-latest) (push) Has been skipped
V3 CI/CD Pipeline / Build V3 (ubuntu-latest) (push) Has been skipped
V3 CI/CD Pipeline / Type Check V3 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 24 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 22 (push) Failing after 2s
V3 CI/CD Pipeline / browser rvf create flag smoke (#2015) (push) Failing after 0s
V3 CI/CD Pipeline / Dependency review (#2046) (push) Has been skipped
V3 CI/CD Pipeline / Supply-chain audit (#2046) (push) Failing after 0s
V3 CI/CD Pipeline / witness marker drift smoke (#2021) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader portfolio CG smoke (#2068, ADR-126 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader backtest signing smoke (#2068, ADR-126 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / kg-extract type-import classification smoke (#2049) (push) Failing after 0s
V3 CI/CD Pipeline / witness verify precondition smoke (#1880) (push) Failing after 2s
V3 CI/CD Pipeline / neural-trader pipeline risk-gate smoke (#2068, ADR-126 Phase 5) (push) Failing after 0s
V3 CI/CD Pipeline / neural-trader feature attribution smoke (#2068, ADR-126 Phase 6) (push) Failing after 0s
V3 CI/CD Pipeline / plugin-registry signature verification smoke (#1922, CWE-347) (push) Failing after 4s
V3 CI/CD Pipeline / memory stats legacy-DB smoke (#2120) (push) Failing after 4s
V3 CI/CD Pipeline / github deprecated actions smoke (#2089, ADR-127 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / graph query + pathfinder smoke (ADR-130 P2+P5) (push) Has been skipped
V3 CI/CD Pipeline / graph trajectory hooks smoke (ADR-130 P3) (push) Has been skipped
V3 CI/CD Pipeline / graph plugin adapter smoke (ADR-130 P4) (push) Has been skipped
V3 CI/CD Pipeline / graph benchmark (ADR-130 P6) (push) Has been skipped
V3 CI/CD Pipeline / statusline generator delegation smoke (#2195) (push) Failing after 1s
V3 CI/CD Pipeline / wizard init regression guard (#2206 (push) Failing after 1s
V3 CI/CD Pipeline / memory no-stray-db smoke (ADR-125 P7) (push) Failing after 1s
V3 CI/CD Pipeline / github-safe injection smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github actions pin smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github attribution opt-in smoke (#2089, ADR-127 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / pre-bash hook safety smoke (#2017) (push) Failing after 1s
V3 CI/CD Pipeline / Memory import smoke / ubuntu-latest (push) Failing after 0s
V3 CI/CD Pipeline / MCP protocol smoke / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / ruvllm WASM auto-init smoke (#2086) (push) Failing after 4s
V3 CI/CD Pipeline / MCP paired-tool round-trip smoke (#1889) (push) Failing after 1s
V3 CI/CD Pipeline / Plugin package install-safety (#1902/#1903/#1904) (push) Failing after 1s
V3 CI/CD Pipeline / Tool description discoverability (ADR-112) (push) Failing after 3s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (22) (push) Failing after 1s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (24) (push) Failing after 1s
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Vector-index dimension audit (#1947) (push) Failing after 0s
V3 CI/CD Pipeline / Hook-command install safety (#1921) (push) Failing after 1s
V3 CI/CD Pipeline / ToolOutputGuardrail smoke (ADR-131, (push) Failing after 1s
V3 CI/CD Pipeline / init-bundle invariants smoke (#2095, ADR-128 Phase 5) (push) Failing after 1s
V3 CI/CD Pipeline / wasm provider bridge smoke (ADR-129 P1) (push) Failing after 2s
V3 CI/CD Pipeline / wasm gallery CRUD smoke (ADR-129 P3) (push) Failing after 1s
V3 CI/CD Pipeline / wasm plugin bridge smoke (ADR-129 P4) (push) Failing after 0s
V3 CI/CD Pipeline / wasm compose smoke (ADR-129 P2) (push) Failing after 4s
V3 CI/CD Pipeline / graph schema smoke (ADR-130 P1) (push) Failing after 0s
Validate Marketplace / validate (push) Failing after 1s
🔍 Verification Pipeline / 🚀 Setup Verification (push) Failing after 1s
🔍 Verification Pipeline / 🛡️ Security Verification (push) Has been skipped
🔍 Verification Pipeline / 📝 Code Quality (push) Has been skipped
🔍 Verification Pipeline / 🧪 Test Verification (${{ matrix.os }}, Node ${{ matrix.node }}) (push) Has been skipped
🔍 Verification Pipeline / 🏗️ Build Verification (push) Has been skipped
🔍 Verification Pipeline / 📚 Documentation Verification (push) Has been skipped
CVE Audit Gate / High-severity report (warn only) (push) Has been cancelled
🔄 Automated Rollback Manager / 🔄 Execute Rollback (push) Has been cancelled
🔄 Automated Rollback Manager / ✅ Post-Rollback Verification (push) Has been cancelled
🔄 Automated Rollback Manager / 📊 Rollback Monitoring (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / windows-latest (push) Has been cancelled
🔄 Automated Rollback Manager / ⏳ Manual Rollback Approval (push) Has been cancelled
V3 CI/CD Pipeline / MCP protocol smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Memory import smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / ubuntu-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Publish to npm (alpha) (push) Has been cancelled
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / macos-latest / Node 22 (push) Has been cancelled
V3 CI/CD Pipeline / Plugin hooks smoke / macos-latest / Node 22 (push) Has been cancelled
CI/CD Pipeline / Deploy & Release (push) Has been cancelled
CI/CD Pipeline / CI Status (push) Has been cancelled
🔗 Cross-Agent Integration Tests / 📊 Integration Test Report (push) Has been cancelled
🔄 Automated Rollback Manager / 🔍 Pre-Rollback Validation (push) Has been cancelled
🔍 Verification Pipeline / ⚡ Performance Verification (push) Has been cancelled
🔍 Verification Pipeline / 📊 Verification Report (push) Has been cancelled
548 lines
20 KiB
TypeScript
548 lines
20 KiB
TypeScript
/**
|
|
* CommandParser Standalone Tests
|
|
*
|
|
* Tests the parser module in isolation, covering:
|
|
* - Flag parsing: long, short, combined, equals, negation, boolean, value
|
|
* - Value coercion: "true"/"false"/number/string
|
|
* - Command + subcommand resolution (including nested 3-level)
|
|
* - End-of-flags marker (--)
|
|
* - Alias resolution from global + command options
|
|
* - Default application
|
|
* - Flag validation: required, choices, custom validator, unknown
|
|
* - getAllCommands deduplication
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { CommandParser } from '../src/parser.js';
|
|
import type { Command } from '../src/types.js';
|
|
|
|
describe('CommandParser', () => {
|
|
let parser: CommandParser;
|
|
|
|
beforeEach(() => {
|
|
parser = new CommandParser({ allowUnknownFlags: true });
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Basic flag parsing
|
|
// -------------------------------------------------------------------------
|
|
describe('flag parsing', () => {
|
|
it('should parse long boolean flag', () => {
|
|
const result = parser.parse(['--verbose']);
|
|
expect(result.flags.verbose).toBe(true);
|
|
});
|
|
|
|
it('should parse --flag=value syntax', () => {
|
|
const result = parser.parse(['--output=file.txt']);
|
|
expect(result.flags.output).toBe('file.txt');
|
|
});
|
|
|
|
it('should parse --flag value syntax (non-boolean)', () => {
|
|
// Register a command so the positional "hello" doesn't get treated as command
|
|
const result = parser.parse(['--name', 'hello']);
|
|
expect(result.flags.name).toBe('hello');
|
|
});
|
|
|
|
it('should parse --no-flag as boolean false', () => {
|
|
const result = parser.parse(['--no-color']);
|
|
expect(result.flags.color).toBe(false);
|
|
});
|
|
|
|
it('should parse short flag -V via global alias', () => {
|
|
const result = parser.parse(['-V']);
|
|
expect(result.flags.version).toBe(true);
|
|
});
|
|
|
|
it('should parse combined short flags like -abc', () => {
|
|
const result = parser.parse(['-abc']);
|
|
expect(result.flags.a).toBe(true);
|
|
expect(result.flags.b).toBe(true);
|
|
// Note: 'c' is aliased to 'config' by global options, so the key is 'config'
|
|
expect(result.flags.config).toBe(true);
|
|
});
|
|
|
|
it('should stop parsing flags after "--"', () => {
|
|
const result = parser.parse(['--verbose', '--', '--not-a-flag']);
|
|
expect(result.flags.verbose).toBe(true);
|
|
expect(result.flags.notAFlag).toBeUndefined();
|
|
expect(result.positional).toContain('--not-a-flag');
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Value coercion via parseValue
|
|
// -------------------------------------------------------------------------
|
|
describe('value coercion', () => {
|
|
it('should coerce "true" to boolean true', () => {
|
|
const result = parser.parse(['--flag=true']);
|
|
expect(result.flags.flag).toBe(true);
|
|
});
|
|
|
|
it('should coerce "false" to boolean false', () => {
|
|
const result = parser.parse(['--flag=false']);
|
|
expect(result.flags.flag).toBe(false);
|
|
});
|
|
|
|
it('should coerce numeric strings to numbers', () => {
|
|
const result = parser.parse(['--count=42']);
|
|
expect(result.flags.count).toBe(42);
|
|
});
|
|
|
|
it('should coerce decimal strings to numbers', () => {
|
|
const result = parser.parse(['--ratio=3.14']);
|
|
expect(result.flags.ratio).toBe(3.14);
|
|
});
|
|
|
|
it('should keep non-numeric strings as strings', () => {
|
|
const result = parser.parse(['--label=hello']);
|
|
expect(result.flags.label).toBe('hello');
|
|
});
|
|
|
|
it('should coerce "TRUE" case-insensitively', () => {
|
|
const result = parser.parse(['--flag=TRUE']);
|
|
expect(result.flags.flag).toBe(true);
|
|
});
|
|
|
|
it('should coerce "FALSE" case-insensitively', () => {
|
|
const result = parser.parse(['--flag=FALSE']);
|
|
expect(result.flags.flag).toBe(false);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// normalizeKey (kebab-case to camelCase)
|
|
// -------------------------------------------------------------------------
|
|
describe('key normalization', () => {
|
|
it('should convert kebab-case keys to camelCase', () => {
|
|
const result = parser.parse(['--some-long-flag=yes']);
|
|
expect(result.flags.someLongFlag).toBe('yes');
|
|
});
|
|
|
|
it('should leave simple keys unchanged', () => {
|
|
const result = parser.parse(['--simple=val']);
|
|
expect(result.flags.simple).toBe('val');
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Command & subcommand resolution
|
|
// -------------------------------------------------------------------------
|
|
describe('command resolution', () => {
|
|
it('should detect registered command', () => {
|
|
const cmd: Command = { name: 'agent', description: 'Agent mgmt' };
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['agent']);
|
|
expect(result.command).toEqual(['agent']);
|
|
});
|
|
|
|
it('should detect subcommand', () => {
|
|
const sub: Command = { name: 'spawn', description: 'Spawn agent' };
|
|
const cmd: Command = { name: 'agent', description: 'Agent mgmt', subcommands: [sub] };
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['agent', 'spawn']);
|
|
expect(result.command).toEqual(['agent', 'spawn']);
|
|
});
|
|
|
|
it('should detect nested subcommand (3 levels)', () => {
|
|
const deep: Command = { name: 'run', description: 'Run it' };
|
|
const mid: Command = { name: 'worker', description: 'Worker', subcommands: [deep] };
|
|
const top: Command = { name: 'hooks', description: 'Hooks', subcommands: [mid] };
|
|
parser.registerCommand(top);
|
|
|
|
const result = parser.parse(['hooks', 'worker', 'run']);
|
|
expect(result.command).toEqual(['hooks', 'worker', 'run']);
|
|
});
|
|
|
|
it('should resolve subcommand by alias', () => {
|
|
const sub: Command = { name: 'list', aliases: ['ls'], description: 'List items' };
|
|
const cmd: Command = { name: 'agent', description: 'Agent mgmt', subcommands: [sub] };
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['agent', 'ls']);
|
|
expect(result.command).toEqual(['agent', 'ls']);
|
|
});
|
|
|
|
it('should treat unregistered words as positional args', () => {
|
|
const result = parser.parse(['unknown', 'thing']);
|
|
expect(result.command).toEqual([]);
|
|
expect(result.positional).toEqual(['unknown', 'thing']);
|
|
});
|
|
|
|
it('should register command aliases', () => {
|
|
const cmd: Command = { name: 'config', aliases: ['cfg'], description: 'Config' };
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['cfg']);
|
|
expect(result.command).toEqual(['cfg']);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Positional arguments
|
|
// -------------------------------------------------------------------------
|
|
describe('positional arguments', () => {
|
|
it('should collect positional args after command', () => {
|
|
const cmd: Command = { name: 'task', description: 'Task mgmt' };
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['task', 'arg1', 'arg2']);
|
|
expect(result.positional).toEqual(['arg1', 'arg2']);
|
|
expect(result.flags._).toEqual(['arg1', 'arg2']);
|
|
});
|
|
|
|
it('should collect args after "--" as positional', () => {
|
|
const result = parser.parse(['--', 'a', 'b']);
|
|
expect(result.positional).toEqual(['a', 'b']);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Defaults
|
|
// -------------------------------------------------------------------------
|
|
describe('defaults', () => {
|
|
it('should apply global option defaults', () => {
|
|
const result = parser.parse([]);
|
|
// Global defaults from initializeGlobalOptions
|
|
expect(result.flags.help).toBe(false);
|
|
expect(result.flags.version).toBe(false);
|
|
expect(result.flags.verbose).toBe(false);
|
|
expect(result.flags.format).toBe('text');
|
|
expect(result.flags.interactive).toBe(true);
|
|
});
|
|
|
|
it('should not overwrite explicitly set flags with defaults', () => {
|
|
const result = parser.parse(['--verbose']);
|
|
expect(result.flags.verbose).toBe(true);
|
|
});
|
|
|
|
it('should apply custom defaults from options', () => {
|
|
const p = new CommandParser({
|
|
allowUnknownFlags: true,
|
|
defaults: { myFlag: 'default-value' },
|
|
});
|
|
const result = p.parse([]);
|
|
expect(result.flags.myFlag).toBe('default-value');
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Alias resolution from command options
|
|
// -------------------------------------------------------------------------
|
|
describe('alias resolution', () => {
|
|
it('should resolve short flags from command options', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [{ name: 'debug', short: 'd', type: 'boolean', description: 'Debug' }],
|
|
};
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['test', '-d']);
|
|
expect(result.flags.debug).toBe(true);
|
|
});
|
|
|
|
it('should resolve short flags from subcommand options', () => {
|
|
const sub: Command = {
|
|
name: 'run',
|
|
description: 'Run',
|
|
options: [{ name: 'timeout', short: 't', type: 'string', description: 'Timeout' }],
|
|
};
|
|
const cmd: Command = { name: 'task', description: 'Task', subcommands: [sub] };
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['task', 'run', '-t', '5000']);
|
|
expect(result.flags.timeout).toBe(5000);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// validateFlags
|
|
// -------------------------------------------------------------------------
|
|
describe('validateFlags', () => {
|
|
it('should return empty array for valid flags', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [{ name: 'name', type: 'string', description: 'Name' }],
|
|
};
|
|
const errors = parser.validateFlags({ _: [], name: 'hello' }, cmd);
|
|
expect(errors).toEqual([]);
|
|
});
|
|
|
|
it('should report missing required flags', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [
|
|
{ name: 'name', type: 'string', required: true, description: 'Name' },
|
|
],
|
|
};
|
|
const errors = parser.validateFlags({ _: [] }, cmd);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toContain('Required option missing');
|
|
expect(errors[0]).toContain('--name');
|
|
});
|
|
|
|
it('should report invalid choice values', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [
|
|
{ name: 'output-mode', type: 'string', choices: ['json', 'text'], description: 'Output mode' },
|
|
],
|
|
};
|
|
const errors = parser.validateFlags({ _: [], outputMode: 'xml' }, cmd);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toContain('Invalid value');
|
|
expect(errors[0]).toContain('xml');
|
|
});
|
|
|
|
it('should accept valid choice values', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [
|
|
{ name: 'output-mode', type: 'string', choices: ['json', 'text'], description: 'Output mode' },
|
|
],
|
|
};
|
|
const errors = parser.validateFlags({ _: [], outputMode: 'json' }, cmd);
|
|
expect(errors).toEqual([]);
|
|
});
|
|
|
|
it('should run custom validator returning string error', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [
|
|
{
|
|
name: 'port',
|
|
type: 'string',
|
|
description: 'Port',
|
|
validate: (v) => (Number(v) > 0 ? true : 'Port must be positive'),
|
|
},
|
|
],
|
|
};
|
|
const errors = parser.validateFlags({ _: [], port: -1 }, cmd);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toBe('Port must be positive');
|
|
});
|
|
|
|
it('should run custom validator returning false', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [
|
|
{
|
|
name: 'count',
|
|
type: 'string',
|
|
description: 'Count',
|
|
validate: () => false,
|
|
},
|
|
],
|
|
};
|
|
const errors = parser.validateFlags({ _: [], count: 'x' }, cmd);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toContain('Invalid value for --count');
|
|
});
|
|
|
|
it('should report unknown flags when allowUnknownFlags is false', () => {
|
|
const strictParser = new CommandParser({ allowUnknownFlags: false });
|
|
const errors = strictParser.validateFlags({ _: [], unknown: true });
|
|
expect(errors.length).toBeGreaterThan(0);
|
|
expect(errors.some(e => e.includes('Unknown option'))).toBe(true);
|
|
});
|
|
|
|
it('should not report unknown flags when allowUnknownFlags is true', () => {
|
|
// Default parser allows unknown flags
|
|
const errors = parser.validateFlags({ _: [], unknown: true });
|
|
expect(errors).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// getAllCommands
|
|
// -------------------------------------------------------------------------
|
|
describe('getAllCommands', () => {
|
|
it('should return unique commands (no duplicates from aliases)', () => {
|
|
const cmd: Command = { name: 'config', aliases: ['cfg', 'conf'], description: 'Config' };
|
|
parser.registerCommand(cmd);
|
|
|
|
const all = parser.getAllCommands();
|
|
const configCmds = all.filter(c => c.name === 'config');
|
|
expect(configCmds.length).toBe(1);
|
|
});
|
|
|
|
it('should return all registered commands', () => {
|
|
parser.registerCommand({ name: 'agent', description: 'Agent' });
|
|
parser.registerCommand({ name: 'swarm', description: 'Swarm' });
|
|
parser.registerCommand({ name: 'memory', description: 'Memory' });
|
|
|
|
const all = parser.getAllCommands();
|
|
expect(all.length).toBe(3);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// getCommand
|
|
// -------------------------------------------------------------------------
|
|
describe('getCommand', () => {
|
|
it('should return command by name', () => {
|
|
const cmd: Command = { name: 'agent', description: 'Agent' };
|
|
parser.registerCommand(cmd);
|
|
|
|
expect(parser.getCommand('agent')).toBe(cmd);
|
|
});
|
|
|
|
it('should return command by alias', () => {
|
|
const cmd: Command = { name: 'config', aliases: ['cfg'], description: 'Config' };
|
|
parser.registerCommand(cmd);
|
|
|
|
expect(parser.getCommand('cfg')).toBe(cmd);
|
|
});
|
|
|
|
it('should return undefined for unregistered command', () => {
|
|
expect(parser.getCommand('nonexistent')).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// getGlobalOptions
|
|
// -------------------------------------------------------------------------
|
|
describe('getGlobalOptions', () => {
|
|
it('should return a copy of global options', () => {
|
|
const opts = parser.getGlobalOptions();
|
|
expect(opts.length).toBeGreaterThan(0);
|
|
expect(opts.some(o => o.name === 'help')).toBe(true);
|
|
expect(opts.some(o => o.name === 'version')).toBe(true);
|
|
expect(opts.some(o => o.name === 'verbose')).toBe(true);
|
|
expect(opts.some(o => o.name === 'format')).toBe(true);
|
|
|
|
// Should be a copy, not the same reference
|
|
opts.push({ name: 'extra', type: 'string', description: 'Extra' });
|
|
expect(parser.getGlobalOptions().length).toBe(opts.length - 1);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// raw preservation
|
|
// -------------------------------------------------------------------------
|
|
describe('raw args', () => {
|
|
it('should preserve the original args array in raw', () => {
|
|
const args = ['agent', 'spawn', '--type', 'coder', '--verbose'];
|
|
const result = parser.parse(args);
|
|
expect(result.raw).toEqual(args);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Boolean flag from registered command
|
|
// -------------------------------------------------------------------------
|
|
describe('boolean flag from command options', () => {
|
|
it('should recognize command-level boolean flags', () => {
|
|
const cmd: Command = {
|
|
name: 'run',
|
|
description: 'Run',
|
|
options: [{ name: 'force', type: 'boolean', description: 'Force' }],
|
|
};
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['run', '--force']);
|
|
expect(result.flags.force).toBe(true);
|
|
});
|
|
|
|
it('should recognize boolean flags from subcommand options', () => {
|
|
const sub: Command = {
|
|
name: 'stop',
|
|
description: 'Stop',
|
|
options: [{ name: 'graceful', type: 'boolean', description: 'Graceful' }],
|
|
};
|
|
const cmd: Command = { name: 'agent', description: 'Agent', subcommands: [sub] };
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['agent', 'stop', '--graceful']);
|
|
expect(result.flags.graceful).toBe(true);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// booleanFlags from constructor options
|
|
// -------------------------------------------------------------------------
|
|
describe('booleanFlags option', () => {
|
|
it('should recognize custom boolean flags passed via constructor', () => {
|
|
const p = new CommandParser({
|
|
allowUnknownFlags: true,
|
|
booleanFlags: ['my-custom-flag'],
|
|
});
|
|
const result = p.parse(['--my-custom-flag']);
|
|
expect(result.flags.myCustomFlag).toBe(true);
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// #1596: lazy command routing — `daemon start` must not be mis-routed to
|
|
// the core `start` command when `daemon` is a lazy-loaded command.
|
|
// -------------------------------------------------------------------------
|
|
describe('lazy command routing (#1596)', () => {
|
|
it('should not mis-route "daemon start" to the sync "start" command', () => {
|
|
const p = new CommandParser({ allowUnknownFlags: true });
|
|
// Sync command "start" is registered with full definition
|
|
p.registerCommand({ name: 'start', description: 'Top-level start', handler: async () => ({ success: true }) } as Command);
|
|
// "daemon" is lazy — only its name is registered
|
|
p.registerLazyCommandName('daemon');
|
|
|
|
const result = p.parse(['daemon', 'start', '--foreground']);
|
|
expect(result.command[0]).toBe('daemon');
|
|
expect(result.positional[0]).toBe('start');
|
|
expect(result.flags.foreground).toBe(true);
|
|
});
|
|
|
|
it('should still recognize bare "start" as the sync start command', () => {
|
|
const p = new CommandParser({ allowUnknownFlags: true });
|
|
p.registerCommand({ name: 'start', description: 'Top-level start', handler: async () => ({ success: true }) } as Command);
|
|
p.registerLazyCommandName('daemon');
|
|
|
|
const result = p.parse(['start']);
|
|
expect(result.command[0]).toBe('start');
|
|
expect(result.positional).toHaveLength(0);
|
|
});
|
|
|
|
it('should route lazy command even when a global flag comes first', () => {
|
|
const p = new CommandParser({ allowUnknownFlags: true });
|
|
p.registerCommand({ name: 'start', description: '', handler: async () => ({}) } as Command);
|
|
p.registerLazyCommandName('daemon');
|
|
|
|
const result = p.parse(['-v', 'daemon', 'start', '--foreground']);
|
|
expect(result.command[0]).toBe('daemon');
|
|
expect(result.positional[0]).toBe('start');
|
|
expect(result.flags.verbose).toBe(true);
|
|
});
|
|
|
|
it('should resolve bare lazy command (e.g. "doctor")', () => {
|
|
const p = new CommandParser({ allowUnknownFlags: true });
|
|
p.registerLazyCommandName('doctor');
|
|
|
|
const result = p.parse(['doctor']);
|
|
expect(result.command[0]).toBe('doctor');
|
|
});
|
|
});
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Edge: short flag with value
|
|
// -------------------------------------------------------------------------
|
|
describe('short flag with value', () => {
|
|
it('should parse single short non-boolean flag followed by value', () => {
|
|
const cmd: Command = {
|
|
name: 'test',
|
|
description: 'Test',
|
|
options: [{ name: 'type', short: 't', type: 'string', description: 'Type' }],
|
|
};
|
|
parser.registerCommand(cmd);
|
|
|
|
const result = parser.parse(['test', '-t', 'coder']);
|
|
expect(result.flags.type).toBe('coder');
|
|
});
|
|
});
|
|
});
|