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 });
517
518 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
519 assert.strictEqual(
520 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
521 platform: "MacIntel",
522 context: { terminalFocus: false },
523 }),
524 "commandPalette.toggle",
525 );
526 assert.notStrictEqual(
527 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
528 platform: "MacIntel",
529 context: { terminalFocus: true },
530 }),
531 "commandPalette.toggle",
532 );
533 });
534
535 it("matches filePicker.toggle shortcut outside terminal focus", () => {
536 assert.strictEqual(
537 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
538 platform: "MacIntel",
539 context: { terminalFocus: false },
540 }),
541 "filePicker.toggle",
542 );
543 assert.notStrictEqual(
544 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
545 platform: "MacIntel",
546 context: { terminalFocus: true },
547 }),
548 "filePicker.toggle",
549 );
550 });
551
552 it("matches projectSearch.toggle shortcut outside terminal focus", () => {
553 assert.strictEqual(
554 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
555 platform: "MacIntel",
556 context: { terminalFocus: false },
557 }),
558 "projectSearch.toggle",
559 );
560 assert.notStrictEqual(
561 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
562 platform: "MacIntel",
563 context: { terminalFocus: true },
564 }),
565 "projectSearch.toggle",
566 );
567 });
568
569 it("matches diff.toggle shortcut outside terminal focus", () => {
570 assert.isTrue(
571 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
572 platform: "MacIntel",
573 context: { terminalFocus: false },
574 }),
575 );
576 assert.isFalse(
577 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
578 platform: "MacIntel",
579 context: { terminalFocus: true },
580 }),
581 );
582 });
583});
584
585describe("cross-command precedence", () => {
586 it("uses when + order so a later focused rule overrides a global rule", () => {
587 const keybindings = compile([
588 { shortcut: modShortcut("n"), command: "chat.new" },
589 {
590 shortcut: modShortcut("n"),
591 command: "terminal.new",
592 whenAst: whenIdentifier("terminalFocus"),
593 },
594 ]);
595
596 assert.isTrue(
597 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
598 platform: "MacIntel",
599 context: { terminalFocus: true },
600 }),
601 );
602 assert.isFalse(
603 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
604 platform: "MacIntel",
605 context: { terminalFocus: true },
606 }),
607 );
608 assert.isFalse(
609 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
610 platform: "MacIntel",
611 context: { terminalFocus: false },
612 }),
613 );
614 assert.isTrue(
615 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
616 platform: "MacIntel",
617 context: { terminalFocus: false },
618 }),
619 );
620 });
621
622 it("still lets a later global rule win when both rules match", () => {
623 const keybindings = compile([
624 {
625 shortcut: modShortcut("n"),
626 command: "terminal.new",
627 whenAst: whenIdentifier("terminalFocus"),
628 },
629 { shortcut: modShortcut("n"), command: "chat.new" },
630 ]);
631
632 assert.isFalse(
633 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
634 platform: "Linux",
635 context: { terminalFocus: true },
636 }),
637 );
638 assert.isTrue(
639 isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
640 platform: "Linux",
641 context: { terminalFocus: true },
642 }),
643 );
644 });
645});
646
647describe("resolveShortcutCommand", () => {
648 it("returns dynamic script commands", () => {
649 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
650
651 assert.strictEqual(
652 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
653 platform: "Linux",
654 }),
655 "script.setup.run",
656 );
657 });
658
659 it("matches bracket shortcuts using the physical key code", () => {
660 assert.strictEqual(
661 resolveShortcutCommand(
662 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
663 DEFAULT_BINDINGS,
664 {
665 platform: "MacIntel",
666 },
667 ),
668 "thread.previous",
669 );
670 assert.strictEqual(
671 resolveShortcutCommand(
672 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
673 DEFAULT_BINDINGS,
674 {
675 platform: "Linux",
676 },
677 ),
678 "thread.next",
679 );
680 });
681
682 it("matches Option-modified letters using the physical key code on macOS", () => {
683 assert.strictEqual(
684 resolveShortcutCommand(
685 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
686 DEFAULT_BINDINGS,
687 { platform: "MacIntel" },
688 ),
689 "rightPanel.toggle",
690 );
691 });
692});
693
694describe("formatShortcutLabel", () => {
695 it("formats labels for macOS", () => {
696 assert.strictEqual(
697 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
698 "⇧⌘D",
699 );
700 });
701
702 it("formats labels for non-macOS", () => {
703 assert.strictEqual(
704 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
705 "Ctrl+Shift+D",
706 );
707 });
708
709 it("formats labels for plus key", () => {
710 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
711 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
712 });
713});
714
715describe("isTerminalClearShortcut", () => {
716 it("matches Ctrl+L on all platforms", () => {
717 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
718 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
719 });
720
721 it("matches Cmd+K on macOS", () => {
722 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
723 });
724
725 it("ignores non-keydown events", () => {
726 assert.isFalse(
727 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
728 );
729 });
730});
731
732describe("terminalDeleteShortcutData", () => {
733 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
734 assert.strictEqual(
735 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
736 "\u0015",
737 );
738 });
739
740 it("ignores non-macOS platforms and modified variants", () => {
741 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
742 assert.isNull(
743 terminalDeleteShortcutData(
744 event({ key: "Backspace", metaKey: true, altKey: true }),
745 "MacIntel",
746 ),
747 );
748 });
749
750 it("ignores non-keydown events", () => {
751 assert.isNull(
752 terminalDeleteShortcutData(
753 event({ type: "keyup", key: "Backspace", metaKey: true }),
754 "MacIntel",
755 ),
756 );
757 });
758});
759
760describe("terminalNavigationShortcutData", () => {
761 it("maps Option+Arrow on macOS to word movement", () => {
762 assert.strictEqual(
763 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
764 "\u001bb",
765 );
766 assert.strictEqual(
767 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
768 "\u001bf",
769 );
770 });
771
772 it("maps Cmd+Arrow on macOS to line movement", () => {
773 assert.strictEqual(
774 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
775 "\u0001",
776 );
777 assert.strictEqual(
778 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
779 "\u0005",
780 );
781 });
782
783 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
784 assert.strictEqual(
785 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
786 "\u001bb",
787 );
788 assert.strictEqual(
789 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
790 "\u001bf",
791 );
792 });
793
794 it("rejects unsupported combinations", () => {
795 assert.isNull(
796 terminalNavigationShortcutData(
797 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
798 "MacIntel",
799 ),
800 );
801 assert.isNull(
802 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
803 );
804 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
805 });
806
807 it("ignores non-keydown events", () => {
808 assert.isNull(
809 terminalNavigationShortcutData(
810 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
811 "MacIntel",
812 ),
813 );
814 });
815});
816
817describe("plus key parsing", () => {
818 it("matches the plus key shortcut", () => {
819 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
820 assert.isTrue(
821 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
822 platform: "MacIntel",
823 }),
824 );
825 assert.isTrue(
826 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
827 platform: "Linux",
828 }),
829 );
830 });
831});