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