mcp-server-process-guards.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { EventEmitter } from "node:events";
  2. import { describe, expect, it, vi } from "vitest";
  3. import {
  4. installNodeReplProcessGuards,
  5. installNodeReplShutdownTriggers,
  6. } from "../src/server.js";
  7. function createFakeProcess() {
  8. const emitter = new EventEmitter();
  9. return {
  10. emitter,
  11. on: emitter.on.bind(emitter) as NodeJS.Process["on"],
  12. once: emitter.once.bind(emitter) as NodeJS.Process["once"],
  13. };
  14. }
  15. describe("node_repl MCP server process guards", () => {
  16. it("logs unhandled rejections to stderr instead of letting the process die", () => {
  17. const fake = createFakeProcess();
  18. const stderrLines: string[] = [];
  19. installNodeReplProcessGuards({
  20. onOutputClosed: vi.fn(),
  21. process: fake,
  22. writeStderr: (text) => stderrLines.push(text),
  23. });
  24. fake.emitter.emit("unhandledRejection", new Error("boom from stale cell promise"));
  25. expect(stderrLines.join("")).toContain("unhandledRejection");
  26. expect(stderrLines.join("")).toContain("boom from stale cell promise");
  27. });
  28. it("logs uncaught exceptions to stderr instead of letting the process die", () => {
  29. const fake = createFakeProcess();
  30. const stderrLines: string[] = [];
  31. installNodeReplProcessGuards({
  32. onOutputClosed: vi.fn(),
  33. process: fake,
  34. writeStderr: (text) => stderrLines.push(text),
  35. });
  36. fake.emitter.emit("uncaughtException", new Error("boom from async timer"));
  37. expect(stderrLines.join("")).toContain("uncaughtException");
  38. expect(stderrLines.join("")).toContain("boom from async timer");
  39. });
  40. it("keeps handler installation idempotent so repeated main() bootstraps do not stack listeners", () => {
  41. const fake = createFakeProcess();
  42. const stderrLines: string[] = [];
  43. const options = {
  44. onOutputClosed: vi.fn(),
  45. process: fake,
  46. writeStderr: (text: string) => stderrLines.push(text),
  47. };
  48. installNodeReplProcessGuards(options);
  49. installNodeReplProcessGuards(options);
  50. fake.emitter.emit("unhandledRejection", new Error("once"));
  51. expect(stderrLines).toHaveLength(1);
  52. });
  53. it("stringifies non-Error rejection reasons", () => {
  54. const fake = createFakeProcess();
  55. const stderrLines: string[] = [];
  56. installNodeReplProcessGuards({
  57. onOutputClosed: vi.fn(),
  58. process: fake,
  59. writeStderr: (text) => stderrLines.push(text),
  60. });
  61. fake.emitter.emit("unhandledRejection", "plain string reason");
  62. expect(stderrLines.join("")).toContain("plain string reason");
  63. });
  64. it.each(["EPIPE", "EIO", "ENXIO", "EBADF", "ERR_STREAM_DESTROYED"])(
  65. "treats %s as a terminal output closure without writing to the broken stderr again",
  66. (code) => {
  67. const fake = createFakeProcess();
  68. const stderrLines: string[] = [];
  69. const onOutputClosed = vi.fn();
  70. installNodeReplProcessGuards({
  71. onOutputClosed,
  72. process: fake,
  73. writeStderr: (text) => stderrLines.push(text),
  74. });
  75. const error = Object.assign(new Error(`write ${code}`), { code });
  76. fake.emitter.emit("uncaughtException", error);
  77. fake.emitter.emit("uncaughtException", error);
  78. expect(onOutputClosed).toHaveBeenCalledOnce();
  79. expect(onOutputClosed).toHaveBeenCalledWith(error);
  80. expect(stderrLines).toEqual([]);
  81. },
  82. );
  83. it("stops reporting queued errors after the output has closed", () => {
  84. const fake = createFakeProcess();
  85. const stderrLines: string[] = [];
  86. installNodeReplProcessGuards({
  87. onOutputClosed: vi.fn(),
  88. process: fake,
  89. writeStderr: (text) => stderrLines.push(text),
  90. });
  91. fake.emitter.emit(
  92. "uncaughtException",
  93. Object.assign(new Error("write EPIPE"), { code: "EPIPE" }),
  94. );
  95. fake.emitter.emit("unhandledRejection", new Error("already queued"));
  96. expect(stderrLines).toEqual([]);
  97. });
  98. it("treats a synchronous EPIPE from the diagnostic write as output closure", () => {
  99. const fake = createFakeProcess();
  100. const onOutputClosed = vi.fn();
  101. const error = Object.assign(new Error("write EPIPE"), { code: "EPIPE" });
  102. installNodeReplProcessGuards({
  103. onOutputClosed,
  104. process: fake,
  105. writeStderr: () => {
  106. throw error;
  107. },
  108. });
  109. fake.emitter.emit("uncaughtException", new Error("original async failure"));
  110. expect(onOutputClosed).toHaveBeenCalledOnce();
  111. expect(onOutputClosed).toHaveBeenCalledWith(error);
  112. });
  113. });
  114. describe("node_repl MCP server shutdown triggers", () => {
  115. it.each(["end", "close"] as const)("shuts down when stdin emits %s", (event) => {
  116. const fakeProcess = createFakeProcess();
  117. const stdin = new EventEmitter();
  118. const shutdown = vi.fn();
  119. installNodeReplShutdownTriggers({
  120. process: fakeProcess,
  121. shutdown,
  122. stdin: {
  123. once: stdin.once.bind(stdin) as NodeJS.ReadStream["once"],
  124. },
  125. });
  126. stdin.emit(event);
  127. expect(shutdown).toHaveBeenCalledOnce();
  128. });
  129. it("coalesces stdin closure and process signals into one shutdown", () => {
  130. const fakeProcess = createFakeProcess();
  131. const stdin = new EventEmitter();
  132. const shutdown = vi.fn();
  133. installNodeReplShutdownTriggers({
  134. process: fakeProcess,
  135. shutdown,
  136. stdin: {
  137. once: stdin.once.bind(stdin) as NodeJS.ReadStream["once"],
  138. },
  139. });
  140. stdin.emit("end");
  141. stdin.emit("close");
  142. fakeProcess.emitter.emit("SIGTERM");
  143. fakeProcess.emitter.emit("SIGINT");
  144. expect(shutdown).toHaveBeenCalledOnce();
  145. });
  146. });