tui-keybindings.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. TuiKeybindingRegistry,
  4. createDefaultTuiKeybindingRegistry,
  5. createTuiHostKeybindings,
  6. resolveTuiKeybinding,
  7. formatTuiKeybinding,
  8. } from "../../src/tui/shell/keybindings.js";
  9. import { MINIMAX_CODE_COMMANDS } from "../../src/tui/commands/catalog.js";
  10. describe("TUI shell keybindings", () => {
  11. it("maps terminal sequences to semantic shell actions", () => {
  12. const context = {
  13. interactionActive: false,
  14. hasLiveRun: true,
  15. hasWaitingMessage: true,
  16. };
  17. expect(resolveTuiKeybinding("\x16", context)).toBe("paste-image");
  18. expect(resolveTuiKeybinding("\u001bv", context)).toBe("paste-image");
  19. expect(resolveTuiKeybinding("\u001b[118;3u", context)).toBe("paste-image");
  20. expect(resolveTuiKeybinding("\u001b[118;9u", context)).toBe("paste-image");
  21. expect(resolveTuiKeybinding("\u001b[118;9:3u", context)).toBeUndefined();
  22. expect(resolveTuiKeybinding("\x07", context)).toBe("open-external-editor");
  23. expect(resolveTuiKeybinding("\x12", context)).toBe("search-history");
  24. expect(resolveTuiKeybinding("\x1b\r", context)).toBe("queue-draft");
  25. expect(resolveTuiKeybinding("\x13", context)).toBeUndefined();
  26. expect(resolveTuiKeybinding("\r", context)).toBe("submit-guidance");
  27. expect(resolveTuiKeybinding("\t", context)).toBeUndefined();
  28. expect(resolveTuiKeybinding("\x18", context)).toBeUndefined();
  29. expect(resolveTuiKeybinding("\x1b[1;3A", context)).toBe("restore-waiting");
  30. expect(resolveTuiKeybinding("\x1b[1;2D", context)).toBe("restore-waiting");
  31. expect(resolveTuiKeybinding("\x1bp", context)).toBe("restore-waiting");
  32. expect(resolveTuiKeybinding("\x14", context)).toBe("toggle-tasks");
  33. expect(resolveTuiKeybinding("\u001B[Z", context)).toBe("toggle-plan");
  34. expect(resolveTuiKeybinding("\u001bm", context)).toBe("cycle-permission");
  35. expect(resolveTuiKeybinding("\u001B[5~", context)).toBeUndefined();
  36. expect(resolveTuiKeybinding("\u001B[6~", context)).toBeUndefined();
  37. expect(resolveTuiKeybinding("\u001B[F", context)).toBeUndefined();
  38. expect(resolveTuiKeybinding("\x0f", context)).toBe("toggle-details");
  39. expect(resolveTuiKeybinding("\x03", context)).toBe("clear");
  40. expect(resolveTuiKeybinding("\u001B", context)).toBe("interrupt");
  41. expect(resolveTuiKeybinding("\x04", context)).toBe("exit");
  42. expect(resolveTuiKeybinding("\x1a", context)).toBe("suspend");
  43. expect(resolveTuiKeybinding("\x15", context)).toBeUndefined();
  44. expect(resolveTuiKeybinding("\u001B[99;5:3u", context)).toBeUndefined();
  45. });
  46. it("toggles the side conversation on Ctrl+/ across CSI-u, modifyOtherKeys and Dvorak reports", () => {
  47. const context = { interactionActive: false, hasLiveRun: true };
  48. expect(resolveTuiKeybinding("\u001B[47;5u", context)).toBe(
  49. "toggle-side-session",
  50. );
  51. expect(resolveTuiKeybinding("\u001B[27;5;47~", context)).toBe(
  52. "toggle-side-session",
  53. );
  54. expect(resolveTuiKeybinding("\u001B[47::91;5u", context)).toBe(
  55. "toggle-side-session",
  56. );
  57. // Legacy terminals (Terminal.app, iTerm without kitty protocol) report
  58. // Ctrl+/ as the bare 0x1F control byte, identical to Ctrl+_.
  59. expect(resolveTuiKeybinding("\x1f", context)).toBe("toggle-side-session");
  60. // Application scope: the toggle stays reachable while an interaction is active.
  61. expect(
  62. resolveTuiKeybinding("\u001B[47;5u", {
  63. interactionActive: true,
  64. hasLiveRun: false,
  65. }),
  66. ).toBe("toggle-side-session");
  67. expect(formatTuiKeybinding("app.toggle-side-session")).toContain("/");
  68. });
  69. it("reserves Ctrl+U for launch handoff only while the shell is idle", () => {
  70. expect(
  71. resolveTuiKeybinding("\x15", {
  72. interactionActive: false,
  73. hasLiveRun: false,
  74. }),
  75. ).toBe("resume-codex");
  76. expect(
  77. resolveTuiKeybinding("\x15", {
  78. interactionActive: false,
  79. hasLiveRun: true,
  80. }),
  81. ).toBeUndefined();
  82. expect(
  83. resolveTuiKeybinding("\x15", {
  84. interactionActive: true,
  85. hasLiveRun: false,
  86. }),
  87. ).toBeUndefined();
  88. });
  89. it("reserves product shortcuts for the composer while keeping application shortcuts global", () => {
  90. expect(
  91. resolveTuiKeybinding("\x16", {
  92. interactionActive: true,
  93. hasLiveRun: true,
  94. }),
  95. ).toBeUndefined();
  96. expect(
  97. resolveTuiKeybinding("\x07", {
  98. interactionActive: true,
  99. hasLiveRun: true,
  100. }),
  101. ).toBeUndefined();
  102. expect(
  103. resolveTuiKeybinding("\x1b[1;3A", {
  104. interactionActive: false,
  105. hasLiveRun: true,
  106. hasWaitingMessage: true,
  107. }),
  108. ).toBe("restore-waiting");
  109. expect(
  110. resolveTuiKeybinding("\x1bp", {
  111. interactionActive: false,
  112. hasLiveRun: true,
  113. hasWaitingMessage: true,
  114. }),
  115. ).toBe("restore-waiting");
  116. expect(
  117. resolveTuiKeybinding("\x03", {
  118. interactionActive: true,
  119. hasLiveRun: false,
  120. }),
  121. ).toBeUndefined();
  122. expect(
  123. resolveTuiKeybinding("\u001B", {
  124. interactionActive: true,
  125. hasLiveRun: true,
  126. }),
  127. ).toBeUndefined();
  128. expect(
  129. resolveTuiKeybinding("\x1a", {
  130. interactionActive: true,
  131. hasLiveRun: true,
  132. }),
  133. ).toBe("suspend");
  134. expect(
  135. resolveTuiKeybinding("\u001B[5~", {
  136. interactionActive: true,
  137. hasLiveRun: false,
  138. }),
  139. ).toBe("scroll-up");
  140. expect(
  141. resolveTuiKeybinding("\u001B[6~", {
  142. interactionActive: true,
  143. hasLiveRun: false,
  144. }),
  145. ).toBe("scroll-down");
  146. });
  147. it("rejects ambiguous bindings in the same context instead of relying on array order", () => {
  148. const registry = new TuiKeybindingRegistry();
  149. registry.register({
  150. id: "composer.paste",
  151. key: "ctrl+v",
  152. action: "paste-image",
  153. when: "composer",
  154. });
  155. expect(() =>
  156. registry.register({
  157. id: "composer.tasks",
  158. key: "ctrl+v",
  159. action: "toggle-tasks",
  160. when: "composer",
  161. }),
  162. ).toThrow(
  163. "Keybinding conflict: ctrl+v is already registered for composer.paste",
  164. );
  165. });
  166. it("treats application bindings as overlapping every focused surface", () => {
  167. const registry = new TuiKeybindingRegistry();
  168. registry.register({
  169. id: "app.search",
  170. key: "ctrl+r",
  171. action: "search-history",
  172. when: "application",
  173. });
  174. expect(() =>
  175. registry.register({
  176. id: "interaction.search",
  177. key: "ctrl+r",
  178. action: "search-history",
  179. when: "interaction",
  180. }),
  181. ).toThrow(
  182. "Keybinding conflict: ctrl+r is already registered for app.search",
  183. );
  184. });
  185. it("allows the same key to have different meanings in disjoint interaction contexts", () => {
  186. const registry = new TuiKeybindingRegistry();
  187. registry.register({
  188. id: "idle.search",
  189. key: "ctrl+r",
  190. action: "search-history",
  191. when: "idle",
  192. });
  193. registry.register({
  194. id: "running.tasks",
  195. key: "ctrl+r",
  196. action: "toggle-tasks",
  197. when: "live-run",
  198. });
  199. expect(
  200. registry.resolve("\x12", { interactionActive: false, hasLiveRun: false }),
  201. ).toBe("search-history");
  202. expect(
  203. registry.resolve("\x12", { interactionActive: false, hasLiveRun: true }),
  204. ).toBe("toggle-tasks");
  205. });
  206. it("applies validated user overrides without mutating the default registry", () => {
  207. const defaults = createDefaultTuiKeybindingRegistry();
  208. const overridden = defaults.withOverrides({
  209. "composer.toggle-tasks": "ctrl+k",
  210. });
  211. const context = { interactionActive: false, hasLiveRun: true };
  212. expect(overridden.resolve("\x0b", context)).toBe("toggle-tasks");
  213. expect(overridden.resolve("\x14", context)).toBeUndefined();
  214. expect(defaults.resolve("\x14", context)).toBe("toggle-tasks");
  215. expect(() =>
  216. defaults.withOverrides({ "composer.missing": "ctrl+k" }),
  217. ).toThrow("Unknown keybinding id: composer.missing");
  218. expect(formatTuiKeybinding("composer.toggle-tasks", overridden)).toBe(
  219. "Ctrl+K",
  220. );
  221. expect(
  222. overridden
  223. .help()
  224. .find((binding) => binding.id === "composer.toggle-tasks")?.description,
  225. ).toContain("Todo list");
  226. });
  227. it("derives compact Help rows from the same registry definitions", () => {
  228. const registry = createDefaultTuiKeybindingRegistry();
  229. const rows = registry.helpRows();
  230. expect(rows).toContainEqual({
  231. ids: ["composer.search-history", "composer.external-editor"],
  232. keys: `${formatTuiKeybinding("composer.search-history", registry)} / ${formatTuiKeybinding("composer.external-editor", registry)}`,
  233. description: "Search prompt history / edit in an external editor",
  234. });
  235. expect(
  236. rows.find((row) => row.ids.includes("interaction.scroll-up"))?.ids,
  237. ).toEqual(["interaction.scroll-up", "interaction.scroll-down"]);
  238. expect(
  239. MINIMAX_CODE_COMMANDS.find((command) => command.name === "transcript")
  240. ?.shortcut,
  241. ).toBeUndefined();
  242. expect(rows).toContainEqual({
  243. ids: ["composer.toggle-tasks"],
  244. keys: formatTuiKeybinding("composer.toggle-tasks", registry),
  245. description: "Show or hide the full Todo list",
  246. });
  247. expect(rows).toContainEqual({
  248. ids: ["composer.toggle-details"],
  249. keys: formatTuiKeybinding("composer.toggle-details", registry),
  250. description: "Show or hide Thinking, Tool output, and diffs",
  251. });
  252. expect(rows).toContainEqual({
  253. ids: ["run.restore-waiting-option", "run.restore-waiting-shift-left"],
  254. keys: `${formatTuiKeybinding("run.restore-waiting-option", registry)} / ${formatTuiKeybinding("run.restore-waiting-shift-left", registry)}`,
  255. description: "Move the latest queued message back to the Composer",
  256. });
  257. });
  258. it("advertises only shortcuts supported by the current host", () => {
  259. const windows = createTuiHostKeybindings({
  260. platform: "win32",
  261. suspendSupported: false,
  262. }).registry;
  263. const windowsRows = windows.helpRows();
  264. const paste = windowsRows.find((row) =>
  265. row.ids.includes("composer.paste-image"),
  266. );
  267. expect(windows.get("app.suspend")).toBeUndefined();
  268. expect(windows.get("composer.paste-image-macos")).toBeUndefined();
  269. expect(paste).toMatchObject({
  270. ids: ["composer.paste-image", "composer.paste-image-windows"],
  271. keys: "Alt+V / Ctrl+V",
  272. });
  273. expect(windows.get("composer.restore-draft")?.key).toBe("ctrl+z");
  274. const linux = createTuiHostKeybindings({
  275. platform: "linux",
  276. suspendSupported: true,
  277. }).registry;
  278. expect(linux.get("app.suspend")).toBeDefined();
  279. expect(linux.get("composer.paste-image-windows")).toBeUndefined();
  280. expect(linux.get("composer.paste-image-macos")).toBeUndefined();
  281. const wsl = createTuiHostKeybindings({
  282. platform: "linux",
  283. suspendSupported: true,
  284. windowsClipboardInterop: true,
  285. }).registry;
  286. expect(
  287. wsl.helpRows().find((row) => row.ids.includes("composer.paste-image")),
  288. ).toMatchObject({
  289. ids: ["composer.paste-image", "composer.paste-image-windows"],
  290. keys: "Alt+V / Ctrl+V",
  291. });
  292. expect(wsl.get("app.suspend")).toBeDefined();
  293. expect(wsl.get("composer.restore-draft")?.key).toBe("ctrl+-");
  294. const macos = createTuiHostKeybindings({
  295. platform: "darwin",
  296. suspendSupported: true,
  297. }).registry;
  298. expect(
  299. macos.helpRows().find((row) => row.ids.includes("composer.paste-image")),
  300. ).toMatchObject({
  301. ids: ["composer.paste-image", "composer.paste-image-macos"],
  302. keys: "Ctrl+V / Command+V",
  303. });
  304. expect(
  305. macos
  306. .helpRows()
  307. .find((row) => row.ids.includes("run.restore-waiting-option")),
  308. ).toMatchObject({
  309. keys: "Option+Up / Shift+Left",
  310. });
  311. });
  312. it("uses Windows Terminal-safe Editor and semantic prompt aliases without changing other hosts", () => {
  313. const windows = createTuiHostKeybindings({
  314. platform: "win32",
  315. suspendSupported: false,
  316. }).manager;
  317. const linux = createTuiHostKeybindings({
  318. platform: "linux",
  319. suspendSupported: true,
  320. }).manager;
  321. expect(windows.getKeys("tui.editor.undo")).toEqual(["ctrl+-", "ctrl+z"]);
  322. expect(windows.getKeys("tui.altScreen.previousPrompt")).toEqual([
  323. "ctrl+shift+up",
  324. "ctrl+up",
  325. ]);
  326. expect(windows.getKeys("tui.altScreen.nextPrompt")).toEqual([
  327. "ctrl+shift+down",
  328. "ctrl+down",
  329. ]);
  330. expect(windows.matches("\x1a", "tui.editor.undo")).toBe(true);
  331. expect(linux.getKeys("tui.editor.undo")).toEqual(["ctrl+-"]);
  332. expect(linux.getKeys("tui.altScreen.previousPrompt")).toEqual([
  333. "ctrl+shift+up",
  334. "ctrl+up",
  335. ]);
  336. expect(linux.getKeys("tui.altScreen.nextPrompt")).toEqual([
  337. "ctrl+shift+down",
  338. "ctrl+down",
  339. ]);
  340. expect(linux.matches("\x1a", "tui.editor.undo")).toBe(false);
  341. });
  342. it("shares one resolved keybinding manager between Engine widgets and product routing", () => {
  343. const keybindings = createTuiHostKeybindings({
  344. platform: "linux",
  345. suspendSupported: true,
  346. });
  347. keybindings.manager.setUserBindings({
  348. ...keybindings.manager.getUserBindings(),
  349. "composer.toggle-tasks": "ctrl+q",
  350. });
  351. expect(
  352. keybindings.registry.resolve("\x11", {
  353. interactionActive: false,
  354. hasLiveRun: true,
  355. }),
  356. ).toBe("toggle-tasks");
  357. expect(
  358. keybindings.registry.resolve("\x14", {
  359. interactionActive: false,
  360. hasLiveRun: true,
  361. }),
  362. ).toBeUndefined();
  363. expect(keybindings.registry.get("composer.toggle-tasks")?.key).toBe(
  364. "ctrl+q",
  365. );
  366. keybindings.manager.setUserBindings({ "composer.toggle-tasks": [] });
  367. expect(
  368. formatTuiKeybinding("composer.toggle-tasks", keybindings.registry),
  369. ).toBe("Unbound");
  370. for (const data of ["\x11", "\x14"]) {
  371. expect(
  372. keybindings.registry.resolve(data, {
  373. interactionActive: false,
  374. hasLiveRun: true,
  375. }),
  376. ).toBeUndefined();
  377. }
  378. keybindings.manager.setUserBindings({});
  379. expect(
  380. keybindings.registry.resolve("\x14", {
  381. interactionActive: false,
  382. hasLiveRun: true,
  383. }),
  384. ).toBe("toggle-tasks");
  385. });
  386. it("checks reload candidates against host defaults instead of stale user bindings", () => {
  387. const keybindings = createTuiHostKeybindings({
  388. platform: "linux",
  389. suspendSupported: true,
  390. userOverrides: { "composer.toggle-details": "ctrl+k" },
  391. });
  392. expect(
  393. keybindings.registry.findConflicts({ "composer.toggle-plan": "ctrl+k" }),
  394. ).toEqual([]);
  395. expect(
  396. keybindings.registry.findConflicts({
  397. "composer.toggle-details": "ctrl+k",
  398. "composer.toggle-plan": "ctrl+k",
  399. }),
  400. ).toHaveLength(1);
  401. expect(keybindings.registry.findConflicts({})).toEqual([]);
  402. });
  403. });