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

keybindings.test.ts

1,272 lines38.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 formatShortcutLabel,
16 isDiffToggleShortcut,
17 isRichTextBoldShortcut,
18 modelPickerJumpCommandForIndex,
19 modelPickerJumpIndexFromCommand,
20 isOpenFavoriteEditorShortcut,
21 isTerminalClearShortcut,
22 isTerminalCloseShortcut,
23 isTerminalNewShortcut,
24 isTerminalSplitShortcut,
25 isTerminalSplitVerticalShortcut,
26 isTerminalToggleShortcut,
27 resolveShortcutCommand,
28 shouldShowThreadJumpHintsForModifiers,
29 shortcutLabelForCommand,
30 terminalDeleteShortcutData,
31 terminalNavigationShortcutData,
32 threadJumpCommandForIndex,
33 threadJumpIndexFromCommand,
34 threadTraversalDirectionFromCommand,
35 type ShortcutEventLike,
36} from "./keybindings";
37​
38function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
39 return {
40 key: "j",
41 metaKey: false,
42 ctrlKey: false,
43 shiftKey: false,
44 altKey: false,
45 ...overrides,
46 };
48​
49function modShortcut(
50 key: string,
51 overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
52): KeybindingShortcut {
53 return {
54 key,
55 metaKey: false,
56 ctrlKey: false,
57 shiftKey: false,
58 altKey: false,
59 modKey: true,
60 ...overrides,
61 };
63​
64function whenIdentifier(name: string): KeybindingWhenNode {
65 return { type: "identifier", name };
67​
68function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
69 return { type: "not", node };
71​
72function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
73 return { type: "and", left, right };
75​
76interface TestBinding {
77 shortcut: KeybindingShortcut;
78 command: KeybindingCommand;
79 whenAst?: KeybindingWhenNode;
81​
82function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
83 return bindings.map((binding) => ({
84 command: binding.command,
85 shortcut: binding.shortcut,
86 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
87 }));
89​
90const DEFAULT_BINDINGS = compile([
91 { shortcut: modShortcut("b"), command: "sidebar.toggle" },
92 { shortcut: modShortcut("j"), command: "terminal.toggle" },
93 { shortcut: modShortcut("b", { altKey: true }), command: "rightPanel.toggle" },
94 {
95 shortcut: modShortcut("d"),
96 command: "terminal.split",
97 whenAst: whenIdentifier("terminalFocus"),
98 },
99 {
100 shortcut: modShortcut("d", { shiftKey: true }),
101 command: "terminal.splitVertical",
102 whenAst: whenIdentifier("terminalFocus"),
103 },
104 {
105 shortcut: modShortcut("n"),
106 command: "terminal.new",
107 whenAst: whenIdentifier("terminalFocus"),
108 },
109 {
110 shortcut: modShortcut("w"),
111 command: "terminal.close",
112 whenAst: whenIdentifier("terminalFocus"),
113 },
114 {
115 shortcut: modShortcut("w"),
116 command: "rightPanel.close",
117 whenAst: whenNot(whenIdentifier("terminalFocus")),
118 },
119 {
120 shortcut: modShortcut("d"),
121 command: "diff.toggle",
122 whenAst: whenNot(whenIdentifier("terminalFocus")),
123 },
124 {
125 shortcut: modShortcut("k"),
126 command: "commandPalette.toggle",
127 whenAst: whenNot(whenIdentifier("terminalFocus")),
128 },
129 {
130 shortcut: modShortcut("p"),
131 command: "filePicker.toggle",
132 whenAst: whenNot(whenIdentifier("terminalFocus")),
133 },
134 {
135 shortcut: modShortcut("f", { shiftKey: true }),
136 command: "projectSearch.toggle",
137 whenAst: whenNot(whenIdentifier("terminalFocus")),
138 },
139 {
140 shortcut: modShortcut("t", { altKey: true, shiftKey: true }),
141 command: "themeEditor.toggle",
142 },
143 {
144 shortcut: modShortcut("m", { shiftKey: true }),
145 command: "modelPicker.toggle",
146 whenAst: whenNot(whenIdentifier("terminalFocus")),
147 },
148 { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
149 { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
150 { shortcut: modShortcut("o"), command: "editor.openFavorite" },
151 { shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },
152 { shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },
153 {
154 shortcut: modShortcut("c", { shiftKey: true }),
155 command: "thread.copyReference",
156 whenAst: whenNot(whenIdentifier("terminalFocus")),
157 },
158 {
159 shortcut: modShortcut("s", { shiftKey: true }),
160 command: "thread.settle",
161 whenAst: whenNot(whenIdentifier("terminalFocus")),
162 },
163 { shortcut: modShortcut("1"), command: "thread.jump.1" },
164 { shortcut: modShortcut("2"), command: "thread.jump.2" },
165 { shortcut: modShortcut("3"), command: "thread.jump.3" },
166 {
167 shortcut: modShortcut("1"),
168 command: "modelPicker.jump.1",
169 whenAst: whenIdentifier("modelPickerOpen"),
170 },
171 {
172 shortcut: modShortcut("2"),
173 command: "modelPicker.jump.2",
174 whenAst: whenIdentifier("modelPickerOpen"),
175 },
176 {
177 shortcut: modShortcut("3"),
178 command: "modelPicker.jump.3",
179 whenAst: whenIdentifier("modelPickerOpen"),
180 },
181]);
182​
183describe("isTerminalToggleShortcut", () => {
184 it("matches Cmd+J on macOS", () => {
185 assert.isTrue(
186 isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, {
187 platform: "MacIntel",
188 }),
189 );
190 });
191​
192 it("matches Ctrl+J on non-macOS", () => {
193 assert.isTrue(
194 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }),
195 );
196 });
197​
198 it("matches Ctrl+J on non-macOS while terminalFocus is true", () => {
199 assert.isTrue(
200 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, {
201 platform: "Win32",
202 context: { terminalFocus: true },
203 }),
204 );
205 });
206});
207​
208describe("settle thread shortcut", () => {
209 it("resolves outside the terminal", () => {
210 assert.equal(
211 resolveShortcutCommand(event({ key: "s", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
212 platform: "MacIntel",
213 context: { terminalFocus: false },
214 }),
215 "thread.settle",
216 );
217 });
218​
219 it("does not intercept the terminal", () => {
220 assert.isNull(
221 resolveShortcutCommand(event({ key: "s", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
222 platform: "Win32",
223 context: { terminalFocus: true },
224 }),
225 );
226 });
227});
228​
229describe("copy thread reference shortcut", () => {
230 it("resolves Cmd+Shift+C on macOS and Ctrl+Shift+C elsewhere", () => {
231 assert.equal(
232 resolveShortcutCommand(event({ key: "c", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
233 platform: "MacIntel",
234 }),
235 "thread.copyReference",
236 );
237 assert.equal(
238 resolveShortcutCommand(event({ key: "c", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
239 platform: "Linux",
240 }),
241 "thread.copyReference",
242 );
243 });
244​
245 it("leaves terminal copy untouched", () => {
246 assert.isNull(
247 resolveShortcutCommand(event({ key: "c", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
248 platform: "Linux",
249 context: { terminalFocus: true },
250 }),
251 );
252 });
253});
254​
255describe("split/new/close terminal shortcuts", () => {
256 it("requires terminalFocus for default split/new/close bindings", () => {
257 assert.isFalse(
258 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
259 platform: "MacIntel",
260 context: { terminalFocus: false },
261 }),
262 );
263 assert.isFalse(
264 isTerminalSplitVerticalShortcut(
265 event({ key: "d", metaKey: true, shiftKey: true }),
266 DEFAULT_BINDINGS,
267 {
268 platform: "MacIntel",
269 context: { terminalFocus: false },
270 },
271 ),
272 );
273 assert.isFalse(
274 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), DEFAULT_BINDINGS, {
275 platform: "Linux",
276 context: { terminalFocus: false },
277 }),
278 );
279 assert.isFalse(
280 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
281 platform: "Linux",
282 context: { terminalFocus: false },
283 }),
284 );
285 });
286​
287 it("matches split/new when terminalFocus is true", () => {
288 assert.isTrue(
289 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
290 platform: "MacIntel",
291 context: { terminalFocus: true },
292 }),
293 );
294 assert.isTrue(
295 isTerminalSplitVerticalShortcut(
296 event({ key: "d", metaKey: true, shiftKey: true }),
297 DEFAULT_BINDINGS,
298 {
299 platform: "MacIntel",
300 context: { terminalFocus: true },
301 },
302 ),
303 );
304 assert.isTrue(
305 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), DEFAULT_BINDINGS, {
306 platform: "Linux",
307 context: { terminalFocus: true },
308 }),
309 );
310 assert.isTrue(
311 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
312 platform: "Linux",
313 context: { terminalFocus: true },
314 }),
315 );
316 });
317​
318 it("supports when expressions", () => {
319 const keybindings = compile([
320 {
321 shortcut: modShortcut("\\"),
322 command: "terminal.split",
323 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
324 },
325 {
326 shortcut: modShortcut("n", { shiftKey: true }),
327 command: "terminal.new",
328 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
329 },
330 { shortcut: modShortcut("j"), command: "terminal.toggle" },
331 ]);
332 assert.isTrue(
333 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
334 platform: "Win32",
335 context: { terminalOpen: true, terminalFocus: false },
336 }),
337 );
338 assert.isFalse(
339 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
340 platform: "Win32",
341 context: { terminalOpen: false, terminalFocus: false },
342 }),
343 );
344 assert.isTrue(
345 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
346 platform: "Win32",
347 context: { terminalOpen: true, terminalFocus: false },
348 }),
349 );
350 });
351​
352 it("supports when boolean literals", () => {
353 const keybindings = compile([
354 { shortcut: modShortcut("n"), command: "terminal.new", whenAst: whenIdentifier("true") },
355 { shortcut: modShortcut("m"), command: "terminal.new", whenAst: whenIdentifier("false") },
356 ]);
357​
358 assert.isTrue(
359 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
360 platform: "Linux",
361 }),
362 );
363 assert.isFalse(
364 isTerminalNewShortcut(event({ key: "m", ctrlKey: true }), keybindings, {
365 platform: "Linux",
366 }),
367 );
368 });
369});
370​
371describe("shortcutLabelForCommand", () => {
372 it("returns the effective binding label", () => {
373 const bindings = compile([
374 {
375 shortcut: modShortcut("\\"),
376 command: "terminal.split",
377 whenAst: whenIdentifier("terminalFocus"),
378 },
379 {
380 shortcut: modShortcut("\\", { shiftKey: true }),
381 command: "terminal.split",
382 whenAst: whenNot(whenIdentifier("terminalFocus")),
383 },
384 ]);
385 assert.strictEqual(
386 shortcutLabelForCommand(bindings, "terminal.split", {
387 platform: "Linux",
388 context: { terminalFocus: false },
389 }),
390 "Ctrl+Shift+\\",
391 );
392 });
393​
394 it("returns effective labels for non-terminal commands", () => {
395 assert.strictEqual(
396 shortcutLabelForCommand(DEFAULT_BINDINGS, "sidebar.toggle", "MacIntel"),
397 "⌘B",
398 );
399 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
400 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");
401 assert.strictEqual(
402 shortcutLabelForCommand(DEFAULT_BINDINGS, "rightPanel.toggle", "MacIntel"),
403 "⌥⌘B",
404 );
405 assert.strictEqual(
406 shortcutLabelForCommand(DEFAULT_BINDINGS, "commandPalette.toggle", "MacIntel"),
407 "⌘K",
408 );
409 assert.strictEqual(
410 shortcutLabelForCommand(DEFAULT_BINDINGS, "filePicker.toggle", "MacIntel"),
411 "⌘P",
412 );
413 assert.strictEqual(
414 shortcutLabelForCommand(DEFAULT_BINDINGS, "projectSearch.toggle", "MacIntel"),
415 "⇧⌘F",
416 );
417 assert.strictEqual(
418 shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.toggle", "Linux"),
419 "Ctrl+Shift+M",
420 );
421 assert.strictEqual(
422 shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
423 "Ctrl+O",
424 );
425 assert.strictEqual(
426 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.jump.3", "MacIntel"),
427 "⌘3",
428 );
429 assert.strictEqual(
430 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.previous", "Linux"),
431 "Ctrl+Shift+[",
432 );
433 assert.strictEqual(
434 shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.jump.3", {
435 platform: "MacIntel",
436 context: { modelPickerOpen: true },
437 }),
438 "⌘3",
439 );
440 });
441​
442 it("returns null for commands shadowed by a later conflicting shortcut", () => {
443 const bindings = compile([
444 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.1" },
445 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.7" },
446 ]);
447​
448 assert.isNull(shortcutLabelForCommand(bindings, "thread.jump.1", "MacIntel"));
449 assert.strictEqual(shortcutLabelForCommand(bindings, "thread.jump.7", "MacIntel"), "⇧⌘1");
450 });
451​
452 it("respects when-context while resolving labels", () => {
453 const bindings = compile([
454 { shortcut: modShortcut("d"), command: "diff.toggle" },
455 {
456 shortcut: modShortcut("d"),
457 command: "terminal.split",
458 whenAst: whenIdentifier("terminalFocus"),
459 },
460 ]);
461​
462 assert.strictEqual(
463 shortcutLabelForCommand(bindings, "diff.toggle", {
464 platform: "Linux",
465 context: { terminalFocus: false },
466 }),
467 "Ctrl+D",
468 );
469 assert.isNull(
470 shortcutLabelForCommand(bindings, "diff.toggle", {
471 platform: "Linux",
472 context: { terminalFocus: true },
473 }),
474 );
475 assert.strictEqual(
476 shortcutLabelForCommand(bindings, "terminal.split", {
477 platform: "Linux",
478 context: { terminalFocus: true },
479 }),
480 "Ctrl+D",
481 );
482 });
483});
484​
485describe("thread navigation helpers", () => {
486 it("maps jump commands to visible thread indices", () => {
487 assert.strictEqual(threadJumpCommandForIndex(0), "thread.jump.1");
488 assert.strictEqual(threadJumpCommandForIndex(2), "thread.jump.3");
489 assert.isNull(threadJumpCommandForIndex(9));
490 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.1"), 0);
491 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.3"), 2);
492 assert.isNull(threadJumpIndexFromCommand("thread.next"));
493 });
494​
495 it("maps traversal commands to directions", () => {
496 assert.strictEqual(threadTraversalDirectionFromCommand("thread.previous"), "previous");
497 assert.strictEqual(threadTraversalDirectionFromCommand("thread.next"), "next");
498 assert.isNull(threadTraversalDirectionFromCommand("thread.jump.1"));
499 assert.isNull(threadTraversalDirectionFromCommand(null));
500 });
501​
502 it("shows jump hints only when configured modifiers match", () => {
503 assert.isTrue(
504 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
505 platform: "MacIntel",
506 }),
507 );
508 assert.isFalse(
509 shouldShowThreadJumpHintsForModifiers(
510 event({ metaKey: true, shiftKey: true }),
511 DEFAULT_BINDINGS,
512 {
513 platform: "MacIntel",
514 },
515 ),
516 );
517 assert.isTrue(
518 shouldShowThreadJumpHintsForModifiers(event({ ctrlKey: true }), DEFAULT_BINDINGS, {
519 platform: "Linux",
520 }),
521 );
522 });
523​
524 it("never shows jump hints while the terminal is focused, even with an unrestricted binding", () => {
525 assert.isFalse(
526 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
527 platform: "MacIntel",
528 context: { terminalFocus: true },
529 }),
530 );
531 assert.isTrue(
532 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
533 platform: "MacIntel",
534 context: { terminalFocus: false },
535 }),
536 );
537 });
538});
539​
540describe("model picker navigation helpers", () => {
541 it("maps jump commands to visible model indices", () => {
542 assert.strictEqual(modelPickerJumpCommandForIndex(0), "modelPicker.jump.1");
543 assert.strictEqual(modelPickerJumpCommandForIndex(2), "modelPicker.jump.3");
544 assert.isNull(modelPickerJumpCommandForIndex(9));
545 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.1"), 0);
546 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.3"), 2);
547 assert.isNull(modelPickerJumpIndexFromCommand("thread.jump.1"));
548 });
549});
550​
551describe("chat/editor shortcuts", () => {
552 it("matches chat.new shortcut", () => {
553 assert.strictEqual(
554 resolveShortcutCommand(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
555 platform: "MacIntel",
556 }),
557 "chat.new",
558 );
559 assert.strictEqual(
560 resolveShortcutCommand(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
561 platform: "Linux",
562 }),
563 "chat.new",
564 );
565 });
566​
567 it("matches chat.newLocal shortcut", () => {
568 assert.strictEqual(
569 resolveShortcutCommand(event({ key: "n", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
570 platform: "MacIntel",
571 }),
572 "chat.newLocal",
573 );
574 assert.strictEqual(
575 resolveShortcutCommand(event({ key: "n", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
576 platform: "Linux",
577 }),
578 "chat.newLocal",
579 );
580 });
581​
582 it("matches editor.openFavorite shortcut", () => {
583 assert.isTrue(
584 isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
585 platform: "MacIntel",
586 }),
587 );
588 assert.isTrue(
589 isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
590 platform: "Linux",
591 }),
592 );
593 });
594​
595 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
596 assert.strictEqual(
597 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
598 platform: "MacIntel",
599 context: { terminalFocus: false },
600 }),
601 "commandPalette.toggle",
602 );
603 assert.notStrictEqual(
604 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
605 platform: "MacIntel",
606 context: { terminalFocus: true },
607 }),
608 "commandPalette.toggle",
609 );
610 });
611​
612 it("matches filePicker.toggle shortcut outside terminal focus", () => {
613 assert.strictEqual(
614 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
615 platform: "MacIntel",
616 context: { terminalFocus: false },
617 }),
618 "filePicker.toggle",
619 );
620 assert.notStrictEqual(
621 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
622 platform: "MacIntel",
623 context: { terminalFocus: true },
624 }),
625 "filePicker.toggle",
626 );
627 });
628​
629 it("matches projectSearch.toggle shortcut outside terminal focus", () => {
630 assert.strictEqual(
631 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
632 platform: "MacIntel",
633 context: { terminalFocus: false },
634 }),
635 "projectSearch.toggle",
636 );
637 assert.notStrictEqual(
638 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
639 platform: "MacIntel",
640 context: { terminalFocus: true },
641 }),
642 "projectSearch.toggle",
643 );
644 });
645​
646 it("matches themeEditor.toggle on macOS and Windows", () => {
647 assert.strictEqual(
648 resolveShortcutCommand(
649 event({ key: "t", metaKey: true, altKey: true, shiftKey: true }),
650 DEFAULT_BINDINGS,
651 { platform: "MacIntel" },
652 ),
653 "themeEditor.toggle",
654 );
655 assert.strictEqual(
656 resolveShortcutCommand(
657 event({ key: "t", ctrlKey: true, altKey: true, shiftKey: true }),
658 DEFAULT_BINDINGS,
659 { platform: "Win32" },
660 ),
661 "themeEditor.toggle",
662 );
663 });
664​
665 it("matches diff.toggle shortcut outside terminal focus", () => {
666 assert.isTrue(
667 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
668 platform: "MacIntel",
669 context: { terminalFocus: false },
670 }),
671 );
672 assert.isFalse(
673 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
674 platform: "MacIntel",
675 context: { terminalFocus: true },
676 }),
677 );
678 });
679});
680​
681describe("cross-command precedence", () => {
682 it("uses when + order so a later focused rule overrides a global rule", () => {
683 const keybindings = compile([
684 { shortcut: modShortcut("n"), command: "chat.new" },
685 {
686 shortcut: modShortcut("n"),
687 command: "terminal.new",
688 whenAst: whenIdentifier("terminalFocus"),
689 },
690 ]);
691​
692 assert.isTrue(
693 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
694 platform: "MacIntel",
695 context: { terminalFocus: true },
696 }),
697 );
698 assert.strictEqual(
699 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
700 platform: "MacIntel",
701 context: { terminalFocus: true },
702 }),
703 "terminal.new",
704 );
705 assert.isFalse(
706 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
707 platform: "MacIntel",
708 context: { terminalFocus: false },
709 }),
710 );
711 assert.strictEqual(
712 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
713 platform: "MacIntel",
714 context: { terminalFocus: false },
715 }),
716 "chat.new",
717 );
718 });
719​
720 it("still lets a later global rule win when both rules match", () => {
721 const keybindings = compile([
722 {
723 shortcut: modShortcut("n"),
724 command: "terminal.new",
725 whenAst: whenIdentifier("terminalFocus"),
726 },
727 { shortcut: modShortcut("n"), command: "chat.new" },
728 ]);
729​
730 assert.isFalse(
731 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
732 platform: "Linux",
733 context: { terminalFocus: true },
734 }),
735 );
736 assert.strictEqual(
737 resolveShortcutCommand(event({ key: "n", ctrlKey: true }), keybindings, {
738 platform: "Linux",
739 context: { terminalFocus: true },
740 }),
741 "chat.new",
742 );
743 });
744});
745​
746describe("resolveShortcutCommand", () => {
747 it("resolves a custom stop-thread shortcut", () => {
748 const keybindings = compile([{ shortcut: modShortcut("escape"), command: "thread.stop" }]);
749​
750 assert.strictEqual(
751 resolveShortcutCommand(event({ key: "Escape", metaKey: true }), keybindings, {
752 platform: "MacIntel",
753 }),
754 "thread.stop",
755 );
756 });
757​
758 it("honors preview conditions for a stop-thread shortcut", () => {
759 const keybindings = compile([
760 {
761 shortcut: modShortcut("escape"),
762 command: "thread.stop",
763 whenAst: whenIdentifier("previewFocus"),
764 },
765 ]);
766 const input = event({ key: "Escape", metaKey: true });
767​
768 assert.isNull(
769 resolveShortcutCommand(input, keybindings, {
770 platform: "MacIntel",
771 context: { previewFocus: false },
772 }),
773 );
774 assert.strictEqual(
775 resolveShortcutCommand(input, keybindings, {
776 platform: "MacIntel",
777 context: { previewFocus: true },
778 }),
779 "thread.stop",
780 );
781 });
782​
783 it("returns dynamic script commands", () => {
784 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
785​
786 assert.strictEqual(
787 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
788 platform: "Linux",
789 }),
790 "script.setup.run",
791 );
792 });
793​
794 it("routes mod+w to the terminal while focused and to the right panel otherwise", () => {
795 const closeEvent = event({ key: "w", metaKey: true });
796 assert.strictEqual(
797 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
798 platform: "MacIntel",
799 context: { terminalFocus: true },
800 }),
801 "terminal.close",
802 );
803 assert.strictEqual(
804 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
805 platform: "MacIntel",
806 context: { terminalFocus: false },
807 }),
808 "rightPanel.close",
809 );
810 });
811​
812 it("resolves a custom right panel maximize binding", () => {
813 const keybindings = compile([
814 {
815 shortcut: modShortcut("m", { shiftKey: true }),
816 command: "rightPanel.toggleMaximized",
817 },
818 ]);
819​
820 assert.strictEqual(
821 resolveShortcutCommand(event({ key: "m", metaKey: true, shiftKey: true }), keybindings, {
822 platform: "MacIntel",
823 }),
824 "rightPanel.toggleMaximized",
825 );
826 });
827​
828 it("matches bracket shortcuts using the physical key code", () => {
829 assert.strictEqual(
830 resolveShortcutCommand(
831 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
832 DEFAULT_BINDINGS,
833 {
834 platform: "MacIntel",
835 },
836 ),
837 "thread.previous",
838 );
839 assert.strictEqual(
840 resolveShortcutCommand(
841 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
842 DEFAULT_BINDINGS,
843 {
844 platform: "Linux",
845 },
846 ),
847 "thread.next",
848 );
849 });
850​
851 it("matches punctuation shortcuts by physical key across keyboard layouts", () => {
852 const keybindings = compile([
853 { shortcut: modShortcut("'", { shiftKey: true }), command: "diff.toggle" },
854 ]);
855​
856 assert.strictEqual(
857 resolveShortcutCommand(
858 event({ key: "@", code: "Quote", metaKey: true, shiftKey: true }),
859 keybindings,
860 { platform: "MacIntel" },
861 ),
862 "diff.toggle",
863 );
864 assert.isNull(
865 resolveShortcutCommand(
866 event({ key: '"', code: "Digit2", metaKey: true, shiftKey: true }),
867 keybindings,
868 { platform: "MacIntel" },
869 ),
870 );
871 });
872​
873 it("does not let a punctuation position shadow a Latin layout key", () => {
874 const keybindings = compile([
875 { shortcut: modShortcut("m"), command: "diff.toggle" },
876 { shortcut: modShortcut(";"), command: "sidebar.toggle" },
877 ]);
878​
879 assert.strictEqual(
880 resolveShortcutCommand(event({ key: "m", code: "Semicolon", metaKey: true }), keybindings, {
881 platform: "MacIntel",
882 }),
883 "diff.toggle",
884 );
885 });
886​
887 it("matches Option-modified letters using the physical key code on macOS", () => {
888 assert.strictEqual(
889 resolveShortcutCommand(
890 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
891 DEFAULT_BINDINGS,
892 { platform: "MacIntel" },
893 ),
894 "rightPanel.toggle",
895 );
896 });
897​
898 it("matches non-Latin layout letters using the physical key code", () => {
899 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
900​
901 assert.strictEqual(
902 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
903 platform: "MacIntel",
904 }),
905 "diff.toggle",
906 );
907 });
908​
909 it("ignores the physical key code when the layout types a different Latin letter", () => {
910 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
911​
912 // On a remapped layout the physical D key types "a"; only the physical
913 // key whose layout output is "d" may trigger the shortcut.
914 assert.isNull(
915 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
916 platform: "MacIntel",
917 }),
918 );
919 assert.strictEqual(
920 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
921 platform: "MacIntel",
922 }),
923 "diff.toggle",
924 );
925 });
926});
927​
928describe("formatShortcutLabel", () => {
929 it("formats labels for macOS", () => {
930 assert.strictEqual(
931 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
932 "⇧⌘D",
933 );
934 });
935​
936 it("formats labels for non-macOS", () => {
937 assert.strictEqual(
938 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
939 "Ctrl+Shift+D",
940 );
941 });
942​
943 it("formats labels for plus key", () => {
944 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
945 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
946 });
947});
948​
949describe("isTerminalClearShortcut", () => {
950 it("matches Ctrl+L on all platforms", () => {
951 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
952 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
953 });
954​
955 it("matches Cmd+K on macOS", () => {
956 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
957 });
958​
959 it("ignores non-keydown events", () => {
960 assert.isFalse(
961 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
962 );
963 });
964});
965​
966describe("isRichTextBoldShortcut", () => {
967 it("matches Mod+B without extra modifiers", () => {
968 assert.isTrue(isRichTextBoldShortcut(event({ key: "b", metaKey: true })));
969 assert.isTrue(isRichTextBoldShortcut(event({ key: "B", ctrlKey: true })));
970 });
971​
972 it("ignores shifted, alted, bare, and non-keydown presses", () => {
973 assert.isFalse(isRichTextBoldShortcut(event({ key: "b", metaKey: true, shiftKey: true })));
974 assert.isFalse(isRichTextBoldShortcut(event({ key: "b", metaKey: true, altKey: true })));
975 assert.isFalse(isRichTextBoldShortcut(event({ key: "b" })));
976 assert.isFalse(isRichTextBoldShortcut(event({ key: "i", metaKey: true })));
977 assert.isFalse(isRichTextBoldShortcut(event({ type: "keyup", key: "b", metaKey: true })));
978 });
979});
980​
981describe("terminalDeleteShortcutData", () => {
982 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
983 assert.strictEqual(
984 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
985 "\u0015",
986 );
987 });
988​
989 it("ignores non-macOS platforms and modified variants", () => {
990 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
991 assert.isNull(
992 terminalDeleteShortcutData(
993 event({ key: "Backspace", metaKey: true, altKey: true }),
994 "MacIntel",
995 ),
996 );
997 });
998​
999 it("ignores non-keydown events", () => {
1000 assert.isNull(
1001 terminalDeleteShortcutData(
1002 event({ type: "keyup", key: "Backspace", metaKey: true }),
1003 "MacIntel",
1004 ),
1005 );
1006 });
1007});
1008​
1009describe("terminalNavigationShortcutData", () => {
1010 it("maps Option+Arrow on macOS to word movement", () => {
1011 assert.strictEqual(
1012 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
1013 "\u001bb",
1014 );
1015 assert.strictEqual(
1016 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
1017 "\u001bf",
1018 );
1019 });
1020​
1021 it("maps Cmd+Arrow on macOS to line movement", () => {
1022 assert.strictEqual(
1023 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
1024 "\u0001",
1025 );
1026 assert.strictEqual(
1027 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
1028 "\u0005",
1029 );
1030 });
1031​
1032 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
1033 assert.strictEqual(
1034 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
1035 "\u001bb",
1036 );
1037 assert.strictEqual(
1038 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
1039 "\u001bf",
1040 );
1041 });
1042​
1043 it("rejects unsupported combinations", () => {
1044 assert.isNull(
1045 terminalNavigationShortcutData(
1046 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
1047 "MacIntel",
1048 ),
1049 );
1050 assert.isNull(
1051 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
1052 );
1053 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
1054 });
1055​
1056 it("ignores non-keydown events", () => {
1057 assert.isNull(
1058 terminalNavigationShortcutData(
1059 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
1060 "MacIntel",
1061 ),
1062 );
1063 });
1064});
1065​
1066describe("plus key parsing", () => {
1067 it("matches the plus key shortcut", () => {
1068 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
1069 assert.isTrue(
1070 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
1071 platform: "MacIntel",
1072 }),
1073 );
1074 assert.isTrue(
1075 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
1076 platform: "Linux",
1077 }),
1078 );
1079 });
1080});
1081​
1082describe("composer and pull request shortcuts", () => {
1083 it("fills missing number shortcuts without replacing the saved URL binding", () => {
1084 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
1085 (binding) =>
1086 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
1087 );
1088 const bindings = mergeWithDefaultKeybindings([
1089 ...olderServerBindings,
1090 ...compileResolvedKeybindingsConfig([
1091 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
1092 ]),
1093 ]);
1094 for (const [key, command] of [
1095 ["k", "pullRequest.copyNumber"],
1096 ["8", "thread.copyReference"],
1097 ["c", null],
1098 ["y", null],
1099 ] as const) {
1100 assert.strictEqual(
1101 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
1102 platform: "MacIntel",
1103 }),
1104 command,
1105 );
1107 });
1108​
1109 it.each(["terminalOpen", "previewFocus", "previewOpen", "modelPickerOpen"])(
1110 "honors custom PR shortcut conditions for %s",
1111 (condition) => {
1112 const bindings = compileResolvedKeybindingsConfig([
1113 { key: "mod+shift+k", command: "thread.copyReference", when: condition },
1114 { key: "mod+shift+k", command: "pullRequest.copyNumber", when: `!${condition}` },
1115 ]);
1116 const input = event({ key: "k", ctrlKey: true, shiftKey: true });
1117 for (const enabled of [false, true]) {
1118 assert.strictEqual(
1119 resolveShortcutCommand(input, bindings, {
1120 platform: "Linux",
1121 context: { [condition]: enabled },
1122 }),
1123 enabled ? "thread.copyReference" : "pullRequest.copyNumber",
1124 );
1126 },
1127 );
1128​
1129 const shortcuts = [
1130 ["h", "composer.host"],
1131 ["e", "composer.effort"],
1132 ["a", "composer.mode"],
1133 ["x", "composer.workspace"],
1134 ["g", "composer.branch"],
1135 ["l", "composer.previousWorktree"],
1136 ["c", "thread.copyReference"],
1137 ["k", "pullRequest.copyNumber"],
1138 ["Enter", "thread.steerQueuedMessage"],
1139 ] as const;
1140​
1141 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1142 it.each(shortcuts)(
1143 `resolves %s on ${platform} and leaves terminal input alone`,
1144 (key, command) => {
1145 const input = event({
1146 key,
1147 shiftKey: true,
1148 metaKey: platform === "MacIntel",
1149 ctrlKey: platform !== "MacIntel",
1150 });
1151 assert.strictEqual(
1152 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1153 platform,
1154 context: { terminalFocus: false },
1155 }),
1156 command,
1157 );
1158 assert.isNull(
1159 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1160 platform,
1161 context: { terminalFocus: true },
1162 }),
1163 );
1164 },
1165 );
1167​
1168 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1169 it.each([
1170 ["s", "thread.settle"],
1171 ["p", "thread.pin"],
1172 ])(`preserves the existing %s shortcut on ${platform}`, (key, command) => {
1173 assert.strictEqual(
1174 resolveShortcutCommand(
1175 event({
1176 key,
1177 shiftKey: true,
1178 metaKey: platform === "MacIntel",
1179 ctrlKey: platform !== "MacIntel",
1180 }),
1181 DEFAULT_RESOLVED_KEYBINDINGS,
1182 { platform },
1183 ),
1184 command,
1185 );
1186 });
1188​
1189 const altEffortBindings = compileResolvedKeybindingsConfig([
1190 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
1191 ]);
1192​
1193 it("leaves AltGr text entry alone with a custom Alt binding", () => {
1194 for (const platform of ["Win32", "Linux"]) {
1195 const input = event({
1196 key: "€",
1197 code: "KeyE",
1198 ctrlKey: true,
1199 altKey: true,
1200 getModifierState: (key) => key === "AltGraph",
1201 });
1202 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
1203 assert.strictEqual(
1204 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
1205 platform,
1206 }),
1207 "composer.effort",
1208 );
1210 });
1211​
1212 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
1213 const getModifierState = (key: string) => key === "AltGraph";
1214 assert.strictEqual(
1215 resolveShortcutCommand(
1216 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
1217 altEffortBindings,
1218 { platform: "Win32" },
1219 ),
1220 "composer.effort",
1221 );
1222 assert.strictEqual(
1223 resolveShortcutCommand(
1224 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
1225 altEffortBindings,
1226 { platform: "MacIntel" },
1227 ),
1228 "composer.effort",
1229 );
1230 });
1231​
1232 it.each(shortcuts)("uses a custom binding for %s", (_key, command) => {
1233 const bindings = compileResolvedKeybindingsConfig([
1234 { key: "mod+shift+y", command, when: "!terminalFocus" },
1235 ]);
1236 assert.strictEqual(
1237 resolveShortcutCommand(
1238 event({ key: "Y", code: "KeyY", ctrlKey: true, shiftKey: true }),
1239 bindings,
1240 { platform: "Linux" },
1241 ),
1242 command,
1243 );
1244 });
1245​
1246 for (const platform of ["MacIntel", "Win32", "Linux"]) {
1247 it.each([
1248 ["ArrowUp", "modelPicker.previousProvider"],
1249 ["ArrowDown", "modelPicker.nextProvider"],
1250 ] as const)(`limits %s to the model picker on ${platform}`, (key, command) => {
1251 const input = event({
1252 key,
1253 shiftKey: true,
1254 metaKey: platform === "MacIntel",
1255 ctrlKey: platform !== "MacIntel",
1256 });
1257 assert.strictEqual(
1258 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1259 platform,
1260 context: { modelPickerOpen: true },
1261 }),
1262 command,
1263 );
1264 assert.isNull(
1265 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
1266 platform,
1267 context: { modelPickerOpen: false },
1268 }),
1269 );
1270 });
1272});