stdio-transport.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
  3. const terminateMcpStdioProcessTree = vi.hoisted(() => vi.fn(async () => undefined));
  4. vi.mock("../src/mcp/process-tree.js", () => ({
  5. terminateMcpStdioProcessTree,
  6. }));
  7. import { ProcessTreeStdioClientTransport } from "../src/mcp/stdio-transport.js";
  8. describe("ProcessTreeStdioClientTransport Windows fallback", () => {
  9. afterEach(() => {
  10. vi.restoreAllMocks();
  11. });
  12. it("uses the existing taskkill tree cleanup when Job Object attach fails", async () => {
  13. const originalPlatform = process.platform;
  14. Object.defineProperty(process, "platform", { configurable: true, value: "win32" });
  15. try {
  16. vi.spyOn(StdioClientTransport.prototype, "start").mockResolvedValue();
  17. vi.spyOn(StdioClientTransport.prototype, "pid", "get").mockReturnValue(4321);
  18. const windowsJobObjectFactory = vi.fn(async () => undefined);
  19. const transport = new ProcessTreeStdioClientTransport(
  20. { command: process.execPath },
  21. { windowsJobObjectFactory },
  22. );
  23. Object.assign(transport as unknown as { _process: object }, {
  24. _process: {
  25. exitCode: null,
  26. once: vi.fn(),
  27. pid: 4321,
  28. signalCode: null,
  29. },
  30. });
  31. await transport.start();
  32. delete (transport as unknown as { _process?: object })._process;
  33. const dispose = Object.getOwnPropertyDescriptor(
  34. ProcessTreeStdioClientTransport.prototype,
  35. "_dispose",
  36. )?.value as ((this: ProcessTreeStdioClientTransport) => Promise<void>) | undefined;
  37. await dispose?.call(transport);
  38. expect(windowsJobObjectFactory).toHaveBeenCalledWith(4321);
  39. expect(terminateMcpStdioProcessTree).toHaveBeenCalledWith(4321);
  40. } finally {
  41. Object.defineProperty(process, "platform", {
  42. configurable: true,
  43. value: originalPlatform,
  44. });
  45. }
  46. });
  47. });