diff --git a/tests/boundary/mcp/sourcePublicationRecovery.test.ts b/tests/boundary/mcp/sourcePublicationRecovery.test.ts index 72de1a7ac1b..146a466f7bd 100644 --- a/tests/boundary/mcp/sourcePublicationRecovery.test.ts +++ b/tests/boundary/mcp/sourcePublicationRecovery.test.ts @@ -1,5 +1,5 @@ import { unknownTarget } from "../../../src/domain/targetIdentity.js"; -import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; +import type { Client } from "@modelcontextprotocol/client"; import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server"; import { expect, it } from "vitest"; import { writeFile } from "node:fs/promises"; @@ -16,7 +16,43 @@ import { createEvidence, type Evidence } from "../../../src/domain/evidence.js"; import { safeParseEvidence } from "../../../src/domain/evidenceValidation.js"; import { ok } from "../../../src/domain/result.js"; import { createServer } from "../../../src/server/createServer.js"; -import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { connectFixtureClient } from "../../support/mcp/mcpFixture.js"; + +const withOpenComFixture = async ( + { + server, + session, + name, + path, + }: { + server: ReturnType; + session: BinarySession; + name: string; + path: string; + }, + check: (client: Client) => Promise, +): Promise => { + const resources: Array<{ close(): Promise }> = []; + try { + const client = await connectFixtureClient( + server, + { name, version: "1" }, + resources, + ); + expect( + ( + await client.callTool({ + name: "open_binary", + arguments: { path, format: "dos-com" }, + }) + ).isError, + ).not.toBe(true); + await check(client); + } finally { + for (const resource of resources) await resource.close(); + await session.close(); + } +}; const assertPublication = ({ error, @@ -260,75 +296,59 @@ it.each([ }); const session = new BinarySession(router, records); const server = createServer({ kind: "session", session }); - const client = new Client( - { name: "source-publication", version: "1" }, - { jsonSchemaValidator: createMcpSchemaValidation() }, + await withOpenComFixture( + { server, session, name: "source-publication", path }, + async (client) => { + const target = session.activeTarget(); + if (target === undefined) throw new Error("Expected open target"); + const primary = preexisting + ? createAnalysisExecutionEvidence( + "address_name", + { document: "fixture", address: "0x100" }, + { + ...createAnalysisExecution(result, identity, { + sourceEvidence: [source], + }), + subject: differentPath + ? { ...target, path: `${path}.old` } + : target, + }, + "observed", + ) + : undefined; + if (primary !== undefined) + expect(session.recordEvidence(primary).ok).toBe(true); + const response = await client.callTool( + { + name: "address_name", + arguments: { document: "fixture", address: "0x100" }, + }, + { timeout: 120000 }, + ); + expect(response.isError).toBe(true); + const error = parseMcpToolError(response).error; + assertPublication({ + error, + source, + primary, + sourceKind, + primaryKind, + preexisting, + differentPath, + selectedPath: path, + session, + }); + expect(records.statistics().evidenceRecords).toBe(preexisting ? 1 : 0); + expect( + ( + await client.callTool({ + name: "get_evidence_bundle", + arguments: { detail: "summary" }, + }) + ).isError, + ).not.toBe(true); + }, ); - const [ct, st] = InMemoryTransport.createLinkedPair(); - try { - await server.connect(st); - await client.connect(ct); - expect( - ( - await client.callTool({ - name: "open_binary", - arguments: { path, format: "dos-com" }, - }) - ).isError, - ).not.toBe(true); - const target = session.activeTarget(); - if (target === undefined) throw new Error("Expected open target"); - const primary = preexisting - ? createAnalysisExecutionEvidence( - "address_name", - { document: "fixture", address: "0x100" }, - { - ...createAnalysisExecution(result, identity, { - sourceEvidence: [source], - }), - subject: differentPath - ? { ...target, path: `${path}.old` } - : target, - }, - "observed", - ) - : undefined; - if (primary !== undefined) - expect(session.recordEvidence(primary).ok).toBe(true); - const response = await client.callTool( - { - name: "address_name", - arguments: { document: "fixture", address: "0x100" }, - }, - { timeout: 120000 }, - ); - expect(response.isError).toBe(true); - const error = parseMcpToolError(response).error; - assertPublication({ - error, - source, - primary, - sourceKind, - primaryKind, - preexisting, - differentPath, - selectedPath: path, - session, - }); - expect(records.statistics().evidenceRecords).toBe(preexisting ? 1 : 0); - expect( - ( - await client.callTool({ - name: "get_evidence_bundle", - arguments: { detail: "summary" }, - }) - ).isError, - ).not.toBe(true); - } finally { - await client.close(); - await server.close(); - await session.close(); - } }, ); @@ -360,46 +380,30 @@ it("reports omission when the primary cannot be safely authenticated", async () }); const session = new BinarySession(router, records); const server = createServer({ kind: "session", session }); - const client = new Client( - { name: "recovery-headroom", version: "1" }, - { jsonSchemaValidator: createMcpSchemaValidation() }, + await withOpenComFixture( + { server, session, name: "recovery-headroom", path }, + async (client) => { + const response = await client.callTool({ + name: "address_name", + arguments: { document: "fixture", address: "0x100" }, + }); + const error = parseMcpToolError(response).error; + expect(error).toMatchObject({ + code: "resource_constraint", + details: { + partial_observation: { + kind: "unretained-evidence", + evidence: { evidence_id: source.evidence_id }, + }, + completed_observation_omitted: { + reason: "insufficient-authentication-headroom", + source_closure: "not-established", + }, + }, + }); + expect(error.details).not.toHaveProperty("completed_observation"); + }, ); - const [ct, st] = InMemoryTransport.createLinkedPair(); - try { - await server.connect(st); - await client.connect(ct); - expect( - ( - await client.callTool({ - name: "open_binary", - arguments: { path, format: "dos-com" }, - }) - ).isError, - ).not.toBe(true); - const response = await client.callTool({ - name: "address_name", - arguments: { document: "fixture", address: "0x100" }, - }); - const error = parseMcpToolError(response).error; - expect(error).toMatchObject({ - code: "resource_constraint", - details: { - partial_observation: { - kind: "unretained-evidence", - evidence: { evidence_id: source.evidence_id }, - }, - completed_observation_omitted: { - reason: "insufficient-authentication-headroom", - source_closure: "not-established", - }, - }, - }); - expect(error.details).not.toHaveProperty("completed_observation"); - } finally { - await client.close(); - await server.close(); - await session.close(); - } }); it("accounts for a wide source ID list before constructing recovery Evidence", async () => { @@ -431,42 +435,26 @@ it("accounts for a wide source ID list before constructing recovery Evidence", a }); const session = new BinarySession(router, records); const server = createServer({ kind: "session", session }); - const client = new Client( - { name: "wide-recovery", version: "1" }, - { jsonSchemaValidator: createMcpSchemaValidation() }, - ); - const [ct, st] = InMemoryTransport.createLinkedPair(); - try { - await server.connect(st); - await client.connect(ct); - expect( - ( - await client.callTool({ - name: "open_binary", - arguments: { path, format: "dos-com" }, - }) - ).isError, - ).not.toBe(true); - const response = await client.callTool({ - name: "address_name", - arguments: { document: "fixture", address: "0x100" }, - }); - const error = parseMcpToolError(response).error; - expect(error).toMatchObject({ - code: "resource_constraint", - details: { - completed_observation_omitted: { - reason: "insufficient-authentication-headroom", - source_closure: "not-established", + await withOpenComFixture( + { server, session, name: "wide-recovery", path }, + async (client) => { + const response = await client.callTool({ + name: "address_name", + arguments: { document: "fixture", address: "0x100" }, + }); + const error = parseMcpToolError(response).error; + expect(error).toMatchObject({ + code: "resource_constraint", + details: { + completed_observation_omitted: { + reason: "insufficient-authentication-headroom", + source_closure: "not-established", + }, }, - }, - }); - expect(error.details).not.toHaveProperty("completed_observation"); - } finally { - await client.close(); - await server.close(); - await session.close(); - } + }); + expect(error.details).not.toHaveProperty("completed_observation"); + }, + ); }); it.each(["compact"] as const)( @@ -504,46 +492,27 @@ it.each(["compact"] as const)( environment: { ...process.env, REA_MCP_OUTPUT_SCHEMA_PROFILE: profile }, }, ); - const client = new Client( - { - name: "source-primary-headroom", - version: "1", - }, - { jsonSchemaValidator: createMcpSchemaValidation() }, - ); - const [ct, st] = InMemoryTransport.createLinkedPair(); - try { - await server.connect(st); - await client.connect(ct); - expect( - ( - await client.callTool({ - name: "open_binary", - arguments: { path, format: "dos-com" }, - }) - ).isError, - ).not.toBe(true); - const response = await client.callTool({ - name: "address_name", - arguments: { document: "fixture", address: "0x100" }, - }); - expect(response.isError).toBe(true); - expect(parseMcpToolError(response).error).toMatchObject({ - code: "resource_constraint", - details: { - completed_observation_omitted: { - reason: "insufficient-authentication-headroom", - source_closure: "not-established", + await withOpenComFixture( + { server, session, name: "source-primary-headroom", path }, + async (client) => { + const response = await client.callTool({ + name: "address_name", + arguments: { document: "fixture", address: "0x100" }, + }); + expect(response.isError).toBe(true); + expect(parseMcpToolError(response).error).toMatchObject({ + code: "resource_constraint", + details: { + completed_observation_omitted: { + reason: "insufficient-authentication-headroom", + source_closure: "not-established", + }, }, - }, - }); - expect(records.statistics().evidenceRecords).toBe(1); - expect(session.hasEvidence(source.evidence_id)).toBe(true); - } finally { - await client.close(); - await server.close(); - await session.close(); - } + }); + expect(records.statistics().evidenceRecords).toBe(1); + expect(session.hasEvidence(source.evidence_id)).toBe(true); + }, + ); }, ); @@ -575,49 +544,33 @@ it("accounts for source links before admitting a completed direct observation", }); const session = new BinarySession(router, records); const server = createServer({ kind: "session", session }); - const client = new Client( - { name: "wide-primary-admission", version: "1" }, - { jsonSchemaValidator: createMcpSchemaValidation() }, - ); - const [ct, st] = InMemoryTransport.createLinkedPair(); - try { - await server.connect(st); - await client.connect(ct); - expect( - ( - await client.callTool({ - name: "open_binary", - arguments: { path, format: "dos-com" }, - }) - ).isError, - ).not.toBe(true); - const response = await client.callTool({ - name: "address_name", - arguments: { document: "fixture", address: "0x100" }, - }); - expect(response.isError).toBe(true); - const error = parseMcpToolError(response).error; - expect(error).toMatchObject({ - code: "resource_constraint", - details: { - completed_observation_omitted: { - reason: "insufficient-authentication-headroom", - source_closure: "not-established", + await withOpenComFixture( + { server, session, name: "wide-primary-admission", path }, + async (client) => { + const response = await client.callTool({ + name: "address_name", + arguments: { document: "fixture", address: "0x100" }, + }); + expect(response.isError).toBe(true); + const error = parseMcpToolError(response).error; + expect(error).toMatchObject({ + code: "resource_constraint", + details: { + completed_observation_omitted: { + reason: "insufficient-authentication-headroom", + source_closure: "not-established", + }, }, - }, - }); - expect(error.details?.reported_limits).toMatchObject({ - available_heap_bytes: 100000, - }); - const reported = error.details?.reported_limits as - | { projected_value_bytes: number } - | undefined; - expect(reported?.projected_value_bytes).toBeGreaterThan(100000); - expect(records.statistics().evidenceRecords).toBe(1); - expect(session.hasEvidence(source.evidence_id)).toBe(true); - } finally { - await client.close(); - await server.close(); - await session.close(); - } + }); + expect(error.details?.reported_limits).toMatchObject({ + available_heap_bytes: 100000, + }); + const reported = error.details?.reported_limits as + | { projected_value_bytes: number } + | undefined; + expect(reported?.projected_value_bytes).toBeGreaterThan(100000); + expect(records.statistics().evidenceRecords).toBe(1); + expect(session.hasEvidence(source.evidence_id)).toBe(true); + }, + ); });