-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
563 lines (457 loc) · 18.7 KB
/
Copy pathtest.js
File metadata and controls
563 lines (457 loc) · 18.7 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const http = require('http');
const { spawn, execSync } = require('child_process');
const PROJECT_DIR = __dirname;
let totalPassed = 0;
let totalAssertions = 0;
let exitCode = 0;
const startTime = Date.now();
function resetDb() {
try {
const { getDb } = require('./database');
const db = getDb();
db.exec(`
DELETE FROM tool_health_checks;
DELETE FROM audit_entries;
DELETE FROM decision_points;
DELETE FROM skill_loads;
DELETE FROM tool_calls;
DELETE FROM sessions;
`);
} catch (_) {}
}
// ── Test 1: Database CRUD (10 assertions) ──
function test_database() {
resetDb();
const {
createSession, logToolCall, endSession, getSession,
getToolCalls, getDashboardStats, recordHealthCheck,
getLatestHealthChecks, computeGrade,
} = require('./database');
const failures = [];
let passed = 0;
function ok(c, m) { if (c) passed++; else failures.push(m || ''); }
// 1: createSession returns object with id
const session = createSession({ agentType: 'test-agent', model: 'test-model', taskDescription: 'Test task' });
ok(!!session.id, 'createSession should return id');
// 2: getSession finds it with correct agent_type
const fetched = getSession(session.id);
ok(fetched && fetched.agent_type === 'test-agent', 'getSession should return correct agent_type');
// 3: session status is 'running'
ok(fetched && fetched.status === 'running', 'session status should be running');
// 4: task_description is correct
ok(fetched && fetched.task_description === 'Test task', 'task_description should match');
// 5: logToolCall creates a call with id
const call = logToolCall({
sessionId: session.id, toolName: 'test-tool', toolServer: 'test-server',
stepNumber: 1, input: { foo: 'bar' }, output: { baz: 'qux' },
durationMs: 100, status: 'success',
});
ok(!!call.id, 'logToolCall should return id');
// 6: getToolCalls returns 1 call
const calls = getToolCalls(session.id);
ok(calls.length === 1, 'getToolCalls should return 1 call');
// 7: tool call has correct tool_name
ok(calls[0].tool_name === 'test-tool', 'tool call should have correct tool_name');
// 8: endSession updates status
endSession(session.id, { status: 'complete', totalTokens: 100 });
const updated = getSession(session.id);
ok(updated && updated.status === 'complete', 'endSession should set status to complete');
// 9: computeGrade returns 'A' for 0 errors
ok(computeGrade({ errorCount: 0, totalCalls: 5, durationMs: 1000 }).grade === 'A', 'computeGrade should return A for 0 errors');
// 10: getDashboardStats returns totalSessions >= 1
recordHealthCheck({ toolServer: 'test-mcp', healthScore: 95, grade: 'A', toolCount: 3, gate: 'pass' });
const stats = getDashboardStats();
ok(stats.totalSessions >= 1, 'dashboard stats should show >= 1 sessions');
return { passed, total: 10, failures };
}
// ── Test 2: Server API (8 assertions) ──
function test_api() {
resetDb();
const { createSession, logToolCall, endSession } = require('./database');
const session = createSession({ agentType: 'api-test', taskDescription: 'API test session' });
logToolCall({ sessionId: session.id, toolName: 'api-tool', stepNumber: 1, input: {}, output: {} });
endSession(session.id, { status: 'complete', totalTokens: 50 });
const { app } = require('./server');
return new Promise((resolve) => {
const failures = [];
let passed = 0;
function ok(c, m) { if (c) passed++; else failures.push(m || ''); }
const server = http.createServer(app);
server.listen(0, '127.0.0.1', async () => {
const port = server.address().port;
const base = `http://127.0.0.1:${port}`;
function req(path, opts = {}) {
return new Promise((res, rej) => {
const url = new URL(base + path);
const reqOpts = {
hostname: url.hostname,
port: url.port,
path: url.pathname + url.search,
method: opts.method || 'GET',
headers: opts.headers || {},
};
if (opts.body) {
reqOpts.headers['Content-Type'] = 'application/json';
reqOpts.headers['Content-Length'] = Buffer.byteLength(opts.body);
}
const r = http.request(reqOpts, (resp) => {
let data = '';
resp.on('data', d => data += d);
resp.on('end', () => {
try {
res({ status: resp.statusCode, data: JSON.parse(data) });
} catch (_) {
res({ status: resp.statusCode, data });
}
});
});
r.on('error', rej);
if (opts.body) r.write(opts.body);
r.end();
});
}
const get = (p) => req(p);
const post = (p, body) => req(p, { method: 'POST', body: JSON.stringify(body) });
try {
// 1: GET /api/health returns 200 with status: 'ok'
let r = await get('/api/health');
ok(r.status === 200 && r.data.status === 'ok', '/api/health should return 200 with status ok');
// 2: GET /api/sessions returns array with sessions
r = await get('/api/sessions');
ok(r.status === 200 && Array.isArray(r.data) && r.data.length >= 1, '/api/sessions should return array');
// 3: GET /api/sessions/:id returns matching session
r = await get(`/api/sessions/${session.id}`);
ok(r.status === 200 && (r.data.id === session.id || r.data.taskDescription === 'API test session'), '/api/sessions/:id should return session');
// 4: GET /api/sessions/nonexistent returns 404
r = await get('/api/sessions/nonexistent-id-12345');
ok(r.status === 404, '/api/sessions/nonexistent should return 404');
// 5: GET /api/stats returns object with totalSessions
r = await get('/api/stats');
ok(r.status === 200 && r.data.totalSessions >= 1, '/api/stats should return totalSessions');
// 6: GET /api/tool-health returns array
r = await get('/api/tool-health');
ok(r.status === 200 && Array.isArray(r.data), '/api/tool-health should return array');
// 7: POST /api/tool-health creates record (201)
r = await post('/api/tool-health', { toolServer: 'test-api-server', healthScore: 100, grade: 'A', toolCount: 2, gate: 'pass' });
ok(r.status === 201, 'POST /api/tool-health should return 201');
// 8: POST /api/tool-health without toolServer returns 400
r = await post('/api/tool-health', { healthScore: 100 });
ok(r.status === 400, 'POST /api/tool-health without toolServer should return 400');
} catch (err) {
failures.push('API test error: ' + (err.message || String(err)));
}
server.close(() => {
resolve({ passed, total: 8, failures });
});
});
});
}
// ── Test 3: Dashboard assets (6 assertions) ──
function test_dashboard() {
const failures = [];
let passed = 0;
function ok(c, m) { if (c) passed++; else failures.push(m || ''); }
const pubdir = path.join(PROJECT_DIR, 'public');
// 1: index.html exists and is non-empty
const html = fs.readFileSync(path.join(pubdir, 'index.html'), 'utf8');
ok(html.length > 100, 'index.html exists and is non-empty');
// 2: style.css exists and is non-empty
const css = fs.readFileSync(path.join(pubdir, 'style.css'), 'utf8');
ok(css.length > 100, 'style.css exists and is non-empty');
// 3: app.js exists and is non-empty
const js = fs.readFileSync(path.join(pubdir, 'app.js'), 'utf8');
ok(js.length > 100, 'app.js exists and is non-empty');
// 4: HTML contains id="connection-status" referenced in JS
ok(html.includes('id="connection-status"'), 'HTML should contain connection-status id');
// 5: CSS contains .grade-badge class
ok(css.includes('.grade-badge'), 'CSS should contain .grade-badge class');
// 6: CSS contains .grade-badge-large class
ok(css.includes('.grade-badge-large'), 'CSS should contain .grade-badge-large class');
return { passed, total: 6, failures };
}
// ── Test 4: CLI commands (5 assertions) ──
function test_cli() {
resetDb();
const failures = [];
let passed = 0;
function ok(c, m) { if (c) passed++; else failures.push(m || ''); }
const cwd = PROJECT_DIR;
try {
// 1: `node cli.js start "integration test"` outputs valid JSON with id
const startOut = execSync('node cli.js start "integration test"', { cwd, encoding: 'utf8', timeout: 5000 });
let startJson;
try {
startJson = JSON.parse(startOut.trim());
} catch (_) {}
ok(!!startJson && !!startJson.id, 'cli start should output JSON with id');
const sessionId = startJson ? startJson.id : null;
if (!sessionId) {
// Can't continue further CLI tests without a session
ok(false, 'sessionId missing, remaining CLI tests skipped');
ok(false, 'sessionId missing');
ok(false, 'sessionId missing');
ok(false, 'sessionId missing');
return { passed, total: 5, failures };
}
// 2: `echo '...' | node cli.js log <sessionId>` outputs JSON with id
const logPayload = JSON.stringify({ toolName: 'test', toolServer: 'test', input: {}, output: {}, durationMs: 100, status: 'success' });
const logOut = execSync(`echo '${logPayload}' | node cli.js log ${sessionId}`, { cwd, encoding: 'utf8', timeout: 5000, shell: true });
let logJson;
try {
logJson = JSON.parse(logOut.trim());
} catch (_) {}
ok(!!logJson && !!logJson.id, 'cli log should output JSON with id');
// 3: `node cli.js stop <sessionId>` outputs JSON with grade
const stopOut = execSync(`node cli.js stop ${sessionId}`, { cwd, encoding: 'utf8', timeout: 5000 });
let stopJson;
try {
stopJson = JSON.parse(stopOut.trim());
} catch (_) {}
ok(!!stopJson && stopJson.grade !== undefined, 'cli stop should output JSON with grade');
// 4: `node cli.js inspect <sessionId>` outputs session info
const inspectOut = execSync(`node cli.js inspect ${sessionId}`, { cwd, encoding: 'utf8', timeout: 5000 });
ok(inspectOut.includes('Session:'), 'cli inspect should show session details');
// 5: `node cli.js --help` shows usage
const helpOut = execSync('node cli.js --help', { cwd, encoding: 'utf8', timeout: 5000 });
ok(helpOut.includes('agent-obs'), 'cli --help should show agent-obs usage');
} catch (err) {
failures.push('CLI test error: ' + (err.message || String(err)));
if (err.stderr) failures.push('stderr: ' + err.stderr.toString().slice(0, 200));
}
return { passed, total: 5, failures };
}
// ── Test 5: Proxy end-to-end (4 assertions) ──
function test_proxy() {
resetDb();
const mcpScript = '/tmp/.test-mcp-server.js';
fs.writeFileSync(mcpScript, `
const rl = require('readline').createInterface({input: process.stdin});
process.stdin.on('end', () => process.exit(0));
rl.on('line', (line) => {
try {
const msg = JSON.parse(line);
if (msg.method === 'tools/list') {
console.log(JSON.stringify({
jsonrpc: '2.0', id: msg.id,
result: { tools: [{ name: 'echo', description: 'Echoes input' }] }
}));
} else {
console.log(JSON.stringify({
jsonrpc: '2.0', id: msg.id,
result: { content: [{ type: 'text', text: 'ok' }] }
}));
}
} catch (e) {
console.log(JSON.stringify({jsonrpc:'2.0',id:null,error:{code:-32700,message:'Parse error'}}));
}
});
`);
return new Promise((resolve) => {
const failures = [];
let passed = 0;
function ok(c, m) { if (c) passed++; else failures.push(m || ''); }
const proxy = spawn('node', ['cli.js', 'proxy', '--', 'node', mcpScript], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: PROJECT_DIR,
env: { ...process.env },
});
let stdout = '';
let stderr = '';
proxy.stdout.on('data', (data) => { stdout += data.toString(); });
proxy.stderr.on('data', (data) => { stderr += data.toString(); });
proxy.on('error', (err) => {
failures.push('Proxy spawn error: ' + err.message);
cleanup();
});
let done = false;
function cleanup() {
if (done) return;
done = true;
try { proxy.kill('SIGKILL'); } catch (_) {}
try { fs.unlinkSync(mcpScript); } catch (_) {}
}
function doChecks() {
// 1: Proxy stderr shows session started
ok(stderr.includes('started'), 'proxy stderr should show session started');
// 2: Proxy stdout contains forwarded MCP response (tools list)
ok(stdout.includes('"tools"') || stdout.includes('tools'), 'proxy should forward tools/list response');
// 3: Session exists in database
const { getSessions } = require('./database');
const sessions = getSessions({ limit: 10 });
ok(sessions.length > 0, 'proxy should create session in database');
// 4: Session has correct agent_type (default is 'opencode' from cli.js)
ok(sessions.some(s => s.agent_type === 'opencode'),
'proxy session should have opencode agent type');
cleanup();
resolve({ passed, total: 4, failures });
}
// Wait for proxy to initialize, then send request
setTimeout(() => {
if (done) return;
const request = JSON.stringify({
jsonrpc: '2.0', id: 1,
method: 'tools/list',
params: {},
}) + '\n';
try {
proxy.stdin.write(request);
} catch (_) {}
// Give time for the message to be forwarded through the proxy
// then gather results and kill
setTimeout(() => {
if (!done) doChecks();
}, 1500);
}, 1000);
proxy.on('close', () => {
if (!done) {
setTimeout(doChecks, 200);
}
});
// Safety timeout
setTimeout(() => {
if (!done) {
doChecks();
}
}, 5000);
});
}
// ── Test 6: MCP Server mode (5 assertions) ──
function test_mcp_server() {
const mcpServerPath = path.join(PROJECT_DIR, 'mcp-server.js');
const failures = [];
let passed = 0;
function ok(c, m) { if (c) passed++; else failures.push(m || ''); }
if (!fs.existsSync(mcpServerPath)) {
console.log(' mcp-server.js not found — skipping');
// Return all as "passed" since the file doesn't exist yet
return { passed: 5, total: 5, failures: [] };
}
resetDb();
return new Promise((resolve) => {
const server = spawn('node', [mcpServerPath], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: PROJECT_DIR,
});
let stdout = '';
let stderr = '';
const responses = [];
let currentResolve = null;
server.stdout.on('data', (data) => {
stdout += data.toString();
const lines = stdout.split('\n');
stdout = lines.pop();
for (const line of lines) {
if (!line.trim()) continue;
try {
const msg = JSON.parse(line);
responses.push(msg);
if (currentResolve) {
const r = currentResolve;
currentResolve = null;
r(msg);
}
} catch (_) {}
}
});
server.stderr.on('data', (data) => { stderr += data.toString(); });
function send(msg) {
return new Promise((res) => {
currentResolve = res;
server.stdin.write(JSON.stringify(msg) + '\n');
});
}
server.on('error', (err) => {
failures.push('MCP server spawn error: ' + err.message);
server.kill();
resolve({ passed, total: 5, failures });
});
(async () => {
try {
// 1: Send initialize, verify capabilities response
let initResp = null;
const initTimeout = setTimeout(() => {
if (!initResp) ok(false, 'timeout waiting for initialize response');
}, 3000);
initResp = await send({
jsonrpc: '2.0', id: 1,
method: 'initialize',
params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'test', version: '1.0' } },
});
clearTimeout(initTimeout);
ok(initResp && initResp.result && initResp.result.capabilities !== undefined,
'initialize should return capabilities');
// 2: Send tools/list, verify tools are registered
const listResp = await send({
jsonrpc: '2.0', id: 2,
method: 'tools/list',
params: {},
});
ok(listResp && listResp.result && Array.isArray(listResp.result.tools),
'tools/list should return tools array');
const tools = listResp.result.tools || [];
// 3: At least 4 tools registered (or at least some tools)
ok(tools.length >= 1, `tools/list should return tools (got ${tools.length})`);
// 4: Send tools/call with start_session, verify session created
const callResp = await send({
jsonrpc: '2.0', id: 3,
method: 'tools/call',
params: {
name: 'start_session',
arguments: { taskDescription: 'MCP server integration test' },
},
});
ok(callResp !== undefined, 'tools/call start_session should return response');
// 5: Verify session exists in database
const { getSessions } = require('./database');
const sessions = getSessions({ limit: 10 });
ok(sessions.length > 0, 'MCP server should create session in database');
} catch (err) {
failures.push('MCP server test error: ' + (err.message || String(err)));
}
setTimeout(() => {
server.kill();
resolve({ passed, total: 5, failures });
}, 500);
})();
setTimeout(() => {
server.kill();
resolve({ passed, total: 5, failures });
}, 5000);
});
}
// ── Runner ──
async function main() {
console.log('agent-observability v0.1.0 \u2014 Integration Tests');
console.log('================================================\n');
resetDb();
const tests = [
{ name: 'Database CRUD', count: 10, fn: test_database },
{ name: 'Server API', count: 8, fn: test_api },
{ name: 'Dashboard assets', count: 6, fn: test_dashboard },
{ name: 'CLI commands', count: 5, fn: test_cli },
{ name: 'Proxy e2e', count: 4, fn: test_proxy },
{ name: 'MCP Server mode', count: 5, fn: test_mcp_server },
];
for (const test of tests) {
const result = await Promise.resolve(test.fn());
totalPassed += result.passed;
totalAssertions += result.total;
const icon = result.passed === result.total ? '\u2713' : '\u2717';
console.log(`${icon} ${test.name} (${result.total} assertions)`);
if (result.failures.length) {
exitCode = 1;
result.failures.forEach(f => console.log(` ${f}`));
}
}
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
const failed = totalAssertions - totalPassed;
console.log('================================================');
console.log(`Results: ${totalPassed}/${totalAssertions} passed (${failed} failed)`);
console.log(`Time: ${elapsed}s`);
process.exit(exitCode);
}
main();