diff --git a/.claude/settings.json b/.claude/settings.json index 14f6878f99..d979474c56 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -47,7 +47,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" pre-bash", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" pre-bash", "timeout": 5000 } ] @@ -59,7 +59,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" post-edit", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" post-edit", "timeout": 10000 } ] @@ -70,7 +70,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" route", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" route", "timeout": 10000 } ] @@ -81,12 +81,12 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" session-restore", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" session-restore", "timeout": 15000 }, { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/auto-memory-hook.mjs\" import", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/auto-memory-hook.mjs\" import", "timeout": 8000 } ] @@ -97,7 +97,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" session-end", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" session-end", "timeout": 10000 } ] @@ -108,7 +108,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/auto-memory-hook.mjs\" sync", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/auto-memory-hook.mjs\" sync", "timeout": 10000 } ] @@ -120,11 +120,11 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" compact-manual" + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" compact-manual" }, { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" session-end", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" session-end", "timeout": 5000 } ] @@ -134,11 +134,11 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" compact-auto" + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" compact-auto" }, { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" session-end", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" session-end", "timeout": 6000 } ] @@ -149,7 +149,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" status", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" status", "timeout": 3000 } ] @@ -160,7 +160,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" post-task", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" post-task", "timeout": 5000 } ] @@ -171,7 +171,7 @@ "hooks": [ { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/hook-handler.cjs\" post-task", + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs\" post-task", "timeout": 5000 } ] @@ -286,7 +286,7 @@ }, "statusLine": { "type": "command", - "command": "node \"$(git rev-parse --show-toplevel)/.claude/helpers/statusline.cjs\"" + "command": "node \"$CLAUDE_PROJECT_DIR/.claude/helpers/statusline.cjs\"" }, "mcpServers": { "claude-flow": { diff --git a/package-lock.json b/package-lock.json index cc1b54cd18..05824d0ed9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "claude-flow", - "version": "3.5.7", + "version": "3.5.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "claude-flow", - "version": "3.5.7", + "version": "3.5.15", "hasInstallScript": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index a688a9ef17..a169ff23d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "claude-flow", - "version": "3.5.7", + "version": "3.5.15", "description": "Ruflo - Enterprise AI agent orchestration for Claude Code. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration", "main": "dist/index.js", "type": "module", diff --git a/ruflo/package.json b/ruflo/package.json index d27dab382d..ea33891a22 100644 --- a/ruflo/package.json +++ b/ruflo/package.json @@ -1,6 +1,6 @@ { "name": "ruflo", - "version": "3.5.7", + "version": "3.5.15", "description": "Ruflo - Enterprise AI agent orchestration platform. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration", "main": "bin/ruflo.js", "type": "module", diff --git a/ruflo/src/ruvocal/Dockerfile b/ruflo/src/ruvocal/Dockerfile index ebe6286d7f..dfb00060a8 100644 --- a/ruflo/src/ruvocal/Dockerfile +++ b/ruflo/src/ruvocal/Dockerfile @@ -30,6 +30,9 @@ USER user COPY --chown=1000 .env /app/.env +# Remove empty placeholder values that block .env.local overrides via dotenv-cli -c +RUN sed -i 's/^MODELS=$/# MODELS=/' /app/.env && \ + sed -i 's/^TASK_MODEL=$/# TASK_MODEL=/' /app/.env COPY --chown=1000 entrypoint.sh /app/entrypoint.sh COPY --chown=1000 package.json /app/package.json COPY --chown=1000 package-lock.json /app/package-lock.json diff --git a/ruflo/src/ruvocal/src/lib/server/urlSafety.ts b/ruflo/src/ruvocal/src/lib/server/urlSafety.ts index 0a9051d352..4ddbc81277 100644 --- a/ruflo/src/ruvocal/src/lib/server/urlSafety.ts +++ b/ruflo/src/ruvocal/src/lib/server/urlSafety.ts @@ -38,8 +38,13 @@ export function isValidUrl(urlString: string): boolean { try { const url = new URL(urlString.trim()); const hostname = url.hostname.toLowerCase(); - // Allow HTTP for localhost/loopback (dev & local MCP bridge) - if (hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1") { + // Allow HTTP for localhost/loopback/Docker-internal (dev & local MCP bridge) + if ( + hostname === "localhost" || + hostname === "127.0.0.1" || + hostname === "::1" || + hostname === "host.docker.internal" + ) { return url.protocol === "http:" || url.protocol === "https:"; } // Allow HTTP for Docker-internal service names (no dots = private network) diff --git a/v3/@claude-flow/cli/.claude/helpers/auto-memory-hook.mjs b/v3/@claude-flow/cli/.claude/helpers/auto-memory-hook.mjs index ec51323d31..94d005fcd4 100755 --- a/v3/@claude-flow/cli/.claude/helpers/auto-memory-hook.mjs +++ b/v3/@claude-flow/cli/.claude/helpers/auto-memory-hook.mjs @@ -355,9 +355,11 @@ try { case 'status': await doStatus(); break; default: console.log('Usage: auto-memory-hook.mjs '); - process.exit(1); + break; } } catch (err) { // Hooks must never crash Claude Code - fail silently - dim(`Error (non-critical): ${err.message}`); + try { dim(`Error (non-critical): ${err.message}`); } catch (_) {} } +// Hooks must ALWAYS exit 0 +process.exitCode = 0; diff --git a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs index 630734067c..78c7783915 100644 --- a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs +++ b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs @@ -50,15 +50,23 @@ const intelligence = safeRequire(path.join(helpersDir, 'intelligence.cjs')); // Get the command from argv const [,, command, ...args] = process.argv; -// Read stdin — Claude Code sends hook data as JSON via stdin +// Read stdin with timeout — Claude Code sends hook data as JSON via stdin. +// Timeout prevents hanging when stdin is not properly closed (common on Windows). async function readStdin() { if (process.stdin.isTTY) return ''; - let data = ''; - process.stdin.setEncoding('utf8'); - for await (const chunk of process.stdin) { - data += chunk; - } - return data; + return new Promise((resolve) => { + let data = ''; + const timer = setTimeout(() => { + process.stdin.removeAllListeners(); + process.stdin.pause(); + resolve(data); + }, 500); + process.stdin.setEncoding('utf8'); + process.stdin.on('data', (chunk) => { data += chunk; }); + process.stdin.on('end', () => { clearTimeout(timer); resolve(data); }); + process.stdin.on('error', () => { clearTimeout(timer); resolve(data); }); + process.stdin.resume(); + }); } async function main() { @@ -254,6 +262,10 @@ const handlers = { } } +// Hooks must ALWAYS exit 0 — Claude Code treats non-zero as "hook error" +// and skips all subsequent hooks for the event. +process.exitCode = 0; main().catch((e) => { - console.log(`[WARN] Hook handler error: ${e.message}`); + try { console.log(`[WARN] Hook handler error: ${e.message}`); } catch (_) {} + process.exitCode = 0; }); diff --git a/v3/@claude-flow/cli/.claude/settings.json b/v3/@claude-flow/cli/.claude/settings.json index 8d064462bc..95d945161c 100644 --- a/v3/@claude-flow/cli/.claude/settings.json +++ b/v3/@claude-flow/cli/.claude/settings.json @@ -11,7 +11,7 @@ "hooks": [ { "type": "command", - "command": "node .claude/helpers/hook-handler.cjs pre-bash", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs" pre-bash", "timeout": 5000 } ] @@ -23,7 +23,7 @@ "hooks": [ { "type": "command", - "command": "node .claude/helpers/hook-handler.cjs post-edit", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs" post-edit", "timeout": 10000 } ] @@ -34,7 +34,7 @@ "hooks": [ { "type": "command", - "command": "node .claude/helpers/hook-handler.cjs route", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs" route", "timeout": 10000 } ] @@ -46,13 +46,13 @@ "hooks": [ { "type": "command", - "command": "node .claude/helpers/hook-handler.cjs session-restore", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs" session-restore", "timeout": 15000, "continueOnError": true }, { "type": "command", - "command": "node .claude/helpers/auto-memory-hook.mjs import", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/auto-memory-hook.mjs" import", "timeout": 8000, "continueOnError": true } @@ -64,7 +64,7 @@ "hooks": [ { "type": "command", - "command": "node .claude/helpers/hook-handler.cjs session-end", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs" session-end", "timeout": 10000, "continueOnError": true } @@ -76,7 +76,7 @@ "hooks": [ { "type": "command", - "command": "node .claude/helpers/hook-handler.cjs status", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/hook-handler.cjs" status", "timeout": 3000, "continueOnError": true } @@ -88,7 +88,7 @@ "hooks": [ { "type": "command", - "command": "node .claude/helpers/auto-memory-hook.mjs sync", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/auto-memory-hook.mjs" sync", "timeout": 10000, "continueOnError": true } @@ -98,7 +98,7 @@ }, "statusLine": { "type": "command", - "command": "node .claude/helpers/statusline.cjs", + "command": "node "$CLAUDE_PROJECT_DIR/.claude/helpers/statusline.cjs"", "refreshMs": 5000, "enabled": true }, diff --git a/v3/@claude-flow/cli/__tests__/cli.test.ts b/v3/@claude-flow/cli/__tests__/cli.test.ts index 5b7571bf22..87c21943f7 100644 --- a/v3/@claude-flow/cli/__tests__/cli.test.ts +++ b/v3/@claude-flow/cli/__tests__/cli.test.ts @@ -46,7 +46,6 @@ describe('CLI', () => { await cli.run(['--version']); const output = consoleOutput.join(''); - expect(output).toContain('claude-flow'); expect(output).toContain(VERSION); }); @@ -54,7 +53,6 @@ describe('CLI', () => { await cli.run(['-V']); const output = consoleOutput.join(''); - expect(output).toContain('claude-flow'); expect(output).toContain(VERSION); }); @@ -62,7 +60,7 @@ describe('CLI', () => { await cli.run(['--version']); const output = consoleOutput.join(''); - expect(output).toMatch(/claude-flow v\d+\.\d+\.\d+/); + expect(output).toMatch(/v\d+\.\d+\.\d+/); }); }); diff --git a/v3/@claude-flow/cli/__tests__/commands-deep.test.ts b/v3/@claude-flow/cli/__tests__/commands-deep.test.ts new file mode 100644 index 0000000000..066fdc6bb0 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/commands-deep.test.ts @@ -0,0 +1,1694 @@ +/** + * V3 CLI Deep Commands & Infrastructure Tests + * 100+ test cases covering all commands, init system, parser, output, and suggest modules + */ + +import { describe, it, expect, beforeEach, vi } from 'vitest'; + +// ============================================================================ +// SECTION 1: Command Definitions (38+ commands) +// ============================================================================ + +// Mock MCP client before importing commands +vi.mock('../src/mcp-client.js', () => ({ + callMCPTool: vi.fn(async () => ({})), + MCPClientError: class MCPClientError extends Error { + constructor(message: string) { + super(message); + this.name = 'MCPClientError'; + } + }, +})); + +// Mock fs/promises for commands that use it +vi.mock('fs/promises', () => ({ + default: { + readFile: vi.fn(async () => '{}'), + writeFile: vi.fn(async () => undefined), + mkdir: vi.fn(async () => undefined), + access: vi.fn(async () => undefined), + readdir: vi.fn(async () => []), + stat: vi.fn(async () => ({ isDirectory: () => true, isFile: () => true })), + unlink: vi.fn(async () => undefined), + rm: vi.fn(async () => undefined), + cp: vi.fn(async () => undefined), + }, + readFile: vi.fn(async () => '{}'), + writeFile: vi.fn(async () => undefined), + mkdir: vi.fn(async () => undefined), + access: vi.fn(async () => undefined), + readdir: vi.fn(async () => []), + stat: vi.fn(async () => ({ isDirectory: () => true, isFile: () => true })), + unlink: vi.fn(async () => undefined), + rm: vi.fn(async () => undefined), + cp: vi.fn(async () => undefined), +})); + +// Mock child_process +vi.mock('child_process', () => ({ + execSync: vi.fn(() => Buffer.from('')), + execFileSync: vi.fn(() => ''), + exec: vi.fn(), + execFile: vi.fn(), + spawn: vi.fn(() => ({ + on: vi.fn(), + stdout: { on: vi.fn() }, + stderr: { on: vi.fn() }, + pid: 12345, + })), +})); + +import { agentCommand } from '../src/commands/agent.js'; +import { swarmCommand } from '../src/commands/swarm.js'; +import { memoryCommand } from '../src/commands/memory.js'; +import { configCommand } from '../src/commands/config.js'; +import { initCommand } from '../src/commands/init.js'; +import { statusCommand } from '../src/commands/status.js'; +import { taskCommand } from '../src/commands/task.js'; +import { sessionCommand } from '../src/commands/session.js'; +import { mcpCommand } from '../src/commands/mcp.js'; +import { hooksCommand } from '../src/commands/hooks.js'; +import { daemonCommand } from '../src/commands/daemon.js'; +import { doctorCommand } from '../src/commands/doctor.js'; +import { embeddingsCommand } from '../src/commands/embeddings.js'; +import { neuralCommand } from '../src/commands/neural.js'; +import { performanceCommand } from '../src/commands/performance.js'; +import { securityCommand } from '../src/commands/security.js'; +import { hiveMindCommand } from '../src/commands/hive-mind.js'; +import { completionsCommand } from '../src/commands/completions.js'; +import { migrateCommand } from '../src/commands/migrate.js'; +import { workflowCommand } from '../src/commands/workflow.js'; +import { claimsCommand } from '../src/commands/claims.js'; +import { deploymentCommand } from '../src/commands/deployment.js'; +import { providersCommand } from '../src/commands/providers.js'; +import { pluginsCommand } from '../src/commands/plugins.js'; +import { processCommand } from '../src/commands/process.js'; +import { startCommand } from '../src/commands/start.js'; +import { analyzeCommand } from '../src/commands/analyze.js'; +import { routeCommand } from '../src/commands/route.js'; +import { progressCommand } from '../src/commands/progress.js'; +import { issuesCommand } from '../src/commands/issues.js'; +import { guidanceCommand } from '../src/commands/guidance.js'; +import { applianceCommand } from '../src/commands/appliance.js'; +import updateCommand from '../src/commands/update.js'; +import { ruvectorCommand } from '../src/commands/ruvector/index.js'; + +import type { Command } from '../src/types.js'; + +// ============================================================================ +// Helper: validate a command structure +// ============================================================================ + +function expectValidCommand(cmd: Command, expectedName: string) { + expect(cmd).toBeDefined(); + expect(cmd.name).toBe(expectedName); + expect(typeof cmd.description).toBe('string'); + expect(cmd.description.length).toBeGreaterThan(0); +} + +function expectHasSubcommands(cmd: Command, minCount: number) { + expect(cmd.subcommands).toBeDefined(); + expect(Array.isArray(cmd.subcommands)).toBe(true); + expect(cmd.subcommands!.length).toBeGreaterThanOrEqual(minCount); +} + +function expectSubcommandNames(cmd: Command, names: string[]) { + const subNames = cmd.subcommands!.map((s) => s.name); + for (const name of names) { + expect(subNames).toContain(name); + } +} + +// ============================================================================ +// 1. Command Structure Tests +// ============================================================================ + +describe('Command Definitions', () => { + describe('agent command', () => { + it('should have correct name and description', () => { + expectValidCommand(agentCommand, 'agent'); + }); + + it('should have 8 subcommands', () => { + expectHasSubcommands(agentCommand, 8); + expectSubcommandNames(agentCommand, [ + 'spawn', 'list', 'status', 'stop', 'metrics', 'pool', 'health', 'logs', + ]); + }); + + it('should have an action or subcommands with actions', () => { + const hasAction = typeof agentCommand.action === 'function'; + const hasSubActions = agentCommand.subcommands?.some((s) => typeof s.action === 'function'); + expect(hasAction || hasSubActions).toBe(true); + }); + + it('each subcommand should have name and description', () => { + for (const sub of agentCommand.subcommands!) { + expect(typeof sub.name).toBe('string'); + expect(typeof sub.description).toBe('string'); + } + }); + }); + + describe('swarm command', () => { + it('should have correct name', () => { + expectValidCommand(swarmCommand, 'swarm'); + }); + + it('should have subcommands including init, status, deploy, scale', () => { + expectHasSubcommands(swarmCommand, 4); + expectSubcommandNames(swarmCommand, ['init', 'status']); + }); + }); + + describe('memory command', () => { + it('should have correct name', () => { + expectValidCommand(memoryCommand, 'memory'); + }); + + it('should have subcommands for store, retrieve, search, list', () => { + expectHasSubcommands(memoryCommand, 4); + expectSubcommandNames(memoryCommand, ['store', 'retrieve', 'search', 'list']); + }); + }); + + describe('config command', () => { + it('should have correct name', () => { + expectValidCommand(configCommand, 'config'); + }); + + it('should have subcommands including init, get, set', () => { + expectHasSubcommands(configCommand, 3); + expectSubcommandNames(configCommand, ['init', 'get', 'set']); + }); + }); + + describe('init command', () => { + it('should have correct name', () => { + expectValidCommand(initCommand, 'init'); + }); + + it('should have options or subcommands', () => { + const hasOptions = initCommand.options && initCommand.options.length > 0; + const hasSubs = initCommand.subcommands && initCommand.subcommands.length > 0; + expect(hasOptions || hasSubs).toBe(true); + }); + }); + + describe('status command', () => { + it('should have correct name', () => { + expectValidCommand(statusCommand, 'status'); + }); + }); + + describe('task command', () => { + it('should have correct name', () => { + expectValidCommand(taskCommand, 'task'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(taskCommand, 3); + }); + }); + + describe('session command', () => { + it('should have correct name', () => { + expectValidCommand(sessionCommand, 'session'); + }); + + it('should have subcommands for start, end, restore, list', () => { + expectHasSubcommands(sessionCommand, 4); + }); + }); + + describe('mcp command', () => { + it('should have correct name', () => { + expectValidCommand(mcpCommand, 'mcp'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(mcpCommand, 3); + }); + }); + + describe('hooks command', () => { + it('should have correct name', () => { + expectValidCommand(hooksCommand, 'hooks'); + }); + + it('should have many subcommands (17+)', () => { + expectHasSubcommands(hooksCommand, 10); + }); + }); + + describe('daemon command', () => { + it('should have correct name', () => { + expectValidCommand(daemonCommand, 'daemon'); + }); + + it('should have subcommands for start, stop, status', () => { + expectHasSubcommands(daemonCommand, 3); + expectSubcommandNames(daemonCommand, ['start', 'stop', 'status']); + }); + }); + + describe('doctor command', () => { + it('should have correct name', () => { + expectValidCommand(doctorCommand, 'doctor'); + }); + }); + + describe('embeddings command', () => { + it('should have correct name', () => { + expectValidCommand(embeddingsCommand, 'embeddings'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(embeddingsCommand, 3); + }); + }); + + describe('neural command', () => { + it('should have correct name', () => { + expectValidCommand(neuralCommand, 'neural'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(neuralCommand, 3); + }); + }); + + describe('performance command', () => { + it('should have correct name', () => { + expectValidCommand(performanceCommand, 'performance'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(performanceCommand, 3); + }); + }); + + describe('security command', () => { + it('should have correct name', () => { + expectValidCommand(securityCommand, 'security'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(securityCommand, 3); + }); + }); + + describe('hive-mind command', () => { + it('should have correct name', () => { + expectValidCommand(hiveMindCommand, 'hive-mind'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(hiveMindCommand, 3); + }); + }); + + describe('completions command', () => { + it('should have correct name', () => { + expectValidCommand(completionsCommand, 'completions'); + }); + + it('should have subcommands for bash, zsh, fish, powershell', () => { + expectHasSubcommands(completionsCommand, 4); + expectSubcommandNames(completionsCommand, ['bash', 'zsh', 'fish', 'powershell']); + }); + }); + + describe('migrate command', () => { + it('should have correct name', () => { + expectValidCommand(migrateCommand, 'migrate'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(migrateCommand, 3); + }); + }); + + describe('workflow command', () => { + it('should have correct name', () => { + expectValidCommand(workflowCommand, 'workflow'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(workflowCommand, 3); + }); + }); + + describe('claims command', () => { + it('should have correct name', () => { + expectValidCommand(claimsCommand, 'claims'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(claimsCommand, 3); + }); + }); + + describe('deployment command', () => { + it('should have correct name', () => { + expectValidCommand(deploymentCommand, 'deployment'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(deploymentCommand, 3); + }); + }); + + describe('providers command', () => { + it('should have correct name', () => { + expectValidCommand(providersCommand, 'providers'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(providersCommand, 3); + }); + }); + + describe('plugins command', () => { + it('should have correct name', () => { + expectValidCommand(pluginsCommand, 'plugins'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(pluginsCommand, 3); + }); + }); + + describe('process command', () => { + it('should have correct name', () => { + expectValidCommand(processCommand, 'process'); + }); + }); + + describe('start command', () => { + it('should have correct name', () => { + expectValidCommand(startCommand, 'start'); + }); + }); + + describe('analyze command', () => { + it('should have correct name', () => { + expectValidCommand(analyzeCommand, 'analyze'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(analyzeCommand, 2); + }); + }); + + describe('route command', () => { + it('should have correct name', () => { + expectValidCommand(routeCommand, 'route'); + }); + }); + + describe('progress command', () => { + it('should have correct name', () => { + expectValidCommand(progressCommand, 'progress'); + }); + }); + + describe('issues command', () => { + it('should have correct name', () => { + expectValidCommand(issuesCommand, 'issues'); + }); + }); + + describe('update command', () => { + it('should have correct name', () => { + expectValidCommand(updateCommand, 'update'); + }); + }); + + describe('guidance command', () => { + it('should have correct name', () => { + expectValidCommand(guidanceCommand, 'guidance'); + }); + + it('should have subcommands', () => { + expectHasSubcommands(guidanceCommand, 3); + }); + }); + + describe('appliance command', () => { + it('should have correct name', () => { + expectValidCommand(applianceCommand, 'appliance'); + }); + + it('should have subcommands including build, inspect, verify', () => { + expectHasSubcommands(applianceCommand, 3); + }); + }); + + describe('ruvector command', () => { + it('should have correct name', () => { + expectValidCommand(ruvectorCommand, 'ruvector'); + }); + }); +}); + +// All commands array for bulk tests +const allCommands: { cmd: Command; name: string }[] = [ + { cmd: agentCommand, name: 'agent' }, + { cmd: swarmCommand, name: 'swarm' }, + { cmd: memoryCommand, name: 'memory' }, + { cmd: configCommand, name: 'config' }, + { cmd: initCommand, name: 'init' }, + { cmd: statusCommand, name: 'status' }, + { cmd: taskCommand, name: 'task' }, + { cmd: sessionCommand, name: 'session' }, + { cmd: mcpCommand, name: 'mcp' }, + { cmd: hooksCommand, name: 'hooks' }, + { cmd: daemonCommand, name: 'daemon' }, + { cmd: doctorCommand, name: 'doctor' }, + { cmd: embeddingsCommand, name: 'embeddings' }, + { cmd: neuralCommand, name: 'neural' }, + { cmd: performanceCommand, name: 'performance' }, + { cmd: securityCommand, name: 'security' }, + { cmd: hiveMindCommand, name: 'hive-mind' }, + { cmd: completionsCommand, name: 'completions' }, + { cmd: migrateCommand, name: 'migrate' }, + { cmd: workflowCommand, name: 'workflow' }, + { cmd: claimsCommand, name: 'claims' }, + { cmd: deploymentCommand, name: 'deployment' }, + { cmd: providersCommand, name: 'providers' }, + { cmd: pluginsCommand, name: 'plugins' }, + { cmd: processCommand, name: 'process' }, + { cmd: startCommand, name: 'start' }, + { cmd: analyzeCommand, name: 'analyze' }, + { cmd: routeCommand, name: 'route' }, + { cmd: progressCommand, name: 'progress' }, + { cmd: issuesCommand, name: 'issues' }, + { cmd: updateCommand, name: 'update' }, + { cmd: guidanceCommand, name: 'guidance' }, + { cmd: applianceCommand, name: 'appliance' }, + { cmd: ruvectorCommand, name: 'ruvector' }, +]; + +describe('Bulk Command Structure Validation', () => { + it.each(allCommands)('$name: should have non-empty description', ({ cmd }) => { + expect(cmd.description).toBeTruthy(); + expect(cmd.description.length).toBeGreaterThan(5); + }); + + it.each(allCommands)('$name: description should not contain placeholder text', ({ cmd }) => { + expect(cmd.description).not.toContain('TODO'); + expect(cmd.description).not.toContain('FIXME'); + expect(cmd.description).not.toContain('placeholder'); + }); + + it.each(allCommands)('$name: subcommands (if any) should have unique names', ({ cmd }) => { + if (cmd.subcommands && cmd.subcommands.length > 0) { + const names = cmd.subcommands.map((s) => s.name); + const uniqueNames = new Set(names); + expect(uniqueNames.size).toBe(names.length); + } + }); + + it.each(allCommands)('$name: options (if any) should have valid types', ({ cmd }) => { + if (cmd.options) { + for (const opt of cmd.options) { + expect(['string', 'boolean', 'number', 'array']).toContain(opt.type); + } + } + }); + + it.each(allCommands)( + '$name: subcommand descriptions should be non-empty strings', + ({ cmd }) => { + if (cmd.subcommands) { + for (const sub of cmd.subcommands) { + expect(typeof sub.description).toBe('string'); + expect(sub.description.length).toBeGreaterThan(0); + } + } + }, + ); +}); + +// ============================================================================ +// SECTION 2: Command Registry Tests +// ============================================================================ + +describe('Command Registry (commands/index.ts)', () => { + it('should export all core commands synchronously', async () => { + const indexModule = await import('../src/commands/index.js'); + expect(indexModule.agentCommand).toBeDefined(); + expect(indexModule.swarmCommand).toBeDefined(); + expect(indexModule.memoryCommand).toBeDefined(); + expect(indexModule.initCommand).toBeDefined(); + expect(indexModule.statusCommand).toBeDefined(); + expect(indexModule.taskCommand).toBeDefined(); + expect(indexModule.sessionCommand).toBeDefined(); + expect(indexModule.mcpCommand).toBeDefined(); + expect(indexModule.hooksCommand).toBeDefined(); + expect(indexModule.daemonCommand).toBeDefined(); + expect(indexModule.doctorCommand).toBeDefined(); + expect(indexModule.embeddingsCommand).toBeDefined(); + expect(indexModule.neuralCommand).toBeDefined(); + expect(indexModule.performanceCommand).toBeDefined(); + expect(indexModule.securityCommand).toBeDefined(); + expect(indexModule.hiveMindCommand).toBeDefined(); + expect(indexModule.guidanceCommand).toBeDefined(); + expect(indexModule.applianceCommand).toBeDefined(); + expect(indexModule.ruvectorCommand).toBeDefined(); + }); + + it('should export commandsByCategory with all categories', async () => { + const indexModule = await import('../src/commands/index.js'); + expect(indexModule.commandsByCategory).toBeDefined(); + const categories = Object.keys(indexModule.commandsByCategory); + expect(categories.length).toBeGreaterThanOrEqual(3); + }); + + it('should export commands array with many entries', async () => { + const indexModule = await import('../src/commands/index.js'); + expect(Array.isArray(indexModule.commands)).toBe(true); + expect(indexModule.commands.length).toBeGreaterThanOrEqual(15); + }); + + it('should export loadAllCommands as async function', async () => { + const indexModule = await import('../src/commands/index.js'); + expect(typeof indexModule.loadAllCommands).toBe('function'); + }); + + it('should export getCommandAsync as async function', async () => { + const indexModule = await import('../src/commands/index.js'); + expect(typeof indexModule.getCommandAsync).toBe('function'); + }); +}); + +// ============================================================================ +// SECTION 3: Parser Tests +// ============================================================================ + +import { CommandParser, commandParser } from '../src/parser.js'; +import type { ParsedFlags } from '../src/types.js'; + +describe('CommandParser', () => { + let parser: CommandParser; + + beforeEach(() => { + parser = new CommandParser({ allowUnknownFlags: true }); + }); + + describe('basic parsing', () => { + it('should parse empty args', () => { + const result = parser.parse([]); + expect(result.command).toEqual([]); + expect(result.positional).toEqual([]); + expect(result.flags._).toEqual([]); + }); + + it('should parse a single positional arg as positional (not registered command)', () => { + const result = parser.parse(['foo']); + expect(result.positional).toContain('foo'); + }); + + it('should parse long boolean flag --help', () => { + const result = parser.parse(['--help']); + expect(result.flags.help).toBe(true); + }); + + it('should parse long boolean flag --verbose', () => { + const result = parser.parse(['--verbose']); + expect(result.flags.verbose).toBe(true); + }); + + it('should parse --flag=value syntax', () => { + const result = parser.parse(['--format=json']); + expect(result.flags.format).toBe('json'); + }); + + it('should parse --flag value syntax for non-boolean flags', () => { + const result = parser.parse(['--config', '/path/to/config']); + expect(result.flags.config).toBe('/path/to/config'); + }); + + it('should parse boolean negation --no-color', () => { + const result = parser.parse(['--no-color']); + expect(result.flags.color).toBe(false); + }); + + it('should parse boolean negation --no-interactive', () => { + const result = parser.parse(['--no-interactive']); + expect(result.flags.interactive).toBe(false); + }); + }); + + describe('short flags', () => { + it('should resolve short flag -h to help', () => { + const result = parser.parse(['-h']); + expect(result.flags.help).toBe(true); + }); + + it('should resolve short flag -v to verbose', () => { + const result = parser.parse(['-v']); + expect(result.flags.verbose).toBe(true); + }); + + it('should resolve short flag -Q to quiet', () => { + const result = parser.parse(['-Q']); + expect(result.flags.quiet).toBe(true); + }); + + it('should parse combined short flags -hv', () => { + const result = parser.parse(['-hv']); + expect(result.flags.help).toBe(true); + expect(result.flags.verbose).toBe(true); + }); + + it('should parse short flag with value -c /path', () => { + const result = parser.parse(['-c', '/path/config.json']); + expect(result.flags.config).toBe('/path/config.json'); + }); + + it('should parse short flag -f with value', () => { + const result = parser.parse(['-f', 'json']); + expect(result.flags.format).toBe('json'); + }); + }); + + describe('value type coercion', () => { + it('should coerce numeric string to number', () => { + const result = parser.parse(['--port=3000']); + expect(result.flags.port).toBe(3000); + }); + + it('should coerce "true" string to boolean true', () => { + const result = parser.parse(['--debug=true']); + expect(result.flags.debug).toBe(true); + }); + + it('should coerce "false" string to boolean false', () => { + const result = parser.parse(['--debug=false']); + expect(result.flags.debug).toBe(false); + }); + + it('should keep non-numeric strings as strings', () => { + const result = parser.parse(['--name=my-project']); + expect(result.flags.name).toBe('my-project'); + }); + + it('should handle empty string value', () => { + const result = parser.parse(['--name=']); + expect(result.flags.name).toBe(''); + }); + }); + + describe('kebab-case to camelCase normalization', () => { + it('should convert --no-color to noColor flag (negation)', () => { + // --no-color is boolean negation => flags.color = false + const result = parser.parse(['--no-color']); + expect(result.flags.color).toBe(false); + }); + + it('should convert --some-flag to someFlag', () => { + const result = parser.parse(['--some-flag=value']); + expect(result.flags.someFlag).toBe('value'); + }); + + it('should convert --my-long-option to myLongOption', () => { + const result = parser.parse(['--my-long-option=test']); + expect(result.flags.myLongOption).toBe('test'); + }); + }); + + describe('end-of-flags marker --', () => { + it('should stop parsing flags after --', () => { + const result = parser.parse(['--verbose', '--', '--not-a-flag']); + expect(result.flags.verbose).toBe(true); + expect(result.positional).toContain('--not-a-flag'); + }); + }); + + describe('command recognition', () => { + it('should recognize registered command', () => { + parser.registerCommand(agentCommand); + const result = parser.parse(['agent']); + expect(result.command).toEqual(['agent']); + }); + + it('should recognize command with subcommand', () => { + parser.registerCommand(agentCommand); + const result = parser.parse(['agent', 'spawn']); + expect(result.command).toEqual(['agent', 'spawn']); + }); + + it('should recognize command with subcommand and flags', () => { + parser.registerCommand(agentCommand); + const result = parser.parse(['agent', 'spawn', '--type', 'coder']); + expect(result.command).toEqual(['agent', 'spawn']); + expect(result.flags.type).toBe('coder'); + }); + + it('should handle unknown subcommand as positional', () => { + parser.registerCommand(agentCommand); + const result = parser.parse(['agent', 'nonexistent']); + // 'agent' is recognized, 'nonexistent' won't match a subcommand + expect(result.command).toEqual(['agent']); + }); + }); + + describe('defaults', () => { + it('should apply global defaults for help', () => { + const result = parser.parse([]); + expect(result.flags.help).toBe(false); + }); + + it('should apply global defaults for verbose', () => { + const result = parser.parse([]); + expect(result.flags.verbose).toBe(false); + }); + + it('should apply global defaults for format', () => { + const result = parser.parse([]); + expect(result.flags.format).toBe('text'); + }); + + it('should apply global defaults for interactive', () => { + const result = parser.parse([]); + expect(result.flags.interactive).toBe(true); + }); + + it('should not override explicit values with defaults', () => { + const result = parser.parse(['--verbose']); + expect(result.flags.verbose).toBe(true); + }); + }); + + describe('validation', () => { + it('should return no errors for valid flags', () => { + const flags: ParsedFlags = { _: [], help: false, verbose: true, format: 'json' }; + const errors = parser.validateFlags(flags); + expect(errors).toEqual([]); + }); + + it('should validate choices for format flag', () => { + const flags: ParsedFlags = { _: [], format: 'invalid-format' }; + const errors = parser.validateFlags(flags); + expect(errors.some((e) => e.includes('format'))).toBe(true); + }); + + it('should report required options as missing', () => { + const cmdWithRequired: Command = { + name: 'test', + description: 'test', + options: [ + { + name: 'required-opt', + description: 'Required option', + type: 'string', + required: true, + }, + ], + }; + const flags: ParsedFlags = { _: [] }; + const errors = parser.validateFlags(flags, cmdWithRequired); + expect(errors.length).toBeGreaterThan(0); + expect(errors[0]).toContain('required-opt'); + }); + }); + + describe('getAllCommands', () => { + it('should return unique commands (no alias duplicates)', () => { + parser.registerCommand(agentCommand); + parser.registerCommand(swarmCommand); + const all = parser.getAllCommands(); + const names = all.map((c) => c.name); + const unique = new Set(names); + expect(unique.size).toBe(names.length); + }); + }); + + describe('getGlobalOptions', () => { + it('should return global options array', () => { + const opts = parser.getGlobalOptions(); + expect(Array.isArray(opts)).toBe(true); + expect(opts.length).toBeGreaterThanOrEqual(5); + }); + + it('should include help, version, verbose, quiet, config, format', () => { + const opts = parser.getGlobalOptions(); + const names = opts.map((o) => o.name); + expect(names).toContain('help'); + expect(names).toContain('version'); + expect(names).toContain('verbose'); + expect(names).toContain('quiet'); + expect(names).toContain('config'); + expect(names).toContain('format'); + }); + }); + + describe('singleton export', () => { + it('commandParser singleton should be an instance of CommandParser', () => { + expect(commandParser).toBeInstanceOf(CommandParser); + }); + + it('commandParser should have allowUnknownFlags enabled', () => { + // It should not report errors for unknown flags + const flags: ParsedFlags = { _: [], someRandomFlag: 'value' }; + const errors = commandParser.validateFlags(flags); + // With allowUnknownFlags, no error for unknown flags + const unknownErrors = errors.filter((e) => e.includes('Unknown option')); + expect(unknownErrors).toEqual([]); + }); + }); +}); + +// ============================================================================ +// SECTION 4: Output Formatter Tests +// ============================================================================ + +import { OutputFormatter, Progress, Spinner, output } from '../src/output.js'; + +describe('OutputFormatter', () => { + let formatter: OutputFormatter; + + beforeEach(() => { + formatter = new OutputFormatter({ color: false }); + }); + + describe('color methods (color disabled)', () => { + it('bold() should return text without ANSI codes when color disabled', () => { + const result = formatter.bold('hello'); + expect(result).toBe('hello'); + }); + + it('dim() should return text without ANSI codes when color disabled', () => { + const result = formatter.dim('hello'); + expect(result).toBe('hello'); + }); + + it('success() should return text without ANSI codes when color disabled', () => { + const result = formatter.success('done'); + expect(result).toBe('done'); + }); + + it('error() should return text without ANSI codes when color disabled', () => { + const result = formatter.error('fail'); + expect(result).toBe('fail'); + }); + + it('warning() should return text without ANSI codes when color disabled', () => { + const result = formatter.warning('warn'); + expect(result).toBe('warn'); + }); + + it('info() should return text without ANSI codes when color disabled', () => { + const result = formatter.info('info'); + expect(result).toBe('info'); + }); + + it('highlight() should return text without ANSI codes when color disabled', () => { + const result = formatter.highlight('hi'); + expect(result).toBe('hi'); + }); + }); + + describe('color methods (color enabled)', () => { + let colorFormatter: OutputFormatter; + + beforeEach(() => { + colorFormatter = new OutputFormatter({ color: true }); + }); + + it('bold() should wrap text with ANSI bold codes', () => { + const result = colorFormatter.bold('hello'); + expect(result).toContain('\x1b[1m'); + expect(result).toContain('hello'); + expect(result).toContain('\x1b[0m'); + }); + + it('error() should wrap text with red ANSI codes', () => { + const result = colorFormatter.error('fail'); + expect(result).toContain('\x1b[31m'); + expect(result).toContain('fail'); + }); + + it('success() should wrap text with green ANSI codes', () => { + const result = colorFormatter.success('pass'); + expect(result).toContain('\x1b[32m'); + }); + + it('warning() should wrap text with yellow ANSI codes', () => { + const result = colorFormatter.warning('caution'); + expect(result).toContain('\x1b[33m'); + }); + + it('info() should wrap text with blue ANSI codes', () => { + const result = colorFormatter.info('note'); + expect(result).toContain('\x1b[34m'); + }); + }); + + describe('verbosity', () => { + it('should default to normal verbosity', () => { + expect(formatter.getVerbosity()).toBe('normal'); + }); + + it('should set verbosity', () => { + formatter.setVerbosity('quiet'); + expect(formatter.getVerbosity()).toBe('quiet'); + expect(formatter.isQuiet()).toBe(true); + }); + + it('isVerbose should return true for verbose and debug', () => { + formatter.setVerbosity('verbose'); + expect(formatter.isVerbose()).toBe(true); + formatter.setVerbosity('debug'); + expect(formatter.isVerbose()).toBe(true); + }); + + it('isDebug should return true only for debug', () => { + formatter.setVerbosity('verbose'); + expect(formatter.isDebug()).toBe(false); + formatter.setVerbosity('debug'); + expect(formatter.isDebug()).toBe(true); + }); + + it('isQuiet should return true only for quiet', () => { + formatter.setVerbosity('normal'); + expect(formatter.isQuiet()).toBe(false); + formatter.setVerbosity('quiet'); + expect(formatter.isQuiet()).toBe(true); + }); + }); + + describe('table formatting', () => { + it('should render a basic table with borders', () => { + const result = formatter.table({ + columns: [ + { key: 'name', header: 'Name' }, + { key: 'value', header: 'Value' }, + ], + data: [ + { name: 'a', value: '1' }, + { name: 'b', value: '2' }, + ], + }); + expect(result).toContain('Name'); + expect(result).toContain('Value'); + expect(result).toContain('a'); + expect(result).toContain('b'); + expect(result).toContain('|'); + expect(result).toContain('+'); + }); + + it('should render a table without borders', () => { + const result = formatter.table({ + columns: [{ key: 'x', header: 'X' }], + data: [{ x: 'hello' }], + border: false, + }); + expect(result).not.toContain('+'); + expect(result).toContain('hello'); + }); + + it('should render a table without header', () => { + const result = formatter.table({ + columns: [{ key: 'x', header: 'X' }], + data: [{ x: 'val' }], + header: false, + }); + expect(result).toContain('val'); + }); + + it('should apply column formatter', () => { + const result = formatter.table({ + columns: [ + { + key: 'x', + header: 'X', + format: (v) => `[${v}]`, + }, + ], + data: [{ x: 'test' }], + }); + expect(result).toContain('[test]'); + }); + + it('should handle empty data', () => { + const result = formatter.table({ + columns: [{ key: 'x', header: 'X' }], + data: [], + }); + expect(result).toContain('X'); + }); + + it('should handle null/undefined values', () => { + const result = formatter.table({ + columns: [{ key: 'x', header: 'X' }], + data: [{ x: null }, { x: undefined }], + }); + expect(result).toBeDefined(); + }); + }); + + describe('list formatting', () => { + it('should format a bulleted list', () => { + const result = formatter.list(['item1', 'item2', 'item3']); + expect(result).toContain('- item1'); + expect(result).toContain('- item2'); + expect(result).toContain('- item3'); + }); + + it('should use custom bullet', () => { + const result = formatter.list(['a', 'b'], '*'); + expect(result).toContain('* a'); + expect(result).toContain('* b'); + }); + + it('should format a numbered list', () => { + const result = formatter.numberedList(['first', 'second', 'third']); + expect(result).toContain('1. first'); + expect(result).toContain('2. second'); + expect(result).toContain('3. third'); + }); + }); + + describe('box formatting', () => { + it('should render a box around content', () => { + const result = formatter.box('Hello World'); + expect(result).toContain('+'); + expect(result).toContain('|'); + expect(result).toContain('Hello World'); + }); + + it('should render a box with title', () => { + const result = formatter.box('Content', 'Title'); + expect(result).toContain('Title'); + expect(result).toContain('Content'); + }); + + it('should handle multiline content', () => { + const result = formatter.box('Line 1\nLine 2\nLine 3'); + expect(result).toContain('Line 1'); + expect(result).toContain('Line 2'); + expect(result).toContain('Line 3'); + }); + }); + + describe('JSON output', () => { + it('should format JSON with indentation by default', () => { + const result = formatter.json({ key: 'value' }); + expect(result).toContain('"key"'); + expect(result).toContain('"value"'); + expect(result).toContain('\n'); + }); + + it('should format compact JSON when pretty is false', () => { + const result = formatter.json({ key: 'value' }, false); + expect(result).not.toContain('\n'); + expect(result).toBe('{"key":"value"}'); + }); + }); + + describe('progress bar', () => { + it('should render progress bar at 0%', () => { + const result = formatter.progressBar(0, 100); + expect(result).toContain('0.0%'); + }); + + it('should render progress bar at 50%', () => { + const result = formatter.progressBar(50, 100); + expect(result).toContain('50.0%'); + }); + + it('should render progress bar at 100%', () => { + const result = formatter.progressBar(100, 100); + expect(result).toContain('100.0%'); + }); + + it('should clamp above 100%', () => { + const result = formatter.progressBar(150, 100); + expect(result).toContain('100.0%'); + }); + }); + + describe('color toggle', () => { + it('should toggle color on and off', () => { + formatter.setColorEnabled(true); + expect(formatter.isColorEnabled()).toBe(true); + formatter.setColorEnabled(false); + expect(formatter.isColorEnabled()).toBe(false); + }); + }); + + describe('singleton export', () => { + it('output should be an instance of OutputFormatter', () => { + expect(output).toBeInstanceOf(OutputFormatter); + }); + }); +}); + +// ============================================================================ +// SECTION 5: Suggest Module Tests +// ============================================================================ + +import { + levenshteinDistance, + similarityScore, + findSimilar, + formatSuggestion, + suggestCommand, + getTypoCorrection, + COMMON_TYPOS, +} from '../src/suggest.js'; + +describe('Suggest Module', () => { + describe('levenshteinDistance', () => { + it('should return 0 for identical strings', () => { + expect(levenshteinDistance('hello', 'hello')).toBe(0); + }); + + it('should return length of b for empty a', () => { + expect(levenshteinDistance('', 'abc')).toBe(3); + }); + + it('should return length of a for empty b', () => { + expect(levenshteinDistance('abc', '')).toBe(3); + }); + + it('should return 0 for two empty strings', () => { + expect(levenshteinDistance('', '')).toBe(0); + }); + + it('should compute correct distance for single char difference', () => { + expect(levenshteinDistance('cat', 'bat')).toBe(1); + }); + + it('should compute correct distance for insertion', () => { + expect(levenshteinDistance('cat', 'cats')).toBe(1); + }); + + it('should compute correct distance for deletion', () => { + expect(levenshteinDistance('cats', 'cat')).toBe(1); + }); + + it('should compute correct distance for completely different strings', () => { + expect(levenshteinDistance('abc', 'xyz')).toBe(3); + }); + + it('should handle longer strings', () => { + const d = levenshteinDistance('sitting', 'kitten'); + expect(d).toBe(3); + }); + }); + + describe('similarityScore', () => { + it('should return 1 for identical strings', () => { + expect(similarityScore('hello', 'hello')).toBe(1); + }); + + it('should return 1 for two empty strings', () => { + expect(similarityScore('', '')).toBe(1); + }); + + it('should return 0 for completely different single-char strings', () => { + expect(similarityScore('a', 'b')).toBe(0); + }); + + it('should return a value between 0 and 1', () => { + const score = similarityScore('agent', 'agnet'); + expect(score).toBeGreaterThan(0); + expect(score).toBeLessThanOrEqual(1); + }); + + it('should be case-insensitive', () => { + const score = similarityScore('Agent', 'agent'); + expect(score).toBe(1); + }); + }); + + describe('findSimilar', () => { + const candidates = ['agent', 'swarm', 'memory', 'config', 'hooks', 'init', 'status']; + + it('should find similar commands for typo', () => { + const results = findSimilar('agnet', candidates); + expect(results).toContain('agent'); + }); + + it('should find similar commands for prefix', () => { + const results = findSimilar('con', candidates); + expect(results).toContain('config'); + }); + + it('should return empty for completely unrelated input', () => { + const results = findSimilar('zzzzzzzzzzzzz', candidates); + expect(results).toEqual([]); + }); + + it('should limit results to maxSuggestions', () => { + const results = findSimilar('a', candidates, { maxSuggestions: 2 }); + expect(results.length).toBeLessThanOrEqual(2); + }); + + it('should respect minSimilarity threshold', () => { + const results = findSimilar('xyz', candidates, { minSimilarity: 0.9 }); + expect(results).toEqual([]); + }); + }); + + describe('formatSuggestion', () => { + it('should return empty string for no suggestions', () => { + expect(formatSuggestion('bad', [])).toBe(''); + }); + + it('should format single suggestion', () => { + const result = formatSuggestion('agnet', ['agent']); + expect(result).toContain('Did you mean'); + expect(result).toContain('agent'); + }); + + it('should format multiple suggestions', () => { + const result = formatSuggestion('con', ['config', 'completions']); + expect(result).toContain('Did you mean'); + expect(result).toContain('config'); + expect(result).toContain('completions'); + }); + + it('should use context-appropriate prefix for subcommand', () => { + const result = formatSuggestion('spwn', ['spawn'], 'subcommand'); + expect(result).toContain('Available subcommands'); + }); + + it('should use context-appropriate prefix for value', () => { + const result = formatSuggestion('jsonn', ['json', 'table'], 'value'); + expect(result).toContain('Valid values'); + }); + }); + + describe('COMMON_TYPOS', () => { + it('should map common agent typos', () => { + expect(COMMON_TYPOS['agnet']).toBe('agent'); + expect(COMMON_TYPOS['agen']).toBe('agent'); + }); + + it('should map common memory typos', () => { + expect(COMMON_TYPOS['memroy']).toBe('memory'); + expect(COMMON_TYPOS['mem']).toBe('memory'); + expect(COMMON_TYPOS['memmory']).toBe('memory'); + }); + + it('should map common swarm typos', () => { + expect(COMMON_TYPOS['swarrm']).toBe('swarm'); + expect(COMMON_TYPOS['swarn']).toBe('swarm'); + }); + + it('should map common hive-mind typos', () => { + expect(COMMON_TYPOS['hive']).toBe('hive-mind'); + expect(COMMON_TYPOS['hivemind']).toBe('hive-mind'); + expect(COMMON_TYPOS['hive_mind']).toBe('hive-mind'); + }); + + it('should map status typos', () => { + expect(COMMON_TYPOS['staus']).toBe('status'); + expect(COMMON_TYPOS['stauts']).toBe('status'); + }); + + it('should map daemon typos', () => { + expect(COMMON_TYPOS['deamon']).toBe('daemon'); + }); + + it('should map workflow typos', () => { + expect(COMMON_TYPOS['wf']).toBe('workflow'); + expect(COMMON_TYPOS['wokflow']).toBe('workflow'); + }); + }); + + describe('getTypoCorrection', () => { + it('should return correction for known typo', () => { + expect(getTypoCorrection('agnet')).toBe('agent'); + }); + + it('should return undefined for unknown input', () => { + expect(getTypoCorrection('qwerty')).toBeUndefined(); + }); + + it('should be case-insensitive', () => { + expect(getTypoCorrection('AGNET')).toBe('agent'); + }); + }); + + describe('suggestCommand', () => { + const commands = ['agent', 'swarm', 'memory', 'config', 'hooks', 'init', 'status', 'daemon']; + + it('should suggest correction for common typo', () => { + const result = suggestCommand('agnet', commands); + expect(result.correction).toBe('agent'); + expect(result.suggestions).toContain('agent'); + }); + + it('should suggest similar commands for unknown input', () => { + const result = suggestCommand('swar', commands); + expect(result.suggestions.length).toBeGreaterThan(0); + expect(result.suggestions).toContain('swarm'); + }); + + it('should provide helpful message when no suggestions found', () => { + const result = suggestCommand('zzzzzzzzzzzzz', commands); + expect(result.suggestions).toEqual([]); + expect(result.message).toContain('--help'); + }); + + it('should provide "Did you mean" message for single suggestion', () => { + const result = suggestCommand('agnet', commands); + expect(result.message).toContain('Did you mean'); + }); + }); +}); + +// ============================================================================ +// SECTION 6: Init System Tests +// ============================================================================ + +import { generateSettings, generateSettingsJson } from '../src/init/settings-generator.js'; +import { generateClaudeMd, generateMinimalClaudeMd, CLAUDE_MD_TEMPLATES } from '../src/init/claudemd-generator.js'; +import { DEFAULT_INIT_OPTIONS, MINIMAL_INIT_OPTIONS, FULL_INIT_OPTIONS, detectPlatform } from '../src/init/types.js'; +import type { InitOptions, ClaudeMdTemplate } from '../src/init/types.js'; + +describe('Init System', () => { + describe('DEFAULT_INIT_OPTIONS', () => { + it('should have all components enabled by default', () => { + expect(DEFAULT_INIT_OPTIONS.components.settings).toBe(true); + expect(DEFAULT_INIT_OPTIONS.components.skills).toBe(true); + expect(DEFAULT_INIT_OPTIONS.components.runtime).toBe(true); + expect(DEFAULT_INIT_OPTIONS.components.claudeMd).toBe(true); + }); + + it('should have hierarchical-mesh topology', () => { + expect(DEFAULT_INIT_OPTIONS.runtime.topology).toBe('hierarchical-mesh'); + }); + + it('should have HNSW enabled', () => { + expect(DEFAULT_INIT_OPTIONS.runtime.enableHNSW).toBe(true); + }); + + it('should have neural enabled', () => { + expect(DEFAULT_INIT_OPTIONS.runtime.enableNeural).toBe(true); + }); + + it('should have hybrid memory backend', () => { + expect(DEFAULT_INIT_OPTIONS.runtime.memoryBackend).toBe('hybrid'); + }); + + it('should have force=false', () => { + expect(DEFAULT_INIT_OPTIONS.force).toBe(false); + }); + + it('should have interactive=true', () => { + expect(DEFAULT_INIT_OPTIONS.interactive).toBe(true); + }); + }); + + describe('MINIMAL_INIT_OPTIONS', () => { + it('should have fewer components enabled', () => { + expect(MINIMAL_INIT_OPTIONS.components.commands).toBe(false); + expect(MINIMAL_INIT_OPTIONS.components.agents).toBe(false); + expect(MINIMAL_INIT_OPTIONS.components.helpers).toBe(false); + expect(MINIMAL_INIT_OPTIONS.components.statusline).toBe(false); + }); + + it('should have simpler topology', () => { + expect(MINIMAL_INIT_OPTIONS.runtime.topology).toBe('mesh'); + }); + + it('should have HNSW disabled', () => { + expect(MINIMAL_INIT_OPTIONS.runtime.enableHNSW).toBe(false); + }); + + it('should have fewer max agents', () => { + expect(MINIMAL_INIT_OPTIONS.runtime.maxAgents).toBeLessThan( + DEFAULT_INIT_OPTIONS.runtime.maxAgents, + ); + }); + }); + + describe('FULL_INIT_OPTIONS', () => { + it('should have all components enabled', () => { + const comps = FULL_INIT_OPTIONS.components; + expect(comps.settings).toBe(true); + expect(comps.skills).toBe(true); + expect(comps.commands).toBe(true); + expect(comps.agents).toBe(true); + expect(comps.helpers).toBe(true); + expect(comps.statusline).toBe(true); + expect(comps.mcp).toBe(true); + expect(comps.runtime).toBe(true); + expect(comps.claudeMd).toBe(true); + }); + + it('should have dualMode skills enabled', () => { + expect(FULL_INIT_OPTIONS.skills.dualMode).toBe(true); + }); + + it('should have all MCP servers enabled', () => { + expect(FULL_INIT_OPTIONS.mcp.claudeFlow).toBe(true); + expect(FULL_INIT_OPTIONS.mcp.ruvSwarm).toBe(true); + expect(FULL_INIT_OPTIONS.mcp.flowNexus).toBe(true); + }); + }); + + describe('detectPlatform', () => { + it('should return valid platform info', () => { + const platform = detectPlatform(); + expect(platform).toBeDefined(); + expect(['windows', 'darwin', 'linux']).toContain(platform.os); + expect(typeof platform.arch).toBe('string'); + expect(typeof platform.nodeVersion).toBe('string'); + expect(typeof platform.shell).toBe('string'); + expect(typeof platform.homeDir).toBe('string'); + expect(typeof platform.configDir).toBe('string'); + }); + + it('should detect node version starting with v', () => { + const platform = detectPlatform(); + expect(platform.nodeVersion).toMatch(/^v\d+/); + }); + }); + + describe('generateSettings', () => { + it('should return an object', () => { + const settings = generateSettings(DEFAULT_INIT_OPTIONS); + expect(typeof settings).toBe('object'); + expect(settings).not.toBeNull(); + }); + + it('should include hooks when settings component is enabled', () => { + const settings = generateSettings(DEFAULT_INIT_OPTIONS) as Record; + expect(settings.hooks).toBeDefined(); + }); + + it('should include permissions', () => { + const settings = generateSettings(DEFAULT_INIT_OPTIONS) as Record; + expect(settings.permissions).toBeDefined(); + const perms = settings.permissions as Record; + expect(perms.allow).toBeDefined(); + expect(perms.deny).toBeDefined(); + }); + + it('should include attribution', () => { + const settings = generateSettings(DEFAULT_INIT_OPTIONS) as Record; + expect(settings.attribution).toBeDefined(); + }); + + it('should include env with agent teams enabled', () => { + const settings = generateSettings(DEFAULT_INIT_OPTIONS) as Record; + expect(settings.env).toBeDefined(); + const env = settings.env as Record; + expect(env.CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS).toBe('1'); + }); + + it('should include claudeFlow v3 settings', () => { + const settings = generateSettings(DEFAULT_INIT_OPTIONS) as Record; + expect(settings.claudeFlow).toBeDefined(); + const cf = settings.claudeFlow as Record; + expect(cf.version).toBe('3.0.0'); + expect(cf.enabled).toBe(true); + }); + + it('should deny reading .env files', () => { + const settings = generateSettings(DEFAULT_INIT_OPTIONS) as Record; + const perms = settings.permissions as { deny: string[] }; + expect(perms.deny).toContain('Read(./.env)'); + }); + }); + + describe('generateSettingsJson', () => { + it('should return valid JSON string', () => { + const json = generateSettingsJson(DEFAULT_INIT_OPTIONS); + expect(typeof json).toBe('string'); + const parsed = JSON.parse(json); + expect(typeof parsed).toBe('object'); + }); + }); + + describe('generateClaudeMd', () => { + it('should return non-empty string', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS); + expect(typeof md).toBe('string'); + expect(md.length).toBeGreaterThan(100); + }); + + it('should contain header with RuFlo V3', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS); + expect(md).toContain('RuFlo V3'); + }); + + it('should contain behavioral rules', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS); + expect(md).toContain('Behavioral Rules'); + }); + + it('should contain file organization', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS); + expect(md).toContain('File Organization'); + }); + + it('should contain project architecture', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS); + expect(md).toContain('Project Architecture'); + }); + + it('should contain anti-drift configuration', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'standard'); + expect(md).toContain('Anti-Drift'); + }); + + it('standard template should include swarm orchestration', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'standard'); + expect(md).toContain('Swarm Orchestration'); + }); + + it('full template should include hooks system', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'full'); + expect(md).toContain('Hooks System'); + }); + + it('full template should include intelligence system', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'full'); + expect(md).toContain('Intelligence System'); + }); + + it('security template should include security rules', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'security'); + expect(md).toContain('Security'); + }); + + it('performance template should include performance section', () => { + const md = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'performance'); + expect(md).toContain('Performance'); + }); + + it('minimal template should be shorter than full template', () => { + const minimal = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'minimal'); + const full = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'full'); + expect(minimal.length).toBeLessThan(full.length); + }); + }); + + describe('generateMinimalClaudeMd', () => { + it('should produce same result as generateClaudeMd with minimal template', () => { + const a = generateMinimalClaudeMd(DEFAULT_INIT_OPTIONS); + const b = generateClaudeMd(DEFAULT_INIT_OPTIONS, 'minimal'); + expect(a).toBe(b); + }); + }); + + describe('CLAUDE_MD_TEMPLATES', () => { + it('should have 6 templates', () => { + expect(CLAUDE_MD_TEMPLATES.length).toBe(6); + }); + + it('should include all template names', () => { + const names = CLAUDE_MD_TEMPLATES.map((t) => t.name); + expect(names).toContain('minimal'); + expect(names).toContain('standard'); + expect(names).toContain('full'); + expect(names).toContain('security'); + expect(names).toContain('performance'); + expect(names).toContain('solo'); + }); + + it('each template should have a description', () => { + for (const tmpl of CLAUDE_MD_TEMPLATES) { + expect(typeof tmpl.description).toBe('string'); + expect(tmpl.description.length).toBeGreaterThan(10); + } + }); + }); +}); + +// ============================================================================ +// SECTION 7: Error Type Tests +// ============================================================================ + +import { + CLIError, + ValidationError, + ConfigError, + CommandNotFoundError, +} from '../src/types.js'; + +describe('Error Types', () => { + describe('CLIError', () => { + it('should have name, message, code, and exitCode', () => { + const err = new CLIError('test error', 'TEST_CODE', 2); + expect(err.name).toBe('CLIError'); + expect(err.message).toBe('test error'); + expect(err.code).toBe('TEST_CODE'); + expect(err.exitCode).toBe(2); + }); + + it('should default exitCode to 1', () => { + const err = new CLIError('err', 'CODE'); + expect(err.exitCode).toBe(1); + }); + + it('should accept details', () => { + const err = new CLIError('err', 'CODE', 1, { extra: 'info' }); + expect(err.details).toEqual({ extra: 'info' }); + }); + + it('should be an instance of Error', () => { + const err = new CLIError('err', 'CODE'); + expect(err).toBeInstanceOf(Error); + }); + }); + + describe('ValidationError', () => { + it('should have VALIDATION_ERROR code', () => { + const err = new ValidationError('invalid input'); + expect(err.code).toBe('VALIDATION_ERROR'); + expect(err.name).toBe('ValidationError'); + }); + + it('should be an instance of CLIError', () => { + const err = new ValidationError('invalid'); + expect(err).toBeInstanceOf(CLIError); + }); + }); + + describe('ConfigError', () => { + it('should have CONFIG_ERROR code', () => { + const err = new ConfigError('bad config'); + expect(err.code).toBe('CONFIG_ERROR'); + expect(err.name).toBe('ConfigError'); + }); + }); + + describe('CommandNotFoundError', () => { + it('should include command name in message', () => { + const err = new CommandNotFoundError('foobar'); + expect(err.message).toContain('foobar'); + expect(err.code).toBe('COMMAND_NOT_FOUND'); + expect(err.exitCode).toBe(127); + }); + }); +}); diff --git a/v3/@claude-flow/cli/__tests__/config-adapter-deep.test.ts b/v3/@claude-flow/cli/__tests__/config-adapter-deep.test.ts new file mode 100644 index 0000000000..8a2ffe6de4 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/config-adapter-deep.test.ts @@ -0,0 +1,415 @@ +/** + * Config Adapter Deep Edge-Case Tests + * + * Covers branches not exercised by the existing config-adapter.test.ts: + * - normalizeTopology: 'adaptive' -> 'hybrid', unknown -> 'hierarchical' + * - normalizeMemoryBackend: 'redis' -> 'memory', unknown -> 'hybrid' + * - denormalizeTopology: 'hybrid' -> 'hierarchical-mesh', passthrough for others + * - Missing/undefined nested fields in SystemConfig + * - v3ConfigToSystemConfig with different coordination + HNSW combos + */ + +import { describe, it, expect } from 'vitest'; +import { systemConfigToV3Config, v3ConfigToSystemConfig } from '../src/config-adapter.js'; +import type { SystemConfig } from '@claude-flow/shared'; +import type { V3Config } from '../src/types.js'; + +// --------------------------------------------------------------------------- +// Helper: minimal valid SystemConfig +// --------------------------------------------------------------------------- +function minimalSystemConfig(overrides: Record = {}): SystemConfig { + return { + orchestrator: { + lifecycle: { + autoStart: false, + maxConcurrentAgents: 5, + shutdownTimeoutMs: 30000, + cleanupOrphanedAgents: true, + }, + session: { + dataDir: '/data', + persistState: true, + stateFile: 'session.json', + }, + monitoring: { + enabled: true, + metricsIntervalMs: 5000, + healthCheckIntervalMs: 10000, + }, + }, + swarm: { topology: 'hierarchical', maxAgents: 10 }, + memory: { type: 'hybrid' }, + mcp: { + enabled: false, + transport: { type: 'stdio', host: 'localhost', port: 3000 }, + enabledTools: [], + security: { + requireAuth: false, + allowedOrigins: ['*'], + rateLimiting: { enabled: true, maxRequestsPerMinute: 100 }, + }, + }, + logging: { level: 'info', pretty: true, destination: 'console', format: 'text' }, + hooks: { enabled: false, autoExecute: false, definitions: [] }, + ...overrides, + } as SystemConfig; +} + +// --------------------------------------------------------------------------- +// Helper: minimal valid V3Config +// --------------------------------------------------------------------------- +function minimalV3Config(overrides: Partial = {}): V3Config { + return { + version: '3.0.0', + projectRoot: '/test', + agents: { + defaultType: 'coder', + autoSpawn: false, + maxConcurrent: 10, + timeout: 30000, + providers: [], + }, + swarm: { + topology: 'hierarchical', + maxAgents: 10, + autoScale: false, + coordinationStrategy: 'leader', + healthCheckInterval: 5000, + }, + memory: { + backend: 'hybrid', + persistPath: '/data', + cacheSize: 100000, + enableHNSW: false, + vectorDimension: 1536, + }, + mcp: { + serverHost: 'localhost', + serverPort: 3000, + autoStart: false, + transportType: 'stdio', + tools: [], + }, + cli: { + colorOutput: true, + interactive: true, + verbosity: 'normal', + outputFormat: 'text', + progressStyle: 'spinner', + }, + hooks: { enabled: false, autoExecute: false, hooks: [] }, + ...overrides, + }; +} + +// =========================================================================== +// systemConfigToV3Config edge cases +// =========================================================================== +describe('ConfigAdapter deep edge cases', () => { + describe('normalizeTopology', () => { + it('should map "adaptive" to "hybrid"', () => { + const cfg = minimalSystemConfig({ swarm: { topology: 'adaptive', maxAgents: 8 } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.topology).toBe('hybrid'); + }); + + it('should default unknown topology to "hierarchical"', () => { + const cfg = minimalSystemConfig({ swarm: { topology: 'banana', maxAgents: 4 } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.topology).toBe('hierarchical'); + }); + + it('should pass through every recognized topology', () => { + const topologies = ['hierarchical', 'mesh', 'ring', 'star', 'hybrid', 'hierarchical-mesh'] as const; + for (const t of topologies) { + const cfg = minimalSystemConfig({ swarm: { topology: t, maxAgents: 5 } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.topology).toBe(t); + } + }); + + it('should default to "hierarchical" when topology is undefined', () => { + const cfg = minimalSystemConfig({ swarm: { maxAgents: 5 } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.topology).toBe('hierarchical'); + }); + }); + + describe('normalizeMemoryBackend', () => { + it('should map "redis" to "memory"', () => { + const cfg = minimalSystemConfig({ memory: { type: 'redis' } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.backend).toBe('memory'); + }); + + it('should default unknown backend to "hybrid"', () => { + const cfg = minimalSystemConfig({ memory: { type: 'unknown-db' } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.backend).toBe('hybrid'); + }); + + it('should default undefined backend to "hybrid"', () => { + const cfg = minimalSystemConfig({ memory: {} }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.backend).toBe('hybrid'); + }); + + it('should pass through every recognized backend', () => { + const backends = ['memory', 'sqlite', 'agentdb', 'hybrid'] as const; + for (const b of backends) { + const cfg = minimalSystemConfig({ memory: { type: b } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.backend).toBe(b); + } + }); + }); + + describe('missing optional fields in SystemConfig', () => { + it('should use default projectRoot from process.cwd when dataDir missing', () => { + const cfg = minimalSystemConfig(); + // Overwrite session to not have dataDir + (cfg.orchestrator as any).session = { + persistState: true, + stateFile: 'session.json', + }; + const v3 = systemConfigToV3Config(cfg); + // When dataDir is undefined, falls back to process.cwd() + expect(typeof v3.projectRoot).toBe('string'); + expect(v3.projectRoot.length).toBeGreaterThan(0); + }); + + it('should use default memory path when path is missing', () => { + const cfg = minimalSystemConfig({ memory: { type: 'sqlite' } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.persistPath).toBe('./data/memory'); + }); + + it('should use default MCP host when transport host is missing', () => { + const cfg = minimalSystemConfig(); + (cfg.mcp as any).transport = { type: 'stdio', port: 3000 }; + const v3 = systemConfigToV3Config(cfg); + expect(v3.mcp.serverHost).toBe('localhost'); + }); + + it('should use default maxAgents of 15 when swarm.maxAgents is missing', () => { + const cfg = minimalSystemConfig({ swarm: { topology: 'mesh' } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.maxAgents).toBe(15); + }); + + it('should use default cacheSize when memory.maxSize is missing', () => { + const cfg = minimalSystemConfig({ memory: { type: 'hybrid' } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.cacheSize).toBe(1000000); + }); + + it('should set enableHNSW false when agentdb indexType is not hnsw', () => { + const cfg = minimalSystemConfig({ + memory: { + type: 'agentdb', + agentdb: { indexType: 'flat', dimensions: 768 }, + }, + }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.enableHNSW).toBe(false); + }); + + it('should set enableHNSW true when agentdb indexType is hnsw', () => { + const cfg = minimalSystemConfig({ + memory: { + type: 'agentdb', + agentdb: { indexType: 'hnsw', dimensions: 768 }, + }, + }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.memory.enableHNSW).toBe(true); + }); + }); + + describe('swarm coordination mapping in systemConfigToV3Config', () => { + it('should set coordinationStrategy to "consensus" when consensusRequired is true', () => { + const cfg = minimalSystemConfig({ + swarm: { + topology: 'mesh', + maxAgents: 10, + coordination: { consensusRequired: true, timeoutMs: 5000 }, + }, + }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.coordinationStrategy).toBe('consensus'); + }); + + it('should set coordinationStrategy to "leader" when consensusRequired is false', () => { + const cfg = minimalSystemConfig({ + swarm: { + topology: 'mesh', + maxAgents: 10, + coordination: { consensusRequired: false, timeoutMs: 5000 }, + }, + }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.coordinationStrategy).toBe('leader'); + }); + + it('should default autoScale to false when swarm.autoScale is missing', () => { + const cfg = minimalSystemConfig({ swarm: { topology: 'mesh', maxAgents: 5 } }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.autoScale).toBe(false); + }); + + it('should pick up autoScale enabled from SystemConfig', () => { + const cfg = minimalSystemConfig({ + swarm: { + topology: 'mesh', + maxAgents: 10, + autoScale: { enabled: true }, + }, + }); + const v3 = systemConfigToV3Config(cfg); + expect(v3.swarm.autoScale).toBe(true); + }); + }); + + // =========================================================================== + // v3ConfigToSystemConfig edge cases + // =========================================================================== + describe('denormalizeTopology in v3ConfigToSystemConfig', () => { + it('should map "hybrid" back to "hierarchical-mesh"', () => { + const v3 = minimalV3Config({ + swarm: { ...minimalV3Config().swarm, topology: 'hybrid' }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.topology).toBe('hierarchical-mesh'); + }); + + it('should pass through non-hybrid topologies unchanged', () => { + const topologies = ['hierarchical', 'mesh', 'ring', 'star', 'hierarchical-mesh'] as const; + for (const t of topologies) { + const v3 = minimalV3Config({ + swarm: { ...minimalV3Config().swarm, topology: t }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.topology).toBe(t); + } + }); + }); + + describe('consensus mapping in v3ConfigToSystemConfig', () => { + it('should set consensusRequired to true for "consensus" strategy', () => { + const v3 = minimalV3Config({ + swarm: { ...minimalV3Config().swarm, coordinationStrategy: 'consensus' }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.coordination?.consensusRequired).toBe(true); + }); + + it('should set consensusRequired to false for non-consensus strategy', () => { + const v3 = minimalV3Config({ + swarm: { ...minimalV3Config().swarm, coordinationStrategy: 'leader' }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.coordination?.consensusRequired).toBe(false); + }); + }); + + describe('HNSW toggle in v3ConfigToSystemConfig', () => { + it('should set indexType "hnsw" when enableHNSW is true', () => { + const v3 = minimalV3Config({ + memory: { ...minimalV3Config().memory, enableHNSW: true }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.memory?.agentdb?.indexType).toBe('hnsw'); + }); + + it('should set indexType "flat" when enableHNSW is false', () => { + const v3 = minimalV3Config({ + memory: { ...minimalV3Config().memory, enableHNSW: false }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.memory?.agentdb?.indexType).toBe('flat'); + }); + }); + + describe('autoScale mapping in v3ConfigToSystemConfig', () => { + it('should set autoScale.enabled true when v3 autoScale is true', () => { + const v3 = minimalV3Config({ + swarm: { ...minimalV3Config().swarm, autoScale: true }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.autoScale?.enabled).toBe(true); + }); + + it('should set autoScale.enabled false when v3 autoScale is false', () => { + const v3 = minimalV3Config({ + swarm: { ...minimalV3Config().swarm, autoScale: false }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.autoScale?.enabled).toBe(false); + }); + + it('should carry maxAgents into autoScale.maxAgents', () => { + const v3 = minimalV3Config({ + swarm: { ...minimalV3Config().swarm, maxAgents: 25, autoScale: true }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.autoScale?.maxAgents).toBe(25); + }); + }); + + describe('MCP transport mapping in v3ConfigToSystemConfig', () => { + it('should map transportType "http" correctly', () => { + const v3 = minimalV3Config({ + mcp: { ...minimalV3Config().mcp, transportType: 'http', serverPort: 8080 }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.mcp?.transport?.type).toBe('http'); + expect(sys.mcp?.transport?.port).toBe(8080); + }); + + it('should map transportType "websocket" correctly', () => { + const v3 = minimalV3Config({ + mcp: { ...minimalV3Config().mcp, transportType: 'websocket', serverHost: '0.0.0.0' }, + }); + const sys = v3ConfigToSystemConfig(v3); + expect(sys.mcp?.transport?.type).toBe('websocket'); + expect(sys.mcp?.transport?.host).toBe('0.0.0.0'); + }); + }); + + describe('round-trip preserves diverse configurations', () => { + it('should round-trip with agentdb + hnsw enabled', () => { + const original = minimalSystemConfig({ + memory: { + type: 'agentdb', + path: '/my/data', + maxSize: 500000, + agentdb: { dimensions: 768, indexType: 'hnsw' }, + }, + }); + const v3 = systemConfigToV3Config(original); + expect(v3.memory.backend).toBe('agentdb'); + expect(v3.memory.enableHNSW).toBe(true); + expect(v3.memory.vectorDimension).toBe(768); + + const sys = v3ConfigToSystemConfig(v3); + expect(sys.memory?.type).toBe('agentdb'); + expect(sys.memory?.agentdb?.indexType).toBe('hnsw'); + expect(sys.memory?.agentdb?.dimensions).toBe(768); + }); + + it('should round-trip with consensus coordination', () => { + const original = minimalSystemConfig({ + swarm: { + topology: 'hierarchical-mesh', + maxAgents: 12, + coordination: { consensusRequired: true, timeoutMs: 8000 }, + }, + }); + const v3 = systemConfigToV3Config(original); + expect(v3.swarm.coordinationStrategy).toBe('consensus'); + + const sys = v3ConfigToSystemConfig(v3); + expect(sys.swarm?.coordination?.consensusRequired).toBe(true); + }); + }); +}); diff --git a/v3/@claude-flow/cli/__tests__/integration-docker.test.ts b/v3/@claude-flow/cli/__tests__/integration-docker.test.ts new file mode 100644 index 0000000000..0cd8a2be8c --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/integration-docker.test.ts @@ -0,0 +1,659 @@ +/** + * Integration Docker Validation Tests + * + * Validates the RuFlo Docker-based deployment stack without running Docker. + * Checks docker-compose.yml, nginx.conf, MCP bridge source, CLI Dockerfile, + * and CLI build/init/doctor commands for correctness. + * + * 40+ test cases covering: + * - Docker Compose config parsing and service definitions + * - Nginx reverse proxy configuration + * - MCP bridge endpoint and CORS validation + * - CLI Dockerfile multi-stage build + * - CLI build, init, and doctor commands + * - Generated file validity + */ + +import { describe, it, expect, beforeAll } from 'vitest'; +import { readFileSync, existsSync, statSync } from 'fs'; +import { execSync } from 'child_process'; +import { resolve, join } from 'path'; + +// --------------------------------------------------------------------------- +// Paths +// --------------------------------------------------------------------------- + +const ROOT = resolve(__dirname, '..', '..', '..', '..'); // /workspaces/claude-flow +const CLI_DIR = resolve(__dirname, '..'); // v3/@claude-flow/cli +const RUFLO_DIR = join(ROOT, 'ruflo'); +const COMPOSE_PATH = join(RUFLO_DIR, 'docker-compose.yml'); +const NGINX_CONF_PATH = join(RUFLO_DIR, 'src', 'nginx', 'nginx.conf'); +const NGINX_DOCKERFILE = join(RUFLO_DIR, 'src', 'nginx', 'Dockerfile'); +const MCP_BRIDGE_INDEX = join(RUFLO_DIR, 'src', 'mcp-bridge', 'index.js'); +const MCP_BRIDGE_DOCKERFILE = join(RUFLO_DIR, 'src', 'mcp-bridge', 'Dockerfile'); +const CLI_DOCKERFILE = join(CLI_DIR, 'docker', 'Dockerfile'); +const ENV_EXAMPLE = join(RUFLO_DIR, '.env.example'); + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +function readFile(p: string): string { + return readFileSync(p, 'utf-8'); +} + +/** Minimal YAML parser: extracts top-level keys under `services:` */ +function parseComposeServices(yaml: string): string[] { + const lines = yaml.split('\n'); + const services: string[] = []; + let inServices = false; + for (const line of lines) { + if (/^services:/.test(line)) { inServices = true; continue; } + if (inServices && /^[a-z]/.test(line)) break; // next top-level key + if (inServices && /^ [a-z][\w-]*:/.test(line)) { + services.push(line.trim().replace(':', '')); + } + } + return services; +} + +/** Extract all EXPOSE directives from a Dockerfile */ +function extractExposePort(content: string): number[] { + return [...content.matchAll(/^EXPOSE\s+(\d+)/gm)].map(m => parseInt(m[1], 10)); +} + +// --------------------------------------------------------------------------- +// 1. Docker Compose Config Validation +// --------------------------------------------------------------------------- + +describe('Docker Compose Configuration', () => { + let composeContent: string; + + beforeAll(() => { + composeContent = readFile(COMPOSE_PATH); + }); + + it('docker-compose.yml exists', () => { + expect(existsSync(COMPOSE_PATH)).toBe(true); + }); + + it('defines exactly 4 services (mongodb, mcp-bridge, nginx, chat-ui)', () => { + const services = parseComposeServices(composeContent); + expect(services).toEqual( + expect.arrayContaining(['mongodb', 'mcp-bridge', 'nginx', 'chat-ui']), + ); + expect(services).toHaveLength(4); + }); + + it('mongodb uses mongo:7 image', () => { + expect(composeContent).toMatch(/image:\s*mongo:7/); + }); + + it('mongodb exposes port 27017', () => { + expect(composeContent).toMatch(/"27017:27017"/); + }); + + it('mongodb has a named volume for data persistence', () => { + expect(composeContent).toMatch(/mongo-data:\/data\/db/); + expect(composeContent).toMatch(/^volumes:\s*\n\s+mongo-data:/m); + }); + + it('mcp-bridge builds from correct context', () => { + expect(composeContent).toMatch(/context:\s*\.\/src\/ruvocal\/mcp-bridge/); + }); + + it('mcp-bridge publishes port 3001', () => { + expect(composeContent).toMatch(/"3001:3001"/); + }); + + it('mcp-bridge has a healthcheck', () => { + // The healthcheck should reference /health + const mcpSection = composeContent.split('mcp-bridge:')[1]?.split(/\n [a-z]/)[0] ?? ''; + expect(composeContent).toContain('healthcheck'); + expect(composeContent).toContain('/health'); + }); + + it('nginx depends on chat-ui and mcp-bridge', () => { + // Extract nginx depends_on block + const nginxBlock = composeContent.split('nginx:')[1]?.split(/\n [a-z]/)[0] ?? ''; + expect(nginxBlock).toContain('chat-ui'); + expect(nginxBlock).toContain('mcp-bridge'); + }); + + it('nginx publishes port 3000', () => { + expect(composeContent).toMatch(/"3000:3000"/); + }); + + it('nginx has a healthcheck', () => { + // The nginx healthcheck should use wget + expect(composeContent).toContain('wget'); + expect(composeContent).toContain('http://localhost:3000'); + }); + + it('chat-ui depends on mongodb and mcp-bridge', () => { + const chatBlock = composeContent.split('chat-ui:')[1] ?? ''; + const depsBlock = chatBlock.split('depends_on:')[1]?.split(/\n [a-z]/)[0] ?? ''; + expect(depsBlock).toContain('mongodb'); + expect(depsBlock).toContain('mcp-bridge'); + }); + + it('chat-ui exposes port 3000 internally only', () => { + const chatBlock = composeContent.split('chat-ui:')[1]?.split(/\n [a-z]/)[0] ?? ''; + // Should use "expose" not "ports" since nginx fronts it + expect(chatBlock).toContain('expose'); + }); + + it('chat-ui injects DOTENV_LOCAL with required env vars', () => { + expect(composeContent).toContain('DOTENV_LOCAL'); + expect(composeContent).toContain('MONGODB_URL=mongodb://mongodb:27017'); + expect(composeContent).toContain('PUBLIC_APP_NAME'); + expect(composeContent).toContain('OPENAI_BASE_URL=http://mcp-bridge:3001'); + }); + + it('all services use restart: unless-stopped', () => { + const matches = composeContent.match(/restart:\s*unless-stopped/g); + expect(matches).not.toBeNull(); + expect(matches!.length).toBeGreaterThanOrEqual(4); + }); + + it('MCP tool group env vars are defined for mcp-bridge', () => { + const groups = [ + 'MCP_GROUP_INTELLIGENCE', 'MCP_GROUP_AGENTS', + 'MCP_GROUP_MEMORY', 'MCP_GROUP_DEVTOOLS', + 'MCP_GROUP_SECURITY', 'MCP_GROUP_BROWSER', + 'MCP_GROUP_NEURAL', 'MCP_GROUP_AGENTIC_FLOW', + 'MCP_GROUP_CLAUDE_CODE', 'MCP_GROUP_GEMINI', + 'MCP_GROUP_CODEX', + ]; + for (const g of groups) { + expect(composeContent).toContain(g); + } + }); +}); + +// --------------------------------------------------------------------------- +// 2. Nginx Config Validation +// --------------------------------------------------------------------------- + +describe('Nginx Configuration', () => { + let nginxContent: string; + + beforeAll(() => { + nginxContent = readFile(NGINX_CONF_PATH); + }); + + it('nginx.conf exists', () => { + expect(existsSync(NGINX_CONF_PATH)).toBe(true); + }); + + it('listens on port 3000', () => { + expect(nginxContent).toMatch(/listen\s+3000/); + }); + + it('gzip is disabled (required for sub_filter)', () => { + expect(nginxContent).toMatch(/gzip\s+off/); + }); + + it('sets Access-Control-Allow-Origin header', () => { + expect(nginxContent).toContain('Access-Control-Allow-Origin'); + }); + + it('sets Access-Control-Allow-Methods header', () => { + expect(nginxContent).toContain('Access-Control-Allow-Methods'); + expect(nginxContent).toContain('GET'); + expect(nginxContent).toContain('POST'); + expect(nginxContent).toContain('OPTIONS'); + }); + + it('sets Access-Control-Allow-Headers header', () => { + expect(nginxContent).toContain('Access-Control-Allow-Headers'); + expect(nginxContent).toContain('Content-Type'); + expect(nginxContent).toContain('Authorization'); + }); + + it('handles OPTIONS preflight requests with 204', () => { + expect(nginxContent).toMatch(/if\s*\(\$request_method\s*=\s*OPTIONS\)/); + expect(nginxContent).toContain('return 204'); + }); + + it('proxies root location to chat-ui:3000', () => { + expect(nginxContent).toMatch(/proxy_pass\s+http:\/\/chat-ui:3000/); + }); + + it('sets WebSocket upgrade headers', () => { + expect(nginxContent).toContain('Upgrade'); + expect(nginxContent).toContain('"upgrade"'); + expect(nginxContent).toContain('$http_upgrade'); + }); + + it('disables upstream gzip via Accept-Encoding header', () => { + expect(nginxContent).toContain('proxy_set_header Accept-Encoding ""'); + }); + + it('injects RuFlo welcome.js script via sub_filter', () => { + expect(nginxContent).toMatch(/sub_filter\s+'<\/head>'\s+'