|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { BlockType } from '@/executor/constants' |
| 6 | +import { ExecutionState } from '@/executor/execution/state' |
| 7 | +import type { ExecutionContext } from '@/executor/types' |
| 8 | +import { VariableResolver } from '@/executor/variables/resolver' |
| 9 | +import type { SerializedBlock, SerializedWorkflow } from '@/serializer/types' |
| 10 | + |
| 11 | +function createBlock(id: string, name: string, type: string, params = {}): SerializedBlock { |
| 12 | + return { |
| 13 | + id, |
| 14 | + metadata: { id: type, name }, |
| 15 | + position: { x: 0, y: 0 }, |
| 16 | + config: { tool: type, params }, |
| 17 | + inputs: {}, |
| 18 | + outputs: { |
| 19 | + result: 'string', |
| 20 | + items: 'json', |
| 21 | + }, |
| 22 | + enabled: true, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function createResolver(language = 'javascript') { |
| 27 | + const producer = createBlock('producer', 'Producer', BlockType.API) |
| 28 | + const functionBlock = createBlock('function', 'Function', BlockType.FUNCTION, { |
| 29 | + language, |
| 30 | + }) |
| 31 | + const workflow: SerializedWorkflow = { |
| 32 | + version: '1', |
| 33 | + blocks: [producer, functionBlock], |
| 34 | + connections: [], |
| 35 | + loops: {}, |
| 36 | + parallels: {}, |
| 37 | + } |
| 38 | + const state = new ExecutionState() |
| 39 | + state.setBlockOutput('producer', { |
| 40 | + result: 'hello world', |
| 41 | + items: ['a', 'b'], |
| 42 | + }) |
| 43 | + const ctx = { |
| 44 | + blockStates: state.getBlockStates(), |
| 45 | + blockLogs: [], |
| 46 | + environmentVariables: {}, |
| 47 | + workflowVariables: {}, |
| 48 | + decisions: { router: new Map(), condition: new Map() }, |
| 49 | + loopExecutions: new Map(), |
| 50 | + executedBlocks: new Set(), |
| 51 | + activeExecutionPath: new Set(), |
| 52 | + completedLoops: new Set(), |
| 53 | + metadata: {}, |
| 54 | + } as ExecutionContext |
| 55 | + |
| 56 | + return { |
| 57 | + block: functionBlock, |
| 58 | + ctx, |
| 59 | + resolver: new VariableResolver(workflow, {}, state), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +describe('VariableResolver function block inputs', () => { |
| 64 | + it('returns empty inputs when params are missing', () => { |
| 65 | + const { block, ctx, resolver } = createResolver() |
| 66 | + |
| 67 | + const result = resolver.resolveInputsForFunctionBlock(ctx, 'function', undefined, block) |
| 68 | + |
| 69 | + expect(result).toEqual({ resolvedInputs: {}, contextVariables: {} }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('resolves JavaScript block references through globalThis context variables', () => { |
| 73 | + const { block, ctx, resolver } = createResolver('javascript') |
| 74 | + |
| 75 | + const result = resolver.resolveInputsForFunctionBlock( |
| 76 | + ctx, |
| 77 | + 'function', |
| 78 | + { code: 'return <Producer.result>' }, |
| 79 | + block |
| 80 | + ) |
| 81 | + |
| 82 | + expect(result.resolvedInputs.code).toBe('return globalThis["__blockRef_0"]') |
| 83 | + expect(result.contextVariables).toEqual({ __blockRef_0: 'hello world' }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('resolves Python block references through globals lookup', () => { |
| 87 | + const { block, ctx, resolver } = createResolver('python') |
| 88 | + |
| 89 | + const result = resolver.resolveInputsForFunctionBlock( |
| 90 | + ctx, |
| 91 | + 'function', |
| 92 | + { code: 'return <Producer.result>' }, |
| 93 | + block |
| 94 | + ) |
| 95 | + |
| 96 | + expect(result.resolvedInputs.code).toBe('return globals()["__blockRef_0"]') |
| 97 | + expect(result.contextVariables).toEqual({ __blockRef_0: 'hello world' }) |
| 98 | + }) |
| 99 | + |
| 100 | + it('uses separate Python context variables for repeated mutable references', () => { |
| 101 | + const { block, ctx, resolver } = createResolver('python') |
| 102 | + |
| 103 | + const result = resolver.resolveInputsForFunctionBlock( |
| 104 | + ctx, |
| 105 | + 'function', |
| 106 | + { code: 'a = <Producer.items>\nb = <Producer.items>\nreturn b' }, |
| 107 | + block |
| 108 | + ) |
| 109 | + |
| 110 | + expect(result.resolvedInputs.code).toBe( |
| 111 | + 'a = globals()["__blockRef_0"]\nb = globals()["__blockRef_1"]\nreturn b' |
| 112 | + ) |
| 113 | + expect(result.contextVariables).toEqual({ |
| 114 | + __blockRef_0: ['a', 'b'], |
| 115 | + __blockRef_1: ['a', 'b'], |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + it('uses shell-safe expansions for block references', () => { |
| 120 | + const { block, ctx, resolver } = createResolver('shell') |
| 121 | + |
| 122 | + const result = resolver.resolveInputsForFunctionBlock( |
| 123 | + ctx, |
| 124 | + 'function', |
| 125 | + { code: 'echo <Producer.result>suffix && echo "<Producer.result>"' }, |
| 126 | + block |
| 127 | + ) |
| 128 | + |
| 129 | + expect(result.resolvedInputs.code).toBe( |
| 130 | + `echo "\${__blockRef_0}"suffix && echo "\${__blockRef_1}"` |
| 131 | + ) |
| 132 | + expect(result.contextVariables).toEqual({ |
| 133 | + __blockRef_0: 'hello world', |
| 134 | + __blockRef_1: 'hello world', |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + it('ignores shell comment quotes when formatting later block references', () => { |
| 139 | + const { block, ctx, resolver } = createResolver('shell') |
| 140 | + |
| 141 | + const result = resolver.resolveInputsForFunctionBlock( |
| 142 | + ctx, |
| 143 | + 'function', |
| 144 | + { code: "# don't confuse quote tracking\necho <Producer.result>" }, |
| 145 | + block |
| 146 | + ) |
| 147 | + |
| 148 | + expect(result.resolvedInputs.code).toBe( |
| 149 | + `# don't confuse quote tracking\necho "\${__blockRef_0}"` |
| 150 | + ) |
| 151 | + expect(result.contextVariables).toEqual({ __blockRef_0: 'hello world' }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments