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