From d3a5105327a8fb0ae0ee04e6ade5c1f3d8e9ad04 Mon Sep 17 00:00:00 2001 From: Studio Date: Tue, 1 Dec 2026 07:02:07 -0500 Subject: [PATCH] T-136: tests --- .../test/mcp-read-knowledge-qa.test.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/libs/research/test/mcp-read-knowledge-qa.test.ts b/libs/research/test/mcp-read-knowledge-qa.test.ts index fbbf4a44..0e2940a3 100644 --- a/libs/research/test/mcp-read-knowledge-qa.test.ts +++ b/libs/research/test/mcp-read-knowledge-qa.test.ts @@ -200,6 +200,48 @@ describe('read_knowledge QA: port failures and malformed port output', () => { expectSanitized(res); } }); + + // Regression: port calls were started eagerly inside the Promise.all array literal, so a + // synchronous throw from a later source abandoned an earlier source's rejected promise. + // Bun treats that unhandled rejection as fatal: the MCP stdio process exits 1 after replying + // and prints the raw error (including any connection string) to stderr. Bun's test runner + // also reports it as an error between tests, so the scenario runs in a child process. + test('earlier async rejection combined with a later synchronous throw does not crash the process', () => { + const indexPath = new URL('../src/mcp/index.ts', import.meta.url).pathname; + const script = ` + import { createMcpServer, createReadKnowledgeTool } from ${JSON.stringify(indexPath)}; + const secret = ${JSON.stringify(SECRET)}; + const combos = [ + { listLibraryCards: () => Promise.reject(new Error(secret)), listFindings: () => { throw new Error('sync'); }, listProviderCaveats: async () => [] }, + { listLibraryCards: () => Promise.reject(new Error(secret)), listFindings: async () => [], listProviderCaveats: () => { throw new Error('sync'); } }, + { listLibraryCards: async () => [], listFindings: () => Promise.reject(new Error(secret)), listProviderCaveats: () => { throw new Error('sync'); } }, + ]; + for (const port of combos) { + const server = createMcpServer({ name: 'qa', version: '0.0.0', tools: [createReadKnowledgeTool({ port })] }); + const raw = await server.handleMessage(JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'read_knowledge', arguments: {} } })); + console.log(JSON.parse(raw).result.content[0].text); + } + await new Promise(resolve => setTimeout(resolve, 20)); + console.log('DONE'); + `; + + const proc = Bun.spawnSync([process.execPath, '-e', script], { + stdout: 'pipe', + stderr: 'pipe', + timeout: 30_000, + }); + const stdout = proc.stdout.toString(); + const stderr = proc.stderr.toString(); + + expect(stdout.trim().split('\n')).toEqual([ + 'Error: Tool execution failed.', + 'Error: Tool execution failed.', + 'Error: Tool execution failed.', + 'DONE', + ]); + expect(stderr).not.toContain('super-secret'); + expect(proc.exitCode).toBe(0); + }); }); describe('read_knowledge QA: read-only port usage', () => {