Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keybindings.test.ts

899 lines26.9 KB

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