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

keybindings.test.ts

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