test(mcp): strengthen stdio lifecycle coverage

This commit is contained in:
Peter Steinberger
2026-04-22 22:11:06 +01:00
parent 56828545b4
commit a971884104
2 changed files with 43 additions and 0 deletions

View File

@@ -78,4 +78,46 @@ describe("OpenClawStdioClientTransport", () => {
child.emit("close", 0);
await closing;
});
it("does not kill the process tree when graceful stdio close exits", async () => {
vi.useFakeTimers();
const child = new MockChildProcess();
spawnMock.mockReturnValue(child);
const { OpenClawStdioClientTransport } = await import("./mcp-stdio-transport.js");
const transport = new OpenClawStdioClientTransport({ command: "npx" });
const started = transport.start();
child.emit("spawn");
await started;
const closing = transport.close();
child.exitCode = 0;
child.emit("close", 0);
await closing;
expect(killProcessTreeMock).not.toHaveBeenCalled();
});
it("sends and receives JSON-RPC messages over stdio", async () => {
const child = new MockChildProcess();
spawnMock.mockReturnValue(child);
const { OpenClawStdioClientTransport } = await import("./mcp-stdio-transport.js");
const transport = new OpenClawStdioClientTransport({ command: "npx" });
const onmessage = vi.fn();
Object.assign(transport, { onmessage });
const started = transport.start();
child.emit("spawn");
await started;
await transport.send({ jsonrpc: "2.0", id: 1, method: "ping" });
expect(child.stdin.read()?.toString()).toBe('{"jsonrpc":"2.0","id":1,"method":"ping"}\n');
child.stdout.write('{"jsonrpc":"2.0","id":1,"result":{"ok":true}}\n');
expect(onmessage).toHaveBeenCalledWith({
jsonrpc: "2.0",
id: 1,
result: { ok: true },
});
});
});

View File

@@ -2665,6 +2665,7 @@ describe("gateway server sessions", () => {
["main", "agent:main:main", "sess-main"],
"sess-main",
);
expect(bundleMcpRuntimeMocks.disposeSessionMcpRuntime).toHaveBeenCalledWith("sess-main");
expect(waitCallCountAtSnapshotClear).toEqual([1]);
expect(browserSessionTabMocks.closeTrackedBrowserTabsForSessions).toHaveBeenCalledTimes(1);
expect(browserSessionTabMocks.closeTrackedBrowserTabsForSessions).toHaveBeenCalledWith({