Files
reverse-engineering-anything/tests/boundary/mcp/webSourceLocationMcp.test.ts
T

171 lines
6.3 KiB
TypeScript

import { withCanonicalOutputSchema } from "../../fixtures/canonicalOutputSchema.js";
import {
webSourceLocationArgs as args,
webSourceLocationFixture,
webSourceMapDecoderFixture,
} from "../../fixtures/webSourceLocation.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { expect, it, onTestFinished, vi } from "vitest";
import { WebSourceLocationService } from "../../../src/application/WebSourceLocationService.js";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { ok } from "../../../src/domain/result.js";
import { createServer } from "../../../src/server/createServer.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { SourceMapDecoder } from "../../../src/javascript/sourceMaps/SourceMapDecoder.js";
import { createSystemProcessOwnershipHost } from "../../../src/process/ProcessOwnershipObservation.js";
import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
it("validates source-location delivery and retains Evidence without a binary target", async () => {
const data = webSourceLocationFixture();
const load = vi.fn(async () => ok(data));
const service = new WebSourceLocationService(
{ load },
webSourceMapDecoderFixture,
);
const session = createTestBinarySession(() => {
throw new Error("binary provider must not start");
});
const server = createServer(
{ kind: "session", session },
{ webSourceLocation: service },
);
const client = new Client(
{ name: "source-location-contract", version: "1" },
{ jsonSchemaValidator: createMcpSchemaValidation() },
);
onTestFinished(async () => {
await client.close();
await server.close();
await session.close();
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const advertised = (await client.listTools()).tools.find(
(tool) => tool.name === "trace_web_source_location",
);
if (advertised === undefined)
throw new Error("source-location trace must advertise both schemas");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
expect(ajv.validate(inputSchema, args)).toBe(true);
const response = await client.callTool(
{ name: "trace_web_source_location", arguments: args },
{ toolDefinition: withCanonicalOutputSchema(advertised) },
);
expect(response.isError).not.toBe(true);
const parsed = toolContract("trace_web_source_location").outputSchema.parse(
response.structuredContent,
);
const evidence = parseEvidence(parsed);
expect(evidence).toMatchObject({
normalized_result: {
source: { sha256: data.sourceFile.sha256 },
source_map: {
association: "caller-selected",
sha256: data.sourceMap.file.sha256,
},
matches: [{ state: "mapped", content: { text: "original" } }],
execution: "unknown",
source_authenticity: "unknown",
},
raw_result: { source_map_text: data.sourceMap.text },
});
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
const invalid = await client.callTool({
name: "trace_web_source_location",
arguments: { ...args, manifest_path: "relative.json" },
});
expect(parseMcpToolError(invalid).error).toMatchObject({
code: "invalid_request",
details: {
issues: [{ path: ["manifest_path"], reason: "invalid_format" }],
},
});
for (const delivery of [
{
detail: "complete",
view: { kind: "source-location", collection: "matches" },
},
{
detail: "summary",
view: {
kind: "source-location",
collection: "source-text",
match_index: -1,
},
},
{
detail: "summary",
view: { kind: "source-location", collection: "matches", limit: 0 },
},
]) {
const result = await client.callTool({
name: "trace_web_source_location",
arguments: { ...args, ...delivery },
});
expect(parseMcpToolError(result).error.code).toBe("invalid_request");
}
expect(load).toHaveBeenCalledTimes(1);
});
it("preserves missing Linux ps capability and recovery through source-map tracing", async () => {
const directory = await createTestTempDirectory("source-map-missing-ps-");
// A Linux host with no ps at a fixed system location or on REA's PATH.
const prepare = createSystemProcessOwnershipHost(
"linux",
{ PATH: directory },
{ linuxPsLocations: [] },
).prepare;
if (prepare === undefined)
throw new Error("Ownership preparation is required");
const createRuntime = vi.fn(async () => {
throw new Error("Missing prerequisite must not acquire a runtime");
});
const launcher = vi.fn(async () => {
throw new Error("Missing prerequisite must not launch a codec");
});
const decoder = new SourceMapDecoder({
prepareOwnership: prepare,
createRuntime,
launcher,
});
const harness = await createApplicationMcpHarness({
serverOptions: {
webSourceLocation: new WebSourceLocationService(
{ load: async () => ok(webSourceLocationFixture()) },
decoder,
),
},
});
onTestFinished(() => harness.close());
for (let attempt = 0; attempt < 2; attempt += 1) {
const response = await harness.client.callTool({
name: "trace_web_source_location",
arguments: args,
});
const { error } = parseMcpToolError(response);
expect(error).toMatchObject({
code: "capability_unavailable",
category: "unsupported_provider",
retryable: false,
message: expect.stringContaining("procps-compatible ps"),
remediation: { action: expect.stringContaining("procps-compatible ps") },
details: {
provider_id: "process-ownership",
operation: "prepare_owned_process",
reason: expect.stringContaining("ENOENT"),
},
});
}
await harness.client.ping();
expect(createRuntime).not.toHaveBeenCalled();
expect(launcher).not.toHaveBeenCalled();
});