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