|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { BlockType } from '@/executor/constants' |
| 3 | +import { ExecutionState } from '@/executor/execution/state' |
| 4 | +import type { ExecutionContext } from '@/executor/types' |
| 5 | +import { VariableResolver } from '@/executor/variables/resolver' |
| 6 | +import type { SerializedBlock, SerializedWorkflow } from '@/serializer/types' |
| 7 | + |
| 8 | +function createSerializedBlock(opts: { id: string; name: string; type: string }): SerializedBlock { |
| 9 | + return { |
| 10 | + id: opts.id, |
| 11 | + position: { x: 0, y: 0 }, |
| 12 | + config: { tool: opts.type, params: {} }, |
| 13 | + inputs: {}, |
| 14 | + outputs: {}, |
| 15 | + metadata: { id: opts.type, name: opts.name }, |
| 16 | + enabled: true, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe('VariableResolver', () => { |
| 21 | + it.concurrent('preserves typed values for workflow_input pure references', () => { |
| 22 | + const workflow: SerializedWorkflow = { |
| 23 | + version: 'test', |
| 24 | + blocks: [createSerializedBlock({ id: 'webhook', name: 'webhook', type: BlockType.TRIGGER })], |
| 25 | + connections: [], |
| 26 | + loops: {}, |
| 27 | + parallels: {}, |
| 28 | + } |
| 29 | + |
| 30 | + const state = new ExecutionState() |
| 31 | + state.setBlockOutput('webhook', { |
| 32 | + conversation_id: 149, |
| 33 | + sender: { id: 10, email: 'user@example.com' }, |
| 34 | + is_active: true, |
| 35 | + }) |
| 36 | + |
| 37 | + const resolver = new VariableResolver(workflow, {}, state) |
| 38 | + const ctx = { blockStates: new Map() } as unknown as ExecutionContext |
| 39 | + |
| 40 | + const workflowInputBlock = createSerializedBlock({ |
| 41 | + id: 'wf', |
| 42 | + name: 'Workflow', |
| 43 | + type: BlockType.WORKFLOW_INPUT, |
| 44 | + }) |
| 45 | + |
| 46 | + const resolved = resolver.resolveInputs( |
| 47 | + ctx, |
| 48 | + 'wf', |
| 49 | + { |
| 50 | + inputMapping: { |
| 51 | + conversation_id: '<webhook.conversation_id>', |
| 52 | + sender: '<webhook.sender>', |
| 53 | + is_active: '<webhook.is_active>', |
| 54 | + }, |
| 55 | + }, |
| 56 | + workflowInputBlock |
| 57 | + ) |
| 58 | + |
| 59 | + expect(resolved.inputMapping.conversation_id).toBe(149) |
| 60 | + expect(resolved.inputMapping.sender).toEqual({ id: 10, email: 'user@example.com' }) |
| 61 | + expect(resolved.inputMapping.is_active).toBe(true) |
| 62 | + }) |
| 63 | + |
| 64 | + it.concurrent('formats pure references as strings for non-workflow blocks', () => { |
| 65 | + const workflow: SerializedWorkflow = { |
| 66 | + version: 'test', |
| 67 | + blocks: [createSerializedBlock({ id: 'webhook', name: 'webhook', type: BlockType.TRIGGER })], |
| 68 | + connections: [], |
| 69 | + loops: {}, |
| 70 | + parallels: {}, |
| 71 | + } |
| 72 | + |
| 73 | + const state = new ExecutionState() |
| 74 | + state.setBlockOutput('webhook', { conversation_id: 149 }) |
| 75 | + |
| 76 | + const resolver = new VariableResolver(workflow, {}, state) |
| 77 | + const ctx = { blockStates: new Map() } as unknown as ExecutionContext |
| 78 | + |
| 79 | + const apiBlock = createSerializedBlock({ |
| 80 | + id: 'api', |
| 81 | + name: 'API', |
| 82 | + type: BlockType.API, |
| 83 | + }) |
| 84 | + |
| 85 | + const resolved = resolver.resolveInputs( |
| 86 | + ctx, |
| 87 | + 'api', |
| 88 | + { conversation_id: '<webhook.conversation_id>' }, |
| 89 | + apiBlock |
| 90 | + ) |
| 91 | + |
| 92 | + expect(resolved.conversation_id).toBe('149') |
| 93 | + }) |
| 94 | + |
| 95 | + it.concurrent('preserves nulls and arrays for workflow blocks with pure references', () => { |
| 96 | + const workflow: SerializedWorkflow = { |
| 97 | + version: 'test', |
| 98 | + blocks: [createSerializedBlock({ id: 'webhook', name: 'webhook', type: BlockType.TRIGGER })], |
| 99 | + connections: [], |
| 100 | + loops: {}, |
| 101 | + parallels: {}, |
| 102 | + } |
| 103 | + |
| 104 | + const state = new ExecutionState() |
| 105 | + state.setBlockOutput('webhook', { |
| 106 | + items: [1, { a: 2 }, [3]], |
| 107 | + nothing: null, |
| 108 | + }) |
| 109 | + |
| 110 | + const resolver = new VariableResolver(workflow, {}, state) |
| 111 | + const ctx = { blockStates: new Map() } as unknown as ExecutionContext |
| 112 | + |
| 113 | + const workflowBlock = createSerializedBlock({ |
| 114 | + id: 'wf', |
| 115 | + name: 'Workflow', |
| 116 | + type: BlockType.WORKFLOW, |
| 117 | + }) |
| 118 | + |
| 119 | + const resolved = resolver.resolveInputs( |
| 120 | + ctx, |
| 121 | + 'wf', |
| 122 | + { |
| 123 | + inputMapping: { |
| 124 | + items: ' <webhook.items> ', |
| 125 | + nothing: '<webhook.nothing>', |
| 126 | + }, |
| 127 | + }, |
| 128 | + workflowBlock |
| 129 | + ) |
| 130 | + |
| 131 | + expect(resolved.inputMapping.items).toEqual([1, { a: 2 }, [3]]) |
| 132 | + expect(resolved.inputMapping.nothing).toBeNull() |
| 133 | + }) |
| 134 | + |
| 135 | + it.concurrent('still stringifies when a reference is embedded in text', () => { |
| 136 | + const workflow: SerializedWorkflow = { |
| 137 | + version: 'test', |
| 138 | + blocks: [createSerializedBlock({ id: 'webhook', name: 'webhook', type: BlockType.TRIGGER })], |
| 139 | + connections: [], |
| 140 | + loops: {}, |
| 141 | + parallels: {}, |
| 142 | + } |
| 143 | + |
| 144 | + const state = new ExecutionState() |
| 145 | + state.setBlockOutput('webhook', { conversation_id: 149 }) |
| 146 | + |
| 147 | + const resolver = new VariableResolver(workflow, {}, state) |
| 148 | + const ctx = { blockStates: new Map() } as unknown as ExecutionContext |
| 149 | + |
| 150 | + const workflowInputBlock = createSerializedBlock({ |
| 151 | + id: 'wf', |
| 152 | + name: 'Workflow', |
| 153 | + type: BlockType.WORKFLOW_INPUT, |
| 154 | + }) |
| 155 | + |
| 156 | + const resolved = resolver.resolveInputs( |
| 157 | + ctx, |
| 158 | + 'wf', |
| 159 | + { |
| 160 | + label: 'id=<webhook.conversation_id>', |
| 161 | + }, |
| 162 | + workflowInputBlock |
| 163 | + ) |
| 164 | + |
| 165 | + expect(resolved.label).toBe('id=149') |
| 166 | + }) |
| 167 | +}) |
0 commit comments