interactive-mode-suspend.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { afterEach, describe, expect, test, vi } from "vitest";
  2. import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts";
  3. type FakeUi = {
  4. start: () => void;
  5. stop: () => void;
  6. requestRender: (force?: boolean) => void;
  7. };
  8. type HandleCtrlZThis = {
  9. ui: FakeUi;
  10. };
  11. type ProcessSignalHandler = () => void;
  12. type InteractiveModePrototypeWithHandleCtrlZ = {
  13. handleCtrlZ(this: HandleCtrlZThis): void;
  14. };
  15. function callHandleCtrlZ(context: HandleCtrlZThis): void {
  16. (interactiveModePrototype as InteractiveModePrototypeWithHandleCtrlZ).handleCtrlZ.call(context);
  17. }
  18. const interactiveModePrototype = InteractiveMode.prototype as unknown;
  19. describe("InteractiveMode.handleCtrlZ", () => {
  20. afterEach(() => {
  21. vi.restoreAllMocks();
  22. });
  23. test("shows a status message and skips suspend on Windows", () => {
  24. const ui: FakeUi = {
  25. start: vi.fn(),
  26. stop: vi.fn(),
  27. requestRender: vi.fn(),
  28. };
  29. const showStatus = vi.fn();
  30. const context: HandleCtrlZThis & { showStatus: (message: string) => void } = { ui, showStatus };
  31. const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
  32. Object.defineProperty(process, "platform", {
  33. configurable: true,
  34. value: "win32",
  35. });
  36. const setIntervalSpy = vi.spyOn(globalThis, "setInterval");
  37. const processOnSpy = vi.spyOn(process, "on");
  38. const processOnceSpy = vi.spyOn(process, "once");
  39. const processKillSpy = vi.spyOn(process, "kill");
  40. try {
  41. callHandleCtrlZ(context);
  42. } finally {
  43. if (platformDescriptor) {
  44. Object.defineProperty(process, "platform", platformDescriptor);
  45. }
  46. }
  47. expect(showStatus).toHaveBeenCalledWith("Suspend to background is not supported on Windows");
  48. expect(ui.stop).not.toHaveBeenCalled();
  49. expect(setIntervalSpy).not.toHaveBeenCalled();
  50. expect(processOnSpy).not.toHaveBeenCalledWith("SIGINT", expect.any(Function));
  51. expect(processOnceSpy).not.toHaveBeenCalledWith("SIGCONT", expect.any(Function));
  52. expect(processKillSpy).not.toHaveBeenCalled();
  53. });
  54. test("keeps the process alive while suspended and restores the TUI on SIGCONT", () => {
  55. const ui: FakeUi = {
  56. start: vi.fn(),
  57. stop: vi.fn(),
  58. requestRender: vi.fn(),
  59. };
  60. const context: HandleCtrlZThis = { ui };
  61. const keepAliveHandle = setTimeout(() => undefined, 0);
  62. clearTimeout(keepAliveHandle);
  63. let sigintHandler: ProcessSignalHandler | undefined;
  64. let sigcontHandler: ProcessSignalHandler | undefined;
  65. const setIntervalSpy = vi.spyOn(globalThis, "setInterval").mockReturnValue(keepAliveHandle);
  66. const clearIntervalSpy = vi.spyOn(globalThis, "clearInterval").mockImplementation(() => undefined);
  67. const processOnSpy = vi.spyOn(process, "on").mockImplementation(((event: string, listener: () => void) => {
  68. if (event === "SIGINT") {
  69. sigintHandler = listener;
  70. }
  71. return process;
  72. }) as typeof process.on);
  73. const processOnceSpy = vi.spyOn(process, "once").mockImplementation(((event: string, listener: () => void) => {
  74. if (event === "SIGCONT") {
  75. sigcontHandler = listener;
  76. }
  77. return process;
  78. }) as typeof process.once);
  79. const removeListenerSpy = vi
  80. .spyOn(process, "removeListener")
  81. .mockImplementation(((_event: string, _listener: () => void) => process) as typeof process.removeListener);
  82. const processKillSpy = vi.spyOn(process, "kill").mockImplementation(() => true);
  83. callHandleCtrlZ(context);
  84. expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 2 ** 30);
  85. expect(processOnSpy).toHaveBeenCalledWith("SIGINT", expect.any(Function));
  86. expect(processOnceSpy).toHaveBeenCalledWith("SIGCONT", expect.any(Function));
  87. expect(ui.stop).toHaveBeenCalledTimes(1);
  88. expect(processKillSpy).toHaveBeenCalledWith(0, "SIGTSTP");
  89. expect(sigintHandler).toBeDefined();
  90. expect(sigcontHandler).toBeDefined();
  91. sigcontHandler?.();
  92. expect(clearIntervalSpy).toHaveBeenCalledWith(keepAliveHandle);
  93. expect(removeListenerSpy).toHaveBeenCalledWith("SIGINT", sigintHandler);
  94. expect(ui.start).toHaveBeenCalledTimes(1);
  95. expect(ui.requestRender).toHaveBeenCalledWith(true);
  96. });
  97. test("cleans up the temporary handlers if suspension fails", () => {
  98. const ui: FakeUi = {
  99. start: vi.fn(),
  100. stop: vi.fn(),
  101. requestRender: vi.fn(),
  102. };
  103. const context: HandleCtrlZThis = { ui };
  104. const keepAliveHandle = setTimeout(() => undefined, 0);
  105. clearTimeout(keepAliveHandle);
  106. const suspendError = new Error("suspend failed");
  107. const setIntervalSpy = vi.spyOn(globalThis, "setInterval").mockReturnValue(keepAliveHandle);
  108. const clearIntervalSpy = vi.spyOn(globalThis, "clearInterval").mockImplementation(() => undefined);
  109. vi.spyOn(process, "on").mockImplementation(
  110. ((_event: string, _listener: () => void) => process) as typeof process.on,
  111. );
  112. const removeListenerSpy = vi
  113. .spyOn(process, "removeListener")
  114. .mockImplementation(((_event: string, _listener: () => void) => process) as typeof process.removeListener);
  115. vi.spyOn(process, "once").mockImplementation(
  116. ((_event: string, _listener: () => void) => process) as typeof process.once,
  117. );
  118. vi.spyOn(process, "kill").mockImplementation(() => {
  119. throw suspendError;
  120. });
  121. expect(() => callHandleCtrlZ(context)).toThrow(suspendError);
  122. expect(ui.stop).toHaveBeenCalledTimes(1);
  123. expect(setIntervalSpy).toHaveBeenCalledTimes(1);
  124. expect(clearIntervalSpy).toHaveBeenCalledWith(keepAliveHandle);
  125. expect(removeListenerSpy).toHaveBeenCalledWith("SIGINT", expect.any(Function));
  126. expect(ui.start).not.toHaveBeenCalled();
  127. expect(ui.requestRender).not.toHaveBeenCalled();
  128. });
  129. });