test(mcp): cover strict compact wire contracts

This commit is contained in:
morluto
2026-07-16 19:50:16 +08:00
parent ee1cd40405
commit 8de25d0230
17 changed files with 1009 additions and 328 deletions
+81 -14
View File
@@ -61,6 +61,59 @@ const structured = (result: CallToolResult): Record<string, unknown> => {
return Object.fromEntries(Object.entries(result.structuredContent));
};
const objectEntries = (value: unknown): readonly [string, unknown][] =>
typeof value === "object" && value !== null && !Array.isArray(value)
? Object.entries(value)
: [];
const referencedSchema = (schema: unknown, root: unknown): unknown => {
const object = Object.fromEntries(objectEntries(schema));
const reference = object.$ref;
if (typeof reference !== "string" || !reference.startsWith("#/$defs/"))
return schema;
return Object.fromEntries(objectEntries(root)).$defs instanceof Object
? Object.fromEntries(
objectEntries(Object.fromEntries(objectEntries(root)).$defs),
)[reference.slice(8)]
: schema;
};
const assertDescribedStrictObjects = (
schema: unknown,
root = schema,
path = "$",
): void => {
for (const [key, value] of objectEntries(schema)) {
if (key === "properties") {
const properties = objectEntries(value);
for (const [property, propertySchema] of properties) {
const resolved = referencedSchema(propertySchema, root);
const propertyObject = Object.fromEntries(
objectEntries(propertySchema),
);
expect(
propertyObject.description ??
Object.fromEntries(objectEntries(resolved)).description,
`${path}.properties.${property}`,
).toEqual(expect.any(String));
assertDescribedStrictObjects(
propertySchema,
root,
`${path}.properties.${property}`,
);
}
expect(
Object.fromEntries(objectEntries(schema)).additionalProperties,
).toBe(false);
continue;
}
assertDescribedStrictObjects(value, root, `${path}.${key}`);
}
if (Array.isArray(schema))
for (const [index, value] of schema.entries())
assertDescribedStrictObjects(value, root, `${path}[${String(index)}]`);
};
describe("full MCP integration with multi-tool sequences", () => {
it("executes a realistic workflow: list methods, decompile selected, get xrefs", async () => {
const client = await connect({
@@ -98,10 +151,7 @@ describe("full MCP integration with multi-tool sequences", () => {
});
expect(listResult.isError).not.toBe(true);
expect(structured(listResult)).toMatchObject({
subject: null,
operation: "list_procedures",
provider: { id: "fixture", version: "1" },
normalized_result: {
result: {
items: [
{ address: "0x1000", value: "main" },
{ address: "0x2000", value: "helper" },
@@ -116,9 +166,7 @@ describe("full MCP integration with multi-tool sequences", () => {
arguments: { procedure: "0x1000" },
});
expect(structured(decompileResult)).toMatchObject({
operation: "procedure_pseudo_code",
parameters: { document: null, procedure: "0x1000" },
normalized_result: "pseudo for 0x1000",
result: "pseudo for 0x1000",
});
const xrefResult = await client.callTool({
@@ -126,8 +174,7 @@ describe("full MCP integration with multi-tool sequences", () => {
arguments: {},
});
expect(structured(xrefResult)).toMatchObject({
operation: "xrefs",
normalized_result: ["0x1000"],
result: ["0x1000"],
});
});
@@ -161,6 +208,30 @@ describe("full MCP integration with multi-tool sequences", () => {
expect(names).toContain("binary_session");
expect(names).toContain("binary_overview");
expect(names).toContain("batch_decompile");
expect(
Buffer.byteLength(JSON.stringify(listed), "utf8"),
).toBeLessThanOrEqual(512 * 1024);
const contracts = new Map<string, (typeof TOOL_CONTRACTS)[number]>(
TOOL_CONTRACTS.map((contract) => [contract.name, contract]),
);
for (const tool of listed.tools) {
expect(
Buffer.byteLength(JSON.stringify(tool), "utf8"),
tool.name,
).toBeLessThanOrEqual(64 * 1024);
const contract = contracts.get(tool.name);
expect(contract, tool.name).toBeDefined();
expect(tool.title, tool.name).toBe(contract?.title);
expect(tool.description, tool.name).toBe(contract?.description);
expect(tool.annotations, tool.name).toEqual(contract?.annotations);
expect(tool.outputSchema, tool.name).toBeDefined();
assertDescribedStrictObjects(
tool.inputSchema,
tool.inputSchema,
tool.name,
);
}
});
it("records approved trace truncation as a deduplicated residual unknown", async () => {
@@ -355,9 +426,7 @@ describe("full MCP integration with multi-tool sequences", () => {
name: "compare_process_captures",
arguments: {
left_evidence_id: leftEvidence.evidence_id,
left,
right_evidence_id: rightEvidence.evidence_id,
right,
unknown_registry_approved: true,
},
});
@@ -408,9 +477,7 @@ describe("full MCP integration with multi-tool sequences", () => {
category: "execution_failure",
},
});
expect(text(result)).toBe(
"Analysis could not complete. Retry once; if it continues, run `rea doctor`.",
);
expect(text(result)).toBe(JSON.stringify(result.structuredContent));
});
it("handles concurrent tool calls without corruption", async () => {