-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-integration.js
More file actions
318 lines (268 loc) · 10.2 KB
/
Copy pathtest-integration.js
File metadata and controls
318 lines (268 loc) · 10.2 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
import { spawn } from 'child_process';
import WebSocket from 'ws';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class MCPIntegrationTester {
constructor() {
this.serverProcess = null;
this.ws = null;
this.testResults = [];
this.timeout = 10000; // 10 seconds
}
log(message, type = 'INFO') {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] [${type}] ${message}`);
}
async sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async startMCPServer() {
return new Promise((resolve, reject) => {
this.log('Starting MCP server...');
// Check if dist directory exists
if (!fs.existsSync('./dist/server.js')) {
reject(new Error('Server build not found. Run "npm run build-server" first.'));
return;
}
this.serverProcess = spawn('node', ['./dist/server.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: process.cwd()
});
this.serverProcess.stdout.on('data', (data) => {
this.log(`Server stdout: ${data.toString().trim()}`);
});
this.serverProcess.stderr.on('data', (data) => {
this.log(`Server stderr: ${data.toString().trim()}`, 'WARN');
});
this.serverProcess.on('error', (error) => {
this.log(`Server process error: ${error.message}`, 'ERROR');
reject(error);
});
this.serverProcess.on('exit', (code, signal) => {
this.log(`Server process exited with code ${code}, signal ${signal}`);
});
// Wait for server to start
setTimeout(() => {
if (this.serverProcess && !this.serverProcess.killed) {
this.log('MCP server started successfully');
resolve();
} else {
reject(new Error('Server failed to start'));
}
}, 3000);
});
}
async testWebSocketConnection() {
return new Promise((resolve, reject) => {
this.log('Testing WebSocket connection on port 3057...');
const timeout = setTimeout(() => {
if (this.ws) {
this.ws.terminate();
}
reject(new Error('WebSocket connection timeout'));
}, this.timeout);
try {
this.ws = new WebSocket('ws://localhost:3057');
this.ws.on('open', () => {
clearTimeout(timeout);
this.log('WebSocket connection established successfully');
resolve();
});
this.ws.on('error', (error) => {
clearTimeout(timeout);
this.log(`WebSocket connection error: ${error.message}`, 'ERROR');
reject(error);
});
this.ws.on('close', (code, reason) => {
this.log(`WebSocket connection closed: ${code} ${reason}`);
});
} catch (error) {
clearTimeout(timeout);
reject(error);
}
});
}
async sendMCPRequest(method, params = {}) {
return new Promise((resolve, reject) => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
reject(new Error('WebSocket not connected'));
return;
}
const requestId = Date.now().toString();
const request = {
jsonrpc: '2.0',
id: requestId,
method: method,
params: params
};
const timeout = setTimeout(() => {
reject(new Error(`Request timeout for method: ${method}`));
}, this.timeout);
const responseHandler = (data) => {
try {
const response = JSON.parse(data);
if (response.id === requestId) {
clearTimeout(timeout);
this.ws.off('message', responseHandler);
if (response.error) {
reject(new Error(`MCP Error: ${response.error.message}`));
} else {
resolve(response.result);
}
}
} catch (error) {
// Ignore parsing errors for other messages
}
};
this.ws.on('message', responseHandler);
this.ws.send(JSON.stringify(request));
this.log(`Sent MCP request: ${method}`);
});
}
async testMCPTools() {
const tests = [
{
name: 'list_tools',
method: 'tools/list',
params: {}
},
{
name: 'list_files',
method: 'tools/call',
params: {
name: 'list_files',
arguments: { path: '.' }
}
},
{
name: 'read_file',
method: 'tools/call',
params: {
name: 'read_file',
arguments: { path: 'package.json' }
}
},
{
name: 'create_test_file',
method: 'tools/call',
params: {
name: 'edit_file',
arguments: {
path: 'test-temp.txt',
content: 'This is a test file created by integration test'
}
}
}
];
for (const test of tests) {
try {
this.log(`Testing ${test.name}...`);
const result = await this.sendMCPRequest(test.method, test.params);
this.log(`✓ ${test.name} passed`);
this.testResults.push({ name: test.name, status: 'PASS', result });
} catch (error) {
this.log(`✗ ${test.name} failed: ${error.message}`, 'ERROR');
this.testResults.push({ name: test.name, status: 'FAIL', error: error.message });
}
}
}
async testErrorHandling() {
this.log('Testing error handling...');
try {
// Test invalid method
await this.sendMCPRequest('invalid_method', {});
this.testResults.push({ name: 'invalid_method', status: 'FAIL', error: 'Should have failed' });
} catch (error) {
this.log('✓ Invalid method properly rejected');
this.testResults.push({ name: 'invalid_method_error', status: 'PASS' });
}
try {
// Test invalid file read
await this.sendMCPRequest('tools/call', {
name: 'read_file',
arguments: { path: 'nonexistent-file.txt' }
});
this.testResults.push({ name: 'invalid_file_read', status: 'FAIL', error: 'Should have failed' });
} catch (error) {
this.log('✓ Invalid file read properly handled');
this.testResults.push({ name: 'invalid_file_read_error', status: 'PASS' });
}
}
async cleanup() {
this.log('Cleaning up test environment...');
// Clean up test file
if (fs.existsSync('test-temp.txt')) {
fs.unlinkSync('test-temp.txt');
this.log('Removed test file');
}
// Close WebSocket
if (this.ws) {
this.ws.close();
this.ws = null;
}
// Stop server process
if (this.serverProcess) {
this.serverProcess.kill('SIGTERM');
await this.sleep(2000);
if (!this.serverProcess.killed) {
this.serverProcess.kill('SIGKILL');
}
this.serverProcess = null;
}
this.log('Cleanup completed');
}
generateReport() {
this.log('\n=== INTEGRATION TEST REPORT ===');
const passedTests = this.testResults.filter(test => test.status === 'PASS');
const failedTests = this.testResults.filter(test => test.status === 'FAIL');
this.log(`Total Tests: ${this.testResults.length}`);
this.log(`Passed: ${passedTests.length}`);
this.log(`Failed: ${failedTests.length}`);
if (failedTests.length > 0) {
this.log('\nFAILED TESTS:');
failedTests.forEach(test => {
this.log(` ✗ ${test.name}: ${test.error}`);
});
}
if (passedTests.length > 0) {
this.log('\nPASSED TESTS:');
passedTests.forEach(test => {
this.log(` ✓ ${test.name}`);
});
}
const success = failedTests.length === 0 && passedTests.length > 0;
this.log(`\nOVERALL RESULT: ${success ? 'SUCCESS' : 'FAILURE'}\n`);
return success;
}
async runAllTests() {
try {
await this.startMCPServer();
await this.sleep(2000); // Give server time to fully initialize
await this.testWebSocketConnection();
await this.testMCPTools();
await this.testErrorHandling();
return this.generateReport();
} catch (error) {
this.log(`Integration test failed: ${error.message}`, 'ERROR');
return false;
} finally {
await this.cleanup();
}
}
}
// Run tests if called directly
if (import.meta.url === `file://${process.argv[1]}` || process.argv[1].endsWith('test-integration.js')) {
const tester = new MCPIntegrationTester();
tester.runAllTests()
.then(success => {
process.exit(success ? 0 : 1);
})
.catch(error => {
console.error('Test runner error:', error);
process.exit(1);
});
}
export default MCPIntegrationTester;