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