|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { buildWorkflowLintReport } = vi.hoisted(() => ({ |
| 7 | + buildWorkflowLintReport: vi.fn().mockResolvedValue({ |
| 8 | + sources: ['block-1'], |
| 9 | + sinks: ['block-2'], |
| 10 | + orphanBlocks: [], |
| 11 | + emptyOutgoingPorts: [], |
| 12 | + invalidBranchPorts: [], |
| 13 | + invalidConnectionTargets: [], |
| 14 | + fieldIssues: [ |
| 15 | + { |
| 16 | + blockId: 'block-2', |
| 17 | + blockName: 'Summarize emails', |
| 18 | + missingRequiredFields: ['model'], |
| 19 | + inactiveModeValues: [], |
| 20 | + }, |
| 21 | + ], |
| 22 | + unresolvedReferences: [], |
| 23 | + }), |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@/lib/workflows/editing/lint-report', () => ({ buildWorkflowLintReport })) |
| 27 | + |
| 28 | +import { runEngine } from '@/lib/mothership/agent-cli/engines' |
| 29 | +import type { AgentCliRuntime } from '@/lib/mothership/agent-cli/types' |
| 30 | + |
| 31 | +const WORKFLOW_STATE = { |
| 32 | + blocks: { |
| 33 | + 'block-1': { type: 'starter', name: 'Start', enabled: true }, |
| 34 | + 'block-2': { type: 'agent', name: 'Summarize emails', enabled: true }, |
| 35 | + }, |
| 36 | + edges: [{ source: 'block-1', target: 'block-2', sourceHandle: 'source', id: 'edge-1' }], |
| 37 | + variables: { apiBase: 'https://api.example.com' }, |
| 38 | +} |
| 39 | + |
| 40 | +function runtimeWith(responses: Record<string, unknown>): AgentCliRuntime { |
| 41 | + return { |
| 42 | + workspaceId: 'ws-1', |
| 43 | + userId: 'user-1', |
| 44 | + client: { |
| 45 | + request: async <T>(path: string): Promise<T> => { |
| 46 | + const hit = responses[path] |
| 47 | + if (hit === undefined) throw new Error(`Unexpected request: ${path}`) |
| 48 | + return hit as T |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +const EXPORT_PATH = '/api/v2/workflows/wf-1/export' |
| 55 | +const exportResponse = { data: { state: WORKFLOW_STATE } } |
| 56 | + |
| 57 | +describe('workflow views', () => { |
| 58 | + it('projects just the blocks', async () => { |
| 59 | + const result = await runEngine( |
| 60 | + 'workflow blocks', |
| 61 | + ['wf-1'], |
| 62 | + runtimeWith({ [EXPORT_PATH]: exportResponse }), |
| 63 | + {} |
| 64 | + ) |
| 65 | + expect(result.exitCode).toBe(0) |
| 66 | + const blocks = JSON.parse(result.stdout) |
| 67 | + expect(blocks).toEqual([ |
| 68 | + { id: 'block-1', type: 'starter', name: 'Start', enabled: true }, |
| 69 | + { id: 'block-2', type: 'agent', name: 'Summarize emails', enabled: true }, |
| 70 | + ]) |
| 71 | + }) |
| 72 | + |
| 73 | + it('projects just the edges', async () => { |
| 74 | + const result = await runEngine( |
| 75 | + 'workflow edges', |
| 76 | + ['wf-1'], |
| 77 | + runtimeWith({ [EXPORT_PATH]: exportResponse }), |
| 78 | + {} |
| 79 | + ) |
| 80 | + expect(result.exitCode).toBe(0) |
| 81 | + expect(JSON.parse(result.stdout)).toEqual([ |
| 82 | + { source: 'block-1', target: 'block-2', sourceHandle: 'source' }, |
| 83 | + ]) |
| 84 | + }) |
| 85 | + |
| 86 | + it('fails usefully without a workflow id', async () => { |
| 87 | + const result = await runEngine('workflow blocks', [], runtimeWith({}), {}) |
| 88 | + expect(result.exitCode).toBe(1) |
| 89 | + expect(result.stderr).toContain('Usage:') |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +describe('files grep', () => { |
| 94 | + const FILES_LIST = { |
| 95 | + data: [ |
| 96 | + { id: 'f1', name: 'report.md', folderPath: 'docs' }, |
| 97 | + { id: 'f2', name: 'logo.png', folderPath: '' }, |
| 98 | + ], |
| 99 | + nextCursor: null, |
| 100 | + } |
| 101 | + const readText = (text: string, degraded = false) => ({ |
| 102 | + data: { text, degraded }, |
| 103 | + }) |
| 104 | + |
| 105 | + it('greps file contents with line numbers, skipping non-text files', async () => { |
| 106 | + const result = await runEngine( |
| 107 | + 'files grep', |
| 108 | + ['quarterly'], |
| 109 | + runtimeWith({ |
| 110 | + '/api/v2/files': FILES_LIST, |
| 111 | + '/api/v2/files/f1/text': readText('# Report\nQuarterly revenue was up.\n'), |
| 112 | + '/api/v2/files/f2/text': readText('', true), |
| 113 | + }), |
| 114 | + {} |
| 115 | + ) |
| 116 | + expect(result.exitCode).toBe(0) |
| 117 | + expect(result.stdout).toContain('docs/report.md:2: Quarterly revenue was up.') |
| 118 | + expect(result.stdout).not.toContain('logo.png') |
| 119 | + }) |
| 120 | + |
| 121 | + it('filters by folder prefix', async () => { |
| 122 | + const result = await runEngine( |
| 123 | + 'files grep', |
| 124 | + ['Quarterly', 'other'], |
| 125 | + runtimeWith({ '/api/v2/files': FILES_LIST }), |
| 126 | + {} |
| 127 | + ) |
| 128 | + expect(result.exitCode).toBe(0) |
| 129 | + expect(result.stdout).toContain('No matches') |
| 130 | + }) |
| 131 | +}) |
| 132 | + |
| 133 | +describe('workflow grep', () => { |
| 134 | + it('reports matches as path: value lines', async () => { |
| 135 | + const result = await runEngine( |
| 136 | + 'workflow grep', |
| 137 | + ['wf-1', 'Summarize'], |
| 138 | + runtimeWith({ [EXPORT_PATH]: exportResponse }), |
| 139 | + {} |
| 140 | + ) |
| 141 | + expect(result.exitCode).toBe(0) |
| 142 | + expect(result.stdout).toContain('.blocks.block-2.name: Summarize emails') |
| 143 | + }) |
| 144 | + |
| 145 | + it('falls back to literal search on an invalid regex', async () => { |
| 146 | + const result = await runEngine( |
| 147 | + 'workflow grep', |
| 148 | + ['wf-1', 'api.example.com['], |
| 149 | + runtimeWith({ [EXPORT_PATH]: exportResponse }), |
| 150 | + {} |
| 151 | + ) |
| 152 | + expect(result.exitCode).toBe(0) |
| 153 | + expect(result.stdout).toBe('No matches.') |
| 154 | + }) |
| 155 | + |
| 156 | + it('searches across all workspace workflows', async () => { |
| 157 | + const result = await runEngine( |
| 158 | + 'workflows grep', |
| 159 | + ['Summarize'], |
| 160 | + runtimeWith({ |
| 161 | + '/api/v2/workflows': { |
| 162 | + data: [{ id: 'wf-1', name: 'Email digest' }], |
| 163 | + nextCursor: null, |
| 164 | + }, |
| 165 | + [EXPORT_PATH]: exportResponse, |
| 166 | + }), |
| 167 | + {} |
| 168 | + ) |
| 169 | + expect(result.exitCode).toBe(0) |
| 170 | + expect(result.stdout).toContain('Email digest (wf-1).blocks.block-2.name: Summarize emails') |
| 171 | + }) |
| 172 | + |
| 173 | + it('lints a workflow through the shared engine with the caller scoped as subject', async () => { |
| 174 | + const result = await runEngine( |
| 175 | + 'workflow lint', |
| 176 | + ['wf-1'], |
| 177 | + runtimeWith({ [EXPORT_PATH]: exportResponse }), |
| 178 | + {} |
| 179 | + ) |
| 180 | + expect(result.stderr).toBe('') |
| 181 | + expect(result.exitCode).toBe(0) |
| 182 | + const report = JSON.parse(result.stdout) |
| 183 | + expect(report.fieldIssues).toHaveLength(1) |
| 184 | + expect(report.summary.length).toBeGreaterThan(0) |
| 185 | + expect(buildWorkflowLintReport).toHaveBeenCalledWith(expect.anything(), { |
| 186 | + workflowId: 'wf-1', |
| 187 | + workspaceId: 'ws-1', |
| 188 | + subjectUserId: 'user-1', |
| 189 | + }) |
| 190 | + }) |
| 191 | + |
| 192 | + it('surfaces execution errors as a failed result, never a throw', async () => { |
| 193 | + const result = await runEngine('workflow grep', ['wf-missing', 'x'], runtimeWith({}), {}) |
| 194 | + expect(result.exitCode).toBe(1) |
| 195 | + expect(result.stderr).toContain('Unexpected request') |
| 196 | + }) |
| 197 | +}) |
0 commit comments