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
+53 -4
View File
@@ -174,6 +174,14 @@ const jsonResult = (result: CallToolResult): JsonValue => {
) {
return structured.data.normalized_result ?? null;
}
if (
typeof structured.data === "object" &&
structured.data !== null &&
!Array.isArray(structured.data) &&
"evidence_id" in structured.data &&
"result" in structured.data
)
return structured.data.result ?? null;
const text = result.content.find((item) => item.type === "text");
if (text?.type !== "text")
throw new Error("Tool result omitted text content");
@@ -567,7 +575,7 @@ describe("enhanced MCP tools", () => {
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
JSON.stringify(result.structuredContent),
);
});
@@ -603,6 +611,47 @@ describe("enhanced MCP tools", () => {
expect(JSON.stringify(result)).toContain("operation budget");
});
it("returns correction-only structured validation issues", async () => {
const client = await connect();
const misspelled = await client.callTool({
name: "trace_feature",
arguments: { query: "needle", max_operatons: 1 },
});
expect(misspelled).toMatchObject({
isError: true,
structuredContent: {
error: {
code: "invalid_request",
retryable: true,
details: {
issues: [{ path: ["max_operatons"], reason: "unknown_argument" }],
},
},
},
});
const bounded = await client.callTool({
name: "trace_feature",
arguments: { query: "needle", max_operations: 101 },
});
expect(bounded.structuredContent).toMatchObject({
error: {
details: {
issues: [
{
path: ["max_operations"],
reason: "out_of_range",
maximum: 100,
},
],
},
},
});
expect(bounded.content[0]).toEqual({
type: "text",
text: JSON.stringify(bounded.structuredContent),
});
});
it("returns a typed tool error for malformed Hopper boundary values", async () => {
const client = await connect({
execute: () => Promise.resolve(ok(["not", "a", "procedure", "map"])),
@@ -614,7 +663,7 @@ describe("enhanced MCP tools", () => {
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
JSON.stringify(result.structuredContent),
);
});
@@ -635,7 +684,7 @@ describe("enhanced MCP tools", () => {
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
JSON.stringify(result.structuredContent),
);
});
@@ -671,7 +720,7 @@ describe("enhanced MCP tools", () => {
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
JSON.stringify(result.structuredContent),
);
});