File size: 7,748 Bytes
da2e594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env node

/**
 * Test script for expression format validation
 * Tests the validation of expression prefixes and resource locator formats
 */

const { WorkflowValidator } = require('../dist/services/workflow-validator.js');
const { NodeRepository } = require('../dist/database/node-repository.js');
const { EnhancedConfigValidator } = require('../dist/services/enhanced-config-validator.js');
const { createDatabaseAdapter } = require('../dist/database/database-adapter.js');
const path = require('path');

async function runTests() {
  // Initialize database
  const dbPath = path.join(__dirname, '..', 'data', 'nodes.db');
  const adapter = await createDatabaseAdapter(dbPath);
  const db = adapter;

  const nodeRepository = new NodeRepository(db);
  const validator = new WorkflowValidator(nodeRepository, EnhancedConfigValidator);

  console.log('\nπŸ§ͺ Testing Expression Format Validation\n');
  console.log('=' .repeat(60));

  // Test 1: Email node with missing = prefix
  console.log('\nπŸ“ Test 1: Email Send node - Missing = prefix');
  console.log('-'.repeat(40));

  const emailWorkflowIncorrect = {
    nodes: [
      {
        id: 'b9dd1cfd-ee66-4049-97e7-1af6d976a4e0',
        name: 'Error Handler',
        type: 'n8n-nodes-base.emailSend',
        typeVersion: 2.1,
        position: [-128, 400],
        parameters: {
          fromEmail: '{{ $env.ADMIN_EMAIL }}',  // INCORRECT - missing =
          toEmail: 'admin@company.com',
          subject: 'GitHub Issue Workflow Error - HIGH PRIORITY',
          options: {}
        },
        credentials: {
          smtp: {
            id: '7AQ08VMFHubmfvzR',
            name: 'romuald@aiadvisors.pl'
          }
        }
      }
    ],
    connections: {}
  };

  const result1 = await validator.validateWorkflow(emailWorkflowIncorrect);

  if (result1.errors.some(e => e.message.includes('Expression format'))) {
    console.log('βœ… ERROR DETECTED (correct behavior):');
    const formatError = result1.errors.find(e => e.message.includes('Expression format'));
    console.log('\n' + formatError.message);
  } else {
    console.log('❌ No expression format error detected (should have detected missing prefix)');
  }

  // Test 2: Email node with correct = prefix
  console.log('\nπŸ“ Test 2: Email Send node - Correct = prefix');
  console.log('-'.repeat(40));

  const emailWorkflowCorrect = {
    nodes: [
      {
        id: 'b9dd1cfd-ee66-4049-97e7-1af6d976a4e0',
        name: 'Error Handler',
        type: 'n8n-nodes-base.emailSend',
        typeVersion: 2.1,
        position: [-128, 400],
        parameters: {
          fromEmail: '={{ $env.ADMIN_EMAIL }}',  // CORRECT - has =
          toEmail: 'admin@company.com',
          subject: 'GitHub Issue Workflow Error - HIGH PRIORITY',
          options: {}
        }
      }
    ],
    connections: {}
  };

  const result2 = await validator.validateWorkflow(emailWorkflowCorrect);

  if (result2.errors.some(e => e.message.includes('Expression format'))) {
    console.log('❌ Unexpected expression format error (should accept = prefix)');
  } else {
    console.log('βœ… No expression format errors (correct!)');
  }

  // Test 3: GitHub node without resource locator format
  console.log('\nπŸ“ Test 3: GitHub node - Missing resource locator format');
  console.log('-'.repeat(40));

  const githubWorkflowIncorrect = {
    nodes: [
      {
        id: '3c742ca1-af8f-4d80-a47e-e68fb1ced491',
        name: 'Send Welcome Comment',
        type: 'n8n-nodes-base.github',
        typeVersion: 1.1,
        position: [-240, 96],
        parameters: {
          operation: 'createComment',
          owner: '{{ $vars.GITHUB_OWNER }}',  // INCORRECT - needs RL format
          repository: '{{ $vars.GITHUB_REPO }}',  // INCORRECT - needs RL format
          issueNumber: null,
          body: 'πŸ‘‹ Hi @{{ $(\'Extract Issue Data\').first().json.author }}!'  // INCORRECT - missing =
        },
        credentials: {
          githubApi: {
            id: 'edgpwh6ldYN07MXx',
            name: 'GitHub account'
          }
        }
      }
    ],
    connections: {}
  };

  const result3 = await validator.validateWorkflow(githubWorkflowIncorrect);

  const formatErrors = result3.errors.filter(e => e.message.includes('Expression format'));
  console.log(`\nFound ${formatErrors.length} expression format errors:`);

  if (formatErrors.length >= 3) {
    console.log('βœ… All format issues detected:');
    formatErrors.forEach((error, index) => {
      const field = error.message.match(/Field '([^']+)'/)?.[1] || 'unknown';
      console.log(`  ${index + 1}. Field '${field}' - ${error.message.includes('resource locator') ? 'Needs RL format' : 'Missing = prefix'}`);
    });
  } else {
    console.log('❌ Not all format issues detected');
  }

  // Test 4: GitHub node with correct resource locator format
  console.log('\nπŸ“ Test 4: GitHub node - Correct resource locator format');
  console.log('-'.repeat(40));

  const githubWorkflowCorrect = {
    nodes: [
      {
        id: '3c742ca1-af8f-4d80-a47e-e68fb1ced491',
        name: 'Send Welcome Comment',
        type: 'n8n-nodes-base.github',
        typeVersion: 1.1,
        position: [-240, 96],
        parameters: {
          operation: 'createComment',
          owner: {
            __rl: true,
            value: '={{ $vars.GITHUB_OWNER }}',  // CORRECT - RL format with =
            mode: 'expression'
          },
          repository: {
            __rl: true,
            value: '={{ $vars.GITHUB_REPO }}',  // CORRECT - RL format with =
            mode: 'expression'
          },
          issueNumber: 123,
          body: '=πŸ‘‹ Hi @{{ $(\'Extract Issue Data\').first().json.author }}!'  // CORRECT - has =
        }
      }
    ],
    connections: {}
  };

  const result4 = await validator.validateWorkflow(githubWorkflowCorrect);

  const formatErrors4 = result4.errors.filter(e => e.message.includes('Expression format'));
  if (formatErrors4.length === 0) {
    console.log('βœ… No expression format errors (correct!)');
  } else {
    console.log(`❌ Unexpected expression format errors: ${formatErrors4.length}`);
    formatErrors4.forEach(e => console.log('  - ' + e.message.split('\n')[0]));
  }

  // Test 5: Mixed content expressions
  console.log('\nπŸ“ Test 5: Mixed content with expressions');
  console.log('-'.repeat(40));

  const mixedContentWorkflow = {
    nodes: [
      {
        id: '1',
        name: 'HTTP Request',
        type: 'n8n-nodes-base.httpRequest',
        typeVersion: 4,
        position: [0, 0],
        parameters: {
          url: 'https://api.example.com/users/{{ $json.userId }}',  // INCORRECT
          headers: {
            'Authorization': '=Bearer {{ $env.API_TOKEN }}'  // CORRECT
          }
        }
      }
    ],
    connections: {}
  };

  const result5 = await validator.validateWorkflow(mixedContentWorkflow);

  const urlError = result5.errors.find(e => e.message.includes('url') && e.message.includes('Expression format'));
  if (urlError) {
    console.log('βœ… Mixed content error detected for URL field');
    console.log('  Should be: "=https://api.example.com/users/{{ $json.userId }}"');
  } else {
    console.log('❌ Mixed content error not detected');
  }

  console.log('\n' + '='.repeat(60));
  console.log('\n✨ Expression Format Validation Summary:');
  console.log('  - Detects missing = prefix in expressions');
  console.log('  - Identifies fields needing resource locator format');
  console.log('  - Provides clear correction examples');
  console.log('  - Handles mixed literal and expression content');

  // Close database
  db.close();
}

runTests().catch(error => {
  console.error('Test failed:', error);
  process.exit(1);
});