|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { workflowExecutorTool } from '@/tools/workflow/executor' |
| 3 | + |
| 4 | +describe('workflowExecutorTool', () => { |
| 5 | + describe('request.body', () => { |
| 6 | + const buildBody = workflowExecutorTool.request.body! |
| 7 | + |
| 8 | + it.concurrent('should pass through object inputMapping unchanged (LLM-provided args)', () => { |
| 9 | + const params = { |
| 10 | + workflowId: 'test-workflow-id', |
| 11 | + inputMapping: { firstName: 'John', lastName: 'Doe', age: 30 }, |
| 12 | + } |
| 13 | + |
| 14 | + const result = buildBody(params) |
| 15 | + |
| 16 | + expect(result).toEqual({ |
| 17 | + input: { firstName: 'John', lastName: 'Doe', age: 30 }, |
| 18 | + triggerType: 'api', |
| 19 | + useDraftState: false, |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + it.concurrent('should parse JSON string inputMapping (UI-provided via tool-input)', () => { |
| 24 | + const params = { |
| 25 | + workflowId: 'test-workflow-id', |
| 26 | + inputMapping: '{"firstName": "John", "lastName": "Doe"}', |
| 27 | + } |
| 28 | + |
| 29 | + const result = buildBody(params) |
| 30 | + |
| 31 | + expect(result).toEqual({ |
| 32 | + input: { firstName: 'John', lastName: 'Doe' }, |
| 33 | + triggerType: 'api', |
| 34 | + useDraftState: false, |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it.concurrent('should handle nested objects in JSON string inputMapping', () => { |
| 39 | + const params = { |
| 40 | + workflowId: 'test-workflow-id', |
| 41 | + inputMapping: '{"user": {"name": "John", "email": "[email protected]"}, "count": 5}', |
| 42 | + } |
| 43 | + |
| 44 | + const result = buildBody(params) |
| 45 | + |
| 46 | + expect(result).toEqual({ |
| 47 | + input: { user: { name: 'John', email: '[email protected]' }, count: 5 }, |
| 48 | + triggerType: 'api', |
| 49 | + useDraftState: false, |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it.concurrent('should handle arrays in JSON string inputMapping', () => { |
| 54 | + const params = { |
| 55 | + workflowId: 'test-workflow-id', |
| 56 | + inputMapping: '{"tags": ["a", "b", "c"], "ids": [1, 2, 3]}', |
| 57 | + } |
| 58 | + |
| 59 | + const result = buildBody(params) |
| 60 | + |
| 61 | + expect(result).toEqual({ |
| 62 | + input: { tags: ['a', 'b', 'c'], ids: [1, 2, 3] }, |
| 63 | + triggerType: 'api', |
| 64 | + useDraftState: false, |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + it.concurrent('should default to empty object when inputMapping is undefined', () => { |
| 69 | + const params = { |
| 70 | + workflowId: 'test-workflow-id', |
| 71 | + inputMapping: undefined, |
| 72 | + } |
| 73 | + |
| 74 | + const result = buildBody(params) |
| 75 | + |
| 76 | + expect(result).toEqual({ |
| 77 | + input: {}, |
| 78 | + triggerType: 'api', |
| 79 | + useDraftState: false, |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it.concurrent('should default to empty object when inputMapping is null', () => { |
| 84 | + const params = { |
| 85 | + workflowId: 'test-workflow-id', |
| 86 | + inputMapping: null as any, |
| 87 | + } |
| 88 | + |
| 89 | + const result = buildBody(params) |
| 90 | + |
| 91 | + expect(result).toEqual({ |
| 92 | + input: {}, |
| 93 | + triggerType: 'api', |
| 94 | + useDraftState: false, |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + it.concurrent('should fallback to empty object for invalid JSON string', () => { |
| 99 | + const params = { |
| 100 | + workflowId: 'test-workflow-id', |
| 101 | + inputMapping: 'not valid json {', |
| 102 | + } |
| 103 | + |
| 104 | + const result = buildBody(params) |
| 105 | + |
| 106 | + expect(result).toEqual({ |
| 107 | + input: {}, |
| 108 | + triggerType: 'api', |
| 109 | + useDraftState: false, |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + it.concurrent('should fallback to empty object for empty string', () => { |
| 114 | + const params = { |
| 115 | + workflowId: 'test-workflow-id', |
| 116 | + inputMapping: '', |
| 117 | + } |
| 118 | + |
| 119 | + const result = buildBody(params) |
| 120 | + |
| 121 | + expect(result).toEqual({ |
| 122 | + input: {}, |
| 123 | + triggerType: 'api', |
| 124 | + useDraftState: false, |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + it.concurrent('should handle empty object inputMapping', () => { |
| 129 | + const params = { |
| 130 | + workflowId: 'test-workflow-id', |
| 131 | + inputMapping: {}, |
| 132 | + } |
| 133 | + |
| 134 | + const result = buildBody(params) |
| 135 | + |
| 136 | + expect(result).toEqual({ |
| 137 | + input: {}, |
| 138 | + triggerType: 'api', |
| 139 | + useDraftState: false, |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + it.concurrent('should handle empty JSON object string', () => { |
| 144 | + const params = { |
| 145 | + workflowId: 'test-workflow-id', |
| 146 | + inputMapping: '{}', |
| 147 | + } |
| 148 | + |
| 149 | + const result = buildBody(params) |
| 150 | + |
| 151 | + expect(result).toEqual({ |
| 152 | + input: {}, |
| 153 | + triggerType: 'api', |
| 154 | + useDraftState: false, |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + it.concurrent('should preserve special characters in string values', () => { |
| 159 | + const params = { |
| 160 | + workflowId: 'test-workflow-id', |
| 161 | + inputMapping: '{"message": "Hello\\nWorld", "path": "C:\\\\Users"}', |
| 162 | + } |
| 163 | + |
| 164 | + const result = buildBody(params) |
| 165 | + |
| 166 | + expect(result).toEqual({ |
| 167 | + input: { message: 'Hello\nWorld', path: 'C:\\Users' }, |
| 168 | + triggerType: 'api', |
| 169 | + useDraftState: false, |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + it.concurrent('should handle unicode characters in JSON string', () => { |
| 174 | + const params = { |
| 175 | + workflowId: 'test-workflow-id', |
| 176 | + inputMapping: '{"greeting": "こんにちは", "emoji": "👋"}', |
| 177 | + } |
| 178 | + |
| 179 | + const result = buildBody(params) |
| 180 | + |
| 181 | + expect(result).toEqual({ |
| 182 | + input: { greeting: 'こんにちは', emoji: '👋' }, |
| 183 | + triggerType: 'api', |
| 184 | + useDraftState: false, |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + it.concurrent('should not modify object with string values that look like JSON', () => { |
| 189 | + const params = { |
| 190 | + workflowId: 'test-workflow-id', |
| 191 | + inputMapping: { data: '{"nested": "json"}' }, |
| 192 | + } |
| 193 | + |
| 194 | + const result = buildBody(params) |
| 195 | + |
| 196 | + expect(result).toEqual({ |
| 197 | + input: { data: '{"nested": "json"}' }, |
| 198 | + triggerType: 'api', |
| 199 | + useDraftState: false, |
| 200 | + }) |
| 201 | + }) |
| 202 | + }) |
| 203 | + |
| 204 | + describe('request.url', () => { |
| 205 | + it.concurrent('should build correct URL with workflowId', () => { |
| 206 | + const url = workflowExecutorTool.request.url as (params: any) => string |
| 207 | + |
| 208 | + expect(url({ workflowId: 'abc-123' })).toBe('/api/workflows/abc-123/execute') |
| 209 | + expect(url({ workflowId: 'my-workflow' })).toBe('/api/workflows/my-workflow/execute') |
| 210 | + }) |
| 211 | + }) |
| 212 | + |
| 213 | + describe('tool metadata', () => { |
| 214 | + it.concurrent('should have correct id', () => { |
| 215 | + expect(workflowExecutorTool.id).toBe('workflow_executor') |
| 216 | + }) |
| 217 | + |
| 218 | + it.concurrent('should have required workflowId param', () => { |
| 219 | + expect(workflowExecutorTool.params.workflowId.required).toBe(true) |
| 220 | + }) |
| 221 | + |
| 222 | + it.concurrent('should have optional inputMapping param', () => { |
| 223 | + expect(workflowExecutorTool.params.inputMapping.required).toBe(false) |
| 224 | + }) |
| 225 | + |
| 226 | + it.concurrent('should use POST method', () => { |
| 227 | + expect(workflowExecutorTool.request.method).toBe('POST') |
| 228 | + }) |
| 229 | + }) |
| 230 | +}) |
0 commit comments