tui-signals.test.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import assert from "node:assert/strict";
  2. import { execFile } from "node:child_process";
  3. import { test } from "node:test";
  4. import { promisify } from "node:util";
  5. import { fileURLToPath } from "node:url";
  6. test(
  7. "built TUI survives SIGPIPE during startup and session replacement, but closes on SIGTERM",
  8. {
  9. skip: process.platform === "win32",
  10. timeout: 15_000,
  11. },
  12. async () => {
  13. const entry = new URL("../../tui/dist/index.js", import.meta.url);
  14. const { stdout } = await promisify(execFile)(
  15. process.execPath,
  16. [
  17. "--input-type=module",
  18. "-e",
  19. `
  20. import assert from "node:assert/strict";
  21. import { PassThrough, Writable } from "node:stream";
  22. import { setTimeout as delay } from "node:timers/promises";
  23. const { runTui } = await import(${JSON.stringify(entry.href)});
  24. const stdin = Object.assign(new PassThrough(), { isTTY: true, setRawMode() {} });
  25. const stdout = Object.assign(new Writable({ write(chunk, encoding, done) { done(); } }), {
  26. isTTY: true, columns: 110, rows: 32,
  27. });
  28. const startup = Promise.withResolvers();
  29. const ready = Promise.withResolvers();
  30. const submitted = Promise.withResolvers();
  31. let closed = false;
  32. const running = runTui({
  33. stdin, stdout, stderr: process.stderr, locale: "en-US", theme: "dark",
  34. loadStartupOptions: () => { startup.resolve(); return ready.promise; },
  35. submitPrompt: async (input) => {
  36. assert.equal(typeof input === "string" ? input : input.text, "/clear");
  37. // Closing an old session's pipes can deliver this signal to the TUI process.
  38. process.kill(process.pid, "SIGPIPE");
  39. submitted.resolve();
  40. return { response: "Started new session", resetSessionProjection: true };
  41. },
  42. }).then((code) => { closed = true; return code; });
  43. await startup.promise;
  44. process.kill(process.pid, "SIGPIPE");
  45. await delay(100);
  46. assert.equal(closed, false, "SIGPIPE must not close the startup screen");
  47. ready.resolve({});
  48. await delay(100);
  49. stdin.write("/clear");
  50. await delay(100);
  51. stdin.write("\\r");
  52. await submitted.promise;
  53. await delay(100);
  54. assert.equal(closed, false, "SIGPIPE must not close the new session");
  55. assert.ok(process.listenerCount("SIGTERM") > 0);
  56. process.kill(process.pid, "SIGTERM");
  57. assert.equal(await running, 0);
  58. process.stdout.write("TUI signal lifecycle passed\\n");
  59. `,
  60. ],
  61. {
  62. cwd: fileURLToPath(new URL("../../tui", import.meta.url)),
  63. env: { ...process.env, NODE_OPTIONS: "" },
  64. timeout: 10_000,
  65. maxBuffer: 1024 * 1024,
  66. },
  67. );
  68. assert.match(stdout, /TUI signal lifecycle passed/);
  69. },
  70. );