|
| 1 | +import { mock } from 'jest-mock-extended'; |
| 2 | + |
| 3 | +import type { SimpleWorkflow } from '@/types'; |
| 4 | + |
| 5 | +import { |
| 6 | + evaluateWorkflowSimilarity, |
| 7 | + evaluateWorkflowSimilarityMultiple, |
| 8 | +} from './workflow-similarity'; |
| 9 | + |
| 10 | +// Mock node modules before any imports |
| 11 | +jest.mock('node:child_process', () => ({ |
| 12 | + execFile: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +// Create the mock inside the factory - must use var for proper hoisting with jest.mock |
| 16 | +// eslint-disable-next-line no-var |
| 17 | +var mockExecFileAsync: jest.Mock; |
| 18 | + |
| 19 | +jest.mock('node:util', () => { |
| 20 | + const mockFn = jest.fn(); |
| 21 | + // Store reference so tests can access it |
| 22 | + mockExecFileAsync = mockFn; |
| 23 | + |
| 24 | + return { |
| 25 | + promisify: jest.fn(() => mockFn), |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +jest.mock('node:fs/promises'); |
| 30 | + |
| 31 | +describe('evaluateWorkflowSimilarity', () => { |
| 32 | + const generatedWorkflow = mock<SimpleWorkflow>({ |
| 33 | + name: 'Generated', |
| 34 | + nodes: [ |
| 35 | + { |
| 36 | + id: '1', |
| 37 | + name: 'Trigger', |
| 38 | + type: 'n8n-nodes-base.manualTrigger', |
| 39 | + typeVersion: 1, |
| 40 | + position: [0, 0], |
| 41 | + parameters: {}, |
| 42 | + }, |
| 43 | + ], |
| 44 | + connections: {}, |
| 45 | + }); |
| 46 | + |
| 47 | + const groundTruthWorkflow = mock<SimpleWorkflow>({ |
| 48 | + name: 'Ground Truth', |
| 49 | + nodes: [ |
| 50 | + { |
| 51 | + id: '1', |
| 52 | + name: 'Trigger', |
| 53 | + type: 'n8n-nodes-base.manualTrigger', |
| 54 | + typeVersion: 1, |
| 55 | + position: [0, 0], |
| 56 | + parameters: {}, |
| 57 | + }, |
| 58 | + { |
| 59 | + id: '2', |
| 60 | + name: 'Code', |
| 61 | + type: 'n8n-nodes-base.code', |
| 62 | + typeVersion: 1, |
| 63 | + position: [200, 0], |
| 64 | + parameters: {}, |
| 65 | + }, |
| 66 | + ], |
| 67 | + connections: {}, |
| 68 | + }); |
| 69 | + |
| 70 | + beforeEach(() => { |
| 71 | + jest.clearAllMocks(); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('successful evaluation', () => { |
| 75 | + it('should parse Python output and map violations correctly', async () => { |
| 76 | + const mockPythonOutput = JSON.stringify({ |
| 77 | + similarity_score: 0.75, |
| 78 | + edit_cost: 25, |
| 79 | + max_possible_cost: 100, |
| 80 | + top_edits: [ |
| 81 | + { |
| 82 | + type: 'node_insert', |
| 83 | + description: 'Missing node: Code', |
| 84 | + cost: 15, |
| 85 | + priority: 'major', |
| 86 | + node_name: 'Code', |
| 87 | + }, |
| 88 | + { |
| 89 | + type: 'edge_delete', |
| 90 | + description: 'Extra connection from Trigger to Code', |
| 91 | + cost: 10, |
| 92 | + priority: 'minor', |
| 93 | + }, |
| 94 | + ], |
| 95 | + metadata: { |
| 96 | + generated_nodes: 1, |
| 97 | + ground_truth_nodes: 2, |
| 98 | + config_name: 'standard', |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + mockExecFileAsync.mockResolvedValue({ stdout: mockPythonOutput, stderr: '' }); |
| 103 | + |
| 104 | + const result = await evaluateWorkflowSimilarity(generatedWorkflow, groundTruthWorkflow); |
| 105 | + |
| 106 | + expect(result.score).toBe(0.75); |
| 107 | + expect(result.violations).toHaveLength(2); |
| 108 | + expect(result.violations[0]).toEqual({ |
| 109 | + name: 'workflow-similarity-node-insert', |
| 110 | + type: 'major', |
| 111 | + description: 'Missing node: Code', |
| 112 | + pointsDeducted: 15, |
| 113 | + }); |
| 114 | + expect(result.violations[1]).toEqual({ |
| 115 | + name: 'workflow-similarity-edge-delete', |
| 116 | + type: 'minor', |
| 117 | + description: 'Extra connection from Trigger to Code', |
| 118 | + pointsDeducted: 10, |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should handle all edit types correctly', async () => { |
| 123 | + const mockPythonOutput = JSON.stringify({ |
| 124 | + similarity_score: 0.5, |
| 125 | + edit_cost: 50, |
| 126 | + max_possible_cost: 100, |
| 127 | + top_edits: [ |
| 128 | + { type: 'node_insert', description: 'Insert', cost: 10, priority: 'major' }, |
| 129 | + { type: 'node_delete', description: 'Delete', cost: 10, priority: 'major' }, |
| 130 | + { type: 'node_substitute', description: 'Substitute', cost: 10, priority: 'major' }, |
| 131 | + { type: 'edge_insert', description: 'Edge insert', cost: 5, priority: 'minor' }, |
| 132 | + { type: 'edge_delete', description: 'Edge delete', cost: 5, priority: 'minor' }, |
| 133 | + { type: 'edge_substitute', description: 'Edge substitute', cost: 10, priority: 'major' }, |
| 134 | + ], |
| 135 | + metadata: { |
| 136 | + generated_nodes: 2, |
| 137 | + ground_truth_nodes: 2, |
| 138 | + config_name: 'standard', |
| 139 | + }, |
| 140 | + }); |
| 141 | + |
| 142 | + mockExecFileAsync.mockResolvedValue({ stdout: mockPythonOutput, stderr: '' }); |
| 143 | + |
| 144 | + const result = await evaluateWorkflowSimilarity(generatedWorkflow, groundTruthWorkflow); |
| 145 | + |
| 146 | + expect(result.violations).toHaveLength(6); |
| 147 | + expect(result.violations[0].name).toBe('workflow-similarity-node-insert'); |
| 148 | + expect(result.violations[1].name).toBe('workflow-similarity-node-delete'); |
| 149 | + expect(result.violations[2].name).toBe('workflow-similarity-node-substitute'); |
| 150 | + expect(result.violations[3].name).toBe('workflow-similarity-edge-insert'); |
| 151 | + expect(result.violations[4].name).toBe('workflow-similarity-edge-delete'); |
| 152 | + expect(result.violations[5].name).toBe('workflow-similarity-edge-substitute'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should round cost values to integers', async () => { |
| 156 | + const mockPythonOutput = JSON.stringify({ |
| 157 | + similarity_score: 0.85, |
| 158 | + edit_cost: 15.7, |
| 159 | + max_possible_cost: 100, |
| 160 | + top_edits: [ |
| 161 | + { |
| 162 | + type: 'node_insert', |
| 163 | + description: 'Missing node', |
| 164 | + cost: 15.7, |
| 165 | + priority: 'major', |
| 166 | + }, |
| 167 | + ], |
| 168 | + metadata: { |
| 169 | + generated_nodes: 1, |
| 170 | + ground_truth_nodes: 2, |
| 171 | + config_name: 'standard', |
| 172 | + }, |
| 173 | + }); |
| 174 | + |
| 175 | + mockExecFileAsync.mockResolvedValue({ stdout: mockPythonOutput, stderr: '' }); |
| 176 | + |
| 177 | + const result = await evaluateWorkflowSimilarity(generatedWorkflow, groundTruthWorkflow); |
| 178 | + |
| 179 | + expect(result.violations[0].pointsDeducted).toBe(16); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('error handling', () => { |
| 184 | + it('should handle uvx command not found error', async () => { |
| 185 | + const error = Object.assign(new Error('Command not found'), { code: 'ENOENT' }); |
| 186 | + mockExecFileAsync.mockRejectedValue(error); |
| 187 | + |
| 188 | + await expect( |
| 189 | + evaluateWorkflowSimilarity(generatedWorkflow, groundTruthWorkflow), |
| 190 | + ).rejects.toThrow('uvx command not found'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should handle timeout error', async () => { |
| 194 | + const error = Object.assign(new Error('Timeout'), { killed: true }); |
| 195 | + mockExecFileAsync.mockRejectedValue(error); |
| 196 | + |
| 197 | + await expect( |
| 198 | + evaluateWorkflowSimilarity(generatedWorkflow, groundTruthWorkflow), |
| 199 | + ).rejects.toThrow('Workflow comparison timed out'); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should handle Python script errors with empty output', async () => { |
| 203 | + const error = Object.assign(new Error('Python error'), { |
| 204 | + stdout: '', |
| 205 | + stderr: 'Something went wrong', |
| 206 | + code: 1, |
| 207 | + }); |
| 208 | + mockExecFileAsync.mockRejectedValue(error); |
| 209 | + |
| 210 | + await expect( |
| 211 | + evaluateWorkflowSimilarity(generatedWorkflow, groundTruthWorkflow), |
| 212 | + ).rejects.toThrow('Workflow similarity evaluation failed'); |
| 213 | + }); |
| 214 | + |
| 215 | + it('should accept non-zero exit code if Python outputs valid JSON', async () => { |
| 216 | + const mockPythonOutput = JSON.stringify({ |
| 217 | + similarity_score: 0.3, |
| 218 | + edit_cost: 70, |
| 219 | + max_possible_cost: 100, |
| 220 | + top_edits: [ |
| 221 | + { |
| 222 | + type: 'node_delete', |
| 223 | + description: 'Major difference', |
| 224 | + cost: 70, |
| 225 | + priority: 'critical', |
| 226 | + }, |
| 227 | + ], |
| 228 | + metadata: { |
| 229 | + generated_nodes: 1, |
| 230 | + ground_truth_nodes: 2, |
| 231 | + config_name: 'standard', |
| 232 | + }, |
| 233 | + }); |
| 234 | + |
| 235 | + const error = Object.assign(new Error('Non-zero exit'), { |
| 236 | + stdout: mockPythonOutput, |
| 237 | + stderr: 'Warning: similarity below threshold', |
| 238 | + code: 1, |
| 239 | + }); |
| 240 | + mockExecFileAsync.mockRejectedValue(error); |
| 241 | + |
| 242 | + const result = await evaluateWorkflowSimilarity(generatedWorkflow, groundTruthWorkflow); |
| 243 | + |
| 244 | + expect(result.score).toBe(0.3); |
| 245 | + expect(result.violations).toHaveLength(1); |
| 246 | + expect(result.violations[0].name).toBe('workflow-similarity-node-delete'); |
| 247 | + }); |
| 248 | + }); |
| 249 | + |
| 250 | + describe('evaluateWorkflowSimilarityMultiple', () => { |
| 251 | + it('should return result with highest similarity score', async () => { |
| 252 | + const referenceWorkflows = [ |
| 253 | + mock<SimpleWorkflow>({ name: 'Ref1', nodes: [], connections: {} }), |
| 254 | + mock<SimpleWorkflow>({ name: 'Ref2', nodes: [], connections: {} }), |
| 255 | + mock<SimpleWorkflow>({ name: 'Ref3', nodes: [], connections: {} }), |
| 256 | + ]; |
| 257 | + |
| 258 | + let callCount = 0; |
| 259 | + mockExecFileAsync.mockImplementation(async () => { |
| 260 | + callCount++; |
| 261 | + const score = callCount === 2 ? 0.9 : 0.5; // Second call has highest score |
| 262 | + const mockOutput = JSON.stringify({ |
| 263 | + similarity_score: score, |
| 264 | + edit_cost: 10, |
| 265 | + max_possible_cost: 100, |
| 266 | + top_edits: [], |
| 267 | + metadata: { generated_nodes: 1, ground_truth_nodes: 1, config_name: 'standard' }, |
| 268 | + }); |
| 269 | + return { stdout: mockOutput, stderr: '' }; |
| 270 | + }); |
| 271 | + |
| 272 | + const result = await evaluateWorkflowSimilarityMultiple( |
| 273 | + generatedWorkflow, |
| 274 | + referenceWorkflows, |
| 275 | + ); |
| 276 | + |
| 277 | + expect(result.score).toBe(0.9); |
| 278 | + expect(mockExecFileAsync).toHaveBeenCalledTimes(3); |
| 279 | + }); |
| 280 | + |
| 281 | + it('should throw error when no reference workflows provided', async () => { |
| 282 | + await expect(evaluateWorkflowSimilarityMultiple(generatedWorkflow, [])).rejects.toThrow( |
| 283 | + 'At least one reference workflow is required', |
| 284 | + ); |
| 285 | + }); |
| 286 | + }); |
| 287 | +}); |
0 commit comments