|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockExecute } = vi.hoisted(() => ({ mockExecute: vi.fn() })) |
| 7 | + |
| 8 | +vi.mock('@/executor/handlers/workflow/workflow-handler', () => ({ |
| 9 | + WorkflowBlockHandler: class { |
| 10 | + execute = mockExecute |
| 11 | + }, |
| 12 | + aggregateChildCost: (spans: Array<{ cost?: { total?: number } }>) => |
| 13 | + spans.reduce((sum, span) => sum + (span?.cost?.total ?? 0), 0), |
| 14 | +})) |
| 15 | + |
| 16 | +import { ChildWorkflowError } from '@/executor/errors/child-workflow-error' |
| 17 | +import { |
| 18 | + buildCustomBlockExecutionContext, |
| 19 | + runCustomBlockTool, |
| 20 | +} from '@/executor/handlers/workflow/custom-block-tool-runner' |
| 21 | + |
| 22 | +describe('buildCustomBlockExecutionContext', () => { |
| 23 | + it('carries consumer identity, inherits the call chain, and is fully scaffolded', () => { |
| 24 | + const ctx = buildCustomBlockExecutionContext({ |
| 25 | + workspaceId: 'ws-consumer', |
| 26 | + userId: 'u-consumer', |
| 27 | + workflowId: 'wf-parent', |
| 28 | + callChain: ['wf-parent'], |
| 29 | + billingAttribution: { actorUserId: 'u-consumer', workspaceId: 'ws-consumer' } as any, |
| 30 | + }) |
| 31 | + |
| 32 | + expect(ctx.workspaceId).toBe('ws-consumer') |
| 33 | + expect(ctx.userId).toBe('u-consumer') |
| 34 | + // Inherited (not reset) so the handler's depth guard keeps bounding recursion. |
| 35 | + expect(ctx.callChain).toEqual(['wf-parent']) |
| 36 | + // metadata must be a real object — the handler reads it unconditionally. |
| 37 | + expect(ctx.metadata).toBeTypeOf('object') |
| 38 | + expect(ctx.metadata.billingAttribution).toEqual({ |
| 39 | + actorUserId: 'u-consumer', |
| 40 | + workspaceId: 'ws-consumer', |
| 41 | + }) |
| 42 | + expect(ctx.metadata.executionMode).toBe('sync') |
| 43 | + // Non-optional scaffolding present. |
| 44 | + expect(ctx.blockStates).toBeInstanceOf(Map) |
| 45 | + expect(ctx.executedBlocks).toBeInstanceOf(Set) |
| 46 | + expect(ctx.completedLoops).toBeInstanceOf(Set) |
| 47 | + expect(ctx.activeExecutionPath).toBeInstanceOf(Set) |
| 48 | + expect(ctx.decisions.router).toBeInstanceOf(Map) |
| 49 | + expect(ctx.decisions.condition).toBeInstanceOf(Map) |
| 50 | + expect(Array.isArray(ctx.blockLogs)).toBe(true) |
| 51 | + expect(ctx.executionId).toBeTruthy() |
| 52 | + }) |
| 53 | + |
| 54 | + it('defaults the call chain to [] when none is provided', () => { |
| 55 | + expect(buildCustomBlockExecutionContext({}).callChain).toEqual([]) |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
| 59 | +describe('runCustomBlockTool', () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks() |
| 62 | + }) |
| 63 | + |
| 64 | + it('runs the handler with the synthetic ctx and returns its projected output', async () => { |
| 65 | + mockExecute.mockResolvedValue({ success: true, result: { answer: 'hi' }, cost: { total: 0.5 } }) |
| 66 | + |
| 67 | + const res = await runCustomBlockTool({ |
| 68 | + blockType: 'custom_block_abc', |
| 69 | + inputMapping: '{"field-question":"hi"}', |
| 70 | + _context: { workspaceId: 'ws-consumer', userId: 'u-consumer' }, |
| 71 | + }) |
| 72 | + |
| 73 | + expect(res.success).toBe(true) |
| 74 | + expect(res.output.cost).toEqual({ total: 0.5 }) |
| 75 | + |
| 76 | + const [ctxArg, blockArg, inputsArg] = mockExecute.mock.calls[0] |
| 77 | + expect(ctxArg.workspaceId).toBe('ws-consumer') |
| 78 | + expect(blockArg.metadata.id).toBe('custom_block_abc') |
| 79 | + expect(inputsArg).toEqual({ inputMapping: '{"field-question":"hi"}' }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('surfaces a handler failure as a clean tool error', async () => { |
| 83 | + mockExecute.mockRejectedValue(new Error('This block’s workflow is not deployed.')) |
| 84 | + |
| 85 | + const res = await runCustomBlockTool({ blockType: 'custom_block_abc', _context: {} }) |
| 86 | + |
| 87 | + expect(res.success).toBe(false) |
| 88 | + expect(res.error).toContain('not deployed') |
| 89 | + }) |
| 90 | + |
| 91 | + it('rolls up already-incurred child cost when the run fails', async () => { |
| 92 | + const err: any = new Error('child blew up') |
| 93 | + err.name = 'ChildWorkflowError' |
| 94 | + err.childTraceSpans = [{ id: 's1', name: 'child', type: 'agent', cost: { total: 0.25 } }] |
| 95 | + Object.setPrototypeOf(err, ChildWorkflowError.prototype) |
| 96 | + mockExecute.mockRejectedValue(err) |
| 97 | + |
| 98 | + const res = await runCustomBlockTool({ blockType: 'custom_block_abc', _context: {} }) |
| 99 | + |
| 100 | + expect(res.success).toBe(false) |
| 101 | + // Partial spend must not be recorded as zero-cost. |
| 102 | + expect((res.output as any).cost.total).toBeGreaterThan(0) |
| 103 | + }) |
| 104 | + |
| 105 | + it('rejects a missing block type without invoking the handler', async () => { |
| 106 | + const res = await runCustomBlockTool({ _context: {} }) |
| 107 | + expect(res.success).toBe(false) |
| 108 | + expect(mockExecute).not.toHaveBeenCalled() |
| 109 | + }) |
| 110 | +}) |
0 commit comments