session-selector-rename.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { setKeybindings } from "@earendil-works/pi-tui";
  2. import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
  3. import { KeybindingsManager } from "../src/core/keybindings.ts";
  4. import type { SessionInfo } from "../src/core/session-manager.ts";
  5. import { SessionSelectorComponent } from "../src/modes/interactive/components/session-selector.ts";
  6. import { initTheme } from "../src/modes/interactive/theme/theme.ts";
  7. async function flushPromises(): Promise<void> {
  8. await new Promise<void>((resolve) => {
  9. setImmediate(resolve);
  10. });
  11. }
  12. function makeSession(overrides: Partial<SessionInfo> & { id: string }): SessionInfo {
  13. return {
  14. path: overrides.path ?? `/tmp/${overrides.id}.jsonl`,
  15. id: overrides.id,
  16. cwd: overrides.cwd ?? "",
  17. name: overrides.name,
  18. created: overrides.created ?? new Date(0),
  19. modified: overrides.modified ?? new Date(0),
  20. messageCount: overrides.messageCount ?? 1,
  21. firstMessage: overrides.firstMessage ?? "hello",
  22. allMessagesText: overrides.allMessagesText ?? "hello",
  23. };
  24. }
  25. // Kitty keyboard protocol encoding for Ctrl+R
  26. const CTRL_R = "\x1b[114;5u";
  27. describe("session selector rename", () => {
  28. beforeAll(() => {
  29. initTheme("dark");
  30. });
  31. beforeEach(() => {
  32. // Ensure test isolation: keybindings are a global singleton
  33. setKeybindings(new KeybindingsManager());
  34. });
  35. it("shows rename hint in interactive /resume picker configuration", async () => {
  36. const sessions = [makeSession({ id: "a" })];
  37. const keybindings = new KeybindingsManager();
  38. const selector = new SessionSelectorComponent(
  39. async () => sessions,
  40. async () => [],
  41. () => {},
  42. () => {},
  43. () => {},
  44. () => {},
  45. { showRenameHint: true, keybindings },
  46. );
  47. await flushPromises();
  48. const output = selector.render(120).join("\n");
  49. expect(output).toContain("ctrl+r");
  50. expect(output).toContain("rename");
  51. });
  52. it("does not show rename hint in --resume picker configuration", async () => {
  53. const sessions = [makeSession({ id: "a" })];
  54. const keybindings = new KeybindingsManager();
  55. const selector = new SessionSelectorComponent(
  56. async () => sessions,
  57. async () => [],
  58. () => {},
  59. () => {},
  60. () => {},
  61. () => {},
  62. { showRenameHint: false, keybindings },
  63. );
  64. await flushPromises();
  65. const output = selector.render(120).join("\n");
  66. expect(output).not.toContain("ctrl+r");
  67. expect(output).not.toContain("rename");
  68. });
  69. it("enters rename mode on Ctrl+R and submits with Enter", async () => {
  70. const sessions = [makeSession({ id: "a", name: "Old" })];
  71. const renameSession = vi.fn(async () => {});
  72. const keybindings = new KeybindingsManager();
  73. const selector = new SessionSelectorComponent(
  74. async () => sessions,
  75. async () => [],
  76. () => {},
  77. () => {},
  78. () => {},
  79. () => {},
  80. { renameSession, showRenameHint: true, keybindings },
  81. );
  82. await flushPromises();
  83. selector.getSessionList().handleInput(CTRL_R);
  84. await flushPromises();
  85. // Rename mode layout
  86. const output = selector.render(120).join("\n");
  87. expect(output).toContain("Rename Session");
  88. expect(output).not.toContain("Resume Session");
  89. // Type and submit
  90. selector.handleInput("X");
  91. selector.handleInput("\r");
  92. await flushPromises();
  93. expect(renameSession).toHaveBeenCalledTimes(1);
  94. expect(renameSession).toHaveBeenCalledWith(sessions[0]!.path, "XOld");
  95. });
  96. });