|
| 1 | +import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; |
| 2 | +import { McpServer } from "@modelcontextprotocol/server"; |
| 3 | +import { expect, it } from "vitest"; |
| 4 | + |
| 5 | +import { registerRizinDebugTools } from "../../../src/server/registerRizinDebugTools.js"; |
| 6 | +import { createToolResultDelivery } from "../../../src/server/toolResult.js"; |
| 7 | +import { toolContract } from "../../../src/contracts/toolContracts.js"; |
| 8 | +import { silentLogger } from "../../../src/logger.js"; |
| 9 | +import { createInjectedProcess } from "../providers/RizinDebugSessionCoverage.fixtures.js"; |
| 10 | +import { |
| 11 | + createRizinTestManager, |
| 12 | + rizinTestInferiorTty, |
| 13 | +} from "../providers/RizinTestInferiorTty.js"; |
| 14 | + |
| 15 | +const test = it.skipIf(process.platform === "win32"); |
| 16 | + |
| 17 | +const observe = async ( |
| 18 | + prefix: string, |
| 19 | + framing: "coalesced" | "separate" | "split-transport", |
| 20 | + maximum?: number, |
| 21 | +) => { |
| 22 | + const injected = createInjectedProcess({ |
| 23 | + onWrite: (text, control) => { |
| 24 | + const marker = /!echo (.+)\n/u.exec(text)?.[1]; |
| 25 | + if (marker === undefined) return; |
| 26 | + setImmediate(() => { |
| 27 | + if (framing === "separate") control.output(`${prefix}\0${marker}\n\0`); |
| 28 | + else if (framing === "split-transport") { |
| 29 | + const bytes = Buffer.from(`${prefix}${marker}\n\0`); |
| 30 | + const split = Math.max(1, Buffer.byteLength(prefix) - 1); |
| 31 | + control.output(bytes.subarray(0, split)); |
| 32 | + control.output(bytes.subarray(split)); |
| 33 | + } else control.output(`${prefix}${marker}\n\0`); |
| 34 | + }); |
| 35 | + }, |
| 36 | + }); |
| 37 | + const manager = createRizinTestManager( |
| 38 | + {}, |
| 39 | + { |
| 40 | + spawnProcess: injected.spawnProcess, |
| 41 | + frameTimeoutMs: 100, |
| 42 | + ...(maximum === undefined ? {} : { maxCommandOutputBytes: maximum }), |
| 43 | + }, |
| 44 | + ); |
| 45 | + const server = new McpServer({ name: "rizin-output-test", version: "1" }); |
| 46 | + registerRizinDebugTools( |
| 47 | + server, |
| 48 | + manager, |
| 49 | + silentLogger, |
| 50 | + createToolResultDelivery(undefined), |
| 51 | + ); |
| 52 | + const client = new Client({ name: "rizin-output-test", version: "1" }); |
| 53 | + const [clientTransport, serverTransport] = |
| 54 | + InMemoryTransport.createLinkedPair(); |
| 55 | + try { |
| 56 | + await server.connect(serverTransport); |
| 57 | + await client.connect(clientTransport); |
| 58 | + const startReply = await client.callTool({ |
| 59 | + name: "start_rizin_debug_session", |
| 60 | + arguments: { |
| 61 | + path: "fixture.bin", |
| 62 | + inferior_tty: await rizinTestInferiorTty(), |
| 63 | + }, |
| 64 | + }); |
| 65 | + expect(startReply.isError).not.toBe(true); |
| 66 | + const started = toolContract( |
| 67 | + "start_rizin_debug_session", |
| 68 | + ).outputSchema.parse(startReply.structuredContent); |
| 69 | + const reply = await client.callTool({ |
| 70 | + name: "rizin_debug_command", |
| 71 | + arguments: { session_id: started.session_id, command: "dr" }, |
| 72 | + }); |
| 73 | + expect(reply.isError).not.toBe(true); |
| 74 | + return toolContract("rizin_debug_command").outputSchema.parse( |
| 75 | + reply.structuredContent, |
| 76 | + ); |
| 77 | + } finally { |
| 78 | + injected.control.exit(); |
| 79 | + await client.close(); |
| 80 | + await server.close(); |
| 81 | + await manager.closeAll(); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +test.each( |
| 86 | + ["value \n", " \t\r\n", "Ω雪\t\r\n", "", "😀"].flatMap((prefix) => |
| 87 | + (["coalesced", "separate", "split-transport"] as const).map((framing) => ({ |
| 88 | + prefix, |
| 89 | + framing, |
| 90 | + })), |
| 91 | + ), |
| 92 | +)( |
| 93 | + "preserves output %j across $framing framing", |
| 94 | + async ({ prefix, framing }) => { |
| 95 | + const evidence = await observe(prefix, framing); |
| 96 | + const expected = { |
| 97 | + output: prefix, |
| 98 | + completion_status: "complete", |
| 99 | + output_truncated: false, |
| 100 | + }; |
| 101 | + expect(evidence.normalized_result).toMatchObject(expected); |
| 102 | + expect(evidence.raw_result).toMatchObject(expected); |
| 103 | + }, |
| 104 | +); |
| 105 | + |
| 106 | +test.each([7, 8])( |
| 107 | + "counts trailing whitespace in the exact %s-byte budget", |
| 108 | + async (maximum) => { |
| 109 | + const evidence = await observe("value \n", "coalesced", maximum); |
| 110 | + const truncated = maximum < 8; |
| 111 | + const expected = { |
| 112 | + output: truncated ? "value " : "value \n", |
| 113 | + output_truncated: truncated, |
| 114 | + completion_status: truncated ? "unknown" : "complete", |
| 115 | + }; |
| 116 | + expect(evidence.normalized_result).toMatchObject(expected); |
| 117 | + expect(evidence.raw_result).toMatchObject(expected); |
| 118 | + }, |
| 119 | +); |
0 commit comments