forked from session-foundation/session-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mcp.mjs
More file actions
313 lines (264 loc) · 9.2 KB
/
test-mcp.mjs
File metadata and controls
313 lines (264 loc) · 9.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
/**
* MCP Server Test Script
* Tests the MCP server components without full Electron
*/
import http from 'http';
import { URL } from 'url';
import { v4 as uuidv4 } from 'uuid';
// Mock components for testing
class MockWebhookManager {
constructor() {
this.subscriptions = new Map();
}
subscribe(url, filters) {
const id = uuidv4();
const subscription = {
id,
url,
filters,
createdAt: Date.now(),
lastTriggeredAt: null,
errorCount: 0,
};
this.subscriptions.set(id, subscription);
return subscription;
}
unsubscribe(subscriptionId) {
const existed = this.subscriptions.has(subscriptionId);
this.subscriptions.delete(subscriptionId);
return existed;
}
listSubscriptions() {
return Array.from(this.subscriptions.values());
}
}
// Test HTTP server (simulates MCP server without Electron dependencies)
function createTestMcpServer(port) {
const webhookManager = new MockWebhookManager();
const server = http.createServer();
server.on('request', async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
const url = new URL(req.url || '/', `http://127.0.0.1:${port}`);
// Health endpoint
if (url.pathname === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'ok', version: '1.0.0-mcp' }));
return;
}
// MCP SSE endpoint
if (url.pathname === '/mcp' && req.method === 'GET') {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
// Send initial connection event
const sessionId = uuidv4();
res.write(`event: endpoint\ndata: /messages?sessionId=${sessionId}\n\n`);
res.write(`event: message\ndata: ${JSON.stringify({ jsonrpc: '2.0', method: 'connected', params: { sessionId } })}\n\n`);
// Keep connection open for a bit then close
setTimeout(() => {
res.write(`event: message\ndata: ${JSON.stringify({ jsonrpc: '2.0', method: 'ping' })}\n\n`);
}, 100);
req.on('close', () => {
console.log(`[TEST] SSE connection closed for session ${sessionId}`);
});
return;
}
// Webhook test endpoints
if (url.pathname === '/api/subscribe' && req.method === 'POST') {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
try {
const { url: webhookUrl, filters } = JSON.parse(body);
const subscription = webhookManager.subscribe(webhookUrl, filters);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(subscription));
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: String(error) }));
}
});
return;
}
if (url.pathname === '/api/subscriptions' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(webhookManager.listSubscriptions()));
return;
}
if (url.pathname.startsWith('/api/unsubscribe/') && req.method === 'DELETE') {
const subId = url.pathname.split('/').pop();
const removed = webhookManager.unsubscribe(subId);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ removed }));
return;
}
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not found' }));
});
return { server, webhookManager };
}
// Test functions
async function testHealthEndpoint(port) {
return new Promise((resolve, reject) => {
http.get(`http://127.0.0.1:${port}/health`, (res) => {
let data = '';
res.on('data', chunk => { data += chunk; });
res.on('end', () => {
try {
const json = JSON.parse(data);
if (json.status === 'ok' && json.version === '1.0.0-mcp') {
resolve({ success: true, data: json });
} else {
reject(new Error(`Unexpected response: ${data}`));
}
} catch (e) {
reject(e);
}
});
}).on('error', reject);
});
}
async function testSseEndpoint(port) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
req.destroy();
reject(new Error('SSE timeout'));
}, 3000);
const req = http.get(`http://127.0.0.1:${port}/mcp`, (res) => {
if (res.statusCode !== 200) {
clearTimeout(timeout);
reject(new Error(`Unexpected status: ${res.statusCode}`));
return;
}
let receivedEvents = [];
res.on('data', chunk => {
const lines = chunk.toString().split('\n');
for (const line of lines) {
if (line.startsWith('event:') || line.startsWith('data:')) {
receivedEvents.push(line);
}
}
// Success - received SSE events
if (receivedEvents.length >= 2) {
clearTimeout(timeout);
req.destroy();
resolve({ success: true, events: receivedEvents });
}
});
});
req.on('error', (err) => {
clearTimeout(timeout);
// Connection closed is expected after we got events
if (receivedEvents && receivedEvents.length >= 2) {
resolve({ success: true, events: receivedEvents });
} else {
reject(err);
}
});
});
}
async function testWebhookSubscription(port) {
// Subscribe
const subscribeRes = await fetch(`http://127.0.0.1:${port}/api/subscribe`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: 'http://localhost:9999/webhook', filters: { includeOutgoing: false } }),
});
const subscription = await subscribeRes.json();
if (!subscription.id || !subscription.url) {
throw new Error('Invalid subscription response');
}
// List subscriptions
const listRes = await fetch(`http://127.0.0.1:${port}/api/subscriptions`);
const list = await listRes.json();
if (!Array.isArray(list) || list.length !== 1) {
throw new Error('List should have 1 subscription');
}
// Unsubscribe
const unsubRes = await fetch(`http://127.0.0.1:${port}/api/unsubscribe/${subscription.id}`, {
method: 'DELETE',
});
const unsubResult = await unsubRes.json();
if (!unsubResult.removed) {
throw new Error('Unsubscribe failed');
}
// Verify removed
const listRes2 = await fetch(`http://127.0.0.1:${port}/api/subscriptions`);
const list2 = await listRes2.json();
if (list2.length !== 0) {
throw new Error('Subscription should be removed');
}
return { success: true, subscription };
}
// Main test runner
async function runTests() {
const port = 16274;
const { server } = createTestMcpServer(port);
console.log('\n=== MCP Server Component Tests ===\n');
await new Promise((resolve, reject) => {
server.listen(port, '127.0.0.1', resolve);
server.on('error', reject);
});
console.log(`[TEST] Server started on port ${port}\n`);
const results = [];
// Test 1: Health endpoint
try {
console.log('Test 1: Health endpoint...');
const result = await testHealthEndpoint(port);
console.log(' ✓ Health endpoint works:', result.data);
results.push({ test: 'health', passed: true });
} catch (error) {
console.log(' ✗ Health endpoint failed:', error.message);
results.push({ test: 'health', passed: false, error: error.message });
}
// Test 2: SSE endpoint
try {
console.log('Test 2: SSE MCP endpoint...');
const result = await testSseEndpoint(port);
console.log(' ✓ SSE endpoint works, received events:', result.events.length);
results.push({ test: 'sse', passed: true });
} catch (error) {
console.log(' ✗ SSE endpoint failed:', error.message);
results.push({ test: 'sse', passed: false, error: error.message });
}
// Test 3: Webhook subscription flow
try {
console.log('Test 3: Webhook subscription flow...');
const result = await testWebhookSubscription(port);
console.log(' ✓ Webhook flow works, subscription ID:', result.subscription.id);
results.push({ test: 'webhook', passed: true });
} catch (error) {
console.log(' ✗ Webhook flow failed:', error.message);
results.push({ test: 'webhook', passed: false, error: error.message });
}
// Summary
console.log('\n=== Test Summary ===\n');
const passed = results.filter(r => r.passed).length;
const failed = results.filter(r => !r.passed).length;
console.log(`Passed: ${passed}/${results.length}`);
console.log(`Failed: ${failed}/${results.length}`);
if (failed > 0) {
console.log('\nFailed tests:');
results.filter(r => !r.passed).forEach(r => {
console.log(` - ${r.test}: ${r.error}`);
});
}
// Cleanup
server.close();
console.log('\n=== Tests Complete ===\n');
// Exit with error code if any tests failed
process.exit(failed > 0 ? 1 : 0);
}
runTests().catch(err => {
console.error('Test runner error:', err);
process.exit(1);
});