|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import type { ResolutionContext } from './reference' |
| 3 | +import { WorkflowResolver } from './workflow' |
| 4 | + |
| 5 | +vi.mock('@/lib/logs/console/logger', () => ({ |
| 6 | + createLogger: vi.fn().mockReturnValue({ |
| 7 | + debug: vi.fn(), |
| 8 | + info: vi.fn(), |
| 9 | + warn: vi.fn(), |
| 10 | + error: vi.fn(), |
| 11 | + }), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/workflows/variables/variable-manager', () => ({ |
| 15 | + VariableManager: { |
| 16 | + resolveForExecution: vi.fn((value) => value), |
| 17 | + }, |
| 18 | +})) |
| 19 | + |
| 20 | +/** |
| 21 | + * Creates a minimal ResolutionContext for testing. |
| 22 | + * The WorkflowResolver only uses context.executionContext.workflowVariables, |
| 23 | + * so we only need to provide that field. |
| 24 | + */ |
| 25 | +function createTestContext(workflowVariables: Record<string, any>): ResolutionContext { |
| 26 | + return { |
| 27 | + executionContext: { workflowVariables }, |
| 28 | + executionState: {}, |
| 29 | + currentNodeId: 'test-node', |
| 30 | + } as ResolutionContext |
| 31 | +} |
| 32 | + |
| 33 | +describe('WorkflowResolver', () => { |
| 34 | + describe('canResolve', () => { |
| 35 | + it.concurrent('should return true for variable references', () => { |
| 36 | + const resolver = new WorkflowResolver({}) |
| 37 | + expect(resolver.canResolve('<variable.myvar>')).toBe(true) |
| 38 | + expect(resolver.canResolve('<variable.test>')).toBe(true) |
| 39 | + }) |
| 40 | + |
| 41 | + it.concurrent('should return false for non-variable references', () => { |
| 42 | + const resolver = new WorkflowResolver({}) |
| 43 | + expect(resolver.canResolve('<block.output>')).toBe(false) |
| 44 | + expect(resolver.canResolve('<loop.index>')).toBe(false) |
| 45 | + expect(resolver.canResolve('plain text')).toBe(false) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + describe('resolve with normalized matching', () => { |
| 50 | + it.concurrent('should resolve variable with exact name match', () => { |
| 51 | + const variables = { |
| 52 | + 'var-1': { id: 'var-1', name: 'myvar', type: 'plain', value: 'test-value' }, |
| 53 | + } |
| 54 | + const resolver = new WorkflowResolver(variables) |
| 55 | + |
| 56 | + const result = resolver.resolve('<variable.myvar>', createTestContext(variables)) |
| 57 | + expect(result).toBe('test-value') |
| 58 | + }) |
| 59 | + |
| 60 | + it.concurrent('should resolve variable with normalized name (lowercase)', () => { |
| 61 | + const variables = { |
| 62 | + 'var-1': { id: 'var-1', name: 'MyVar', type: 'plain', value: 'test-value' }, |
| 63 | + } |
| 64 | + const resolver = new WorkflowResolver(variables) |
| 65 | + |
| 66 | + const result = resolver.resolve('<variable.myvar>', createTestContext(variables)) |
| 67 | + expect(result).toBe('test-value') |
| 68 | + }) |
| 69 | + |
| 70 | + it.concurrent('should resolve variable with normalized name (spaces removed)', () => { |
| 71 | + const variables = { |
| 72 | + 'var-1': { id: 'var-1', name: 'My Variable', type: 'plain', value: 'test-value' }, |
| 73 | + } |
| 74 | + const resolver = new WorkflowResolver(variables) |
| 75 | + |
| 76 | + const result = resolver.resolve('<variable.myvariable>', createTestContext(variables)) |
| 77 | + expect(result).toBe('test-value') |
| 78 | + }) |
| 79 | + |
| 80 | + it.concurrent( |
| 81 | + 'should resolve variable with fully normalized name (JIRA TEAM UUID case)', |
| 82 | + () => { |
| 83 | + const variables = { |
| 84 | + 'var-1': { id: 'var-1', name: 'JIRA TEAM UUID', type: 'plain', value: 'uuid-123' }, |
| 85 | + } |
| 86 | + const resolver = new WorkflowResolver(variables) |
| 87 | + |
| 88 | + const result = resolver.resolve('<variable.jirateamuuid>', createTestContext(variables)) |
| 89 | + expect(result).toBe('uuid-123') |
| 90 | + } |
| 91 | + ) |
| 92 | + |
| 93 | + it.concurrent('should resolve variable regardless of reference case', () => { |
| 94 | + const variables = { |
| 95 | + 'var-1': { id: 'var-1', name: 'jirateamuuid', type: 'plain', value: 'uuid-123' }, |
| 96 | + } |
| 97 | + const resolver = new WorkflowResolver(variables) |
| 98 | + |
| 99 | + const result = resolver.resolve('<variable.JIRATEAMUUID>', createTestContext(variables)) |
| 100 | + expect(result).toBe('uuid-123') |
| 101 | + }) |
| 102 | + |
| 103 | + it.concurrent('should resolve by variable ID (exact match)', () => { |
| 104 | + const variables = { |
| 105 | + 'my-uuid-id': { id: 'my-uuid-id', name: 'Some Name', type: 'plain', value: 'id-value' }, |
| 106 | + } |
| 107 | + const resolver = new WorkflowResolver(variables) |
| 108 | + |
| 109 | + const result = resolver.resolve('<variable.my-uuid-id>', createTestContext(variables)) |
| 110 | + expect(result).toBe('id-value') |
| 111 | + }) |
| 112 | + |
| 113 | + it.concurrent('should return undefined for non-existent variable', () => { |
| 114 | + const variables = { |
| 115 | + 'var-1': { id: 'var-1', name: 'existing', type: 'plain', value: 'test' }, |
| 116 | + } |
| 117 | + const resolver = new WorkflowResolver(variables) |
| 118 | + |
| 119 | + const result = resolver.resolve('<variable.nonexistent>', createTestContext(variables)) |
| 120 | + expect(result).toBeUndefined() |
| 121 | + }) |
| 122 | + |
| 123 | + it.concurrent('should handle nested path access', () => { |
| 124 | + const variables = { |
| 125 | + 'var-1': { |
| 126 | + id: 'var-1', |
| 127 | + name: 'config', |
| 128 | + type: 'object', |
| 129 | + value: { nested: { value: 'deep' } }, |
| 130 | + }, |
| 131 | + } |
| 132 | + const resolver = new WorkflowResolver(variables) |
| 133 | + |
| 134 | + const result = resolver.resolve( |
| 135 | + '<variable.config.nested.value>', |
| 136 | + createTestContext(variables) |
| 137 | + ) |
| 138 | + expect(result).toBe('deep') |
| 139 | + }) |
| 140 | + |
| 141 | + it.concurrent('should resolve with mixed case and spaces in reference', () => { |
| 142 | + const variables = { |
| 143 | + 'var-1': { id: 'var-1', name: 'api key', type: 'plain', value: 'secret-key' }, |
| 144 | + } |
| 145 | + const resolver = new WorkflowResolver(variables) |
| 146 | + |
| 147 | + const result = resolver.resolve('<variable.APIKEY>', createTestContext(variables)) |
| 148 | + expect(result).toBe('secret-key') |
| 149 | + }) |
| 150 | + |
| 151 | + it.concurrent('should handle real-world variable naming patterns', () => { |
| 152 | + const testCases = [ |
| 153 | + { varName: 'User ID', refName: 'userid', value: 'user-123' }, |
| 154 | + { varName: 'API Key', refName: 'apikey', value: 'key-456' }, |
| 155 | + { varName: 'STRIPE SECRET KEY', refName: 'stripesecretkey', value: 'sk_test' }, |
| 156 | + { varName: 'Database URL', refName: 'databaseurl', value: 'postgres://...' }, |
| 157 | + ] |
| 158 | + |
| 159 | + for (const { varName, refName, value } of testCases) { |
| 160 | + const variables = { |
| 161 | + 'var-1': { id: 'var-1', name: varName, type: 'plain', value }, |
| 162 | + } |
| 163 | + const resolver = new WorkflowResolver(variables) |
| 164 | + |
| 165 | + const result = resolver.resolve(`<variable.${refName}>`, createTestContext(variables)) |
| 166 | + expect(result).toBe(value) |
| 167 | + } |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + describe('edge cases', () => { |
| 172 | + it.concurrent('should handle empty workflow variables', () => { |
| 173 | + const resolver = new WorkflowResolver({}) |
| 174 | + |
| 175 | + const result = resolver.resolve('<variable.anyvar>', createTestContext({})) |
| 176 | + expect(result).toBeUndefined() |
| 177 | + }) |
| 178 | + |
| 179 | + it.concurrent('should handle invalid reference format', () => { |
| 180 | + const resolver = new WorkflowResolver({}) |
| 181 | + |
| 182 | + const result = resolver.resolve('<variable>', createTestContext({})) |
| 183 | + expect(result).toBeUndefined() |
| 184 | + }) |
| 185 | + |
| 186 | + it.concurrent('should handle null variable values in the map', () => { |
| 187 | + const variables: Record<string, any> = { |
| 188 | + 'var-1': null, |
| 189 | + 'var-2': { id: 'var-2', name: 'valid', type: 'plain', value: 'exists' }, |
| 190 | + } |
| 191 | + const resolver = new WorkflowResolver(variables) |
| 192 | + |
| 193 | + const result = resolver.resolve('<variable.valid>', createTestContext(variables)) |
| 194 | + expect(result).toBe('exists') |
| 195 | + }) |
| 196 | + |
| 197 | + it.concurrent('should handle variable with empty name', () => { |
| 198 | + const variables = { |
| 199 | + 'var-1': { id: 'var-1', name: '', type: 'plain', value: 'empty-name' }, |
| 200 | + } |
| 201 | + const resolver = new WorkflowResolver(variables) |
| 202 | + |
| 203 | + // Empty name normalizes to empty string, which matches "<variable.>" reference |
| 204 | + const result = resolver.resolve('<variable.>', createTestContext(variables)) |
| 205 | + expect(result).toBe('empty-name') |
| 206 | + }) |
| 207 | + |
| 208 | + it.concurrent('should prefer name match over ID match when both could apply', () => { |
| 209 | + const variables = { |
| 210 | + apikey: { id: 'apikey', name: 'different', type: 'plain', value: 'by-id' }, |
| 211 | + 'var-2': { id: 'var-2', name: 'apikey', type: 'plain', value: 'by-name' }, |
| 212 | + } |
| 213 | + const resolver = new WorkflowResolver(variables) |
| 214 | + |
| 215 | + const result = resolver.resolve('<variable.apikey>', createTestContext(variables)) |
| 216 | + expect(['by-id', 'by-name']).toContain(result) |
| 217 | + }) |
| 218 | + }) |
| 219 | +}) |
0 commit comments