|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + agentCliHelpSection, |
| 7 | + executeAgentCliCommand, |
| 8 | + isRootHelpInvocation, |
| 9 | + matchAgentCliCommand, |
| 10 | +} from '@/lib/mothership/tools/handlers/agent-cli' |
| 11 | +import type { AgentCliRuntime } from '@/lib/mothership/tools/handlers/agent-cli/types' |
| 12 | + |
| 13 | +const WORKFLOW_STATE = { |
| 14 | + blocks: { |
| 15 | + 'block-1': { type: 'starter', name: 'Start', enabled: true }, |
| 16 | + 'block-2': { type: 'agent', name: 'Summarize emails', enabled: true }, |
| 17 | + }, |
| 18 | + edges: [{ source: 'block-1', target: 'block-2', sourceHandle: 'source', id: 'edge-1' }], |
| 19 | + variables: { apiBase: 'https://api.example.com' }, |
| 20 | +} |
| 21 | + |
| 22 | +function runtimeWith(responses: Record<string, unknown>): AgentCliRuntime { |
| 23 | + return { |
| 24 | + workspaceId: 'ws-1', |
| 25 | + client: { |
| 26 | + request: async <T>(path: string): Promise<T> => { |
| 27 | + const hit = responses[path] |
| 28 | + if (hit === undefined) throw new Error(`Unexpected request: ${path}`) |
| 29 | + return hit as T |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const EXPORT_PATH = '/api/v2/workflows/wf-1/export' |
| 36 | +const exportResponse = { data: { state: WORKFLOW_STATE } } |
| 37 | + |
| 38 | +describe('agent-cli routing', () => { |
| 39 | + it('matches agent commands through leading global flags', () => { |
| 40 | + const match = matchAgentCliCommand(['--output', 'json', 'workflow', 'edges', 'wf-1']) |
| 41 | + expect(match?.command.path).toEqual(['workflow', 'edges']) |
| 42 | + expect(match?.rest).toEqual(['wf-1']) |
| 43 | + }) |
| 44 | + |
| 45 | + it('leaves real CLI commands unmatched', () => { |
| 46 | + expect(matchAgentCliCommand(['workflows', 'list'])).toBeNull() |
| 47 | + expect(matchAgentCliCommand(['tables', 'get', 'tbl_1'])).toBeNull() |
| 48 | + }) |
| 49 | + |
| 50 | + it('detects root help invocations only', () => { |
| 51 | + expect(isRootHelpInvocation(['--help'])).toBe(true) |
| 52 | + expect(isRootHelpInvocation(['--output', 'json', 'help'])).toBe(true) |
| 53 | + expect(isRootHelpInvocation(['workflows', '--help'])).toBe(false) |
| 54 | + }) |
| 55 | + |
| 56 | + it('lists every registered command in the help section', () => { |
| 57 | + const section = agentCliHelpSection() |
| 58 | + for (const usage of ['workflow blocks', 'workflow edges', 'workflow grep', 'workflows grep']) { |
| 59 | + expect(section).toContain(usage) |
| 60 | + } |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +describe('workflow views', () => { |
| 65 | + it('projects just the blocks', async () => { |
| 66 | + const match = matchAgentCliCommand(['workflow', 'blocks', 'wf-1']) |
| 67 | + const result = await executeAgentCliCommand( |
| 68 | + match!, |
| 69 | + runtimeWith({ [EXPORT_PATH]: exportResponse }) |
| 70 | + ) |
| 71 | + expect(result.exitCode).toBe(0) |
| 72 | + const blocks = JSON.parse(result.stdout) |
| 73 | + expect(blocks).toEqual([ |
| 74 | + { id: 'block-1', type: 'starter', name: 'Start', enabled: true }, |
| 75 | + { id: 'block-2', type: 'agent', name: 'Summarize emails', enabled: true }, |
| 76 | + ]) |
| 77 | + }) |
| 78 | + |
| 79 | + it('projects just the edges', async () => { |
| 80 | + const match = matchAgentCliCommand(['workflow', 'edges', 'wf-1']) |
| 81 | + const result = await executeAgentCliCommand( |
| 82 | + match!, |
| 83 | + runtimeWith({ [EXPORT_PATH]: exportResponse }) |
| 84 | + ) |
| 85 | + expect(result.exitCode).toBe(0) |
| 86 | + expect(JSON.parse(result.stdout)).toEqual([ |
| 87 | + { source: 'block-1', target: 'block-2', sourceHandle: 'source' }, |
| 88 | + ]) |
| 89 | + }) |
| 90 | + |
| 91 | + it('fails usefully without a workflow id', async () => { |
| 92 | + const match = matchAgentCliCommand(['workflow', 'blocks']) |
| 93 | + const result = await executeAgentCliCommand(match!, runtimeWith({})) |
| 94 | + expect(result.exitCode).toBe(1) |
| 95 | + expect(result.stderr).toContain('Usage:') |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +describe('workflow grep', () => { |
| 100 | + it('reports matches as path: value lines', async () => { |
| 101 | + const match = matchAgentCliCommand(['workflow', 'grep', 'wf-1', 'Summarize']) |
| 102 | + const result = await executeAgentCliCommand( |
| 103 | + match!, |
| 104 | + runtimeWith({ [EXPORT_PATH]: exportResponse }) |
| 105 | + ) |
| 106 | + expect(result.exitCode).toBe(0) |
| 107 | + expect(result.stdout).toContain('.blocks.block-2.name: Summarize emails') |
| 108 | + }) |
| 109 | + |
| 110 | + it('falls back to literal search on an invalid regex', async () => { |
| 111 | + const match = matchAgentCliCommand(['workflow', 'grep', 'wf-1', 'api.example.com[']) |
| 112 | + const result = await executeAgentCliCommand( |
| 113 | + match!, |
| 114 | + runtimeWith({ [EXPORT_PATH]: exportResponse }) |
| 115 | + ) |
| 116 | + expect(result.exitCode).toBe(0) |
| 117 | + expect(result.stdout).toBe('No matches.') |
| 118 | + }) |
| 119 | + |
| 120 | + it('searches across all workspace workflows', async () => { |
| 121 | + const match = matchAgentCliCommand(['workflows', 'grep', 'Summarize']) |
| 122 | + const result = await executeAgentCliCommand( |
| 123 | + match!, |
| 124 | + runtimeWith({ |
| 125 | + '/api/v2/workflows': { |
| 126 | + data: [{ id: 'wf-1', name: 'Email digest' }], |
| 127 | + nextCursor: null, |
| 128 | + }, |
| 129 | + [EXPORT_PATH]: exportResponse, |
| 130 | + }) |
| 131 | + ) |
| 132 | + expect(result.exitCode).toBe(0) |
| 133 | + expect(result.stdout).toContain('Email digest (wf-1).blocks.block-2.name: Summarize emails') |
| 134 | + }) |
| 135 | + |
| 136 | + it('surfaces execution errors as a failed result, never a throw', async () => { |
| 137 | + const match = matchAgentCliCommand(['workflow', 'grep', 'wf-missing', 'x']) |
| 138 | + const result = await executeAgentCliCommand(match!, runtimeWith({})) |
| 139 | + expect(result.exitCode).toBe(1) |
| 140 | + expect(result.stderr).toContain('Unexpected request') |
| 141 | + }) |
| 142 | +}) |
0 commit comments