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