From baf80358cb3c79818d4d5cd3b8a8e388ac6c978c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:11:28 -0500 Subject: [PATCH] test(jeb): consolidate HTTP deadline and cancellation checks (#12843) test(jeb): consolidate HTTP deadline and cancellation coverage --- src/jeb/JebMcpConnection.test.ts | 400 ++++-------------- .../boundary/providers/jebMcpCatalog.test.ts | 111 ++--- .../providers/jebMcpConnection.test.ts | 56 ++- 3 files changed, 177 insertions(+), 390 deletions(-) diff --git a/src/jeb/JebMcpConnection.test.ts b/src/jeb/JebMcpConnection.test.ts index 56e3a160f8..644e4fedb8 100644 --- a/src/jeb/JebMcpConnection.test.ts +++ b/src/jeb/JebMcpConnection.test.ts @@ -1,5 +1,11 @@ import { createServer } from "node:http"; -import { Client } from "@modelcontextprotocol/client"; +import { withVerifierCleanup } from "../../scripts/lib/verifier-cleanup.mjs"; +import { startJebMcpFixture } from "../../tests/fixtures/jebMcp.js"; +import { + phaseGate, + settledCause, +} from "../../tests/support/lifecycleHarness.js"; +import { testWaitDeadline } from "../../tests/support/testWait.js"; import { describe, expect, it, vi } from "vitest"; import { @@ -9,193 +15,9 @@ import { import { AnalysisCancelledError, AnalysisProtocolError, - AnalysisTimeoutError, + OwnerCancellation, } from "../domain/analysisErrorCore.js"; -const jebTools = [ - "get_client_information", - "open_project", - "list_units", - "decompile_code_item", -] as const; - -const waitForRequestCount = async ( - readCount: () => number, - expected: number, - method: string, -): Promise => { - const deadline = performance.now() + 1_000; - while (readCount() < expected && performance.now() < deadline) - await new Promise((wake) => setTimeout(wake, 5)); - if (readCount() < expected) - throw new Error(`Expected ${expected} ${method} requests`); -}; - -const jebCatalogResult = ( - paginateCatalog: boolean, - cursor: string | undefined, -) => { - const pageTools = - paginateCatalog && cursor === undefined - ? jebTools.slice(0, 1) - : paginateCatalog - ? jebTools.slice(1) - : jebTools; - return { - tools: pageTools.map((name) => ({ - name, - inputSchema: { type: "object" as const, properties: {} }, - })), - ...(paginateCatalog && cursor === undefined - ? { nextCursor: "page-2" } - : {}), - }; -}; - -const startJebHttpFixture = ( - responseDelayMs: number, - catalogRefreshDelayMs = 0, - options: { readonly paginateCatalog?: boolean } = {}, -) => { - let callCount = 0; - let catalogCount = 0; - const pendingTimers = new Set(); - const server = createServer((request, response) => { - const chunks: Buffer[] = []; - request.on("data", (chunk: Buffer) => chunks.push(chunk)); - request.on("end", () => { - if (chunks.length === 0) { - response.writeHead(202).end(); - return; - } - const message: { - readonly id?: string | number; - readonly method: string; - readonly params?: Record; - } = JSON.parse(Buffer.concat(chunks).toString("utf8")); - if (message.method === "notifications/initialized") { - response.writeHead(202).end(); - return; - } - if (message.method === "notifications/cancelled") { - response.writeHead(202).end(); - return; - } - if (message.method === "initialize") { - response.writeHead(200, { "Content-Type": "application/json" }).end( - JSON.stringify({ - jsonrpc: "2.0", - id: message.id, - result: { - protocolVersion: message.params?.protocolVersion, - capabilities: { tools: {} }, - serverInfo: { name: "jeb-timeout-fixture", version: "1" }, - }, - }), - ); - return; - } - if (message.method === "tools/list") { - catalogCount++; - let responseClosed = false; - response.on("close", () => { - if (!response.writableEnded) responseClosed = true; - }); - const cursor = - typeof message.params?.cursor === "string" - ? message.params.cursor - : undefined; - const sendCatalog = () => { - if (responseClosed) return; - response.writeHead(200, { "Content-Type": "application/json" }).end( - JSON.stringify({ - jsonrpc: "2.0", - id: message.id, - result: jebCatalogResult( - options.paginateCatalog === true, - cursor, - ), - }), - ); - }; - if (catalogCount > 1 && catalogRefreshDelayMs > 0) { - const timer = setTimeout(() => { - pendingTimers.delete(timer); - sendCatalog(); - }, catalogRefreshDelayMs); - pendingTimers.add(timer); - } else { - sendCatalog(); - } - return; - } - if (message.method !== "tools/call") { - response.writeHead(202).end(); - return; - } - callCount++; - let responseClosed = false; - response.on("close", () => { - if (!response.writableEnded) { - responseClosed = true; - } - }); - const timer = setTimeout(() => { - pendingTimers.delete(timer); - if (responseClosed) return; - response.writeHead(200, { "Content-Type": "application/json" }).end( - JSON.stringify({ - jsonrpc: "2.0", - id: message.id, - result: { - structuredContent: { success: true, text: "finished" }, - content: [{ type: "text", text: '{"success":true}' }], - }, - }), - ); - }, responseDelayMs); - pendingTimers.add(timer); - }); - }); - - return new Promise<{ - readonly endpoint: URL; - readonly callCount: () => number; - readonly waitForCall: (count?: number) => Promise; - readonly waitForCatalog: (count?: number) => Promise; - readonly close: () => Promise; - }>((resolve, reject) => { - server.once("error", reject); - server.listen(0, "127.0.0.1", () => { - const address = server.address(); - if (address === null || typeof address === "string") { - reject(new Error("Expected TCP listener")); - return; - } - server.removeListener("error", reject); - resolve({ - endpoint: new URL(`http://127.0.0.1:${address.port}/mcp`), - callCount: () => callCount, - waitForCall: (count = 1) => - waitForRequestCount(() => callCount, count, "tools/call"), - waitForCatalog: (count = 1) => - waitForRequestCount(() => catalogCount, count, "tools/list"), - close: async () => { - for (const timer of pendingTimers) clearTimeout(timer); - pendingTimers.clear(); - const closed = new Promise((done, fail) => - server.close((cause) => - cause === undefined ? done() : fail(cause), - ), - ); - server.closeAllConnections(); - await closed; - }, - }); - }); - }); -}; - describe("decodeJebToolResult", () => { it("passes refusal envelopes through so the provider preserves the engine reason", () => { expect( @@ -310,144 +132,88 @@ it("refuses HTTP redirects before sending requests to another endpoint", async ( } }); -it("applies the selected timeout to real MCP tool calls", async () => { - const fixture = await startJebHttpFixture(180); - const short = createStreamableHttpJebMcpConnection(fixture.endpoint, { - requestTimeoutMs: 50, +it("accepts a tool reply beyond the SDK default under the selected timeout", async () => { + const observation = { success: true, text: "finished" }; + const fixture = await startJebMcpFixture(() => { + vi.advanceTimersByTime(65_000); + return observation; }); - const extended = createStreamableHttpJebMcpConnection(fixture.endpoint, { - requestTimeoutMs: 500, - }); - try { - await short.connect(); - await expect(short.call("decompile_code_item", {})).rejects.toBeInstanceOf( - AnalysisTimeoutError, - ); - expect(fixture.callCount()).toBe(1); - - await extended.connect(); + const connection = createStreamableHttpJebMcpConnection( + new URL(fixture.endpoint), + { requestTimeoutMs: 120_000 }, + ); + await withVerifierCleanup(async () => { + const signal = AbortSignal.timeout(testWaitDeadline()); + await connection.connect(signal); + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] }); await expect( - extended.call("decompile_code_item", {}), - ).resolves.toMatchObject({ success: true, text: "finished" }); - expect(fixture.callCount()).toBe(2); - } finally { - try { - await Promise.all([short.close(), extended.close()]); - } finally { - await fixture.close(); - } - } + connection.call("decompile_code_item", {}, signal), + ).resolves.toEqual(observation); + expect(fixture.calls).toEqual(["decompile_code_item"]); + }, [() => vi.useRealTimers(), () => connection.close(), fixture.close]); }); -it("keeps caller abort and connection close as cancellations for active HTTP calls", async () => { - const fixture = await startJebHttpFixture(1_000); - const connection = createStreamableHttpJebMcpConnection(fixture.endpoint, { - requestTimeoutMs: 2_000, - }); - try { - await connection.connect(); - const alreadyAborted = new AbortController(); - alreadyAborted.abort(); - await expect( - connection.call("decompile_code_item", {}, alreadyAborted.signal), - ).rejects.toBeInstanceOf(AnalysisCancelledError); - expect(fixture.callCount()).toBe(0); - - const caller = new AbortController(); - const callerCall = connection.call( - "decompile_code_item", - {}, - caller.signal, +it.each(["tools/call", "tools/list"])( + "cancels an active %s when the connection owner closes", + async (method) => { + const reply = phaseGate(); + let catalogs = 0; + const catalog = { + tools: [ + { + name: "decompile_code_item", + inputSchema: { type: "object" }, + }, + ], + }; + const fixture = await startJebMcpFixture( + async (tool) => { + if (tool === "decompile_code_item") await reply.hold(); + return { success: true }; + }, + async () => { + if (++catalogs > 1) await reply.hold(); + return catalog; + }, ); - await fixture.waitForCall(1); - caller.abort(); - await expect(callerCall).rejects.toBeInstanceOf(AnalysisCancelledError); + const connection = createStreamableHttpJebMcpConnection( + new URL(fixture.endpoint), + ); + await withVerifierCleanup(async () => { + await connection.connect(); + if (method === "tools/call") { + const reason = new Error("Cancelled before dispatch"); + const cause = await settledCause( + connection.call( + "get_client_information", + {}, + AbortSignal.abort(reason), + ), + ); + expect(cause).toBeInstanceOf(AnalysisCancelledError); + if (cause instanceof AnalysisCancelledError) + expect(cause.cause).toBe(reason); + expect(fixture.calls).toHaveLength(0); + } - const closingCall = connection.call("decompile_code_item", {}); - await fixture.waitForCall(2); - await connection.close(); - await expect(closingCall).rejects.toBeInstanceOf(AnalysisCancelledError); - } finally { - try { + const pending = + method === "tools/call" + ? connection.call("decompile_code_item", {}) + : connection.listTools(); + const rejected = settledCause(pending); + await reply.reached(pending, method); await connection.close(); - } finally { - await fixture.close(); - } - } -}); - -it("keeps caller abort and connection close as cancellations during catalog refresh", async () => { - const fixture = await startJebHttpFixture(0, 1_000); - const connection = createStreamableHttpJebMcpConnection(fixture.endpoint); - try { - await connection.connect(); - const caller = new AbortController(); - const callerRefresh = connection.listTools(caller.signal); - await fixture.waitForCatalog(2); - caller.abort(); - await expect(callerRefresh).rejects.toBeInstanceOf(AnalysisCancelledError); - - const closingRefresh = connection.listTools(); - await fixture.waitForCatalog(3); - await connection.close(); - await expect(closingRefresh).rejects.toBeInstanceOf(AnalysisCancelledError); - } finally { - try { - await connection.close(); - } finally { - await fixture.close(); - } - } -}); - -it("passes a discovery budget above the SDK default to connect and every catalog page", async () => { - const discoveryTimeoutMs = 120_000; - const connect = vi.spyOn(Client.prototype, "connect"); - const request = vi.spyOn(Client.prototype, "request"); - const fixture = await startJebHttpFixture(0, 0, { paginateCatalog: true }); - const connection = createStreamableHttpJebMcpConnection(fixture.endpoint, { - discoveryTimeoutMs, - requestTimeoutMs: discoveryTimeoutMs, - }); - try { - await expect(connection.connect()).resolves.toEqual([...jebTools]); - const connectTimeout = connect.mock.calls[0]?.[1]?.timeout; - expect(connectTimeout).toBeGreaterThan(60_000); - expect(connectTimeout).toBeLessThanOrEqual(discoveryTimeoutMs); - const listTimeouts = request.mock.calls.flatMap((call) => { - const message = call[0]; - if ( - typeof message !== "object" || - message === null || - !("method" in message) || - message.method !== "tools/list" - ) - return []; - const options = call[1]; - return [ - typeof options === "object" && - options !== null && - "timeout" in options && - typeof options.timeout === "number" - ? options.timeout - : -1, - ]; - }); - expect(listTimeouts.length).toBeGreaterThanOrEqual(2); - for (const timeout of listTimeouts) { - expect(timeout).toBeGreaterThan(60_000); - expect(timeout).toBeLessThanOrEqual(discoveryTimeoutMs); - } - } finally { - connect.mockRestore(); - request.mockRestore(); - try { - await connection.close(); - } finally { - await fixture.close(); - } - } -}); + const cause = await rejected; + expect(cause).toBeInstanceOf(AnalysisCancelledError); + expect(cause).toMatchObject({ + operation: + method === "tools/call" ? "decompile_code_item" : "jeb_mcp_catalog", + }); + if (cause instanceof AnalysisCancelledError) + expect(cause.cause).toBe(OwnerCancellation.ownerClosed); + }, [() => reply.release(), () => connection.close(), fixture.close]); + }, +); it.each([false, true])( "retains structured own members and nested dictionaries in a JEB reply (isError=%s)", diff --git a/tests/boundary/providers/jebMcpCatalog.test.ts b/tests/boundary/providers/jebMcpCatalog.test.ts index 3322cb67d9..cf2a55a0c2 100644 --- a/tests/boundary/providers/jebMcpCatalog.test.ts +++ b/tests/boundary/providers/jebMcpCatalog.test.ts @@ -2,11 +2,12 @@ import { createVerifierHttpServer } from "../../../scripts/lib/verifier-http-ser import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs"; import type { IncomingMessage, ServerResponse } from "node:http"; -import { expect, it } from "vitest"; +import { expect, it, vi } from "vitest"; import { z } from "zod"; import { createStreamableHttpJebMcpConnection } from "../../../src/jeb/JebMcpConnection.js"; import { AnalysisTimeoutError } from "../../../src/domain/analysisErrorCore.js"; +import { testWaitDeadline } from "../../support/testWait.js"; const requestSchema = z.object({ id: z.union([z.string(), z.number()]).optional(), @@ -18,7 +19,9 @@ const createCatalogServer = async () => { const controls = { includeOpen: true, repeatedCursor: false, - infinitePages: false, + continuedPages: 0, + initializeElapsedMs: 0, + pageElapsedMs: 0, pageCount: 0, }; const cursors: (string | undefined)[] = []; @@ -45,6 +48,8 @@ const createCatalogServer = async () => { } let result: unknown; if (message.method === "initialize") { + if (controls.initializeElapsedMs > 0) + vi.advanceTimersByTime(controls.initializeElapsedMs); result = { protocolVersion: "2025-11-25", capabilities: { tools: {} }, @@ -53,23 +58,29 @@ const createCatalogServer = async () => { } else if (message.method === "tools/list") { const cursor = message.params?.cursor; controls.pageCount += 1; - if (!controls.infinitePages) cursors.push(cursor); + cursors.push(cursor); + if (controls.pageElapsedMs > 0) + vi.advanceTimersByTime(controls.pageElapsedMs); const names = cursor === undefined ? ["get_client_information"] : controls.includeOpen ? ["open_project"] : ["list_units"]; + let nextCursor: string | undefined; + if (controls.continuedPages > 0) { + controls.continuedPages -= 1; + if (controls.continuedPages > 0) + nextCursor = `fresh-page-${controls.pageCount}`; + } else if (cursor === undefined || controls.repeatedCursor) { + nextCursor = "second-page"; + } result = { tools: [...names, "unused_extension"].map((name) => ({ name, inputSchema: { type: "object" }, })), - ...(controls.infinitePages - ? { nextCursor: `fresh-page-${controls.pageCount}` } - : cursor === undefined || controls.repeatedCursor - ? { nextCursor: "second-page" } - : {}), + ...(nextCursor === undefined ? {} : { nextCursor }), }; } else { throw new Error(`Unexpected JEB request: ${message.method}`); @@ -91,66 +102,60 @@ const createCatalogServer = async () => { }; }; -it("reads every JEB catalog page and refreshes the connected engine catalog", async () => { +it("uses the selected discovery budget across handshake and catalog pages", async () => { const { controls, cursors, endpoint, close } = await createCatalogServer(); - const connection = createStreamableHttpJebMcpConnection(new URL(endpoint), { - discoveryTimeoutMs: 1_000, + const discoveryTimeoutMs = 220_000; + const url = new URL(endpoint); + const connection = createStreamableHttpJebMcpConnection(url, { + discoveryTimeoutMs, }); - try { - expect(await connection.listTools()).toEqual([ - "get_client_information", - "open_project", - ]); + const acquiring = createStreamableHttpJebMcpConnection(url, { + discoveryTimeoutMs, + }); + const initialTools = ["get_client_information", "open_project"]; + const refreshedTools = ["get_client_information", "list_units"]; + await withVerifierCleanup(async () => { + const signal = AbortSignal.timeout(testWaitDeadline()); + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] }); + // Each reply arrives beyond the SDK default, within one shared limit. + controls.initializeElapsedMs = 65_000; + controls.pageElapsedMs = 65_000; + expect(await connection.listTools(signal)).toEqual(initialTools); + controls.initializeElapsedMs = 0; + controls.pageElapsedMs = 0; controls.includeOpen = false; - expect(await connection.listTools()).toEqual([ - "get_client_information", - "list_units", - ]); + expect(await connection.listTools(signal)).toEqual(refreshedTools); expect(cursors).toEqual([ undefined, "second-page", undefined, "second-page", ]); - controls.infinitePages = true; - const before = controls.pageCount; - await expect(connection.listTools()).rejects.toBeInstanceOf( - AnalysisTimeoutError, - ); - expect(controls.pageCount - before).toBeGreaterThan(2); - controls.infinitePages = false; - expect(await connection.listTools()).toEqual([ - "get_client_information", - "list_units", - ]); - // Shared initial acquisition also owns a finite deadline and can recover. - const acquiring = createStreamableHttpJebMcpConnection(new URL(endpoint), { - discoveryTimeoutMs: 1_000, - }); - try { - controls.infinitePages = true; - await expect(acquiring.listTools()).rejects.toBeInstanceOf( + for (const owner of [connection, acquiring]) { + // A fifth page would be available if the deadline were renewed per page. + controls.continuedPages = 5; + controls.pageElapsedMs = 55_000; + const before = controls.pageCount; + await expect(owner.listTools(signal)).rejects.toBeInstanceOf( AnalysisTimeoutError, ); - controls.infinitePages = false; - expect(await acquiring.listTools()).toEqual([ - "get_client_information", - "list_units", - ]); - } finally { - await acquiring.close(); + expect(controls.pageCount - before).toBe(4); + controls.continuedPages = 0; + controls.pageElapsedMs = 0; + expect(await owner.listTools(signal)).toEqual(refreshedTools); } + controls.repeatedCursor = true; - await expect(connection.listTools()).rejects.toThrow( + await expect(connection.listTools(signal)).rejects.toThrow( "repeated a tool catalog cursor", ); controls.repeatedCursor = false; - expect(await connection.listTools()).toEqual([ - "get_client_information", - "list_units", - ]); - } finally { - await withVerifierCleanup(() => connection.close(), [close]); - } + expect(await connection.listTools(signal)).toEqual(refreshedTools); + }, [ + () => vi.useRealTimers(), + () => connection.close(), + () => acquiring.close(), + close, + ]); }); diff --git a/tests/boundary/providers/jebMcpConnection.test.ts b/tests/boundary/providers/jebMcpConnection.test.ts index eb4b3ad9d1..36b624f924 100644 --- a/tests/boundary/providers/jebMcpConnection.test.ts +++ b/tests/boundary/providers/jebMcpConnection.test.ts @@ -2,6 +2,9 @@ import { createServer } from "node:http"; import { Client } from "@modelcontextprotocol/client"; import { expect, it, vi } from "vitest"; import { z } from "zod"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; +import { settledCause } from "../../support/lifecycleHarness.js"; +import { testWaitDeadline } from "../../support/testWait.js"; import { AnalysisCancelledError, @@ -309,21 +312,26 @@ it.each(["tools/call", "tools/list"])( it.each(["tools/call", "tools/list"])( "reconnects after a %s timeout while preserving its typed failure", async (method) => { + const timeoutMs = 1_000; const endpoint = await startEndpoint(200, { - discoveryTimeoutMs: 1_000, - requestTimeoutMs: 1_000, + discoveryTimeoutMs: timeoutMs, + requestTimeoutMs: timeoutMs, }); try { await endpoint.connection.connect(); + const signal = AbortSignal.timeout(testWaitDeadline()); + vi.useFakeTimers({ + toFake: ["setTimeout", "clearTimeout", "performance"], + }); const received = endpoint.holdNextRequest(method); const pending = method === "tools/call" - ? endpoint.connection.call("open_project", {}) - : endpoint.connection.listTools(); - const rejected = - expect(pending).rejects.toBeInstanceOf(AnalysisTimeoutError); - await received; - await rejected; + ? endpoint.connection.call("open_project", {}, signal) + : endpoint.connection.listTools(signal); + const rejected = settledCause(pending); + await awaitAcknowledgement(received, pending, method); + vi.advanceTimersByTime(timeoutMs); + expect(await rejected).toBeInstanceOf(AnalysisTimeoutError); expect(endpoint.handshakes()).toBe(1); await expect( endpoint.connection.call("get_client_information", {}), @@ -333,6 +341,7 @@ it.each(["tools/call", "tools/list"])( endpoint.calls.filter((name) => name === "open_project"), ).toHaveLength(method === "tools/call" ? 1 : 0); } finally { + vi.useRealTimers(); await endpoint.close(); } }, @@ -358,12 +367,14 @@ it("retains a working connection after an engine refusal or caller cancellation" method === "tools/call" ? endpoint.connection.call("list_units", {}, cancellation.signal) : endpoint.connection.listTools(cancellation.signal); - const rejected = expect(pending).rejects.toBeInstanceOf( - AnalysisCancelledError, - ); - await received; - cancellation.abort(); - await rejected; + const rejected = settledCause(pending); + await awaitAcknowledgement(received, pending, method); + const reason = new Error(`Cancel ${method}`); + cancellation.abort(reason); + const failure = await rejected; + expect(failure).toBeInstanceOf(AnalysisCancelledError); + if (failure instanceof AnalysisCancelledError) + expect(failure.cause).toBe(reason); } await expect( endpoint.connection.call("get_client_information", {}), @@ -375,21 +386,25 @@ it("retains a working connection after an engine refusal or caller cancellation" }); it("retains the typed timeout when disposal fails and retries owned cleanup before reconnecting", async () => { - const endpoint = await startEndpoint(200, { requestTimeoutMs: 1_000 }); + const timeoutMs = 1_000; + const endpoint = await startEndpoint(200, { requestTimeoutMs: timeoutMs }); const cleanupFailure = new Error("Client cleanup failed"); const close = vi.spyOn(Client.prototype, "close"); try { await endpoint.connection.connect(); + const signal = AbortSignal.timeout(testWaitDeadline()); + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] }); const catalogReceived = endpoint.holdNextRequest("tools/list"); const overlapping = endpoint.connection - .listTools() + .listTools(signal) .catch((cause: unknown) => cause); - await catalogReceived; + await awaitAcknowledgement(catalogReceived, overlapping, "tools/list"); close.mockRejectedValueOnce(cleanupFailure); const received = endpoint.holdNextRequest(); - const pending = endpoint.connection.call("open_project", {}); - const rejected = pending.catch((cause: unknown) => cause); - await received; + const pending = endpoint.connection.call("open_project", {}, signal); + const rejected = settledCause(pending); + await awaitAcknowledgement(received, pending, "tools/call"); + vi.advanceTimersByTime(timeoutMs); const failure = await rejected; expect(failure).toBeInstanceOf(JebMcpCleanupFailure); if (!(failure instanceof JebMcpCleanupFailure)) throw failure; @@ -410,6 +425,7 @@ it("retains the typed timeout when disposal fails and retries owned cleanup befo expect(failure.cleanupIncomplete).toBe(false); expect(failure.cleanupResources).toEqual([]); } finally { + vi.useRealTimers(); close.mockRestore(); await endpoint.close(); }