Open History to see the last commit
1import { assert, describe, it } from "vite-plus/test";
2
3import {
4 type KeybindingCommand,
5 type KeybindingShortcut,
6 type KeybindingWhenNode,
7 type ResolvedKeybindingsConfig,
8} from "@t3tools/contracts";
9import {
10 formatShortcutLabel,
11 isDiffToggleShortcut,
12 modelPickerJumpCommandForIndex,
13 modelPickerJumpIndexFromCommand,
14 isOpenFavoriteEditorShortcut,
15 isTerminalClearShortcut,
16 isTerminalCloseShortcut,
17 isTerminalNewShortcut,
18 isTerminalSplitShortcut,
19 isTerminalSplitVerticalShortcut,
20 isTerminalToggleShortcut,
21 resolveShortcutCommand,
22 shouldShowModelPickerJumpHints,
23 shouldShowThreadJumpHintsForModifiers,
24 shortcutLabelForCommand,
25 terminalDeleteShortcutData,
26 terminalNavigationShortcutData,
27 threadJumpCommandForIndex,
28 threadJumpIndexFromCommand,
29 threadTraversalDirectionFromCommand,
30 type ShortcutEventLike,
31} from "./keybindings";
32
33function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
34 return {
35 key: "j",
36 metaKey: false,
37 ctrlKey: false,
38 shiftKey: false,
39 altKey: false,
40 ...overrides,
41 };
42}
43
44function modShortcut(
45 key: string,
46 overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
47): KeybindingShortcut {
48 return {
49 key,
50 metaKey: false,
51 ctrlKey: false,
52 shiftKey: false,
53 altKey: false,
54 modKey: true,
55 ...overrides,
56 };
57}
58
59function whenIdentifier(name: string): KeybindingWhenNode {
60 return { type: "identifier", name };
61}
62
63function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
64 return { type: "not", node };
65}
66
67function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
68 return { type: "and", left, right };
69}
70
71interface TestBinding {
72 shortcut: KeybindingShortcut;
73 command: KeybindingCommand;
74 whenAst?: KeybindingWhenNode;
75}
76
77function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
78 return bindings.map((binding) => ({
79 command: binding.command,
80 shortcut: binding.shortcut,
81 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
82 }));
83}
84
85const DEFAULT_BINDINGS = compile([
86 { shortcut: modShortcut("b"), command: "sidebar.toggle" },
87 { shortcut: modShortcut("j"), command: "terminal.toggle" },
88 { shortcut: modShortcut("b", { altKey: true }), command: "rightPanel.toggle" },
89 {
90 shortcut: modShortcut("d"),
91 command: "terminal.split",
92 whenAst: whenIdentifier("terminalFocus"),
93 },
94 {
95 shortcut: modShortcut("d", { shiftKey: true }),
96 command: "terminal.splitVertical",
97 whenAst: whenIdentifier("terminalFocus"),
98 },
99 {
100 shortcut: modShortcut("n"),
101 command: "terminal.new",
102 whenAst: whenIdentifier("terminalFocus"),
103 },
104 {
105 shortcut: modShortcut("w"),
106 command: "terminal.close",
107 whenAst: whenIdentifier("terminalFocus"),
108 },
109 {
110 shortcut: modShortcut("w"),
111 command: "rightPanel.close",
112 whenAst: whenNot(whenIdentifier("terminalFocus")),
113 },
114 {
115 shortcut: modShortcut("d"),
116 command: "diff.toggle",
117 whenAst: whenNot(whenIdentifier("terminalFocus")),
118 },
119 {
120 shortcut: modShortcut("k"),
121 command: "commandPalette.toggle",
122 whenAst: whenNot(whenIdentifier("terminalFocus")),
123 },
124 {
125 shortcut: modShortcut("p"),
126 command: "filePicker.toggle",
127 whenAst: whenNot(whenIdentifier("terminalFocus")),
128 },
129 {
130 shortcut: modShortcut("f", { shiftKey: true }),
131 command: "projectSearch.toggle",
132 whenAst: whenNot(whenIdentifier("terminalFocus")),
133 },
134 {
135 shortcut: modShortcut("t", { altKey: true, shiftKey: true }),
136 command: "themeEditor.toggle",
137 },
138 {
139 shortcut: modShortcut("m", { shiftKey: true }),
140 command: "modelPicker.toggle",
141 whenAst: whenNot(whenIdentifier("terminalFocus")),
142 },
143 { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
144 { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
145 { shortcut: modShortcut("o"), command: "editor.openFavorite" },
146 { shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },
147 { shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },
148 {
149 shortcut: modShortcut("c", { shiftKey: true }),
150 command: "thread.copyReference",
151 whenAst: whenNot(whenIdentifier("terminalFocus")),
152 },
153 {
154 shortcut: modShortcut("s", { shiftKey: true }),
155 command: "thread.settle",
156 whenAst: whenNot(whenIdentifier("terminalFocus")),
157 },
158 { shortcut: modShortcut("1"), command: "thread.jump.1" },
159 { shortcut: modShortcut("2"), command: "thread.jump.2" },
160 { shortcut: modShortcut("3"), command: "thread.jump.3" },
161 {
162 shortcut: modShortcut("1"),
163 command: "modelPicker.jump.1",
164 whenAst: whenIdentifier("modelPickerOpen"),
165 },
166 {
167 shortcut: modShortcut("2"),
168 command: "modelPicker.jump.2",
169 whenAst: whenIdentifier("modelPickerOpen"),
170 },
171 {
172 shortcut: modShortcut("3"),
173 command: "modelPicker.jump.3",
174 whenAst: whenIdentifier("modelPickerOpen"),
175 },
176]);
177
178describe("isTerminalToggleShortcut", () => {
179 it("matches Cmd+J on macOS", () => {
180 assert.isTrue(
181 isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, {
182 platform: "MacIntel",
183 }),
184 );
185 });
186
187 it("matches Ctrl+J on non-macOS", () => {
188 assert.isTrue(
189 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }),
190 );
191 });
192
193 it("matches Ctrl+J on non-macOS while terminalFocus is true", () => {
194 assert.isTrue(
195 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, {
196 platform: "Win32",
197 context: { terminalFocus: true },
198 }),
199 );
200 });
201});
202
203describe("settle thread shortcut", () => {
204 it("resolves outside the terminal", () => {
205 assert.equal(
206 resolveShortcutCommand(event({ key: "s", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
207 platform: "MacIntel",
208 context: { terminalFocus: false },
209 }),
210 "thread.settle",
211 );
212 });
213
214 it("does not intercept the terminal", () => {
215 assert.isNull(
216 resolveShortcutCommand(event({ key: "s", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
217 platform: "Win32",
218 context: { terminalFocus: true },
219 }),
220 );
221 });
222});
223
224describe("copy thread reference shortcut", () => {
225 it("resolves Cmd+Shift+C on macOS and Ctrl+Shift+C elsewhere", () => {
226 assert.equal(
227 resolveShortcutCommand(event({ key: "c", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
228 platform: "MacIntel",
229 }),
230 "thread.copyReference",
231 );
232 assert.equal(
233 resolveShortcutCommand(event({ key: "c", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
234 platform: "Linux",
235 }),
236 "thread.copyReference",
237 );
238 });
239
240 it("leaves terminal copy untouched", () => {
241 assert.isNull(
242 resolveShortcutCommand(event({ key: "c", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
243 platform: "Linux",
244 context: { terminalFocus: true },
245 }),
246 );
247 });
248});
249
250describe("split/new/close terminal shortcuts", () => {
251 it("requires terminalFocus for default split/new/close bindings", () => {
252 assert.isFalse(
253 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
254 platform: "MacIntel",
255 context: { terminalFocus: false },
256 }),
257 );
258 assert.isFalse(
259 isTerminalSplitVerticalShortcut(
260 event({ key: "d", metaKey: true, shiftKey: true }),
261 DEFAULT_BINDINGS,
262 {
263 platform: "MacIntel",
264 context: { terminalFocus: false },
265 },
266 ),
267 );
268 assert.isFalse(
269 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), DEFAULT_BINDINGS, {
270 platform: "Linux",
271 context: { terminalFocus: false },
272 }),
273 );
274 assert.isFalse(
275 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
276 platform: "Linux",
277 context: { terminalFocus: false },
278 }),
279 );
280 });
281
282 it("matches split/new when terminalFocus is true", () => {
283 assert.isTrue(
284 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
285 platform: "MacIntel",
286 context: { terminalFocus: true },
287 }),
288 );
289 assert.isTrue(
290 isTerminalSplitVerticalShortcut(
291 event({ key: "d", metaKey: true, shiftKey: true }),
292 DEFAULT_BINDINGS,
293 {
294 platform: "MacIntel",
295 context: { terminalFocus: true },
296 },
297 ),
298 );
299 assert.isTrue(
300 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), DEFAULT_BINDINGS, {
301 platform: "Linux",
302 context: { terminalFocus: true },
303 }),
304 );
305 assert.isTrue(
306 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
307 platform: "Linux",
308 context: { terminalFocus: true },
309 }),
310 );
311 });
312
313 it("supports when expressions", () => {
314 const keybindings = compile([
315 {
316 shortcut: modShortcut("\\"),
317 command: "terminal.split",
318 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
319 },
320 {
321 shortcut: modShortcut("n", { shiftKey: true }),
322 command: "terminal.new",
323 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
324 },
325 { shortcut: modShortcut("j"), command: "terminal.toggle" },
326 ]);
327 assert.isTrue(
328 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
329 platform: "Win32",
330 context: { terminalOpen: true, terminalFocus: false },
331 }),
332 );
333 assert.isFalse(
334 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
335 platform: "Win32",
336 context: { terminalOpen: false, terminalFocus: false },
337 }),
338 );
339 assert.isTrue(
340 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
341 platform: "Win32",
342 context: { terminalOpen: true, terminalFocus: false },
343 }),
344 );
345 });
346
347 it("supports when boolean literals", () => {
348 const keybindings = compile([
349 { shortcut: modShortcut("n"), command: "terminal.new", whenAst: whenIdentifier("true") },
350 { shortcut: modShortcut("m"), command: "terminal.new", whenAst: whenIdentifier("false") },
351 ]);
352
353 assert.isTrue(
354 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
355 platform: "Linux",
356 }),
357 );
358 assert.isFalse(
359 isTerminalNewShortcut(event({ key: "m", ctrlKey: true }), keybindings, {
360 platform: "Linux",
361 }),
362 );
363 });
364});
365
366describe("shortcutLabelForCommand", () => {
367 it("returns the effective binding label", () => {
368 const bindings = compile([
369 {
370 shortcut: modShortcut("\\"),
371 command: "terminal.split",
372 whenAst: whenIdentifier("terminalFocus"),
373 },
374 {
375 shortcut: modShortcut("\\", { shiftKey: true }),
376 command: "terminal.split",
377 whenAst: whenNot(whenIdentifier("terminalFocus")),
378 },
379 ]);
380 assert.strictEqual(
381 shortcutLabelForCommand(bindings, "terminal.split", {
382 platform: "Linux",
383 context: { terminalFocus: false },
384 }),
385 "Ctrl+Shift+\\",
386 );
387 });
388
389 it("returns effective labels for non-terminal commands", () => {
390 assert.strictEqual(
391 shortcutLabelForCommand(DEFAULT_BINDINGS, "sidebar.toggle", "MacIntel"),
392 "⌘B",
393 );
394 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
395 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");
396 assert.strictEqual(
397 shortcutLabelForCommand(DEFAULT_BINDINGS, "rightPanel.toggle", "MacIntel"),
398 "⌥⌘B",
399 );
400 assert.strictEqual(
401 shortcutLabelForCommand(DEFAULT_BINDINGS, "commandPalette.toggle", "MacIntel"),
402 "⌘K",
403 );
404 assert.strictEqual(
405 shortcutLabelForCommand(DEFAULT_BINDINGS, "filePicker.toggle", "MacIntel"),
406 "⌘P",
407 );
408 assert.strictEqual(
409 shortcutLabelForCommand(DEFAULT_BINDINGS, "projectSearch.toggle", "MacIntel"),
410 "⇧⌘F",
411 );
412 assert.strictEqual(
413 shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.toggle", "Linux"),
414 "Ctrl+Shift+M",
415 );
416 assert.strictEqual(
417 shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
418 "Ctrl+O",
419 );
420 assert.strictEqual(
421 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.jump.3", "MacIntel"),
422 "⌘3",
423 );
424 assert.strictEqual(
425 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.previous", "Linux"),
426 "Ctrl+Shift+[",
427 );
428 assert.strictEqual(
429 shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.jump.3", {
430 platform: "MacIntel",
431 context: { modelPickerOpen: true },
432 }),
433 "⌘3",
434 );
435 });
436
437 it("returns null for commands shadowed by a later conflicting shortcut", () => {
438 const bindings = compile([
439 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.1" },
440 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.7" },
441 ]);
442
443 assert.isNull(shortcutLabelForCommand(bindings, "thread.jump.1", "MacIntel"));
444 assert.strictEqual(shortcutLabelForCommand(bindings, "thread.jump.7", "MacIntel"), "⇧⌘1");
445 });
446
447 it("respects when-context while resolving labels", () => {
448 const bindings = compile([
449 { shortcut: modShortcut("d"), command: "diff.toggle" },
450 {
451 shortcut: modShortcut("d"),
452 command: "terminal.split",
453 whenAst: whenIdentifier("terminalFocus"),
454 },
455 ]);
456
457 assert.strictEqual(
458 shortcutLabelForCommand(bindings, "diff.toggle", {
459 platform: "Linux",
460 context: { terminalFocus: false },
461 }),
462 "Ctrl+D",
463 );
464 assert.isNull(
465 shortcutLabelForCommand(bindings, "diff.toggle", {
466 platform: "Linux",
467 context: { terminalFocus: true },
468 }),
469 );
470 assert.strictEqual(
471 shortcutLabelForCommand(bindings, "terminal.split", {
472 platform: "Linux",
473 context: { terminalFocus: true },
474 }),
475 "Ctrl+D",
476 );
477 });
478});
479
480describe("thread navigation helpers", () => {
481 it("maps jump commands to visible thread indices", () => {
482 assert.strictEqual(threadJumpCommandForIndex(0), "thread.jump.1");
483 assert.strictEqual(threadJumpCommandForIndex(2), "thread.jump.3");
484 assert.isNull(threadJumpCommandForIndex(9));
485 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.1"), 0);
486 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.3"), 2);
487 assert.isNull(threadJumpIndexFromCommand("thread.next"));
488 });
489
490 it("maps traversal commands to directions", () => {
491 assert.strictEqual(threadTraversalDirectionFromCommand("thread.previous"), "previous");
492 assert.strictEqual(threadTraversalDirectionFromCommand("thread.next"), "next");
493 assert.isNull(threadTraversalDirectionFromCommand("thread.jump.1"));
494 assert.isNull(threadTraversalDirectionFromCommand(null));
495 });
496
497 it("shows jump hints only when configured modifiers match", () => {
498 assert.isTrue(
499 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
500 platform: "MacIntel",
501 }),
502 );
503 assert.isFalse(
504 shouldShowThreadJumpHintsForModifiers(
505 event({ metaKey: true, shiftKey: true }),
506 DEFAULT_BINDINGS,
507 {
508 platform: "MacIntel",
509 },
510 ),
511 );
512 assert.isTrue(
513 shouldShowThreadJumpHintsForModifiers(event({ ctrlKey: true }), DEFAULT_BINDINGS, {
514 platform: "Linux",
515 }),
516 );
517 });
518
519 it("never shows jump hints while the terminal is focused, even with an unrestricted binding", () => {
520 assert.isFalse(
521 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
522 platform: "MacIntel",
523 context: { terminalFocus: true },
524 }),
525 );
526 assert.isTrue(
527 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
528 platform: "MacIntel",
529 context: { terminalFocus: false },
530 }),
531 );
532 });
533});
534
535describe("model picker navigation helpers", () => {
536 it("maps jump commands to visible model indices", () => {
537 assert.strictEqual(modelPickerJumpCommandForIndex(0), "modelPicker.jump.1");
538 assert.strictEqual(modelPickerJumpCommandForIndex(2), "modelPicker.jump.3");
539 assert.isNull(modelPickerJumpCommandForIndex(9));
540 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.1"), 0);
541 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.3"), 2);
542 assert.isNull(modelPickerJumpIndexFromCommand("thread.jump.1"));
543 });
544
545 it("shows jump hints only while the model picker context is active", () => {
546 assert.isFalse(
547 shouldShowModelPickerJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
548 platform: "MacIntel",
549 context: { modelPickerOpen: false },
550 }),
551 );
552 assert.isTrue(
553 shouldShowModelPickerJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
554 platform: "MacIntel",
555 context: { modelPickerOpen: true },
556 }),
557 );
558 });
559});
560
561describe("chat/editor shortcuts", () => {
562 it("matches chat.new shortcut", () => {
563 assert.strictEqual(
564 resolveShortcutCommand(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
565 platform: "MacIntel",
566 }),
567 "chat.new",
568 );
569 assert.strictEqual(
570 resolveShortcutCommand(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
571 platform: "Linux",
572 }),
573 "chat.new",
574 );
575 });
576
577 it("matches chat.newLocal shortcut", () => {
578 assert.strictEqual(
579 resolveShortcutCommand(event({ key: "n", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
580 platform: "MacIntel",
581 }),
582 "chat.newLocal",
583 );
584 assert.strictEqual(
585 resolveShortcutCommand(event({ key: "n", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
586 platform: "Linux",
587 }),
588 "chat.newLocal",
589 );
590 });
591
592 it("matches editor.openFavorite shortcut", () => {
593 assert.isTrue(
594 isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
595 platform: "MacIntel",
596 }),
597 );
598 assert.isTrue(
599 isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
600 platform: "Linux",
601 }),
602 );
603 });
604
605 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
606 assert.strictEqual(
607 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
608 platform: "MacIntel",
609 context: { terminalFocus: false },
610 }),
611 "commandPalette.toggle",
612 );
613 assert.notStrictEqual(
614 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
615 platform: "MacIntel",
616 context: { terminalFocus: true },
617 }),
618 "commandPalette.toggle",
619 );
620 });
621
622 it("matches filePicker.toggle shortcut outside terminal focus", () => {
623 assert.strictEqual(
624 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
625 platform: "MacIntel",
626 context: { terminalFocus: false },
627 }),
628 "filePicker.toggle",
629 );
630 assert.notStrictEqual(
631 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
632 platform: "MacIntel",
633 context: { terminalFocus: true },
634 }),
635 "filePicker.toggle",
636 );
637 });
638
639 it("matches projectSearch.toggle shortcut outside terminal focus", () => {
640 assert.strictEqual(
641 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
642 platform: "MacIntel",
643 context: { terminalFocus: false },
644 }),
645 "projectSearch.toggle",
646 );
647 assert.notStrictEqual(
648 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
649 platform: "MacIntel",
650 context: { terminalFocus: true },
651 }),
652 "projectSearch.toggle",
653 );
654 });
655
656 it("matches themeEditor.toggle on macOS and Windows", () => {
657 assert.strictEqual(
658 resolveShortcutCommand(
659 event({ key: "t", metaKey: true, altKey: true, shiftKey: true }),
660 DEFAULT_BINDINGS,
661 { platform: "MacIntel" },
662 ),
663 "themeEditor.toggle",
664 );
665 assert.strictEqual(
666 resolveShortcutCommand(
667 event({ key: "t", ctrlKey: true, altKey: true, shiftKey: true }),
668 DEFAULT_BINDINGS,
669 { platform: "Win32" },
670 ),
671 "themeEditor.toggle",
672 );
673 });
674
675 it("matches diff.toggle shortcut outside terminal focus", () => {
676 assert.isTrue(
677 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
678 platform: "MacIntel",
679 context: { terminalFocus: false },
680 }),
681 );
682 assert.isFalse(
683 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
684 platform: "MacIntel",
685 context: { terminalFocus: true },
686 }),
687 );
688 });
689});
690
691describe("cross-command precedence", () => {
692 it("uses when + order so a later focused rule overrides a global rule", () => {
693 const keybindings = compile([
694 { shortcut: modShortcut("n"), command: "chat.new" },
695 {
696 shortcut: modShortcut("n"),
697 command: "terminal.new",
698 whenAst: whenIdentifier("terminalFocus"),
699 },
700 ]);
701
702 assert.isTrue(
703 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
704 platform: "MacIntel",
705 context: { terminalFocus: true },
706 }),
707 );
708 assert.strictEqual(
709 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
710 platform: "MacIntel",
711 context: { terminalFocus: true },
712 }),
713 "terminal.new",
714 );
715 assert.isFalse(
716 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
717 platform: "MacIntel",
718 context: { terminalFocus: false },
719 }),
720 );
721 assert.strictEqual(
722 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
723 platform: "MacIntel",
724 context: { terminalFocus: false },
725 }),
726 "chat.new",
727 );
728 });
729
730 it("still lets a later global rule win when both rules match", () => {
731 const keybindings = compile([
732 {
733 shortcut: modShortcut("n"),
734 command: "terminal.new",
735 whenAst: whenIdentifier("terminalFocus"),
736 },
737 { shortcut: modShortcut("n"), command: "chat.new" },
738 ]);
739
740 assert.isFalse(
741 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
742 platform: "Linux",
743 context: { terminalFocus: true },
744 }),
745 );
746 assert.strictEqual(
747 resolveShortcutCommand(event({ key: "n", ctrlKey: true }), keybindings, {
748 platform: "Linux",
749 context: { terminalFocus: true },
750 }),
751 "chat.new",
752 );
753 });
754});
755
756describe("resolveShortcutCommand", () => {
757 it("returns dynamic script commands", () => {
758 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
759
760 assert.strictEqual(
761 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
762 platform: "Linux",
763 }),
764 "script.setup.run",
765 );
766 });
767
768 it("routes mod+w to the terminal while focused and to the right panel otherwise", () => {
769 const closeEvent = event({ key: "w", metaKey: true });
770 assert.strictEqual(
771 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
772 platform: "MacIntel",
773 context: { terminalFocus: true },
774 }),
775 "terminal.close",
776 );
777 assert.strictEqual(
778 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
779 platform: "MacIntel",
780 context: { terminalFocus: false },
781 }),
782 "rightPanel.close",
783 );
784 });
785
786 it("resolves a custom right panel maximize binding", () => {
787 const keybindings = compile([
788 {
789 shortcut: modShortcut("m", { shiftKey: true }),
790 command: "rightPanel.toggleMaximized",
791 },
792 ]);
793
794 assert.strictEqual(
795 resolveShortcutCommand(event({ key: "m", metaKey: true, shiftKey: true }), keybindings, {
796 platform: "MacIntel",
797 }),
798 "rightPanel.toggleMaximized",
799 );
800 });
801
802 it("matches bracket shortcuts using the physical key code", () => {
803 assert.strictEqual(
804 resolveShortcutCommand(
805 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
806 DEFAULT_BINDINGS,
807 {
808 platform: "MacIntel",
809 },
810 ),
811 "thread.previous",
812 );
813 assert.strictEqual(
814 resolveShortcutCommand(
815 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
816 DEFAULT_BINDINGS,
817 {
818 platform: "Linux",
819 },
820 ),
821 "thread.next",
822 );
823 });
824
825 it("matches Option-modified letters using the physical key code on macOS", () => {
826 assert.strictEqual(
827 resolveShortcutCommand(
828 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
829 DEFAULT_BINDINGS,
830 { platform: "MacIntel" },
831 ),
832 "rightPanel.toggle",
833 );
834 });
835
836 it("matches non-Latin layout letters using the physical key code", () => {
837 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
838
839 assert.strictEqual(
840 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
841 platform: "MacIntel",
842 }),
843 "diff.toggle",
844 );
845 });
846
847 it("ignores the physical key code when the layout types a different Latin letter", () => {
848 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
849
850 // On a remapped layout the physical D key types "a"; only the physical
851 // key whose layout output is "d" may trigger the shortcut.
852 assert.isNull(
853 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
854 platform: "MacIntel",
855 }),
856 );
857 assert.strictEqual(
858 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
859 platform: "MacIntel",
860 }),
861 "diff.toggle",
862 );
863 });
864});
865
866describe("formatShortcutLabel", () => {
867 it("formats labels for macOS", () => {
868 assert.strictEqual(
869 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
870 "⇧⌘D",
871 );
872 });
873
874 it("formats labels for non-macOS", () => {
875 assert.strictEqual(
876 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
877 "Ctrl+Shift+D",
878 );
879 });
880
881 it("formats labels for plus key", () => {
882 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
883 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
884 });
885});
886
887describe("isTerminalClearShortcut", () => {
888 it("matches Ctrl+L on all platforms", () => {
889 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
890 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
891 });
892
893 it("matches Cmd+K on macOS", () => {
894 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
895 });
896
897 it("ignores non-keydown events", () => {
898 assert.isFalse(
899 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
900 );
901 });
902});
903
904describe("terminalDeleteShortcutData", () => {
905 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
906 assert.strictEqual(
907 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
908 "\u0015",
909 );
910 });
911
912 it("ignores non-macOS platforms and modified variants", () => {
913 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
914 assert.isNull(
915 terminalDeleteShortcutData(
916 event({ key: "Backspace", metaKey: true, altKey: true }),
917 "MacIntel",
918 ),
919 );
920 });
921
922 it("ignores non-keydown events", () => {
923 assert.isNull(
924 terminalDeleteShortcutData(
925 event({ type: "keyup", key: "Backspace", metaKey: true }),
926 "MacIntel",
927 ),
928 );
929 });
930});
931
932describe("terminalNavigationShortcutData", () => {
933 it("maps Option+Arrow on macOS to word movement", () => {
934 assert.strictEqual(
935 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
936 "\u001bb",
937 );
938 assert.strictEqual(
939 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
940 "\u001bf",
941 );
942 });
943
944 it("maps Cmd+Arrow on macOS to line movement", () => {
945 assert.strictEqual(
946 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
947 "\u0001",
948 );
949 assert.strictEqual(
950 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
951 "\u0005",
952 );
953 });
954
955 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
956 assert.strictEqual(
957 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
958 "\u001bb",
959 );
960 assert.strictEqual(
961 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
962 "\u001bf",
963 );
964 });
965
966 it("rejects unsupported combinations", () => {
967 assert.isNull(
968 terminalNavigationShortcutData(
969 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
970 "MacIntel",
971 ),
972 );
973 assert.isNull(
974 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
975 );
976 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
977 });
978
979 it("ignores non-keydown events", () => {
980 assert.isNull(
981 terminalNavigationShortcutData(
982 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
983 "MacIntel",
984 ),
985 );
986 });
987});
988
989describe("plus key parsing", () => {
990 it("matches the plus key shortcut", () => {
991 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
992 assert.isTrue(
993 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
994 platform: "MacIntel",
995 }),
996 );
997 assert.isTrue(
998 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
999 platform: "Linux",
1000 }),
1001 );
1002 });
1003});