|
| 1 | +import { test } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { spawn } from 'node:child_process'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { dirname, resolve } from 'node:path'; |
| 6 | + |
| 7 | +const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', '..'); |
| 8 | +const SERVER = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'bin', 'node-core-mcp.mjs'); |
| 9 | + |
| 10 | +function createClient() { |
| 11 | + const proc = spawn(process.execPath, [SERVER, '--repo', REPO_ROOT], { |
| 12 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 13 | + }); |
| 14 | + |
| 15 | + let buffer = ''; |
| 16 | + let nextId = 1; |
| 17 | + const pending = new Map(); |
| 18 | + |
| 19 | + proc.stdout.setEncoding('utf8'); |
| 20 | + proc.stdout.on('data', (chunk) => { |
| 21 | + buffer += chunk; |
| 22 | + const lines = buffer.split('\n'); |
| 23 | + buffer = lines.pop(); |
| 24 | + for (const line of lines) { |
| 25 | + if (!line.trim()) continue; |
| 26 | + try { |
| 27 | + const msg = JSON.parse(line); |
| 28 | + const cb = pending.get(msg.id); |
| 29 | + if (cb) { pending.delete(msg.id); cb(msg); } |
| 30 | + } catch { /* ignore malformed lines */ } |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + const call = (method, params = {}) => new Promise((resolve, reject) => { |
| 35 | + const id = nextId++; |
| 36 | + const timer = setTimeout(() => { |
| 37 | + pending.delete(id); |
| 38 | + reject(new Error(`Timeout: no response to "${method}"`)); |
| 39 | + }, 10_000); |
| 40 | + pending.set(id, (msg) => { clearTimeout(timer); resolve(msg); }); |
| 41 | + proc.stdin.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n'); |
| 42 | + }); |
| 43 | + |
| 44 | + return { call, close: () => proc.kill() }; |
| 45 | +} |
| 46 | + |
| 47 | +test('initialize returns protocol version and server info', async () => { |
| 48 | + const client = createClient(); |
| 49 | + try { |
| 50 | + const res = await client.call('initialize', { |
| 51 | + protocolVersion: '2024-11-05', |
| 52 | + capabilities: {}, |
| 53 | + clientInfo: { name: 'test', version: '0.1' }, |
| 54 | + }); |
| 55 | + assert.equal(res.result.protocolVersion, '2024-11-05'); |
| 56 | + assert.equal(res.result.serverInfo.name, 'node-core'); |
| 57 | + assert.ok(res.result.capabilities.tools); |
| 58 | + } finally { |
| 59 | + client.close(); |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +test('unknown method returns error -32601', async () => { |
| 64 | + const client = createClient(); |
| 65 | + try { |
| 66 | + const res = await client.call('no/such/method'); |
| 67 | + assert.ok(res.error); |
| 68 | + assert.equal(res.error.code, -32601); |
| 69 | + } finally { |
| 70 | + client.close(); |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +test('tools/list returns all expected tools in order', async () => { |
| 75 | + const client = createClient(); |
| 76 | + try { |
| 77 | + const res = await client.call('tools/list'); |
| 78 | + const names = res.result.tools.map((t) => t.name); |
| 79 | + assert.deepEqual(names, [ |
| 80 | + 'configure', 'build', 'run_test', 'run_tests', |
| 81 | + 'search_code', 'list_docs', 'read_doc', |
| 82 | + 'find_subsystem', 'list_relevant_tests', 'explain_test_failure', 'search_docs', |
| 83 | + 'get_pr_metadata', |
| 84 | + ]); |
| 85 | + } finally { |
| 86 | + client.close(); |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +test('each tool has name, description, and inputSchema', async () => { |
| 91 | + const client = createClient(); |
| 92 | + try { |
| 93 | + const res = await client.call('tools/list'); |
| 94 | + for (const tool of res.result.tools) { |
| 95 | + assert.ok(tool.name, 'missing name'); |
| 96 | + assert.ok(tool.description, `${tool.name}: missing description`); |
| 97 | + assert.ok(tool.inputSchema, `${tool.name}: missing inputSchema`); |
| 98 | + } |
| 99 | + } finally { |
| 100 | + client.close(); |
| 101 | + } |
| 102 | +}); |
| 103 | + |
| 104 | +test('tools/call unknown tool returns error -32601', async () => { |
| 105 | + const client = createClient(); |
| 106 | + try { |
| 107 | + const res = await client.call('tools/call', { name: 'nonexistent', arguments: {} }); |
| 108 | + assert.ok(res.error); |
| 109 | + assert.equal(res.error.code, -32601); |
| 110 | + } finally { |
| 111 | + client.close(); |
| 112 | + } |
| 113 | +}); |
0 commit comments