Open History to see the last commit
1import { assert, describe, it } from "vite-plus/test";
2import {
3 compileResolvedKeybindingsConfig,
4 DEFAULT_RESOLVED_KEYBINDINGS,
5 mergeWithDefaultKeybindings,
6} from "@t3tools/shared/keybindings";
7
8import {
9 type KeybindingCommand,
10 type KeybindingShortcut,
11 type KeybindingWhenNode,
12 type ResolvedKeybindingsConfig,
13} from "@t3tools/contracts";
14import {
15 formatShortcutLabel,
16 isDiffToggleShortcut,
17 modelPickerJumpCommandForIndex,
18 modelPickerJumpIndexFromCommand,
19 isOpenFavoriteEditorShortcut,
20 isTerminalClearShortcut,
21 isTerminalCloseShortcut,
22 isTerminalNewShortcut,
23 isTerminalSplitShortcut,
24 isTerminalSplitVerticalShortcut,
25 isTerminalToggleShortcut,
26 resolveShortcutCommand,
27 shouldShowThreadJumpHintsForModifiers,
28 shortcutLabelForCommand,
29 terminalDeleteShortcutData,
30 terminalNavigationShortcutData,
31 threadJumpCommandForIndex,
32 threadJumpIndexFromCommand,
33 threadTraversalDirectionFromCommand,
34 type ShortcutEventLike,
35} from "./keybindings";
36
37function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
38 return {
39 key: "j",
40 metaKey: false,
41 ctrlKey: false,
42 shiftKey: false,
43 altKey: false,
44 ...overrides,
45 };
46}
47
48function modShortcut(
49 key: string,
50 overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
51): KeybindingShortcut {
52 return {
53 key,
54 metaKey: false,
55 ctrlKey: false,
56 shiftKey: false,
57 altKey: false,
58 modKey: true,
59 ...overrides,
60 };
61}
62
63function whenIdentifier(name: string): KeybindingWhenNode {
64 return { type: "identifier", name };
65}
66
67function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
68 return { type: "not", node };
69}
70
71function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
72 return { type: "and", left, right };
73}
74
75interface TestBinding {
76 shortcut: KeybindingShortcut;
77 command: KeybindingCommand;
78 whenAst?: KeybindingWhenNode;
79}
80
81function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
82 return bindings.map((binding) => ({
83 command: binding.command,
84 shortcut: binding.shortcut,
85 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
86 }));
87}
88
89const DEFAULT_BINDINGS = compile([
90 { shortcut: modShortcut("b"), command: "sidebar.toggle" },
91 { shortcut: modShortcut("j"), command: "terminal.toggle" },
92 { shortcut: modShortcut("b", { altKey: true }), command: "rightPanel.toggle" },
93 {
94 shortcut: modShortcut("d"),
95 command: "terminal.split",
96 whenAst: whenIdentifier("terminalFocus"),
97 },
98 {
99 shortcut: modShortcut("d", { shiftKey: true }),
100 command: "terminal.splitVertical",
101 whenAst: whenIdentifier("terminalFocus"),
102 },
103 {
104 shortcut: modShortcut("n"),
105 command: "terminal.new",
106 whenAst: whenIdentifier("terminalFocus"),
107 },
108 {
109 shortcut: modShortcut("w"),
110 command: "terminal.close",
111 whenAst: whenIdentifier("terminalFocus"),
112 },
113 {
114 shortcut: modShortcut("w"),
115 command: "rightPanel.close",
116 whenAst: whenNot(whenIdentifier("terminalFocus")),
117 },
118 {
119 shortcut: modShortcut("d"),
120 command: "diff.toggle",
121 whenAst: whenNot(whenIdentifier("terminalFocus")),
122 },
123 {
124 shortcut: modShortcut("k"),
125 command: "commandPalette.toggle",
126 whenAst: whenNot(whenIdentifier("terminalFocus")),
127 },
128 {
129 shortcut: modShortcut("p"),
130 command: "filePicker.toggle",
131 whenAst: whenNot(whenIdentifier("terminalFocus")),
132 },
133 {
134 shortcut: modShortcut("f", { shiftKey: true }),
135 command: "projectSearch.toggle",
136 whenAst: whenNot(whenIdentifier("terminalFocus")),
137 },
138 {
139 shortcut: modShortcut("t", { altKey: true, shiftKey: true }),
140 command: "themeEditor.toggle",
141 },
142 {
143 shortcut: modShortcut("m", { shiftKey: true }),
144 command: "modelPicker.toggle",
145 whenAst: whenNot(whenIdentifier("terminalFocus")),
146 },
147 { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
148 { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
149 { shortcut: modShortcut("o"), command: "editor.openFavorite" },
150 { shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },
151 { shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },
152 {
153 shortcut: modShortcut("c", { shiftKey: true }),
154 command: "thread.copyReference",
155 whenAst: whenNot(whenIdentifier("terminalFocus")),
156 },
157 {
158 shortcut: modShortcut("s", { shiftKey: true }),
159 command: "thread.settle",
160 whenAst: whenNot(whenIdentifier("terminalFocus")),
161 },
162 { shortcut: modShortcut("1"), command: "thread.jump.1" },
163 { shortcut: modShortcut("2"), command: "thread.jump.2" },
164 { shortcut: modShortcut("3"), command: "thread.jump.3" },
165 {
166 shortcut: modShortcut("1"),
167 command: "modelPicker.jump.1",
168 whenAst: whenIdentifier("modelPickerOpen"),
169 },
170 {
171 shortcut: modShortcut("2"),
172 command: "modelPicker.jump.2",
173 whenAst: whenIdentifier("modelPickerOpen"),
174 },
175 {
176 shortcut: modShortcut("3"),
177 command: "modelPicker.jump.3",
178 whenAst: whenIdentifier("modelPickerOpen"),
179 },
180]);
181
182describe("isTerminalToggleShortcut", () => {
183 it("matches Cmd+J on macOS", () => {
184 assert.isTrue(
185 isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, {
186 platform: "MacIntel",
187 }),
188 );
189 });
190
191 it("matches Ctrl+J on non-macOS", () => {
192 assert.isTrue(
193 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }),
194 );
195 });
196
197 it("matches Ctrl+J on non-macOS while terminalFocus is true", () => {
198 assert.isTrue(
199 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, {
200 platform: "Win32",
201 context: { terminalFocus: true },
202 }),
203 );
204 });
205});
206
207describe("settle thread shortcut", () => {
208 it("resolves outside the terminal", () => {
209 assert.equal(
210 resolveShortcutCommand(event({ key: "s", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
211 platform: "MacIntel",
212 context: { terminalFocus: false },
213 }),
214 "thread.settle",
215 );
216 });
217
218 it("does not intercept the terminal", () => {
219 assert.isNull(
220 resolveShortcutCommand(event({ key: "s", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
221 platform: "Win32",
222 context: { terminalFocus: true },
223 }),
224 );
225 });
226});
227
228describe("copy thread reference shortcut", () => {
229 it("resolves Cmd+Shift+C on macOS and Ctrl+Shift+C elsewhere", () => {
230 assert.equal(
231 resolveShortcutCommand(event({ key: "c", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
232 platform: "MacIntel",
233 }),
234 "thread.copyReference",
235 );
236 assert.equal(
237 resolveShortcutCommand(event({ key: "c", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
238 platform: "Linux",
239 }),
240 "thread.copyReference",
241 );
242 });
243
244 it("leaves terminal copy untouched", () => {
245 assert.isNull(
246 resolveShortcutCommand(event({ key: "c", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
247 platform: "Linux",
248 context: { terminalFocus: true },
249 }),
250 );
251 });
252});
253
254describe("split/new/close terminal shortcuts", () => {
255 it("requires terminalFocus for default split/new/close bindings", () => {
256 assert.isFalse(
257 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
258 platform: "MacIntel",
259 context: { terminalFocus: false },
260 }),
261 );
262 assert.isFalse(
263 isTerminalSplitVerticalShortcut(
264 event({ key: "d", metaKey: true, shiftKey: true }),
265 DEFAULT_BINDINGS,
266 {
267 platform: "MacIntel",
268 context: { terminalFocus: false },
269 },
270 ),
271 );
272 assert.isFalse(
273 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), DEFAULT_BINDINGS, {
274 platform: "Linux",
275 context: { terminalFocus: false },
276 }),
277 );
278 assert.isFalse(
279 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
280 platform: "Linux",
281 context: { terminalFocus: false },
282 }),
283 );
284 });
285
286 it("matches split/new when terminalFocus is true", () => {
287 assert.isTrue(
288 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
289 platform: "MacIntel",
290 context: { terminalFocus: true },
291 }),
292 );
293 assert.isTrue(
294 isTerminalSplitVerticalShortcut(
295 event({ key: "d", metaKey: true, shiftKey: true }),
296 DEFAULT_BINDINGS,
297 {
298 platform: "MacIntel",
299 context: { terminalFocus: true },
300 },
301 ),
302 );
303 assert.isTrue(
304 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), DEFAULT_BINDINGS, {
305 platform: "Linux",
306 context: { terminalFocus: true },
307 }),
308 );
309 assert.isTrue(
310 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
311 platform: "Linux",
312 context: { terminalFocus: true },
313 }),
314 );
315 });
316
317 it("supports when expressions", () => {
318 const keybindings = compile([
319 {
320 shortcut: modShortcut("\\"),
321 command: "terminal.split",
322 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
323 },
324 {
325 shortcut: modShortcut("n", { shiftKey: true }),
326 command: "terminal.new",
327 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
328 },
329 { shortcut: modShortcut("j"), command: "terminal.toggle" },
330 ]);
331 assert.isTrue(
332 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
333 platform: "Win32",
334 context: { terminalOpen: true, terminalFocus: false },
335 }),
336 );
337 assert.isFalse(
338 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
339 platform: "Win32",
340 context: { terminalOpen: false, terminalFocus: false },
341 }),
342 );
343 assert.isTrue(
344 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
345 platform: "Win32",
346 context: { terminalOpen: true, terminalFocus: false },
347 }),
348 );
349 });
350
351 it("supports when boolean literals", () => {
352 const keybindings = compile([
353 { shortcut: modShortcut("n"), command: "terminal.new", whenAst: whenIdentifier("true") },
354 { shortcut: modShortcut("m"), command: "terminal.new", whenAst: whenIdentifier("false") },
355 ]);
356
357 assert.isTrue(
358 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
359 platform: "Linux",
360 }),
361 );
362 assert.isFalse(
363 isTerminalNewShortcut(event({ key: "m", ctrlKey: true }), keybindings, {
364 platform: "Linux",
365 }),
366 );
367 });
368});
369
370describe("shortcutLabelForCommand", () => {
371 it("returns the effective binding label", () => {
372 const bindings = compile([
373 {
374 shortcut: modShortcut("\\"),
375 command: "terminal.split",
376 whenAst: whenIdentifier("terminalFocus"),
377 },
378 {
379 shortcut: modShortcut("\\", { shiftKey: true }),
380 command: "terminal.split",
381 whenAst: whenNot(whenIdentifier("terminalFocus")),
382 },
383 ]);
384 assert.strictEqual(
385 shortcutLabelForCommand(bindings, "terminal.split", {
386 platform: "Linux",
387 context: { terminalFocus: false },
388 }),
389 "Ctrl+Shift+\\",
390 );
391 });
392
393 it("returns effective labels for non-terminal commands", () => {
394 assert.strictEqual(
395 shortcutLabelForCommand(DEFAULT_BINDINGS, "sidebar.toggle", "MacIntel"),
396 "⌘B",
397 );
398 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
399 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");
400 assert.strictEqual(
401 shortcutLabelForCommand(DEFAULT_BINDINGS, "rightPanel.toggle", "MacIntel"),
402 "⌥⌘B",
403 );
404 assert.strictEqual(
405 shortcutLabelForCommand(DEFAULT_BINDINGS, "commandPalette.toggle", "MacIntel"),
406 "⌘K",
407 );
408 assert.strictEqual(
409 shortcutLabelForCommand(DEFAULT_BINDINGS, "filePicker.toggle", "MacIntel"),
410 "⌘P",
411 );
412 assert.strictEqual(
413 shortcutLabelForCommand(DEFAULT_BINDINGS, "projectSearch.toggle", "MacIntel"),
414 "⇧⌘F",
415 );
416 assert.strictEqual(
417 shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.toggle", "Linux"),
418 "Ctrl+Shift+M",
419 );
420 assert.strictEqual(
421 shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
422 "Ctrl+O",
423 );
424 assert.strictEqual(
425 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.jump.3", "MacIntel"),
426 "⌘3",
427 );
428 assert.strictEqual(
429 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.previous", "Linux"),
430 "Ctrl+Shift+[",
431 );
432 assert.strictEqual(
433 shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.jump.3", {
434 platform: "MacIntel",
435 context: { modelPickerOpen: true },
436 }),
437 "⌘3",
438 );
439 });
440
441 it("returns null for commands shadowed by a later conflicting shortcut", () => {
442 const bindings = compile([
443 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.1" },
444 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.7" },
445 ]);
446
447 assert.isNull(shortcutLabelForCommand(bindings, "thread.jump.1", "MacIntel"));
448 assert.strictEqual(shortcutLabelForCommand(bindings, "thread.jump.7", "MacIntel"), "⇧⌘1");
449 });
450
451 it("respects when-context while resolving labels", () => {
452 const bindings = compile([
453 { shortcut: modShortcut("d"), command: "diff.toggle" },
454 {
455 shortcut: modShortcut("d"),
456 command: "terminal.split",
457 whenAst: whenIdentifier("terminalFocus"),
458 },
459 ]);
460
461 assert.strictEqual(
462 shortcutLabelForCommand(bindings, "diff.toggle", {
463 platform: "Linux",
464 context: { terminalFocus: false },
465 }),
466 "Ctrl+D",
467 );
468 assert.isNull(
469 shortcutLabelForCommand(bindings, "diff.toggle", {
470 platform: "Linux",
471 context: { terminalFocus: true },
472 }),
473 );
474 assert.strictEqual(
475 shortcutLabelForCommand(bindings, "terminal.split", {
476 platform: "Linux",
477 context: { terminalFocus: true },
478 }),
479 "Ctrl+D",
480 );
481 });
482});
483
484describe("thread navigation helpers", () => {
485 it("maps jump commands to visible thread indices", () => {
486 assert.strictEqual(threadJumpCommandForIndex(0), "thread.jump.1");
487 assert.strictEqual(threadJumpCommandForIndex(2), "thread.jump.3");
488 assert.isNull(threadJumpCommandForIndex(9));
489 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.1"), 0);
490 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.3"), 2);
491 assert.isNull(threadJumpIndexFromCommand("thread.next"));
492 });
493
494 it("maps traversal commands to directions", () => {
495 assert.strictEqual(threadTraversalDirectionFromCommand("thread.previous"), "previous");
496 assert.strictEqual(threadTraversalDirectionFromCommand("thread.next"), "next");
497 assert.isNull(threadTraversalDirectionFromCommand("thread.jump.1"));
498 assert.isNull(threadTraversalDirectionFromCommand(null));
499 });
500
501 it("shows jump hints only when configured modifiers match", () => {
502 assert.isTrue(
503 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
504 platform: "MacIntel",
505 }),
506 );
507 assert.isFalse(
508 shouldShowThreadJumpHintsForModifiers(
509 event({ metaKey: true, shiftKey: true }),
510 DEFAULT_BINDINGS,
511 {
512 platform: "MacIntel",
513 },
514 ),
515 );
516 assert.isTrue(
517 shouldShowThreadJumpHintsForModifiers(event({ ctrlKey: true }), DEFAULT_BINDINGS, {
518 platform: "Linux",
519 }),
520 );
521 });
522
523 it("never shows jump hints while the terminal is focused, even with an unrestricted binding", () => {
524 assert.isFalse(
525 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
526 platform: "MacIntel",
527 context: { terminalFocus: true },
528 }),
529 );
530 assert.isTrue(
531 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
532 platform: "MacIntel",
533 context: { terminalFocus: false },
534 }),
535 );
536 });
537});
538
539describe("model picker navigation helpers", () => {
540 it("maps jump commands to visible model indices", () => {
541 assert.strictEqual(modelPickerJumpCommandForIndex(0), "modelPicker.jump.1");
542 assert.strictEqual(modelPickerJumpCommandForIndex(2), "modelPicker.jump.3");
543 assert.isNull(modelPickerJumpCommandForIndex(9));
544 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.1"), 0);
545 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.3"), 2);
546 assert.isNull(modelPickerJumpIndexFromCommand("thread.jump.1"));
547 });
548});
549
550describe("chat/editor shortcuts", () => {
551 it("matches chat.new shortcut", () => {
552 assert.strictEqual(
553 resolveShortcutCommand(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
554 platform: "MacIntel",
555 }),
556 "chat.new",
557 );
558 assert.strictEqual(
559 resolveShortcutCommand(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
560 platform: "Linux",
561 }),
562 "chat.new",
563 );
564 });
565
566 it("matches chat.newLocal shortcut", () => {
567 assert.strictEqual(
568 resolveShortcutCommand(event({ key: "n", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
569 platform: "MacIntel",
570 }),
571 "chat.newLocal",
572 );
573 assert.strictEqual(
574 resolveShortcutCommand(event({ key: "n", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
575 platform: "Linux",
576 }),
577 "chat.newLocal",
578 );
579 });
580
581 it("matches editor.openFavorite shortcut", () => {
582 assert.isTrue(
583 isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
584 platform: "MacIntel",
585 }),
586 );
587 assert.isTrue(
588 isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
589 platform: "Linux",
590 }),
591 );
592 });
593
594 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
595 assert.strictEqual(
596 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
597 platform: "MacIntel",
598 context: { terminalFocus: false },
599 }),
600 "commandPalette.toggle",
601 );
602 assert.notStrictEqual(
603 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
604 platform: "MacIntel",
605 context: { terminalFocus: true },
606 }),
607 "commandPalette.toggle",
608 );
609 });
610
611 it("matches filePicker.toggle shortcut outside terminal focus", () => {
612 assert.strictEqual(
613 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
614 platform: "MacIntel",
615 context: { terminalFocus: false },
616 }),
617 "filePicker.toggle",
618 );
619 assert.notStrictEqual(
620 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
621 platform: "MacIntel",
622 context: { terminalFocus: true },
623 }),
624 "filePicker.toggle",
625 );
626 });
627
628 it("matches projectSearch.toggle shortcut outside terminal focus", () => {
629 assert.strictEqual(
630 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
631 platform: "MacIntel",
632 context: { terminalFocus: false },
633 }),
634 "projectSearch.toggle",
635 );
636 assert.notStrictEqual(
637 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
638 platform: "MacIntel",
639 context: { terminalFocus: true },
640 }),
641 "projectSearch.toggle",
642 );
643 });
644
645 it("matches themeEditor.toggle on macOS and Windows", () => {
646 assert.strictEqual(
647 resolveShortcutCommand(
648 event({ key: "t", metaKey: true, altKey: true, shiftKey: true }),
649 DEFAULT_BINDINGS,
650 { platform: "MacIntel" },
651 ),
652 "themeEditor.toggle",
653 );
654 assert.strictEqual(
655 resolveShortcutCommand(
656 event({ key: "t", ctrlKey: true, altKey: true, shiftKey: true }),
657 DEFAULT_BINDINGS,
658 { platform: "Win32" },
659 ),
660 "themeEditor.toggle",
661 );
662 });
663
664 it("matches diff.toggle shortcut outside terminal focus", () => {
665 assert.isTrue(
666 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
667 platform: "MacIntel",
668 context: { terminalFocus: false },
669 }),
670 );
671 assert.isFalse(
672 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
673 platform: "MacIntel",
674 context: { terminalFocus: true },
675 }),
676 );
677 });
678});
679
680describe("cross-command precedence", () => {
681 it("uses when + order so a later focused rule overrides a global rule", () => {
682 const keybindings = compile([
683 { shortcut: modShortcut("n"), command: "chat.new" },
684 {
685 shortcut: modShortcut("n"),
686 command: "terminal.new",
687 whenAst: whenIdentifier("terminalFocus"),
688 },
689 ]);
690
691 assert.isTrue(
692 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
693 platform: "MacIntel",
694 context: { terminalFocus: true },
695 }),
696 );
697 assert.strictEqual(
698 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
699 platform: "MacIntel",
700 context: { terminalFocus: true },
701 }),
702 "terminal.new",
703 );
704 assert.isFalse(
705 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
706 platform: "MacIntel",
707 context: { terminalFocus: false },
708 }),
709 );
710 assert.strictEqual(
711 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
712 platform: "MacIntel",
713 context: { terminalFocus: false },
714 }),
715 "chat.new",
716 );
717 });
718
719 it("still lets a later global rule win when both rules match", () => {
720 const keybindings = compile([
721 {
722 shortcut: modShortcut("n"),
723 command: "terminal.new",
724 whenAst: whenIdentifier("terminalFocus"),
725 },
726 { shortcut: modShortcut("n"), command: "chat.new" },
727 ]);
728
729 assert.isFalse(
730 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
731 platform: "Linux",
732 context: { terminalFocus: true },
733 }),
734 );
735 assert.strictEqual(
736 resolveShortcutCommand(event({ key: "n", ctrlKey: true }), keybindings, {
737 platform: "Linux",
738 context: { terminalFocus: true },
739 }),
740 "chat.new",
741 );
742 });
743});
744
745describe("resolveShortcutCommand", () => {
746 it("resolves a custom stop-thread shortcut", () => {
747 const keybindings = compile([{ shortcut: modShortcut("escape"), command: "thread.stop" }]);
748
749 assert.strictEqual(
750 resolveShortcutCommand(event({ key: "Escape", metaKey: true }), keybindings, {
751 platform: "MacIntel",
752 }),
753 "thread.stop",
754 );
755 });
756
757 it("honors preview conditions for a stop-thread shortcut", () => {
758 const keybindings = compile([
759 {
760 shortcut: modShortcut("escape"),
761 command: "thread.stop",
762 whenAst: whenIdentifier("previewFocus"),
763 },
764 ]);
765 const input = event({ key: "Escape", metaKey: true });
766
767 assert.isNull(
768 resolveShortcutCommand(input, keybindings, {
769 platform: "MacIntel",
770 context: { previewFocus: false },
771 }),
772 );
773 assert.strictEqual(
774 resolveShortcutCommand(input, keybindings, {
775 platform: "MacIntel",
776 context: { previewFocus: true },
777 }),
778 "thread.stop",
779 );
780 });
781
782 it("returns dynamic script commands", () => {
783 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
784
785 assert.strictEqual(
786 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
787 platform: "Linux",
788 }),
789 "script.setup.run",
790 );
791 });
792
793 it("routes mod+w to the terminal while focused and to the right panel otherwise", () => {
794 const closeEvent = event({ key: "w", metaKey: true });
795 assert.strictEqual(
796 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
797 platform: "MacIntel",
798 context: { terminalFocus: true },
799 }),
800 "terminal.close",
801 );
802 assert.strictEqual(
803 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
804 platform: "MacIntel",
805 context: { terminalFocus: false },
806 }),
807 "rightPanel.close",
808 );
809 });
810
811 it("resolves a custom right panel maximize binding", () => {
812 const keybindings = compile([
813 {
814 shortcut: modShortcut("m", { shiftKey: true }),
815 command: "rightPanel.toggleMaximized",
816 },
817 ]);
818
819 assert.strictEqual(
820 resolveShortcutCommand(event({ key: "m", metaKey: true, shiftKey: true }), keybindings, {
821 platform: "MacIntel",
822 }),
823 "rightPanel.toggleMaximized",
824 );
825 });
826
827 it("matches bracket shortcuts using the physical key code", () => {
828 assert.strictEqual(
829 resolveShortcutCommand(
830 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
831 DEFAULT_BINDINGS,
832 {
833 platform: "MacIntel",
834 },
835 ),
836 "thread.previous",
837 );
838 assert.strictEqual(
839 resolveShortcutCommand(
840 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
841 DEFAULT_BINDINGS,
842 {
843 platform: "Linux",
844 },
845 ),
846 "thread.next",
847 );
848 });
849
850 it("matches punctuation shortcuts by physical key across keyboard layouts", () => {
851 const keybindings = compile([
852 { shortcut: modShortcut("'", { shiftKey: true }), command: "diff.toggle" },
853 ]);
854
855 assert.strictEqual(
856 resolveShortcutCommand(
857 event({ key: "@", code: "Quote", metaKey: true, shiftKey: true }),
858 keybindings,
859 { platform: "MacIntel" },
860 ),
861 "diff.toggle",
862 );
863 assert.isNull(
864 resolveShortcutCommand(
865 event({ key: '"', code: "Digit2", metaKey: true, shiftKey: true }),
866 keybindings,
867 { platform: "MacIntel" },
868 ),
869 );
870 });
871
872 it("does not let a punctuation position shadow a Latin layout key", () => {
873 const keybindings = compile([
874 { shortcut: modShortcut("m"), command: "diff.toggle" },
875 { shortcut: modShortcut(";"), command: "sidebar.toggle" },
876 ]);
877
878 assert.strictEqual(
879 resolveShortcutCommand(event({ key: "m", code: "Semicolon", metaKey: true }), keybindings, {
880 platform: "MacIntel",
881 }),
882 "diff.toggle",
883 );
884 });
885
886 it("matches Option-modified letters using the physical key code on macOS", () => {
887 assert.strictEqual(
888 resolveShortcutCommand(
889 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
890 DEFAULT_BINDINGS,
891 { platform: "MacIntel" },
892 ),
893 "rightPanel.toggle",
894 );
895 });
896
897 it("matches non-Latin layout letters using the physical key code", () => {
898 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
899
900 assert.strictEqual(
901 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
902 platform: "MacIntel",
903 }),
904 "diff.toggle",
905 );
906 });
907
908 it("ignores the physical key code when the layout types a different Latin letter", () => {
909 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
910
911 // On a remapped layout the physical D key types "a"; only the physical
912 // key whose layout output is "d" may trigger the shortcut.
913 assert.isNull(
914 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
915 platform: "MacIntel",
916 }),
917 );
918 assert.strictEqual(
919 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
920 platform: "MacIntel",
921 }),
922 "diff.toggle",
923 );
924 });
925});
926
927describe("formatShortcutLabel", () => {
928 it("formats labels for macOS", () => {
929 assert.strictEqual(
930 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
931 "⇧⌘D",
932 );
933 });
934
935 it("formats labels for non-macOS", () => {
936 assert.strictEqual(
937 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
938 "Ctrl+Shift+D",
939 );
940 });
941
942 it("formats labels for plus key", () => {
943 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
944 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
945 });
946});
947
948describe("isTerminalClearShortcut", () => {
949 it("matches Ctrl+L on all platforms", () => {
950 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
951 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
952 });
953
954 it("matches Cmd+K on macOS", () => {
955 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
956 });
957
958 it("ignores non-keydown events", () => {
959 assert.isFalse(
960 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
961 );
962 });
963});
964
965describe("terminalDeleteShortcutData", () => {
966 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
967 assert.strictEqual(
968 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
969 "\u0015",
970 );
971 });
972
973 it("ignores non-macOS platforms and modified variants", () => {
974 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
975 assert.isNull(
976 terminalDeleteShortcutData(
977 event({ key: "Backspace", metaKey: true, altKey: true }),
978 "MacIntel",
979 ),
980 );
981 });
982
983 it("ignores non-keydown events", () => {
984 assert.isNull(
985 terminalDeleteShortcutData(
986 event({ type: "keyup", key: "Backspace", metaKey: true }),
987 "MacIntel",
988 ),
989 );
990 });
991});
992
993describe("terminalNavigationShortcutData", () => {
994 it("maps Option+Arrow on macOS to word movement", () => {
995 assert.strictEqual(
996 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
997 "\u001bb",
998 );
999 assert.strictEqual(
1000 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
1001 "\u001bf",
1002 );
1003 });
1004
1005 it("maps Cmd+Arrow on macOS to line movement", () => {
1006 assert.strictEqual(
1007 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
1008 "\u0001",
1009 );
1010 assert.strictEqual(
1011 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
1012 "\u0005",
1013 );
1014 });
1015
1016 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
1017 assert.strictEqual(
1018 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
1019 "\u001bb",
1020 );
1021 assert.strictEqual(
1022 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
1023 "\u001bf",
1024 );
1025 });
1026
1027 it("rejects unsupported combinations", () => {
1028 assert.isNull(
1029 terminalNavigationShortcutData(
1030 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
1031 "MacIntel",
1032 ),
1033 );
1034 assert.isNull(
1035 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
1036 );
1037 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
1038 });
1039
1040 it("ignores non-keydown events", () => {
1041 assert.isNull(
1042 terminalNavigationShortcutData(
1043 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
1044 "MacIntel",
1045 ),
1046 );
1047 });
1048});
1049
1050describe("plus key parsing", () => {
1051 it("matches the plus key shortcut", () => {
1052 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
1053 assert.isTrue(
1054 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
1055 platform: "MacIntel",
1056 }),
1057 );
1058 assert.isTrue(
1059 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
1060 platform: "Linux",
1061 }),
1062 );
1063 });
1064});
1065
1066describe("composer and pull request shortcuts", () => {
1067 it("fills missing number shortcuts without replacing the saved URL binding", () => {
1068 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
1069 (binding) =>
1070 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
1071 );
1072 const bindings = mergeWithDefaultKeybindings([
1073 ...olderServerBindings,
1074 ...compileResolvedKeybindingsConfig([
1075 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
1076 ]),
1077 ]);
1078 for (const [key, command] of [
1079 ["k", "pullRequest.copyNumber"],
1080 ["8", "thread.copyReference"],
1081 ["c", null],
1082 ["y", null],
1083 ] as const) {
1084 assert.strictEqual(
1085 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
1086 platform: "MacIntel",
1087 }),
1088 command,
1089 );
1090 }
1091 });
1092
1093 it.each(["terminalOpen", "previewFocus", "previewOpen", "modelPickerOpen"])(
1094 "honors custom PR shortcut conditions for %s",
1095 (condition) => {
1096 const bindings = compileResolvedKeybindingsConfig([
1097 { key: "mod+shift+k", command: "thread.copyReference", when: condition },
1098 { key: "mod+shift+k", command: "pullRequest.copyNumber", when: `!${condition}` },
1099 ]);
1100 const input = event({ key: "k", ctrlKey: true, shiftKey: true });
1101 for (const enabled of [false, true]) {
1102 assert.strictEqual(
1103 resolveShortcutCommand(input, bindings, {
1104 platform: "Linux",
1105 context: { [condition]: enabled },
1106 }),
1107 enabled ? "thread.copyReference" : "pullRequest.copyNumber",
1108 );
1109 }
1110 },
1111 );
1112
1113 const shortcuts = [
1114 ["h", "composer.host"],
1115 ["e", "composer.effort"],
1116 ["a", "composer.mode"],
1117 ["x", "composer.workspace"],
1118 ["g", "composer.branch"],
1119 ["l", "composer.previousWorktree"],
1120 ["c", "thread.copyReference"],
1121 ["k", "pullRequest.copyNumber"],
1122 ["Enter", "thread.steerQueuedMessage"],
1123 ] as const;
1124
1125 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1126 it.each(shortcuts)(
1127 `resolves %s on ${platform} and leaves terminal input alone`,
1128 (key, command) => {
1129 const input = event({
1130 key,
1131 shiftKey: true,
1132 metaKey: platform === "MacIntel",
1133 ctrlKey: platform !== "MacIntel",
1134 });
1135 assert.strictEqual(
1136 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1137 platform,
1138 context: { terminalFocus: false },
1139 }),
1140 command,
1141 );
1142 assert.isNull(
1143 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1144 platform,
1145 context: { terminalFocus: true },
1146 }),
1147 );
1148 },
1149 );
1150 }
1151
1152 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1153 it.each([
1154 ["s", "thread.settle"],
1155 ["p", "thread.pin"],
1156 ])(`preserves the existing %s shortcut on ${platform}`, (key, command) => {
1157 assert.strictEqual(
1158 resolveShortcutCommand(
1159 event({
1160 key,
1161 shiftKey: true,
1162 metaKey: platform === "MacIntel",
1163 ctrlKey: platform !== "MacIntel",
1164 }),
1165 DEFAULT_RESOLVED_KEYBINDINGS,
1166 { platform },
1167 ),
1168 command,
1169 );
1170 });
1171 }
1172
1173 const altEffortBindings = compileResolvedKeybindingsConfig([
1174 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
1175 ]);
1176
1177 it("leaves AltGr text entry alone with a custom Alt binding", () => {
1178 for (const platform of ["Win32", "Linux"]) {
1179 const input = event({
1180 key: "€",
1181 code: "KeyE",
1182 ctrlKey: true,
1183 altKey: true,
1184 getModifierState: (key) => key === "AltGraph",
1185 });
1186 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
1187 assert.strictEqual(
1188 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
1189 platform,
1190 }),
1191 "composer.effort",
1192 );
1193 }
1194 });
1195
1196 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
1197 const getModifierState = (key: string) => key === "AltGraph";
1198 assert.strictEqual(
1199 resolveShortcutCommand(
1200 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
1201 altEffortBindings,
1202 { platform: "Win32" },
1203 ),
1204 "composer.effort",
1205 );
1206 assert.strictEqual(
1207 resolveShortcutCommand(
1208 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
1209 altEffortBindings,
1210 { platform: "MacIntel" },
1211 ),
1212 "composer.effort",
1213 );
1214 });
1215
1216 it.each(shortcuts)("uses a custom binding for %s", (_key, command) => {
1217 const bindings = compileResolvedKeybindingsConfig([
1218 { key: "mod+shift+y", command, when: "!terminalFocus" },
1219 ]);
1220 assert.strictEqual(
1221 resolveShortcutCommand(
1222 event({ key: "Y", code: "KeyY", ctrlKey: true, shiftKey: true }),
1223 bindings,
1224 { platform: "Linux" },
1225 ),
1226 command,
1227 );
1228 });
1229
1230 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1231 it.each([
1232 ["ArrowUp", "modelPicker.previousProvider"],
1233 ["ArrowDown", "modelPicker.nextProvider"],
1234 ] as const)(`limits %s to the model picker on ${platform}`, (key, command) => {
1235 const input = event({
1236 key,
1237 shiftKey: true,
1238 metaKey: platform === "MacIntel",
1239 ctrlKey: platform !== "MacIntel",
1240 });
1241 assert.strictEqual(
1242 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1243 platform,
1244 context: { modelPickerOpen: true },
1245 }),
1246 command,
1247 );
1248 assert.isNull(
1249 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1250 platform,
1251 context: { modelPickerOpen: false },
1252 }),
1253 );
1254 });
1255 }
1256});