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

keybindings.test.ts

1,085 lines33.2 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 effectiveShortcutsForCommand,
16 formatShortcutLabel,
17 isRichTextBoldShortcut,
18 isSidebarToggleShortcut,
19 isTerminalNewShortcut,
20 isTerminalSplitShortcut,
21 resolveChatShortcutCommand,
22 resolveShortcutCommand,
23 shouldShowThreadJumpHintsForModifiers,
24 shortcutLabelForCommand,
25 terminalNavigationShortcutData,
26 type ShortcutEventLike,
27} from "./keybindings";
28​
29function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
30 return {
31 key: "j",
32 metaKey: false,
33 ctrlKey: false,
34 shiftKey: false,
35 altKey: false,
36 ...overrides,
37 };
39​
40describe("terminal word navigation", () => {
41 it.each(["Win32", "Linux x86_64"])(
42 "leaves Ctrl+Arrow to the terminal encoder on %s",
43 (platform) => {
44 for (const key of ["ArrowLeft", "ArrowRight"]) {
45 assert.equal(terminalNavigationShortcutData(event({ key, ctrlKey: true }), platform), null);
46 }
47 },
48 );
49​
50 it.each(["Win32", "Linux x86_64", "MacIntel"])(
51 "preserves Alt+Arrow word navigation on %s",
52 (platform) => {
53 assert.equal(
54 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), platform),
55 "\u001bb",
56 );
57 assert.equal(
58 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), platform),
59 "\u001bf",
60 );
61 },
62 );
63});
64​
65function modShortcut(
66 key: string,
67 overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
68): KeybindingShortcut {
69 return {
70 key,
71 metaKey: false,
72 ctrlKey: false,
73 shiftKey: false,
74 altKey: false,
75 modKey: true,
76 ...overrides,
77 };
79​
80function whenIdentifier(name: string): KeybindingWhenNode {
81 return { type: "identifier", name };
83​
84function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
85 return { type: "not", node };
87​
88function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
89 return { type: "and", left, right };
91​
92interface TestBinding {
93 shortcut: KeybindingShortcut;
94 command: KeybindingCommand;
95 whenAst?: KeybindingWhenNode;
97​
98function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
99 return bindings.map((binding) => ({
100 command: binding.command,
101 shortcut: binding.shortcut,
102 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
103 }));
105​
106const DEFAULT_BINDINGS = compile([
107 { shortcut: modShortcut("b"), command: "sidebar.toggle" },
108 { shortcut: modShortcut("j"), command: "terminal.toggle" },
109 { shortcut: modShortcut("b", { altKey: true }), command: "rightPanel.toggle" },
110 {
111 shortcut: modShortcut("d"),
112 command: "terminal.split",
113 whenAst: whenIdentifier("terminalFocus"),
114 },
115 {
116 shortcut: modShortcut("d", { shiftKey: true }),
117 command: "terminal.splitVertical",
118 whenAst: whenIdentifier("terminalFocus"),
119 },
120 {
121 shortcut: modShortcut("n"),
122 command: "terminal.new",
123 whenAst: whenIdentifier("terminalFocus"),
124 },
125 {
126 shortcut: modShortcut("w"),
127 command: "terminal.close",
128 whenAst: whenIdentifier("terminalFocus"),
129 },
130 {
131 shortcut: modShortcut("w"),
132 command: "rightPanel.close",
133 whenAst: whenNot(whenIdentifier("terminalFocus")),
134 },
135 {
136 shortcut: modShortcut("d"),
137 command: "diff.toggle",
138 whenAst: whenNot(whenIdentifier("terminalFocus")),
139 },
140 {
141 shortcut: modShortcut("k"),
142 command: "commandPalette.toggle",
143 whenAst: whenNot(whenIdentifier("terminalFocus")),
144 },
145 {
146 shortcut: modShortcut("p"),
147 command: "filePicker.toggle",
148 whenAst: whenNot(whenIdentifier("terminalFocus")),
149 },
150 {
151 shortcut: modShortcut("f", { shiftKey: true }),
152 command: "projectSearch.toggle",
153 whenAst: whenNot(whenIdentifier("terminalFocus")),
154 },
155 {
156 shortcut: modShortcut("t", { altKey: true, shiftKey: true }),
157 command: "themeEditor.toggle",
158 },
159 {
160 shortcut: modShortcut("f"),
161 command: "chat.find",
162 whenAst: whenAnd(
163 whenNot(whenIdentifier("terminalFocus")),
164 whenNot(whenIdentifier("previewFocus")),
165 ),
166 },
167 {
168 shortcut: modShortcut("m", { shiftKey: true }),
169 command: "modelPicker.toggle",
170 whenAst: whenNot(whenIdentifier("terminalFocus")),
171 },
172 { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
173 { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
174 { shortcut: modShortcut("o"), command: "editor.openFavorite" },
175 { shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },
176 { shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },
177 {
178 shortcut: modShortcut("c", { shiftKey: true }),
179 command: "thread.copyReference",
180 whenAst: whenNot(whenIdentifier("terminalFocus")),
181 },
182 {
183 shortcut: modShortcut("s", { shiftKey: true }),
184 command: "thread.settle",
185 whenAst: whenNot(whenIdentifier("terminalFocus")),
186 },
187 { shortcut: modShortcut("1"), command: "thread.jump.1" },
188 { shortcut: modShortcut("2"), command: "thread.jump.2" },
189 { shortcut: modShortcut("3"), command: "thread.jump.3" },
190 {
191 shortcut: modShortcut("1"),
192 command: "modelPicker.jump.1",
193 whenAst: whenIdentifier("modelPickerOpen"),
194 },
195 {
196 shortcut: modShortcut("2"),
197 command: "modelPicker.jump.2",
198 whenAst: whenIdentifier("modelPickerOpen"),
199 },
200 {
201 shortcut: modShortcut("3"),
202 command: "modelPicker.jump.3",
203 whenAst: whenIdentifier("modelPickerOpen"),
204 },
205]);
206​
207describe("effectiveShortcutsForCommand", () => {
208 it("passes only effective preview shortcuts to the desktop bridge", () => {
209 const reopen = modShortcut("t", { shiftKey: true });
210 const second = modShortcut("y", { shiftKey: true });
211 const keybindings = compile([
212 { shortcut: reopen, command: "view.reopenClosed" },
213 {
214 shortcut: second,
215 command: "view.reopenClosed",
216 whenAst: whenIdentifier("previewFocus"),
217 },
218 {
219 shortcut: reopen,
220 command: "preview.toggle",
221 whenAst: whenIdentifier("previewFocus"),
222 },
223 ]);
224 assert.deepEqual(
225 effectiveShortcutsForCommand(keybindings, "view.reopenClosed", {
226 platform: "MacIntel",
227 context: { previewFocus: true, previewOpen: true },
228 }),
229 [second],
230 );
231 assert.deepEqual(
232 effectiveShortcutsForCommand(keybindings, "view.reopenClosed", {
233 platform: "MacIntel",
234 context: { previewFocus: false },
235 }),
236 [reopen],
237 );
238 });
239});
240​
241describe("settle thread shortcut", () => {
242 it("does not intercept the terminal", () => {
243 assert.isNull(
244 resolveShortcutCommand(event({ key: "s", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
245 platform: "Win32",
246 context: { terminalFocus: true },
247 }),
248 );
249 });
250});
251​
252describe("thread undo shortcut", () => {
253 it("leaves native undo alone inside text fields and terminals", () => {
254 assert.isNull(
255 resolveShortcutCommand(event({ key: "z", ctrlKey: true }), DEFAULT_RESOLVED_KEYBINDINGS, {
256 platform: "Win32",
257 context: { editableFocus: true },
258 }),
259 );
260 assert.isNull(
261 resolveShortcutCommand(event({ key: "z", ctrlKey: true }), DEFAULT_RESOLVED_KEYBINDINGS, {
262 platform: "Win32",
263 context: { terminalFocus: true },
264 }),
265 );
266 });
267});
268​
269describe("split/new/close terminal shortcuts", () => {
270 it("supports when expressions", () => {
271 const keybindings = compile([
272 {
273 shortcut: modShortcut("\\"),
274 command: "terminal.split",
275 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
276 },
277 {
278 shortcut: modShortcut("n", { shiftKey: true }),
279 command: "terminal.new",
280 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
281 },
282 { shortcut: modShortcut("j"), command: "terminal.toggle" },
283 ]);
284 assert.isTrue(
285 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
286 platform: "Win32",
287 context: { terminalOpen: true, terminalFocus: false },
288 }),
289 );
290 assert.isFalse(
291 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
292 platform: "Win32",
293 context: { terminalOpen: false, terminalFocus: false },
294 }),
295 );
296 assert.isTrue(
297 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
298 platform: "Win32",
299 context: { terminalOpen: true, terminalFocus: false },
300 }),
301 );
302 });
303});
304​
305describe("shortcutLabelForCommand", () => {
306 it("returns null for commands shadowed by a later conflicting shortcut", () => {
307 const bindings = compile([
308 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.1" },
309 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.7" },
310 ]);
311​
312 assert.isNull(shortcutLabelForCommand(bindings, "thread.jump.1", "MacIntel"));
313 assert.strictEqual(shortcutLabelForCommand(bindings, "thread.jump.7", "MacIntel"), "⇧⌘1");
314 });
315​
316 it("respects when-context while resolving labels", () => {
317 const bindings = compile([
318 { shortcut: modShortcut("d"), command: "diff.toggle" },
319 {
320 shortcut: modShortcut("d"),
321 command: "terminal.split",
322 whenAst: whenIdentifier("terminalFocus"),
323 },
324 ]);
325​
326 assert.strictEqual(
327 shortcutLabelForCommand(bindings, "diff.toggle", {
328 platform: "Linux",
329 context: { terminalFocus: false },
330 }),
331 "Ctrl+D",
332 );
333 assert.isNull(
334 shortcutLabelForCommand(bindings, "diff.toggle", {
335 platform: "Linux",
336 context: { terminalFocus: true },
337 }),
338 );
339 assert.strictEqual(
340 shortcutLabelForCommand(bindings, "terminal.split", {
341 platform: "Linux",
342 context: { terminalFocus: true },
343 }),
344 "Ctrl+D",
345 );
346 });
347});
348​
349describe("thread navigation helpers", () => {
350 it("never shows jump hints while the terminal is focused, even with an unrestricted binding", () => {
351 assert.isFalse(
352 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
353 platform: "MacIntel",
354 context: { terminalFocus: true },
355 }),
356 );
357 assert.isTrue(
358 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
359 platform: "MacIntel",
360 context: { terminalFocus: false },
361 }),
362 );
363 });
364​
365 it("keeps default thread jumps off the web so the browser can switch tabs", () => {
366 const input = event({ key: "1", metaKey: true });
367 assert.isNull(
368 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
369 platform: "MacIntel",
370 context: { isDesktop: false },
371 }),
372 );
373 assert.strictEqual(
374 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
375 platform: "MacIntel",
376 context: { isDesktop: true },
377 }),
378 "thread.jump.1",
379 );
380 assert.isFalse(
381 shouldShowThreadJumpHintsForModifiers(
382 event({ metaKey: true }),
383 DEFAULT_RESOLVED_KEYBINDINGS,
384 {
385 platform: "MacIntel",
386 context: { isDesktop: false },
387 },
388 ),
389 );
390 assert.isTrue(
391 shouldShowThreadJumpHintsForModifiers(
392 event({ metaKey: true }),
393 DEFAULT_RESOLVED_KEYBINDINGS,
394 {
395 platform: "MacIntel",
396 context: { isDesktop: true },
397 },
398 ),
399 );
400 });
401});
402​
403describe("model picker navigation helpers", () => {
404 it("keeps default model jumps off the web even while the picker is open", () => {
405 const input = event({ key: "3", metaKey: true });
406 assert.isNull(
407 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
408 platform: "MacIntel",
409 context: { isDesktop: false, modelPickerOpen: true },
410 }),
411 );
412 assert.strictEqual(
413 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
414 platform: "MacIntel",
415 context: { isDesktop: true, modelPickerOpen: true },
416 }),
417 "modelPicker.jump.3",
418 );
419 assert.strictEqual(
420 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
421 platform: "MacIntel",
422 context: { isDesktop: true, modelPickerOpen: false },
423 }),
424 "thread.jump.3",
425 );
426 });
427});
428​
429describe("chat/editor shortcuts", () => {
430 it("toggles the thread panel by default without taking terminal keystrokes", () => {
431 for (const platform of ["MacIntel", "Win32", "Linux"]) {
432 const input = event({
433 key: "b",
434 metaKey: platform === "MacIntel",
435 ctrlKey: platform !== "MacIntel",
436 shiftKey: true,
437 });
438 assert.strictEqual(
439 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
440 platform,
441 context: { terminalFocus: false, editableFocus: true },
442 }),
443 "threadPanel.toggle",
444 );
445 assert.isNull(
446 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
447 platform,
448 context: { terminalFocus: true },
449 }),
450 );
451 }
452 });
453​
454 it("preserves a user-configured thread panel shortcut instead of its default", () => {
455 const bindings = mergeWithDefaultKeybindings(
456 compile([
457 {
458 shortcut: modShortcut("b", { altKey: true, shiftKey: true }),
459 command: "threadPanel.toggle",
460 },
461 ]),
462 );
463​
464 assert.strictEqual(
465 resolveShortcutCommand(
466 event({ key: "b", metaKey: true, altKey: true, shiftKey: true }),
467 bindings,
468 { platform: "MacIntel" },
469 ),
470 "threadPanel.toggle",
471 );
472 assert.isNull(
473 resolveShortcutCommand(event({ key: "b", metaKey: true, shiftKey: true }), bindings, {
474 platform: "MacIntel",
475 }),
476 );
477 });
478});
479​
480describe("cross-command precedence", () => {
481 it("uses when + order so a later focused rule overrides a global rule", () => {
482 const keybindings = compile([
483 { shortcut: modShortcut("n"), command: "chat.new" },
484 {
485 shortcut: modShortcut("n"),
486 command: "terminal.new",
487 whenAst: whenIdentifier("terminalFocus"),
488 },
489 ]);
490​
491 assert.isTrue(
492 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
493 platform: "MacIntel",
494 context: { terminalFocus: true },
495 }),
496 );
497 assert.strictEqual(
498 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
499 platform: "MacIntel",
500 context: { terminalFocus: true },
501 }),
502 "terminal.new",
503 );
504 assert.isFalse(
505 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
506 platform: "MacIntel",
507 context: { terminalFocus: false },
508 }),
509 );
510 assert.strictEqual(
511 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
512 platform: "MacIntel",
513 context: { terminalFocus: false },
514 }),
515 "chat.new",
516 );
517 });
518​
519 it("still lets a later global rule win when both rules match", () => {
520 const keybindings = compile([
521 {
522 shortcut: modShortcut("n"),
523 command: "terminal.new",
524 whenAst: whenIdentifier("terminalFocus"),
525 },
526 { shortcut: modShortcut("n"), command: "chat.new" },
527 ]);
528​
529 assert.isFalse(
530 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
531 platform: "Linux",
532 context: { terminalFocus: true },
533 }),
534 );
535 assert.strictEqual(
536 resolveShortcutCommand(event({ key: "n", ctrlKey: true }), keybindings, {
537 platform: "Linux",
538 context: { terminalFocus: true },
539 }),
540 "chat.new",
541 );
542 });
543});
544​
545describe("resolveChatShortcutCommand", () => {
546 it("runs the active environment's script shortcut instead of the primary environment's", () => {
547 const primary = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
548 const active = compile([{ shortcut: modShortcut("t"), command: "script.setup.run" }]);
549​
550 assert.strictEqual(
551 resolveChatShortcutCommand(event({ key: "t", ctrlKey: true }), primary, active, {
552 platform: "Linux",
553 }),
554 "script.setup.run",
555 );
556 assert.isNull(
557 resolveChatShortcutCommand(event({ key: "r", ctrlKey: true }), primary, active, {
558 platform: "Linux",
559 }),
560 );
561 });
562​
563 it("does not run primary scripts before the active environment's bindings arrive", () => {
564 const primary = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
565​
566 assert.isNull(
567 resolveChatShortcutCommand(event({ key: "r", ctrlKey: true }), primary, [], {
568 platform: "Linux",
569 }),
570 );
571 });
572​
573 it("keeps primary app commands ahead of conflicting active script shortcuts", () => {
574 const primary = compile([{ shortcut: modShortcut("k"), command: "commandPalette.toggle" }]);
575 const active = compile([{ shortcut: modShortcut("k"), command: "script.setup.run" }]);
576​
577 assert.strictEqual(
578 resolveChatShortcutCommand(event({ key: "k", ctrlKey: true }), primary, active, {
579 platform: "Linux",
580 }),
581 "commandPalette.toggle",
582 );
583 });
584​
585 it("keeps app shortcuts on the primary environment when the active environment remaps them", () => {
586 const primary = compile([{ shortcut: modShortcut("j"), command: "terminal.toggle" }]);
587 const active = compile([{ shortcut: modShortcut("t"), command: "terminal.toggle" }]);
588​
589 assert.strictEqual(
590 resolveChatShortcutCommand(event({ key: "j", ctrlKey: true }), primary, active, {
591 platform: "Linux",
592 }),
593 "terminal.toggle",
594 );
595 assert.isNull(
596 resolveChatShortcutCommand(event({ key: "t", ctrlKey: true }), primary, active, {
597 platform: "Linux",
598 }),
599 );
600 });
601​
602 it.each([false, true])(
603 "preserves active binding precedence with terminalFocus=%s",
604 (terminalFocus) => {
605 const primary = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
606 const active = compile([
607 { shortcut: modShortcut("r"), command: "script.deploy.run" },
608 {
609 shortcut: modShortcut("r"),
610 command: "terminal.new",
611 whenAst: whenIdentifier("terminalFocus"),
612 },
613 ]);
614 const options = { platform: "Linux", context: { terminalFocus } };
615 const shortcut = event({ key: "r", ctrlKey: true });
616​
617 assert.strictEqual(
618 resolveChatShortcutCommand(shortcut, primary, active, options),
619 terminalFocus ? null : "script.deploy.run",
620 );
621 assert.strictEqual(
622 resolveChatShortcutCommand(shortcut, active, active, options),
623 resolveShortcutCommand(shortcut, active, options),
624 );
625 },
626 );
627});
628​
629describe("resolveShortcutCommand", () => {
630 it("routes mod+w to the terminal while focused and to the right panel otherwise", () => {
631 const closeEvent = event({ key: "w", metaKey: true });
632 assert.strictEqual(
633 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
634 platform: "MacIntel",
635 context: { terminalFocus: true },
636 }),
637 "terminal.close",
638 );
639 assert.strictEqual(
640 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
641 platform: "MacIntel",
642 context: { terminalFocus: false },
643 }),
644 "rightPanel.close",
645 );
646 });
647​
648 it("navigates history with mod+[ and mod+] outside the terminal", () => {
649 const back = event({ key: "[", code: "BracketLeft", metaKey: true });
650 const forward = event({ key: "]", code: "BracketRight", ctrlKey: true });
651 assert.strictEqual(
652 resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }),
653 "navigation.back",
654 );
655 assert.strictEqual(
656 resolveShortcutCommand(forward, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }),
657 "navigation.forward",
658 );
659 assert.isNull(
660 resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, {
661 platform: "MacIntel",
662 context: { terminalFocus: true },
663 }),
664 );
665 });
666​
667 it("matches punctuation shortcuts by physical key across keyboard layouts", () => {
668 const keybindings = compile([
669 { shortcut: modShortcut("'", { shiftKey: true }), command: "diff.toggle" },
670 ]);
671​
672 assert.strictEqual(
673 resolveShortcutCommand(
674 event({ key: "@", code: "Quote", metaKey: true, shiftKey: true }),
675 keybindings,
676 { platform: "MacIntel" },
677 ),
678 "diff.toggle",
679 );
680 assert.isNull(
681 resolveShortcutCommand(
682 event({ key: '"', code: "Digit2", metaKey: true, shiftKey: true }),
683 keybindings,
684 { platform: "MacIntel" },
685 ),
686 );
687 });
688​
689 it('matches Ctrl+layout "+" as itself unless Shift, Alt or the number row is involved', () => {
690 assert.isNull(
691 resolveShortcutCommand(
692 event({ key: "+", code: "BracketRight", ctrlKey: true }),
693 DEFAULT_RESOLVED_KEYBINDINGS,
694 { platform: "Linux", context: { terminalFocus: false } },
695 ),
696 );
697 assert.strictEqual(
698 resolveShortcutCommand(
699 event({ key: "+", code: "Minus", ctrlKey: true }),
700 DEFAULT_RESOLVED_KEYBINDINGS,
701 { platform: "Linux", context: { previewFocus: true } },
702 ),
703 "preview.zoomIn",
704 );
705 assert.strictEqual(
706 resolveShortcutCommand(
707 event({ key: "+", code: "Digit1", ctrlKey: true }),
708 DEFAULT_RESOLVED_KEYBINDINGS,
709 { platform: "Linux", context: { isDesktop: true } },
710 ),
711 "thread.jump.1",
712 );
713 assert.strictEqual(
714 resolveShortcutCommand(
715 event({ key: "+", code: "BracketRight", altKey: true }),
716 compile([
717 {
718 shortcut: modShortcut("]", { modKey: false, altKey: true }),
719 command: "terminal.toggle",
720 },
721 ]),
722 { platform: "Linux" },
723 ),
724 "terminal.toggle",
725 );
726 assert.strictEqual(
727 resolveShortcutCommand(
728 event({ key: "+", code: "Equal", ctrlKey: true, shiftKey: true }),
729 compile([{ shortcut: modShortcut("=", { shiftKey: true }), command: "terminal.toggle" }]),
730 { platform: "Linux" },
731 ),
732 "terminal.toggle",
733 );
734 });
735​
736 it("does not let a punctuation position shadow a Latin layout key", () => {
737 const keybindings = compile([
738 { shortcut: modShortcut("m"), command: "diff.toggle" },
739 { shortcut: modShortcut(";"), command: "sidebar.toggle" },
740 ]);
741​
742 assert.strictEqual(
743 resolveShortcutCommand(event({ key: "m", code: "Semicolon", metaKey: true }), keybindings, {
744 platform: "MacIntel",
745 }),
746 "diff.toggle",
747 );
748 });
749​
750 it("matches Option-modified letters using the physical key code on macOS", () => {
751 assert.strictEqual(
752 resolveShortcutCommand(
753 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
754 DEFAULT_BINDINGS,
755 { platform: "MacIntel" },
756 ),
757 "rightPanel.toggle",
758 );
759 });
760​
761 it("matches Option symbols against the active Latin layout rather than the US position", () => {
762 const keybindings = compile([
763 { shortcut: modShortcut("a", { altKey: true, shiftKey: true }), command: "appearance.cycle" },
764 { shortcut: modShortcut("q", { altKey: true, shiftKey: true }), command: "sidebar.toggle" },
765 ]);
766 assert.strictEqual(
767 resolveShortcutCommand(
768 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true, shiftKey: true }),
769 keybindings,
770 { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) },
771 ),
772 "appearance.cycle",
773 );
774 });
775​
776 it("preserves physical punctuation alongside an Option-modified mapped letter", () => {
777 for (const key of [";", "m"]) {
778 const keybindings = compile([
779 { shortcut: modShortcut(key, { altKey: true }), command: "diff.toggle" },
780 ]);
781 assert.strictEqual(
782 resolveShortcutCommand(
783 event({ key: "µ", code: "Semicolon", metaKey: true, altKey: true }),
784 keybindings,
785 { platform: "MacIntel", keyboardLayoutMap: new Map([["Semicolon", "m"]]) },
786 ),
787 "diff.toggle",
788 );
789 }
790 });
791​
792 it("keeps Latin event keys and unsupported layout fallbacks", () => {
793 const keybindings = compile([
794 { shortcut: modShortcut("a", { altKey: true }), command: "appearance.cycle" },
795 { shortcut: modShortcut("q", { altKey: true }), command: "sidebar.toggle" },
796 ]);
797 const options = { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) };
798 assert.strictEqual(
799 resolveShortcutCommand(
800 event({ key: "q", code: "KeyQ", metaKey: true, altKey: true }),
801 keybindings,
802 options,
803 ),
804 "sidebar.toggle",
805 );
806 for (const keyboardLayoutMap of [new Map<string, string>(), new Map([["KeyQ", "й"]])]) {
807 assert.strictEqual(
808 resolveShortcutCommand(
809 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true }),
810 keybindings,
811 { ...options, keyboardLayoutMap },
812 ),
813 "sidebar.toggle",
814 );
815 }
816 assert.strictEqual(
817 resolveShortcutCommand(
818 event({
819 key: "Æ",
820 code: "KeyQ",
821 ctrlKey: true,
822 altKey: true,
823 getModifierState: () => true,
824 }),
825 keybindings,
826 { ...options, platform: "Win32" },
827 ),
828 null,
829 );
830 });
831​
832 it("matches non-Latin layout letters using the physical key code", () => {
833 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
834​
835 assert.strictEqual(
836 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
837 platform: "MacIntel",
838 }),
839 "diff.toggle",
840 );
841 });
842​
843 it("ignores the physical key code when the layout types a different Latin letter", () => {
844 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
845​
846 assert.isNull(
847 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
848 platform: "MacIntel",
849 }),
850 );
851 assert.strictEqual(
852 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
853 platform: "MacIntel",
854 }),
855 "diff.toggle",
856 );
857 });
858});
859​
860describe("isRichTextBoldShortcut", () => {
861 const selected = { isCollapsed: false };
862​
863 it("matches the B key on non-Latin layouts, like the sidebar toggle does", () => {
864 const cyrillicB = event({ key: "и", code: "KeyB", ctrlKey: true });
865 assert.isTrue(isRichTextBoldShortcut(cyrillicB, selected));
866 assert.strictEqual(
867 resolveShortcutCommand(cyrillicB, DEFAULT_BINDINGS, { platform: "Win32" }),
868 "sidebar.toggle",
869 );
870 });
871​
872 it("follows the letter a Latin layout types, not the physical key", () => {
873 assert.isFalse(
874 isRichTextBoldShortcut(event({ key: "x", code: "KeyB", ctrlKey: true }), selected),
875 );
876 assert.isTrue(
877 isRichTextBoldShortcut(event({ key: "b", code: "KeyN", ctrlKey: true }), selected),
878 );
879 });
880​
881 it("leaves the chord to the sidebar toggle when no text is selected", () => {
882 const modB = event({ key: "b", metaKey: true });
883 assert.isFalse(isRichTextBoldShortcut(modB, { isCollapsed: true }));
884 assert.isFalse(isRichTextBoldShortcut(modB, null));
885 });
886});
887​
888describe("isSidebarToggleShortcut", () => {
889 it("leaves Ctrl+B to a focused terminal, where it is the tmux prefix", () => {
890 const ctrlB = event({ key: "b", ctrlKey: true });
891 assert.isFalse(
892 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
893 platform: "Linux",
894 context: { terminalFocus: true },
895 }),
896 );
897 assert.isTrue(
898 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
899 platform: "Linux",
900 context: { terminalFocus: false },
901 }),
902 );
903 });
904​
905 it("keeps Cmd+B toggling the sidebar from a focused terminal on macOS", () => {
906 assert.isTrue(
907 isSidebarToggleShortcut(event({ key: "b", metaKey: true }), DEFAULT_BINDINGS, {
908 platform: "MacIntel",
909 context: { terminalFocus: true },
910 }),
911 );
912 });
913});
914​
915describe("composer and pull request shortcuts", () => {
916 it("uses the dictation chord outside the terminal and preserves terminal split", () => {
917 const chord = event({ key: "d", ctrlKey: true, shiftKey: true });
918 assert.strictEqual(
919 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
920 platform: "Linux",
921 context: { terminalFocus: false },
922 }),
923 "composer.dictation",
924 );
925 assert.strictEqual(
926 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
927 platform: "Linux",
928 context: { terminalFocus: true },
929 }),
930 "terminal.splitVertical",
931 );
932 });
933 it("fills missing number shortcuts without replacing the saved URL binding", () => {
934 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
935 (binding) =>
936 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
937 );
938 const bindings = mergeWithDefaultKeybindings([
939 ...olderServerBindings,
940 ...compileResolvedKeybindingsConfig([
941 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
942 ]),
943 ]);
944 for (const [key, command] of [
945 ["k", "pullRequest.copyNumber"],
946 ["8", "thread.copyReference"],
947 ["c", null],
948 ["y", null],
949 ] as const) {
950 assert.strictEqual(
951 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
952 platform: "MacIntel",
953 }),
954 command,
955 );
956 }
957 });
958​
959 it.each(["MacIntel", "Win32", "Linux"])(
960 "reserves the alternate send chord for the focused composer on %s",
961 (platform) => {
962 const modifier = {
963 metaKey: platform === "MacIntel",
964 ctrlKey: platform !== "MacIntel",
965 };
966 const shiftedEnter = event({ key: "Enter", shiftKey: true, ...modifier });
967 for (const composerRequiresModifier of [false, true]) {
968 const context = {
969 composerFocus: true,
970 turnRunning: true,
971 composerRequiresModifier,
972 };
973 assert.strictEqual(
974 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
975 platform,
976 context,
977 }),
978 composerRequiresModifier ? "composer.sendAlternate" : "thread.steerQueuedMessage",
979 );
980 assert.strictEqual(
981 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "composer.sendAlternate", {
982 platform,
983 context,
984 }),
985 formatShortcutLabel(
986 modShortcut("enter", { shiftKey: composerRequiresModifier }),
987 platform,
988 ),
989 );
990 assert.strictEqual(
991 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
992 platform,
993 context: { ...context, composerFocus: false },
994 }),
995 "thread.steerQueuedMessage",
996 );
997 }
998 },
999 );
1000​
1001 const altEffortBindings = compileResolvedKeybindingsConfig([
1002 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
1003 ]);
1004​
1005 it("leaves AltGr text entry alone with a custom Alt binding", () => {
1006 for (const platform of ["Win32", "Linux"]) {
1007 const input = event({
1008 key: "€",
1009 code: "KeyE",
1010 ctrlKey: true,
1011 altKey: true,
1012 getModifierState: (key) => key === "AltGraph",
1013 });
1014 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
1015 assert.strictEqual(
1016 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
1017 platform,
1018 }),
1019 "composer.effort",
1020 );
1022 });
1023​
1024 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
1025 const getModifierState = (key: string) => key === "AltGraph";
1026 assert.strictEqual(
1027 resolveShortcutCommand(
1028 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
1029 altEffortBindings,
1030 { platform: "Win32" },
1031 ),
1032 "composer.effort",
1033 );
1034 assert.strictEqual(
1035 resolveShortcutCommand(
1036 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
1037 altEffortBindings,
1038 { platform: "MacIntel" },
1039 ),
1040 "composer.effort",
1041 );
1042 });
1043});
1044​
1045describe("Usage shortcuts", () => {
1046 it.each(["Linux", "MacIntel"])(
1047 "preserves desktop numbered thread shortcuts on Usage on %s",
1048 (platform) => {
1049 const shortcut = event({
1050 key: "2",
1051 ctrlKey: platform === "Linux",
1052 metaKey: platform === "MacIntel",
1053 });
1054 assert.strictEqual(
1055 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1056 platform,
1057 context: { usagePageOpen: true, isDesktop: true },
1058 }),
1059 "thread.jump.2",
1060 );
1061 assert.isNotNull(
1062 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "thread.jump.2", {
1063 platform,
1064 context: { usagePageOpen: true, isDesktop: true },
1065 }),
1066 );
1067 },
1068 );
1069​
1070 it("matches shifted number keys for periods and only on Usage", () => {
1071 const shortcut = event({ key: "!", code: "Digit1", ctrlKey: true, shiftKey: true });
1072 assert.strictEqual(
1073 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1074 platform: "Linux",
1075 context: { usagePageOpen: true },
1076 }),
1077 "usage.period.day",
1078 );
1079 assert.isNull(
1080 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1081 platform: "Linux",
1082 }),
1083 );
1084 });
1085});