The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,014 lines 30.8 KB
Raw
1 import { assert, describe, it } from "vite-plus/test";
2 import {
3 compileResolvedKeybindingsConfig,
4 DEFAULT_RESOLVED_KEYBINDINGS,
5 mergeWithDefaultKeybindings,
6 } from "@t3tools/shared/keybindings";
7
8 import {
9 type KeybindingCommand,
10 type KeybindingShortcut,
11 type KeybindingWhenNode,
12 type ResolvedKeybindingsConfig,
13 } from "@t3tools/contracts";
14 import {
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
29 function 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 };
38 }
39
40 describe("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
65 function 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 };
78 }
79
80 function whenIdentifier(name: string): KeybindingWhenNode {
81 return { type: "identifier", name };
82 }
83
84 function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
85 return { type: "not", node };
86 }
87
88 function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
89 return { type: "and", left, right };
90 }
91
92 interface TestBinding {
93 shortcut: KeybindingShortcut;
94 command: KeybindingCommand;
95 whenAst?: KeybindingWhenNode;
96 }
97
98 function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
99 return bindings.map((binding) => ({
100 command: binding.command,
101 shortcut: binding.shortcut,
102 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
103 }));
104 }
105
106 const 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
207 describe("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
241 describe("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
252 describe("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
269 describe("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
305 describe("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
349 describe("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
403 describe("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
429 describe("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
480 describe("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
545 describe("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
629 describe("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 non-Latin layout letters using the physical key code", () => {
762 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
763
764 assert.strictEqual(
765 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
766 platform: "MacIntel",
767 }),
768 "diff.toggle",
769 );
770 });
771
772 it("ignores the physical key code when the layout types a different Latin letter", () => {
773 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
774
775 assert.isNull(
776 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
777 platform: "MacIntel",
778 }),
779 );
780 assert.strictEqual(
781 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
782 platform: "MacIntel",
783 }),
784 "diff.toggle",
785 );
786 });
787 });
788
789 describe("isRichTextBoldShortcut", () => {
790 const selected = { isCollapsed: false };
791
792 it("matches the B key on non-Latin layouts, like the sidebar toggle does", () => {
793 const cyrillicB = event({ key: "и", code: "KeyB", ctrlKey: true });
794 assert.isTrue(isRichTextBoldShortcut(cyrillicB, selected));
795 assert.strictEqual(
796 resolveShortcutCommand(cyrillicB, DEFAULT_BINDINGS, { platform: "Win32" }),
797 "sidebar.toggle",
798 );
799 });
800
801 it("follows the letter a Latin layout types, not the physical key", () => {
802 assert.isFalse(
803 isRichTextBoldShortcut(event({ key: "x", code: "KeyB", ctrlKey: true }), selected),
804 );
805 assert.isTrue(
806 isRichTextBoldShortcut(event({ key: "b", code: "KeyN", ctrlKey: true }), selected),
807 );
808 });
809
810 it("leaves the chord to the sidebar toggle when no text is selected", () => {
811 const modB = event({ key: "b", metaKey: true });
812 assert.isFalse(isRichTextBoldShortcut(modB, { isCollapsed: true }));
813 assert.isFalse(isRichTextBoldShortcut(modB, null));
814 });
815 });
816
817 describe("isSidebarToggleShortcut", () => {
818 it("leaves Ctrl+B to a focused terminal, where it is the tmux prefix", () => {
819 const ctrlB = event({ key: "b", ctrlKey: true });
820 assert.isFalse(
821 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
822 platform: "Linux",
823 context: { terminalFocus: true },
824 }),
825 );
826 assert.isTrue(
827 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
828 platform: "Linux",
829 context: { terminalFocus: false },
830 }),
831 );
832 });
833
834 it("keeps Cmd+B toggling the sidebar from a focused terminal on macOS", () => {
835 assert.isTrue(
836 isSidebarToggleShortcut(event({ key: "b", metaKey: true }), DEFAULT_BINDINGS, {
837 platform: "MacIntel",
838 context: { terminalFocus: true },
839 }),
840 );
841 });
842 });
843
844 describe("composer and pull request shortcuts", () => {
845 it("uses the dictation chord outside the terminal and preserves terminal split", () => {
846 const chord = event({ key: "d", ctrlKey: true, shiftKey: true });
847 assert.strictEqual(
848 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
849 platform: "Linux",
850 context: { terminalFocus: false },
851 }),
852 "composer.dictation",
853 );
854 assert.strictEqual(
855 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
856 platform: "Linux",
857 context: { terminalFocus: true },
858 }),
859 "terminal.splitVertical",
860 );
861 });
862 it("fills missing number shortcuts without replacing the saved URL binding", () => {
863 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
864 (binding) =>
865 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
866 );
867 const bindings = mergeWithDefaultKeybindings([
868 ...olderServerBindings,
869 ...compileResolvedKeybindingsConfig([
870 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
871 ]),
872 ]);
873 for (const [key, command] of [
874 ["k", "pullRequest.copyNumber"],
875 ["8", "thread.copyReference"],
876 ["c", null],
877 ["y", null],
878 ] as const) {
879 assert.strictEqual(
880 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
881 platform: "MacIntel",
882 }),
883 command,
884 );
885 }
886 });
887
888 it.each(["MacIntel", "Win32", "Linux"])(
889 "reserves the alternate send chord for the focused composer on %s",
890 (platform) => {
891 const modifier = {
892 metaKey: platform === "MacIntel",
893 ctrlKey: platform !== "MacIntel",
894 };
895 const shiftedEnter = event({ key: "Enter", shiftKey: true, ...modifier });
896 for (const composerRequiresModifier of [false, true]) {
897 const context = {
898 composerFocus: true,
899 turnRunning: true,
900 composerRequiresModifier,
901 };
902 assert.strictEqual(
903 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
904 platform,
905 context,
906 }),
907 composerRequiresModifier ? "composer.sendAlternate" : "thread.steerQueuedMessage",
908 );
909 assert.strictEqual(
910 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "composer.sendAlternate", {
911 platform,
912 context,
913 }),
914 formatShortcutLabel(
915 modShortcut("enter", { shiftKey: composerRequiresModifier }),
916 platform,
917 ),
918 );
919 assert.strictEqual(
920 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
921 platform,
922 context: { ...context, composerFocus: false },
923 }),
924 "thread.steerQueuedMessage",
925 );
926 }
927 },
928 );
929
930 const altEffortBindings = compileResolvedKeybindingsConfig([
931 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
932 ]);
933
934 it("leaves AltGr text entry alone with a custom Alt binding", () => {
935 for (const platform of ["Win32", "Linux"]) {
936 const input = event({
937 key: "€",
938 code: "KeyE",
939 ctrlKey: true,
940 altKey: true,
941 getModifierState: (key) => key === "AltGraph",
942 });
943 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
944 assert.strictEqual(
945 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
946 platform,
947 }),
948 "composer.effort",
949 );
950 }
951 });
952
953 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
954 const getModifierState = (key: string) => key === "AltGraph";
955 assert.strictEqual(
956 resolveShortcutCommand(
957 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
958 altEffortBindings,
959 { platform: "Win32" },
960 ),
961 "composer.effort",
962 );
963 assert.strictEqual(
964 resolveShortcutCommand(
965 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
966 altEffortBindings,
967 { platform: "MacIntel" },
968 ),
969 "composer.effort",
970 );
971 });
972 });
973
974 describe("Usage shortcuts", () => {
975 it.each(["Linux", "MacIntel"])(
976 "preserves desktop numbered thread shortcuts on Usage on %s",
977 (platform) => {
978 const shortcut = event({
979 key: "2",
980 ctrlKey: platform === "Linux",
981 metaKey: platform === "MacIntel",
982 });
983 assert.strictEqual(
984 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
985 platform,
986 context: { usagePageOpen: true, isDesktop: true },
987 }),
988 "thread.jump.2",
989 );
990 assert.isNotNull(
991 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "thread.jump.2", {
992 platform,
993 context: { usagePageOpen: true, isDesktop: true },
994 }),
995 );
996 },
997 );
998
999 it("matches shifted number keys for periods and only on Usage", () => {
1000 const shortcut = event({ key: "!", code: "Digit1", ctrlKey: true, shiftKey: true });
1001 assert.strictEqual(
1002 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1003 platform: "Linux",
1004 context: { usagePageOpen: true },
1005 }),
1006 "usage.period.day",
1007 );
1008 assert.isNull(
1009 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1010 platform: "Linux",
1011 }),
1012 );
1013 });
1014 });