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

keybindings.test.ts

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