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

keybindings.test.ts

1,179 lines36.4 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.each(["shift+İ", "shift+'"])("keeps %s ahead of the dotted-I fallback", (key) => {
631 const input = event({ key: "İ", code: "Quote", shiftKey: true });
632 const competing = { key, command: "rightPanel.toggle" } as const;
633 const fallback = { key: "shift+i", command: "sidebar.toggle" } as const;
634 const options = { platform: "Linux" };
635 assert.strictEqual(
636 resolveShortcutCommand(
637 input,
638 compileResolvedKeybindingsConfig([competing, fallback]),
639 options,
640 ),
641 "rightPanel.toggle",
642 );
643 assert.strictEqual(
644 resolveShortcutCommand(input, compileResolvedKeybindingsConfig([fallback]), options),
645 "sidebar.toggle",
646 );
647 });
648​
649 it.each(["constructor", "!constructor"])(
650 "treats inherited object properties in when-condition %s as unset",
651 (when) => {
652 const keybindings = compileResolvedKeybindingsConfig([
653 { key: "mod+j", command: "terminal.toggle", when },
654 ]);
655 assert.strictEqual(
656 resolveShortcutCommand(event({ code: "KeyJ", ctrlKey: true }), keybindings, {
657 platform: "Linux",
658 }),
659 when.startsWith("!") ? "terminal.toggle" : null,
660 );
661 },
662 );
663​
664 it("routes mod+w to the terminal while focused and to the right panel otherwise", () => {
665 const closeEvent = event({ key: "w", metaKey: true });
666 assert.strictEqual(
667 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
668 platform: "MacIntel",
669 context: { terminalFocus: true },
670 }),
671 "terminal.close",
672 );
673 assert.strictEqual(
674 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
675 platform: "MacIntel",
676 context: { terminalFocus: false },
677 }),
678 "rightPanel.close",
679 );
680 });
681​
682 it("maximizes the right panel with mod+alt+e by default", () => {
683 // macOS reports Option+E as the dead key "´", so the physical key decides.
684 const mac = event({ key: "´", code: "KeyE", metaKey: true, altKey: true });
685 const linux = event({ key: "e", code: "KeyE", ctrlKey: true, altKey: true });
686 assert.strictEqual(
687 resolveShortcutCommand(mac, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }),
688 "rightPanel.toggleMaximized",
689 );
690 assert.strictEqual(
691 resolveShortcutCommand(linux, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }),
692 "rightPanel.toggleMaximized",
693 );
694 });
695​
696 it("uses terminal cycling shortcuts without changing bracket history shortcuts", () => {
697 const back = event({ key: "[", code: "BracketLeft", metaKey: true });
698 const forward = event({ key: "]", code: "BracketRight", ctrlKey: true });
699 assert.strictEqual(
700 resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }),
701 "navigation.back",
702 );
703 assert.strictEqual(
704 resolveShortcutCommand(forward, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }),
705 "navigation.forward",
706 );
707 for (const platform of ["MacIntel", "Win32", "Linux"]) {
708 const mac = platform === "MacIntel";
709 for (const [key, shiftedKey, code, direction] of [
710 ["[", "{", "BracketLeft", "previous"],
711 ["]", "}", "BracketRight", "next"],
712 ] as const) {
713 const shortcut = event({
714 key: mac ? shiftedKey : key,
715 code,
716 metaKey: mac,
717 ctrlKey: !mac,
718 altKey: !mac,
719 shiftKey: mac,
720 });
721 assert.strictEqual(
722 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
723 platform,
724 context: { terminalFocus: true },
725 }),
726 `terminal.${direction}`,
727 );
728 if (mac) {
729 assert.strictEqual(
730 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
731 platform,
732 context: { terminalFocus: false },
733 }),
734 `thread.${direction}`,
735 );
736 }
737 }
738 if (mac) {
739 for (const key of ["[", "]"]) {
740 const command = resolveShortcutCommand(
741 event({ key, metaKey: true }),
742 DEFAULT_RESOLVED_KEYBINDINGS,
743 {
744 platform,
745 context: { terminalFocus: true },
746 },
747 );
748 assert.notStrictEqual(command, "terminal.previous");
749 assert.notStrictEqual(command, "terminal.next");
750 }
751 }
752 assert.isNull(
753 resolveShortcutCommand(event({ key: "[", ctrlKey: true }), DEFAULT_RESOLVED_KEYBINDINGS, {
754 platform,
755 context: { terminalFocus: true },
756 }),
757 );
758 }
759 });
760​
761 it("matches punctuation shortcuts by physical key across keyboard layouts", () => {
762 const keybindings = compile([
763 { shortcut: modShortcut("'", { shiftKey: true }), command: "diff.toggle" },
764 ]);
765​
766 assert.strictEqual(
767 resolveShortcutCommand(
768 event({ key: "@", code: "Quote", metaKey: true, shiftKey: true }),
769 keybindings,
770 { platform: "MacIntel" },
771 ),
772 "diff.toggle",
773 );
774 assert.isNull(
775 resolveShortcutCommand(
776 event({ key: '"', code: "Digit2", metaKey: true, shiftKey: true }),
777 keybindings,
778 { platform: "MacIntel" },
779 ),
780 );
781 });
782​
783 it('matches Ctrl+layout "+" as itself unless Shift, Alt or the number row is involved', () => {
784 assert.isNull(
785 resolveShortcutCommand(
786 event({ key: "+", code: "BracketRight", ctrlKey: true }),
787 DEFAULT_RESOLVED_KEYBINDINGS,
788 { platform: "Linux", context: { terminalFocus: false } },
789 ),
790 );
791 assert.strictEqual(
792 resolveShortcutCommand(
793 event({ key: "+", code: "Minus", ctrlKey: true }),
794 DEFAULT_RESOLVED_KEYBINDINGS,
795 { platform: "Linux", context: { previewFocus: true } },
796 ),
797 "preview.zoomIn",
798 );
799 assert.strictEqual(
800 resolveShortcutCommand(
801 event({ key: "+", code: "Digit1", ctrlKey: true }),
802 DEFAULT_RESOLVED_KEYBINDINGS,
803 { platform: "Linux", context: { isDesktop: true } },
804 ),
805 "thread.jump.1",
806 );
807 assert.strictEqual(
808 resolveShortcutCommand(
809 event({ key: "+", code: "BracketRight", altKey: true }),
810 compile([
811 {
812 shortcut: modShortcut("]", { modKey: false, altKey: true }),
813 command: "terminal.toggle",
814 },
815 ]),
816 { platform: "Linux" },
817 ),
818 "terminal.toggle",
819 );
820 assert.strictEqual(
821 resolveShortcutCommand(
822 event({ key: "+", code: "Equal", ctrlKey: true, shiftKey: true }),
823 compile([{ shortcut: modShortcut("=", { shiftKey: true }), command: "terminal.toggle" }]),
824 { platform: "Linux" },
825 ),
826 "terminal.toggle",
827 );
828 });
829​
830 it("does not let a punctuation position shadow a Latin layout key", () => {
831 const keybindings = compile([
832 { shortcut: modShortcut("m"), command: "diff.toggle" },
833 { shortcut: modShortcut(";"), command: "sidebar.toggle" },
834 ]);
835​
836 assert.strictEqual(
837 resolveShortcutCommand(event({ key: "m", code: "Semicolon", metaKey: true }), keybindings, {
838 platform: "MacIntel",
839 }),
840 "diff.toggle",
841 );
842 });
843​
844 it("matches Option-modified letters using the physical key code on macOS", () => {
845 assert.strictEqual(
846 resolveShortcutCommand(
847 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
848 DEFAULT_BINDINGS,
849 { platform: "MacIntel" },
850 ),
851 "rightPanel.toggle",
852 );
853 });
854​
855 it("matches Option symbols against the active Latin layout rather than the US position", () => {
856 const keybindings = compile([
857 { shortcut: modShortcut("a", { altKey: true, shiftKey: true }), command: "appearance.cycle" },
858 { shortcut: modShortcut("q", { altKey: true, shiftKey: true }), command: "sidebar.toggle" },
859 ]);
860 assert.strictEqual(
861 resolveShortcutCommand(
862 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true, shiftKey: true }),
863 keybindings,
864 { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) },
865 ),
866 "appearance.cycle",
867 );
868 });
869​
870 it("preserves physical punctuation alongside an Option-modified mapped letter", () => {
871 for (const key of [";", "m"]) {
872 const keybindings = compile([
873 { shortcut: modShortcut(key, { altKey: true }), command: "diff.toggle" },
874 ]);
875 assert.strictEqual(
876 resolveShortcutCommand(
877 event({ key: "µ", code: "Semicolon", metaKey: true, altKey: true }),
878 keybindings,
879 { platform: "MacIntel", keyboardLayoutMap: new Map([["Semicolon", "m"]]) },
880 ),
881 "diff.toggle",
882 );
883 }
884 });
885​
886 it("keeps Latin event keys and unsupported layout fallbacks", () => {
887 const keybindings = compile([
888 { shortcut: modShortcut("a", { altKey: true }), command: "appearance.cycle" },
889 { shortcut: modShortcut("q", { altKey: true }), command: "sidebar.toggle" },
890 ]);
891 const options = { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) };
892 assert.strictEqual(
893 resolveShortcutCommand(
894 event({ key: "q", code: "KeyQ", metaKey: true, altKey: true }),
895 keybindings,
896 options,
897 ),
898 "sidebar.toggle",
899 );
900 for (const keyboardLayoutMap of [new Map<string, string>(), new Map([["KeyQ", "й"]])]) {
901 assert.strictEqual(
902 resolveShortcutCommand(
903 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true }),
904 keybindings,
905 { ...options, keyboardLayoutMap },
906 ),
907 "sidebar.toggle",
908 );
909 }
910 assert.strictEqual(
911 resolveShortcutCommand(
912 event({
913 key: "Æ",
914 code: "KeyQ",
915 ctrlKey: true,
916 altKey: true,
917 getModifierState: () => true,
918 }),
919 keybindings,
920 { ...options, platform: "Win32" },
921 ),
922 null,
923 );
924 });
925​
926 it("matches non-Latin layout letters using the physical key code", () => {
927 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
928​
929 assert.strictEqual(
930 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
931 platform: "MacIntel",
932 }),
933 "diff.toggle",
934 );
935 });
936​
937 it("ignores the physical key code when the layout types a different Latin letter", () => {
938 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
939​
940 assert.isNull(
941 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
942 platform: "MacIntel",
943 }),
944 );
945 assert.strictEqual(
946 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
947 platform: "MacIntel",
948 }),
949 "diff.toggle",
950 );
951 });
952});
953​
954describe("isRichTextBoldShortcut", () => {
955 const selected = { isCollapsed: false };
956​
957 it("matches the B key on non-Latin layouts, like the sidebar toggle does", () => {
958 const cyrillicB = event({ key: "и", code: "KeyB", ctrlKey: true });
959 assert.isTrue(isRichTextBoldShortcut(cyrillicB, selected));
960 assert.strictEqual(
961 resolveShortcutCommand(cyrillicB, DEFAULT_BINDINGS, { platform: "Win32" }),
962 "sidebar.toggle",
963 );
964 });
965​
966 it("follows the letter a Latin layout types, not the physical key", () => {
967 assert.isFalse(
968 isRichTextBoldShortcut(event({ key: "x", code: "KeyB", ctrlKey: true }), selected),
969 );
970 assert.isTrue(
971 isRichTextBoldShortcut(event({ key: "b", code: "KeyN", ctrlKey: true }), selected),
972 );
973 });
974​
975 it("leaves the chord to the sidebar toggle when no text is selected", () => {
976 const modB = event({ key: "b", metaKey: true });
977 assert.isFalse(isRichTextBoldShortcut(modB, { isCollapsed: true }));
978 assert.isFalse(isRichTextBoldShortcut(modB, null));
979 });
980});
981​
982describe("isSidebarToggleShortcut", () => {
983 it("leaves Ctrl+B to a focused terminal, where it is the tmux prefix", () => {
984 const ctrlB = event({ key: "b", ctrlKey: true });
985 assert.isFalse(
986 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
987 platform: "Linux",
988 context: { terminalFocus: true },
989 }),
990 );
991 assert.isTrue(
992 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
993 platform: "Linux",
994 context: { terminalFocus: false },
995 }),
996 );
997 });
998​
999 it("keeps Cmd+B toggling the sidebar from a focused terminal on macOS", () => {
1000 assert.isTrue(
1001 isSidebarToggleShortcut(event({ key: "b", metaKey: true }), DEFAULT_BINDINGS, {
1002 platform: "MacIntel",
1003 context: { terminalFocus: true },
1004 }),
1005 );
1006 });
1007});
1008​
1009describe("composer and pull request shortcuts", () => {
1010 it("uses the dictation chord outside the terminal and preserves terminal split", () => {
1011 const chord = event({ key: "d", ctrlKey: true, shiftKey: true });
1012 assert.strictEqual(
1013 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
1014 platform: "Linux",
1015 context: { terminalFocus: false },
1016 }),
1017 "composer.dictation",
1018 );
1019 assert.strictEqual(
1020 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
1021 platform: "Linux",
1022 context: { terminalFocus: true },
1023 }),
1024 "terminal.splitVertical",
1025 );
1026 });
1027 it("fills missing number shortcuts without replacing the saved URL binding", () => {
1028 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
1029 (binding) =>
1030 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
1031 );
1032 const bindings = mergeWithDefaultKeybindings([
1033 ...olderServerBindings,
1034 ...compileResolvedKeybindingsConfig([
1035 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
1036 ]),
1037 ]);
1038 for (const [key, command] of [
1039 ["k", "pullRequest.copyNumber"],
1040 ["8", "thread.copyReference"],
1041 ["c", null],
1042 ["y", null],
1043 ] as const) {
1044 assert.strictEqual(
1045 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
1046 platform: "MacIntel",
1047 }),
1048 command,
1049 );
1051 });
1052​
1053 it.each(["MacIntel", "Win32", "Linux"])(
1054 "reserves the alternate send chord for the focused composer on %s",
1055 (platform) => {
1056 const modifier = {
1057 metaKey: platform === "MacIntel",
1058 ctrlKey: platform !== "MacIntel",
1059 };
1060 const shiftedEnter = event({ key: "Enter", shiftKey: true, ...modifier });
1061 for (const composerRequiresModifier of [false, true]) {
1062 const context = {
1063 composerFocus: true,
1064 turnRunning: true,
1065 composerRequiresModifier,
1066 };
1067 assert.strictEqual(
1068 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
1069 platform,
1070 context,
1071 }),
1072 composerRequiresModifier ? "composer.sendAlternate" : "thread.steerQueuedMessage",
1073 );
1074 assert.strictEqual(
1075 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "composer.sendAlternate", {
1076 platform,
1077 context,
1078 }),
1079 formatShortcutLabel(
1080 modShortcut("enter", { shiftKey: composerRequiresModifier }),
1081 platform,
1082 ),
1083 );
1084 assert.strictEqual(
1085 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
1086 platform,
1087 context: { ...context, composerFocus: false },
1088 }),
1089 "thread.steerQueuedMessage",
1090 );
1092 },
1093 );
1094​
1095 const altEffortBindings = compileResolvedKeybindingsConfig([
1096 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
1097 ]);
1098​
1099 it("leaves AltGr text entry alone with a custom Alt binding", () => {
1100 for (const platform of ["Win32", "Linux"]) {
1101 const input = event({
1102 key: "€",
1103 code: "KeyE",
1104 ctrlKey: true,
1105 altKey: true,
1106 getModifierState: (key) => key === "AltGraph",
1107 });
1108 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
1109 assert.strictEqual(
1110 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
1111 platform,
1112 }),
1113 "composer.effort",
1114 );
1116 });
1117​
1118 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
1119 const getModifierState = (key: string) => key === "AltGraph";
1120 assert.strictEqual(
1121 resolveShortcutCommand(
1122 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
1123 altEffortBindings,
1124 { platform: "Win32" },
1125 ),
1126 "composer.effort",
1127 );
1128 assert.strictEqual(
1129 resolveShortcutCommand(
1130 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
1131 altEffortBindings,
1132 { platform: "MacIntel" },
1133 ),
1134 "composer.effort",
1135 );
1136 });
1137});
1138​
1139describe("Usage shortcuts", () => {
1140 it.each(["Linux", "MacIntel"])(
1141 "preserves desktop numbered thread shortcuts on Usage on %s",
1142 (platform) => {
1143 const shortcut = event({
1144 key: "2",
1145 ctrlKey: platform === "Linux",
1146 metaKey: platform === "MacIntel",
1147 });
1148 assert.strictEqual(
1149 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1150 platform,
1151 context: { usagePageOpen: true, isDesktop: true },
1152 }),
1153 "thread.jump.2",
1154 );
1155 assert.isNotNull(
1156 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "thread.jump.2", {
1157 platform,
1158 context: { usagePageOpen: true, isDesktop: true },
1159 }),
1160 );
1161 },
1162 );
1163​
1164 it("matches shifted number keys for periods and only on Usage", () => {
1165 const shortcut = event({ key: "!", code: "Digit1", ctrlKey: true, shiftKey: true });
1166 assert.strictEqual(
1167 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1168 platform: "Linux",
1169 context: { usagePageOpen: true },
1170 }),
1171 "usage.period.day",
1172 );
1173 assert.isNull(
1174 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1175 platform: "Linux",
1176 }),
1177 );
1178 });
1179});