Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keybindings.test.ts

1,275 lines38.0 KB

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 };
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 };
62​
63function whenIdentifier(name: string): KeybindingWhenNode {
64 return { type: "identifier", name };
66​
67function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
68 return { type: "not", node };
70​
71function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
72 return { type: "and", left, right };
74​
75interface TestBinding {
76 shortcut: KeybindingShortcut;
77 command: KeybindingCommand;
78 whenAst?: KeybindingWhenNode;
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 }));
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 assert.isFalse(
593 isOpenFavoriteEditorShortcut(
594 event({ key: "o", metaKey: true, repeat: true }),
595 DEFAULT_BINDINGS,
596 { platform: "MacIntel" },
597 ),
598 );
599 });
600​
601 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
602 assert.strictEqual(
603 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
604 platform: "MacIntel",
605 context: { terminalFocus: false },
606 }),
607 "commandPalette.toggle",
608 );
609 assert.notStrictEqual(
610 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
611 platform: "MacIntel",
612 context: { terminalFocus: true },
613 }),
614 "commandPalette.toggle",
615 );
616 });
617​
618 it("resolves a user-configured thread panel shortcut without assigning a default", () => {
619 const bindings = compile([
620 { shortcut: modShortcut("b", { shiftKey: true }), command: "threadPanel.toggle" },
621 ]);
622​
623 assert.strictEqual(
624 resolveShortcutCommand(event({ key: "b", metaKey: true, shiftKey: true }), bindings, {
625 platform: "MacIntel",
626 }),
627 "threadPanel.toggle",
628 );
629 });
630​
631 it("matches themeEditor.toggle on macOS and Windows", () => {
632 assert.strictEqual(
633 resolveShortcutCommand(
634 event({ key: "t", metaKey: true, altKey: true, shiftKey: true }),
635 DEFAULT_BINDINGS,
636 { platform: "MacIntel" },
637 ),
638 "themeEditor.toggle",
639 );
640 assert.strictEqual(
641 resolveShortcutCommand(
642 event({ key: "t", ctrlKey: true, altKey: true, shiftKey: true }),
643 DEFAULT_BINDINGS,
644 { platform: "Win32" },
645 ),
646 "themeEditor.toggle",
647 );
648 });
649​
650 it("matches filePicker.toggle shortcut outside terminal focus", () => {
651 assert.strictEqual(
652 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
653 platform: "MacIntel",
654 context: { terminalFocus: false },
655 }),
656 "filePicker.toggle",
657 );
658 assert.notStrictEqual(
659 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
660 platform: "MacIntel",
661 context: { terminalFocus: true },
662 }),
663 "filePicker.toggle",
664 );
665 });
666​
667 it("matches projectSearch.toggle shortcut outside terminal focus", () => {
668 assert.strictEqual(
669 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
670 platform: "MacIntel",
671 context: { terminalFocus: false },
672 }),
673 "projectSearch.toggle",
674 );
675 assert.notStrictEqual(
676 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
677 platform: "MacIntel",
678 context: { terminalFocus: true },
679 }),
680 "projectSearch.toggle",
681 );
682 });
683​
684 it("matches diff.toggle shortcut outside terminal focus", () => {
685 assert.isTrue(
686 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
687 platform: "MacIntel",
688 context: { terminalFocus: false },
689 }),
690 );
691 assert.isFalse(
692 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
693 platform: "MacIntel",
694 context: { terminalFocus: true },
695 }),
696 );
697 });
698});
699​
700describe("cross-command precedence", () => {
701 it("uses when + order so a later focused rule overrides a global rule", () => {
702 const keybindings = compile([
703 { shortcut: modShortcut("n"), command: "chat.new" },
704 {
705 shortcut: modShortcut("n"),
706 command: "terminal.new",
707 whenAst: whenIdentifier("terminalFocus"),
708 },
709 ]);
710​
711 assert.isTrue(
712 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
713 platform: "MacIntel",
714 context: { terminalFocus: true },
715 }),
716 );
717 assert.strictEqual(
718 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
719 platform: "MacIntel",
720 context: { terminalFocus: true },
721 }),
722 "terminal.new",
723 );
724 assert.isFalse(
725 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
726 platform: "MacIntel",
727 context: { terminalFocus: false },
728 }),
729 );
730 assert.strictEqual(
731 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
732 platform: "MacIntel",
733 context: { terminalFocus: false },
734 }),
735 "chat.new",
736 );
737 });
738​
739 it("still lets a later global rule win when both rules match", () => {
740 const keybindings = compile([
741 {
742 shortcut: modShortcut("n"),
743 command: "terminal.new",
744 whenAst: whenIdentifier("terminalFocus"),
745 },
746 { shortcut: modShortcut("n"), command: "chat.new" },
747 ]);
748​
749 assert.isFalse(
750 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
751 platform: "Linux",
752 context: { terminalFocus: true },
753 }),
754 );
755 assert.strictEqual(
756 resolveShortcutCommand(event({ key: "n", ctrlKey: true }), keybindings, {
757 platform: "Linux",
758 context: { terminalFocus: true },
759 }),
760 "chat.new",
761 );
762 });
763});
764​
765describe("resolveShortcutCommand", () => {
766 it("resolves a custom stop-thread shortcut", () => {
767 const keybindings = compile([{ shortcut: modShortcut("escape"), command: "thread.stop" }]);
768​
769 assert.strictEqual(
770 resolveShortcutCommand(event({ key: "Escape", metaKey: true }), keybindings, {
771 platform: "MacIntel",
772 }),
773 "thread.stop",
774 );
775 });
776​
777 it("honors preview conditions for a stop-thread shortcut", () => {
778 const keybindings = compile([
779 {
780 shortcut: modShortcut("escape"),
781 command: "thread.stop",
782 whenAst: whenIdentifier("previewFocus"),
783 },
784 ]);
785 const input = event({ key: "Escape", metaKey: true });
786​
787 assert.isNull(
788 resolveShortcutCommand(input, keybindings, {
789 platform: "MacIntel",
790 context: { previewFocus: false },
791 }),
792 );
793 assert.strictEqual(
794 resolveShortcutCommand(input, keybindings, {
795 platform: "MacIntel",
796 context: { previewFocus: true },
797 }),
798 "thread.stop",
799 );
800 });
801​
802 it("returns dynamic script commands", () => {
803 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
804​
805 assert.strictEqual(
806 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
807 platform: "Linux",
808 }),
809 "script.setup.run",
810 );
811 });
812​
813 it("routes mod+w to the terminal while focused and to the right panel otherwise", () => {
814 const closeEvent = event({ key: "w", metaKey: true });
815 assert.strictEqual(
816 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
817 platform: "MacIntel",
818 context: { terminalFocus: true },
819 }),
820 "terminal.close",
821 );
822 assert.strictEqual(
823 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
824 platform: "MacIntel",
825 context: { terminalFocus: false },
826 }),
827 "rightPanel.close",
828 );
829 });
830​
831 it("resolves a custom right panel maximize binding", () => {
832 const keybindings = compile([
833 {
834 shortcut: modShortcut("m", { shiftKey: true }),
835 command: "rightPanel.toggleMaximized",
836 },
837 ]);
838​
839 assert.strictEqual(
840 resolveShortcutCommand(event({ key: "m", metaKey: true, shiftKey: true }), keybindings, {
841 platform: "MacIntel",
842 }),
843 "rightPanel.toggleMaximized",
844 );
845 });
846​
847 it("matches bracket shortcuts using the physical key code", () => {
848 assert.strictEqual(
849 resolveShortcutCommand(
850 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
851 DEFAULT_BINDINGS,
852 {
853 platform: "MacIntel",
854 },
855 ),
856 "thread.previous",
857 );
858 assert.strictEqual(
859 resolveShortcutCommand(
860 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
861 DEFAULT_BINDINGS,
862 {
863 platform: "Linux",
864 },
865 ),
866 "thread.next",
867 );
868 });
869​
870 it("matches punctuation shortcuts by physical key across keyboard layouts", () => {
871 const keybindings = compile([
872 { shortcut: modShortcut("'", { shiftKey: true }), command: "diff.toggle" },
873 ]);
874​
875 assert.strictEqual(
876 resolveShortcutCommand(
877 event({ key: "@", code: "Quote", metaKey: true, shiftKey: true }),
878 keybindings,
879 { platform: "MacIntel" },
880 ),
881 "diff.toggle",
882 );
883 assert.isNull(
884 resolveShortcutCommand(
885 event({ key: '"', code: "Digit2", metaKey: true, shiftKey: true }),
886 keybindings,
887 { platform: "MacIntel" },
888 ),
889 );
890 });
891​
892 it("does not let a punctuation position shadow a Latin layout key", () => {
893 const keybindings = compile([
894 { shortcut: modShortcut("m"), command: "diff.toggle" },
895 { shortcut: modShortcut(";"), command: "sidebar.toggle" },
896 ]);
897​
898 assert.strictEqual(
899 resolveShortcutCommand(event({ key: "m", code: "Semicolon", metaKey: true }), keybindings, {
900 platform: "MacIntel",
901 }),
902 "diff.toggle",
903 );
904 });
905​
906 it("matches Option-modified letters using the physical key code on macOS", () => {
907 assert.strictEqual(
908 resolveShortcutCommand(
909 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
910 DEFAULT_BINDINGS,
911 { platform: "MacIntel" },
912 ),
913 "rightPanel.toggle",
914 );
915 });
916​
917 it("matches non-Latin layout letters using the physical key code", () => {
918 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
919​
920 assert.strictEqual(
921 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
922 platform: "MacIntel",
923 }),
924 "diff.toggle",
925 );
926 });
927​
928 it("ignores the physical key code when the layout types a different Latin letter", () => {
929 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
930​
931 // On a remapped layout the physical D key types "a"; only the physical
932 // key whose layout output is "d" may trigger the shortcut.
933 assert.isNull(
934 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
935 platform: "MacIntel",
936 }),
937 );
938 assert.strictEqual(
939 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
940 platform: "MacIntel",
941 }),
942 "diff.toggle",
943 );
944 });
945});
946​
947describe("formatShortcutLabel", () => {
948 it("formats labels for macOS", () => {
949 assert.strictEqual(
950 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
951 "⇧⌘D",
952 );
953 });
954​
955 it("formats labels for non-macOS", () => {
956 assert.strictEqual(
957 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
958 "Ctrl+Shift+D",
959 );
960 });
961​
962 it("formats labels for plus key", () => {
963 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
964 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
965 });
966});
967​
968describe("isTerminalClearShortcut", () => {
969 it("matches Ctrl+L on all platforms", () => {
970 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
971 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
972 });
973​
974 it("matches Cmd+K on macOS", () => {
975 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
976 });
977​
978 it("ignores non-keydown events", () => {
979 assert.isFalse(
980 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
981 );
982 });
983});
984​
985describe("terminalDeleteShortcutData", () => {
986 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
987 assert.strictEqual(
988 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
989 "\u0015",
990 );
991 });
992​
993 it("ignores non-macOS platforms and modified variants", () => {
994 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
995 assert.isNull(
996 terminalDeleteShortcutData(
997 event({ key: "Backspace", metaKey: true, altKey: true }),
998 "MacIntel",
999 ),
1000 );
1001 });
1002​
1003 it("ignores non-keydown events", () => {
1004 assert.isNull(
1005 terminalDeleteShortcutData(
1006 event({ type: "keyup", key: "Backspace", metaKey: true }),
1007 "MacIntel",
1008 ),
1009 );
1010 });
1011});
1012​
1013describe("terminalNavigationShortcutData", () => {
1014 it("maps Option+Arrow on macOS to word movement", () => {
1015 assert.strictEqual(
1016 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
1017 "\u001bb",
1018 );
1019 assert.strictEqual(
1020 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
1021 "\u001bf",
1022 );
1023 });
1024​
1025 it("maps Cmd+Arrow on macOS to line movement", () => {
1026 assert.strictEqual(
1027 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
1028 "\u0001",
1029 );
1030 assert.strictEqual(
1031 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
1032 "\u0005",
1033 );
1034 });
1035​
1036 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
1037 assert.strictEqual(
1038 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
1039 "\u001bb",
1040 );
1041 assert.strictEqual(
1042 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
1043 "\u001bf",
1044 );
1045 });
1046​
1047 it("rejects unsupported combinations", () => {
1048 assert.isNull(
1049 terminalNavigationShortcutData(
1050 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
1051 "MacIntel",
1052 ),
1053 );
1054 assert.isNull(
1055 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
1056 );
1057 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
1058 });
1059​
1060 it("ignores non-keydown events", () => {
1061 assert.isNull(
1062 terminalNavigationShortcutData(
1063 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
1064 "MacIntel",
1065 ),
1066 );
1067 });
1068});
1069​
1070describe("plus key parsing", () => {
1071 it("matches the plus key shortcut", () => {
1072 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
1073 assert.isTrue(
1074 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
1075 platform: "MacIntel",
1076 }),
1077 );
1078 assert.isTrue(
1079 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
1080 platform: "Linux",
1081 }),
1082 );
1083 });
1084});
1085​
1086describe("composer and pull request shortcuts", () => {
1087 it("fills missing number shortcuts without replacing the saved URL binding", () => {
1088 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
1089 (binding) =>
1090 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
1091 );
1092 const bindings = mergeWithDefaultKeybindings([
1093 ...olderServerBindings,
1094 ...compileResolvedKeybindingsConfig([
1095 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
1096 ]),
1097 ]);
1098 for (const [key, command] of [
1099 ["k", "pullRequest.copyNumber"],
1100 ["8", "thread.copyReference"],
1101 ["c", null],
1102 ["y", null],
1103 ] as const) {
1104 assert.strictEqual(
1105 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
1106 platform: "MacIntel",
1107 }),
1108 command,
1109 );
1111 });
1112​
1113 it.each(["terminalOpen", "previewFocus", "previewOpen", "modelPickerOpen"])(
1114 "honors custom PR shortcut conditions for %s",
1115 (condition) => {
1116 const bindings = compileResolvedKeybindingsConfig([
1117 { key: "mod+shift+k", command: "thread.copyReference", when: condition },
1118 { key: "mod+shift+k", command: "pullRequest.copyNumber", when: `!${condition}` },
1119 ]);
1120 const input = event({ key: "k", ctrlKey: true, shiftKey: true });
1121 for (const enabled of [false, true]) {
1122 assert.strictEqual(
1123 resolveShortcutCommand(input, bindings, {
1124 platform: "Linux",
1125 context: { [condition]: enabled },
1126 }),
1127 enabled ? "thread.copyReference" : "pullRequest.copyNumber",
1128 );
1130 },
1131 );
1132​
1133 const shortcuts = [
1134 ["h", "composer.host"],
1135 ["e", "composer.effort"],
1136 ["a", "composer.mode"],
1137 ["x", "composer.workspace"],
1138 ["g", "composer.branch"],
1139 ["l", "composer.previousWorktree"],
1140 ["c", "thread.copyReference"],
1141 ["k", "pullRequest.copyNumber"],
1142 ] as const;
1143​
1144 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1145 it.each(shortcuts)(
1146 `resolves %s on ${platform} and leaves terminal input alone`,
1147 (key, command) => {
1148 const input = event({
1149 key,
1150 shiftKey: true,
1151 metaKey: platform === "MacIntel",
1152 ctrlKey: platform !== "MacIntel",
1153 });
1154 assert.strictEqual(
1155 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1156 platform,
1157 context: { terminalFocus: false },
1158 }),
1159 command,
1160 );
1161 assert.isNull(
1162 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1163 platform,
1164 context: { terminalFocus: true },
1165 }),
1166 );
1167 },
1168 );
1170​
1171 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1172 it.each([
1173 ["s", "thread.settle"],
1174 ["p", "thread.pin"],
1175 ])(`preserves the existing %s shortcut on ${platform}`, (key, command) => {
1176 assert.strictEqual(
1177 resolveShortcutCommand(
1178 event({
1179 key,
1180 shiftKey: true,
1181 metaKey: platform === "MacIntel",
1182 ctrlKey: platform !== "MacIntel",
1183 }),
1184 DEFAULT_RESOLVED_KEYBINDINGS,
1185 { platform },
1186 ),
1187 command,
1188 );
1189 });
1191​
1192 const altEffortBindings = compileResolvedKeybindingsConfig([
1193 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
1194 ]);
1195​
1196 it("leaves AltGr text entry alone with a custom Alt binding", () => {
1197 for (const platform of ["Win32", "Linux"]) {
1198 const input = event({
1199 key: "€",
1200 code: "KeyE",
1201 ctrlKey: true,
1202 altKey: true,
1203 getModifierState: (key) => key === "AltGraph",
1204 });
1205 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
1206 assert.strictEqual(
1207 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
1208 platform,
1209 }),
1210 "composer.effort",
1211 );
1213 });
1214​
1215 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
1216 const getModifierState = (key: string) => key === "AltGraph";
1217 assert.strictEqual(
1218 resolveShortcutCommand(
1219 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
1220 altEffortBindings,
1221 { platform: "Win32" },
1222 ),
1223 "composer.effort",
1224 );
1225 assert.strictEqual(
1226 resolveShortcutCommand(
1227 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
1228 altEffortBindings,
1229 { platform: "MacIntel" },
1230 ),
1231 "composer.effort",
1232 );
1233 });
1234​
1235 it.each(shortcuts)("uses a custom binding for %s", (_key, command) => {
1236 const bindings = compileResolvedKeybindingsConfig([
1237 { key: "mod+shift+y", command, when: "!terminalFocus" },
1238 ]);
1239 assert.strictEqual(
1240 resolveShortcutCommand(
1241 event({ key: "Y", code: "KeyY", ctrlKey: true, shiftKey: true }),
1242 bindings,
1243 { platform: "Linux" },
1244 ),
1245 command,
1246 );
1247 });
1248​
1249 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1250 it.each([
1251 ["ArrowUp", "modelPicker.previousProvider"],
1252 ["ArrowDown", "modelPicker.nextProvider"],
1253 ] as const)(`limits %s to the model picker on ${platform}`, (key, command) => {
1254 const input = event({
1255 key,
1256 shiftKey: true,
1257 metaKey: platform === "MacIntel",
1258 ctrlKey: platform !== "MacIntel",
1259 });
1260 assert.strictEqual(
1261 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1262 platform,
1263 context: { modelPickerOpen: true },
1264 }),
1265 command,
1266 );
1267 assert.isNull(
1268 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1269 platform,
1270 context: { modelPickerOpen: false },
1271 }),
1272 );
1273 });
1275});