ctrl-c-exit-guard.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import {
  4. CTRL_C_EXIT_CONFIRMATION_WINDOW_MS,
  5. createCtrlCExitGuard,
  6. resetCtrlCExitGuard,
  7. resolveCtrlCExitIntent,
  8. } from "../src/app-keyboard.js";
  9. test("requires two consecutive Ctrl-C presses inside the confirmation window", () => {
  10. const guard = createCtrlCExitGuard();
  11. const firstPressAtMs = 1_000;
  12. assert.equal(resolveCtrlCExitIntent(guard, firstPressAtMs), "show_prompt");
  13. assert.equal(
  14. resolveCtrlCExitIntent(guard, firstPressAtMs + CTRL_C_EXIT_CONFIRMATION_WINDOW_MS),
  15. "confirm_exit",
  16. );
  17. assert.equal(guard.lastPressAtMs, undefined);
  18. });
  19. test("restarts Ctrl-C confirmation after the timeout", () => {
  20. const guard = createCtrlCExitGuard();
  21. const firstPressAtMs = 1_000;
  22. const afterTimeoutMs = firstPressAtMs + CTRL_C_EXIT_CONFIRMATION_WINDOW_MS + 1;
  23. assert.equal(resolveCtrlCExitIntent(guard, firstPressAtMs), "show_prompt");
  24. assert.equal(resolveCtrlCExitIntent(guard, afterTimeoutMs), "show_prompt");
  25. assert.equal(guard.lastPressAtMs, afterTimeoutMs);
  26. });
  27. test("resets Ctrl-C confirmation when another key is handled", () => {
  28. const guard = createCtrlCExitGuard();
  29. const firstPressAtMs = 1_000;
  30. assert.equal(resolveCtrlCExitIntent(guard, firstPressAtMs), "show_prompt");
  31. resetCtrlCExitGuard(guard);
  32. assert.equal(resolveCtrlCExitIntent(guard, firstPressAtMs + 1), "show_prompt");
  33. });