test(mcp): strengthen stdio lifecycle coverage · openclaw/openclaw@a971884
steipete
·
2026-04-23
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -78,4 +78,46 @@ describe("OpenClawStdioClientTransport", () => {
|
78 | 78 | child.emit("close", 0); |
79 | 79 | await closing; |
80 | 80 | }); |
| 81 | + |
| 82 | +it("does not kill the process tree when graceful stdio close exits", async () => { |
| 83 | +vi.useFakeTimers(); |
| 84 | +const child = new MockChildProcess(); |
| 85 | +spawnMock.mockReturnValue(child); |
| 86 | +const { OpenClawStdioClientTransport } = await import("./mcp-stdio-transport.js"); |
| 87 | + |
| 88 | +const transport = new OpenClawStdioClientTransport({ command: "npx" }); |
| 89 | +const started = transport.start(); |
| 90 | +child.emit("spawn"); |
| 91 | +await started; |
| 92 | + |
| 93 | +const closing = transport.close(); |
| 94 | +child.exitCode = 0; |
| 95 | +child.emit("close", 0); |
| 96 | +await closing; |
| 97 | + |
| 98 | +expect(killProcessTreeMock).not.toHaveBeenCalled(); |
| 99 | +}); |
| 100 | + |
| 101 | +it("sends and receives JSON-RPC messages over stdio", async () => { |
| 102 | +const child = new MockChildProcess(); |
| 103 | +spawnMock.mockReturnValue(child); |
| 104 | +const { OpenClawStdioClientTransport } = await import("./mcp-stdio-transport.js"); |
| 105 | + |
| 106 | +const transport = new OpenClawStdioClientTransport({ command: "npx" }); |
| 107 | +const onmessage = vi.fn(); |
| 108 | +Object.assign(transport, { onmessage }); |
| 109 | +const started = transport.start(); |
| 110 | +child.emit("spawn"); |
| 111 | +await started; |
| 112 | + |
| 113 | +await transport.send({ jsonrpc: "2.0", id: 1, method: "ping" }); |
| 114 | +expect(child.stdin.read()?.toString()).toBe('{"jsonrpc":"2.0","id":1,"method":"ping"}\n'); |
| 115 | + |
| 116 | +child.stdout.write('{"jsonrpc":"2.0","id":1,"result":{"ok":true}}\n'); |
| 117 | +expect(onmessage).toHaveBeenCalledWith({ |
| 118 | +jsonrpc: "2.0", |
| 119 | +id: 1, |
| 120 | +result: { ok: true }, |
| 121 | +}); |
| 122 | +}); |
81 | 123 | }); |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。