mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 06:55:55 +00:00
test(mcp): consolidate browser source boundary checks (#13234)
This commit is contained in:
1 parent
190395eade
commit
2e83fbd394
2 files changed
+90
-142
No files matched your search
@@ -1,64 +1,80 @@
|
||||
import { withCanonicalOutputSchema } from "../../fixtures/canonicalOutputSchema.js";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
||||
import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js";
|
||||
import { withCanonicalOutputSchema } from "../../fixtures/canonicalOutputSchema.js";
|
||||
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
|
||||
|
||||
/** Report one call frame for a console event, a request initiator and a WebMCP registration. */
|
||||
const inspectSourceCoordinates = async (position: {
|
||||
readonly lineNumber: number;
|
||||
readonly columnNumber: number;
|
||||
}) => {
|
||||
const positions = [
|
||||
{
|
||||
name: "malformed",
|
||||
declaration: { lineNumber: -3, columnNumber: 7.5 },
|
||||
observed: { line: null, column: null },
|
||||
},
|
||||
{
|
||||
name: "inline",
|
||||
declaration: { lineNumber: 21, columnNumber: -40 },
|
||||
observed: { line: 21, column: -40 },
|
||||
},
|
||||
] as const;
|
||||
|
||||
it("keeps valid and malformed producer source coordinates distinct through MCP", async () => {
|
||||
const browser = await startFakeCdpBrowser({
|
||||
webMcpTools: true,
|
||||
webMcpStackTrace: (origin) => ({
|
||||
callFrames: [{ url: `${origin}/app.js`, ...position }],
|
||||
}),
|
||||
commandEvents(command, origin) {
|
||||
const frame = { url: `${origin}/app.js`, ...position };
|
||||
const session =
|
||||
command.sessionId === undefined ? {} : { sessionId: command.sessionId };
|
||||
if (command.method === "Runtime.enable")
|
||||
return [
|
||||
{
|
||||
const stack = ({ name, declaration }: (typeof positions)[number]) => ({
|
||||
callFrames: [{ url: `${origin}/${name}.js`, ...declaration }],
|
||||
});
|
||||
switch (command.method) {
|
||||
case "Runtime.enable":
|
||||
return positions.map((position, index) => ({
|
||||
...session,
|
||||
method: "Runtime.consoleAPICalled",
|
||||
params: {
|
||||
type: "log",
|
||||
timestamp: 1,
|
||||
timestamp: index + 1,
|
||||
args: [],
|
||||
stackTrace: { callFrames: [frame] },
|
||||
stackTrace: stack(position),
|
||||
},
|
||||
},
|
||||
];
|
||||
if (command.method === "Network.enable")
|
||||
return [
|
||||
{
|
||||
}));
|
||||
case "Network.enable":
|
||||
return positions.map((position, index) => ({
|
||||
...session,
|
||||
method: "Network.requestWillBeSent",
|
||||
params: {
|
||||
requestId: "coordinate-request",
|
||||
timestamp: 1,
|
||||
requestId: position.name,
|
||||
timestamp: index + 1,
|
||||
type: "Script",
|
||||
request: { url: `${origin}/app.js`, method: "GET" },
|
||||
initiator: { type: "script", stack: { callFrames: [frame] } },
|
||||
request: {
|
||||
url: `${origin}/${position.name}.js`,
|
||||
method: "GET",
|
||||
},
|
||||
initiator: { type: "script", stack: stack(position) },
|
||||
},
|
||||
},
|
||||
];
|
||||
return undefined;
|
||||
}));
|
||||
case "WebMCP.enable":
|
||||
return [
|
||||
{
|
||||
...session,
|
||||
method: "WebMCP.toolsAdded",
|
||||
params: {
|
||||
tools: positions.map((position) => ({
|
||||
name: position.name,
|
||||
frameId: "frame-main",
|
||||
stackTrace: stack(position),
|
||||
})),
|
||||
},
|
||||
},
|
||||
];
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
});
|
||||
onTestFinished(() => browser.close());
|
||||
const { client } = await createApplicationMcpHarness({
|
||||
clientIdentity: { name: "source-coordinate-test", version: "1" },
|
||||
serverOptions: {
|
||||
environment: { ...process.env },
|
||||
browserObservation: new CdpBrowserProvider(),
|
||||
availabilityPolicy: () => ({
|
||||
processCaptureEnabled: false,
|
||||
investigationInputRoots: 0,
|
||||
}),
|
||||
},
|
||||
serverOptions: { browserObservation: new CdpBrowserProvider() },
|
||||
});
|
||||
const args = {
|
||||
cdp_endpoint: browser.endpoint,
|
||||
@@ -68,118 +84,59 @@ const inspectSourceCoordinates = async (position: {
|
||||
};
|
||||
const definitions = (await client.listTools()).tools;
|
||||
const call = async (name: "inspect_web_page" | "discover_webmcp_tools") => {
|
||||
const toolDefinition = definitions.find((tool) => tool.name === name);
|
||||
if (toolDefinition === undefined)
|
||||
const definition = definitions.find((tool) => tool.name === name);
|
||||
if (definition === undefined)
|
||||
throw new Error(`Missing advertised output schema: ${name}`);
|
||||
return client.callTool(
|
||||
const reply = await client.callTool(
|
||||
{ name, arguments: args },
|
||||
{ toolDefinition: withCanonicalOutputSchema(toolDefinition) },
|
||||
{ toolDefinition: withCanonicalOutputSchema(definition) },
|
||||
);
|
||||
expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true);
|
||||
return reply.structuredContent;
|
||||
};
|
||||
const page = await call("inspect_web_page");
|
||||
const tools = await call("discover_webmcp_tools");
|
||||
return { page, tools };
|
||||
};
|
||||
|
||||
it("reports malformed source coordinates as unknown through MCP", async () => {
|
||||
const { page, tools } = await inspectSourceCoordinates({
|
||||
lineNumber: -3,
|
||||
columnNumber: 7.5,
|
||||
const observed = positions.map(({ name, observed }) => ({
|
||||
name,
|
||||
location: { url: `${browser.allowedOrigin}/${name}.js`, ...observed },
|
||||
}));
|
||||
const invalid = (section: string) => ({
|
||||
section,
|
||||
reason: "invalid_protocol_value",
|
||||
count: 1,
|
||||
});
|
||||
expect(page.isError).not.toBe(true);
|
||||
expect(page.structuredContent).toMatchObject({
|
||||
|
||||
expect(await call("inspect_web_page")).toMatchObject({
|
||||
normalized_result: {
|
||||
console: {
|
||||
events: [expect.objectContaining({ line: null, column: null })],
|
||||
},
|
||||
console: { events: observed.map(({ location }) => location) },
|
||||
network: {
|
||||
requests: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
request_id: "coordinate-request",
|
||||
initiator: expect.objectContaining({ line: null, column: null }),
|
||||
}),
|
||||
]),
|
||||
requests: observed.map(({ name, location }) => ({
|
||||
request_id: name,
|
||||
initiator: location,
|
||||
})),
|
||||
},
|
||||
completeness: {
|
||||
excluded: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
section: "console_events",
|
||||
reason: "invalid_protocol_value",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
section: "network_initiators",
|
||||
reason: "invalid_protocol_value",
|
||||
}),
|
||||
invalid("console_events"),
|
||||
invalid("network_initiators"),
|
||||
]),
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(tools.isError).not.toBe(true);
|
||||
expect(tools.structuredContent).toMatchObject({
|
||||
expect(await call("discover_webmcp_tools")).toMatchObject({
|
||||
normalized_result: {
|
||||
tools: {
|
||||
items: [expect.objectContaining({ registration_source: null })],
|
||||
},
|
||||
completeness: {
|
||||
excluded: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
section: "webmcp_tools",
|
||||
reason: "invalid_protocol_value",
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps a negative column Chrome reports in passive source locations", async () => {
|
||||
const { page, tools } = await inspectSourceCoordinates({
|
||||
lineNumber: 21,
|
||||
columnNumber: -40,
|
||||
});
|
||||
for (const response of [page, tools]) {
|
||||
expect(response.isError).not.toBe(true);
|
||||
}
|
||||
const location = { line: 21, column: -40 };
|
||||
expect(page.structuredContent).toMatchObject({
|
||||
normalized_result: {
|
||||
console: { events: [expect.objectContaining(location)] },
|
||||
network: {
|
||||
requests: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
request_id: "coordinate-request",
|
||||
initiator: expect.objectContaining(location),
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(tools.structuredContent).toMatchObject({
|
||||
normalized_result: {
|
||||
tools: {
|
||||
items: [
|
||||
expect.objectContaining({
|
||||
registration_source: expect.objectContaining(location),
|
||||
}),
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
for (const [response, section] of [
|
||||
[page, "console_events"],
|
||||
[page, "network_initiators"],
|
||||
[tools, "webmcp_tools"],
|
||||
] as const)
|
||||
expect(response.structuredContent).toMatchObject({
|
||||
normalized_result: {
|
||||
completeness: {
|
||||
excluded: expect.not.arrayContaining([
|
||||
total: 2,
|
||||
items: expect.arrayContaining(
|
||||
observed.map(({ name, location }) =>
|
||||
expect.objectContaining({
|
||||
section,
|
||||
reason: "invalid_protocol_value",
|
||||
name,
|
||||
registration_source: name === "malformed" ? null : location,
|
||||
}),
|
||||
]),
|
||||
},
|
||||
),
|
||||
),
|
||||
},
|
||||
});
|
||||
completeness: {
|
||||
excluded: expect.arrayContaining([invalid("webmcp_tools")]),
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,7 @@ import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
||||
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { webBundleAcquisitionPartialObservationSchema } from "../../../src/domain/webBundleAnalysis.js";
|
||||
import { startSourceMapCancellationBrowser } from "../../fixtures/browserSourceMapCancellation.js";
|
||||
import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js";
|
||||
import {
|
||||
createApplicationMcpHarness,
|
||||
type ApplicationMcpHarness,
|
||||
@@ -48,17 +49,7 @@ it("retains completed source maps through MCP cancellation for later Evidence re
|
||||
},
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
await Promise.race([
|
||||
secondRequest,
|
||||
call.then(
|
||||
() => {
|
||||
throw new Error("MCP completed before the second map request");
|
||||
},
|
||||
(cause: unknown) => {
|
||||
throw cause;
|
||||
},
|
||||
),
|
||||
]);
|
||||
await awaitAcknowledgement(secondRequest, call, "second source-map request");
|
||||
const retainedMapPath = await completedMapPath;
|
||||
controller.abort(new Error("Caller cancelled after the first map"));
|
||||
await expect(call).rejects.toThrow();
|
||||
|
||||
Reference in new issue
Block a user