-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest-comprehensive.js
More file actions
468 lines (402 loc) · 15.5 KB
/
test-comprehensive.js
File metadata and controls
468 lines (402 loc) · 15.5 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env node
/**
* Comprehensive Integration Test for MCP n8n Workflow Builder
* Tests all MCP functionality to ensure nothing broke with notification handler changes
*/
const http = require('http');
const PORT = process.env.MCP_PORT || 3456;
const HOST = 'localhost';
let testsPassed = 0;
let testsFailed = 0;
// Helper function to send JSON-RPC request
function sendRequest(data) {
return new Promise((resolve, reject) => {
const postData = JSON.stringify(data);
const options = {
hostname: HOST,
port: PORT,
path: '/mcp',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
resolve({
statusCode: res.statusCode,
statusMessage: res.statusMessage,
headers: res.headers,
body: body ? (body.length > 0 ? JSON.parse(body) : null) : null
});
});
});
req.on('error', reject);
req.write(postData);
req.end();
});
}
function logTest(name, passed, details = '') {
if (passed) {
console.log(` ✅ PASS: ${name}`);
testsPassed++;
} else {
console.log(` ❌ FAIL: ${name}`);
if (details) console.log(` ${details}`);
testsFailed++;
}
}
// Test cases
async function runTests() {
console.log('\n╔══════════════════════════════════════════════════════════════╗');
console.log('║ MCP n8n Workflow Builder - Comprehensive Integration Test ║');
console.log('╚══════════════════════════════════════════════════════════════╝\n');
try {
// ==========================================
// 1. BASIC CONNECTIVITY TESTS
// ==========================================
console.log('📡 1. Basic Connectivity Tests');
console.log('─────────────────────────────────────────────────────────────');
// Test 1.1: Health Check
const healthResult = await new Promise((resolve, reject) => {
http.get(`http://${HOST}:${PORT}/health`, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => resolve({
statusCode: res.statusCode,
body: JSON.parse(body)
}));
}).on('error', reject);
});
logTest(
'Health check endpoint',
healthResult.statusCode === 200 && healthResult.body.status === 'ok',
JSON.stringify(healthResult.body)
);
// ==========================================
// 2. NOTIFICATION HANDLING TESTS (NEW)
// ==========================================
console.log('\n🔔 2. Notification Handling Tests (New Functionality)');
console.log('─────────────────────────────────────────────────────────────');
// Test 2.1: notifications/initialized
const initNotification = await sendRequest({
jsonrpc: '2.0',
method: 'notifications/initialized',
params: {}
});
logTest(
'notifications/initialized → 204 No Content',
initNotification.statusCode === 204 && initNotification.body === null
);
// Test 2.2: notifications/cancelled
const cancelNotification = await sendRequest({
jsonrpc: '2.0',
method: 'notifications/cancelled',
params: { requestId: 123 }
});
logTest(
'notifications/cancelled → 204 No Content',
cancelNotification.statusCode === 204 && cancelNotification.body === null
);
// Test 2.3: notifications/progress
const progressNotification = await sendRequest({
jsonrpc: '2.0',
method: 'notifications/progress',
params: { progress: 50, total: 100 }
});
logTest(
'notifications/progress → 204 No Content',
progressNotification.statusCode === 204 && progressNotification.body === null
);
// ==========================================
// 3. MCP TOOLS TESTS
// ==========================================
console.log('\n🛠️ 3. MCP Tools Tests (Core Functionality)');
console.log('─────────────────────────────────────────────────────────────');
// Test 3.1: List Tools
const listToolsResult = await sendRequest({
jsonrpc: '2.0',
method: 'tools/list',
params: {},
id: 1
});
logTest(
'tools/list - Returns list of available tools',
listToolsResult.statusCode === 200 &&
listToolsResult.body.result &&
Array.isArray(listToolsResult.body.result.tools) &&
listToolsResult.body.result.tools.length > 0,
`Found ${listToolsResult.body.result?.tools?.length || 0} tools`
);
// Test 3.2: Call Tool - list_workflows (may fail without n8n connection)
const listWorkflowsResult = await sendRequest({
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'list_workflows',
arguments: {}
},
id: 2
});
// This test is expected to fail if n8n is not configured, but should return proper error structure
const hasProperStructure = listWorkflowsResult.statusCode === 200 &&
listWorkflowsResult.body.jsonrpc === '2.0' &&
listWorkflowsResult.body.id === 2;
logTest(
'tools/call - list_workflows (structure check)',
hasProperStructure,
listWorkflowsResult.body.error ?
`Expected error (no n8n): ${listWorkflowsResult.body.error.message}` :
'Success'
);
// Test 3.3: Call Tool - list_executions
const listExecutionsResult = await sendRequest({
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'list_executions',
arguments: {}
},
id: 3
});
const hasProperExecStructure = listExecutionsResult.statusCode === 200 &&
listExecutionsResult.body.jsonrpc === '2.0' &&
listExecutionsResult.body.id === 3;
logTest(
'tools/call - list_executions (structure check)',
hasProperExecStructure,
listExecutionsResult.body.error ?
`Expected error (no n8n): ${listExecutionsResult.body.error.message}` :
'Success'
);
// ==========================================
// 4. MCP RESOURCES TESTS
// ==========================================
console.log('\n📦 4. MCP Resources Tests');
console.log('─────────────────────────────────────────────────────────────');
// Test 4.1: List Resources
const listResourcesResult = await sendRequest({
jsonrpc: '2.0',
method: 'resources/list',
params: {},
id: 4
});
logTest(
'resources/list - Returns available resources',
listResourcesResult.statusCode === 200 &&
listResourcesResult.body.result &&
Array.isArray(listResourcesResult.body.result.resources),
`Found ${listResourcesResult.body.result?.resources?.length || 0} resources`
);
// Test 4.2: List Resource Templates
const listTemplatesResult = await sendRequest({
jsonrpc: '2.0',
method: 'resources/templates/list',
params: {},
id: 5
});
logTest(
'resources/templates/list - Returns resource templates',
listTemplatesResult.statusCode === 200 &&
listTemplatesResult.body.result &&
Array.isArray(listTemplatesResult.body.result.resourceTemplates),
`Found ${listTemplatesResult.body.result?.resourceTemplates?.length || 0} templates`
);
// Test 4.3: Read Resource - workflows
const readResourceResult = await sendRequest({
jsonrpc: '2.0',
method: 'resources/read',
params: {
uri: 'n8n://workflows'
},
id: 6
});
const hasResourceStructure = readResourceResult.statusCode === 200 &&
readResourceResult.body.jsonrpc === '2.0' &&
readResourceResult.body.id === 6;
logTest(
'resources/read - Read workflows resource',
hasResourceStructure,
readResourceResult.body.error ?
`Expected error (no n8n): ${readResourceResult.body.error.message}` :
'Success'
);
// ==========================================
// 5. MCP PROMPTS TESTS
// ==========================================
console.log('\n📝 5. MCP Prompts Tests');
console.log('─────────────────────────────────────────────────────────────');
// Test 5.1: List Prompts
const listPromptsResult = await sendRequest({
jsonrpc: '2.0',
method: 'prompts/list',
params: {},
id: 7
});
logTest(
'prompts/list - Returns available prompts',
listPromptsResult.statusCode === 200 &&
listPromptsResult.body.result &&
Array.isArray(listPromptsResult.body.result.prompts),
`Found ${listPromptsResult.body.result?.prompts?.length || 0} prompts`
);
// ==========================================
// 6. JSON-RPC 2.0 COMPLIANCE TESTS
// ==========================================
console.log('\n⚙️ 6. JSON-RPC 2.0 Compliance Tests');
console.log('─────────────────────────────────────────────────────────────');
// Test 6.1: Request with ID returns proper response structure
const validRequest = await sendRequest({
jsonrpc: '2.0',
method: 'tools/list',
params: {},
id: 100
});
logTest(
'Request with ID returns response with same ID',
validRequest.body.id === 100 &&
validRequest.body.jsonrpc === '2.0' &&
validRequest.body.result !== undefined
);
// Test 6.2: Invalid method returns proper error
const invalidMethod = await sendRequest({
jsonrpc: '2.0',
method: 'invalid/method',
params: {},
id: 101
});
logTest(
'Invalid method returns JSON-RPC error',
invalidMethod.body.error &&
invalidMethod.body.error.code === -32601 &&
invalidMethod.body.id === 101
);
// Test 6.3: Notification with unknown method is ignored gracefully
const unknownNotification = await sendRequest({
jsonrpc: '2.0',
method: 'notifications/unknown',
params: {}
});
logTest(
'Unknown notification returns 204 (ignored gracefully)',
unknownNotification.statusCode === 204
);
// ==========================================
// 7. BACKWARD COMPATIBILITY TESTS
// ==========================================
console.log('\n🔄 7. Backward Compatibility Tests');
console.log('─────────────────────────────────────────────────────────────');
// Test 7.1: Multiple sequential requests work correctly
const seq1 = await sendRequest({
jsonrpc: '2.0',
method: 'tools/list',
params: {},
id: 201
});
const seq2 = await sendRequest({
jsonrpc: '2.0',
method: 'resources/list',
params: {},
id: 202
});
const seq3 = await sendRequest({
jsonrpc: '2.0',
method: 'prompts/list',
params: {},
id: 203
});
logTest(
'Sequential requests maintain proper ID mapping',
seq1.body.id === 201 &&
seq2.body.id === 202 &&
seq3.body.id === 203
);
// Test 7.2: Mixed notifications and requests
const mixedSeq1 = await sendRequest({
jsonrpc: '2.0',
method: 'notifications/initialized',
params: {}
});
const mixedSeq2 = await sendRequest({
jsonrpc: '2.0',
method: 'tools/list',
params: {},
id: 301
});
const mixedSeq3 = await sendRequest({
jsonrpc: '2.0',
method: 'notifications/progress',
params: { progress: 75 }
});
logTest(
'Mixed notifications and requests work correctly',
mixedSeq1.statusCode === 204 &&
mixedSeq2.body.id === 301 &&
mixedSeq3.statusCode === 204
);
// ==========================================
// 8. ERROR HANDLING TESTS
// ==========================================
console.log('\n🚨 8. Error Handling Tests');
console.log('─────────────────────────────────────────────────────────────');
// Test 8.1: Malformed JSON-RPC request
const malformedRequest = await sendRequest({
method: 'tools/list',
// Missing jsonrpc field
id: 401
});
logTest(
'Malformed request handled gracefully',
malformedRequest.statusCode === 200 || malformedRequest.statusCode === 500,
'Server responds without crashing'
);
// Test 8.2: Tool call with missing arguments
const missingArgs = await sendRequest({
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'create_workflow'
// Missing required arguments
},
id: 402
});
logTest(
'Tool call with missing arguments returns error',
missingArgs.body.error !== undefined,
`Error: ${missingArgs.body.error?.message || 'Unknown'}`
);
// ==========================================
// FINAL SUMMARY
// ==========================================
console.log('\n╔══════════════════════════════════════════════════════════════╗');
console.log('║ TEST SUMMARY ║');
console.log('╚══════════════════════════════════════════════════════════════╝\n');
const totalTests = testsPassed + testsFailed;
const successRate = ((testsPassed / totalTests) * 100).toFixed(1);
console.log(` Total Tests: ${totalTests}`);
console.log(` ✅ Passed: ${testsPassed}`);
console.log(` ❌ Failed: ${testsFailed}`);
console.log(` Success Rate: ${successRate}%`);
console.log('\n─────────────────────────────────────────────────────────────');
if (testsFailed === 0) {
console.log('\n 🎉 All tests passed! Server functionality is intact.\n');
process.exit(0);
} else {
console.log('\n ⚠️ Some tests failed. Review the output above.\n');
process.exit(1);
}
} catch (error) {
console.error('\n❌ Test suite failed with error:', error.message);
console.error('\nMake sure the MCP server is running with:');
console.error(' MCP_STANDALONE=true npm start\n');
process.exit(1);
}
}
// Run tests
console.log('\nStarting comprehensive integration tests...');
setTimeout(runTests, 1000); // Wait 1 second for any startup delays