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

keybindings.test.ts

914 lines27.4 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 it("never shows jump hints while the terminal is focused, even with an unrestricted binding", () => {
456 assert.isFalse(
457 shouldShowThreadJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
458 platform: "MacIntel",
459 context: { terminalFocus: true },
460 }),
461 );
462 assert.isTrue(
463 shouldShowThreadJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
464 platform: "MacIntel",
465 context: { terminalFocus: false },
466 }),
467 );
468 });
469});
470​
471describe("model picker navigation helpers", () => {
472 it("maps jump commands to visible model indices", () => {
473 assert.strictEqual(modelPickerJumpCommandForIndex(0), "modelPicker.jump.1");
474 assert.strictEqual(modelPickerJumpCommandForIndex(2), "modelPicker.jump.3");
475 assert.isNull(modelPickerJumpCommandForIndex(9));
476 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.1"), 0);
477 assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.3"), 2);
478 assert.isNull(modelPickerJumpIndexFromCommand("thread.jump.1"));
479 });
480​
481 it("shows jump hints only while the model picker context is active", () => {
482 assert.isFalse(
483 shouldShowModelPickerJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
484 platform: "MacIntel",
485 context: { modelPickerOpen: false },
486 }),
487 );
488 assert.isTrue(
489 shouldShowModelPickerJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
490 platform: "MacIntel",
491 context: { modelPickerOpen: true },
492 }),
493 );
494 });
495});
496​
497describe("chat/editor shortcuts", () => {
498 it("matches chat.new shortcut", () => {
499 assert.isTrue(
500 isChatNewShortcut(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
501 platform: "MacIntel",
502 }),
503 );
504 assert.isTrue(
505 isChatNewShortcut(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
506 platform: "Linux",
507 }),
508 );
509 });
510​
511 it("matches chat.newLocal shortcut", () => {
512 assert.isTrue(
513 isChatNewLocalShortcut(event({ key: "n", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
514 platform: "MacIntel",
515 }),
516 );
517 assert.isTrue(
518 isChatNewLocalShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
519 platform: "Linux",
520 }),
521 );
522 });
523​
524 it("matches editor.openFavorite shortcut", () => {
525 assert.isTrue(
526 isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
527 platform: "MacIntel",
528 }),
529 );
530 assert.isTrue(
531 isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
532 platform: "Linux",
533 }),
534 );
535 });
536​
537 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
538 assert.strictEqual(
539 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
540 platform: "MacIntel",
541 context: { terminalFocus: false },
542 }),
543 "commandPalette.toggle",
544 );
545 assert.notStrictEqual(
546 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
547 platform: "MacIntel",
548 context: { terminalFocus: true },
549 }),
550 "commandPalette.toggle",
551 );
552 });
553​
554 it("matches filePicker.toggle shortcut outside terminal focus", () => {
555 assert.strictEqual(
556 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
557 platform: "MacIntel",
558 context: { terminalFocus: false },
559 }),
560 "filePicker.toggle",
561 );
562 assert.notStrictEqual(
563 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
564 platform: "MacIntel",
565 context: { terminalFocus: true },
566 }),
567 "filePicker.toggle",
568 );
569 });
570​
571 it("matches projectSearch.toggle shortcut outside terminal focus", () => {
572 assert.strictEqual(
573 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
574 platform: "MacIntel",
575 context: { terminalFocus: false },
576 }),
577 "projectSearch.toggle",
578 );
579 assert.notStrictEqual(
580 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
581 platform: "MacIntel",
582 context: { terminalFocus: true },
583 }),
584 "projectSearch.toggle",
585 );
586 });
587​
588 it("matches themeEditor.toggle on macOS and Windows", () => {
589 assert.strictEqual(
590 resolveShortcutCommand(
591 event({ key: "t", metaKey: true, altKey: true, shiftKey: true }),
592 DEFAULT_BINDINGS,
593 { platform: "MacIntel" },
594 ),
595 "themeEditor.toggle",
596 );
597 assert.strictEqual(
598 resolveShortcutCommand(
599 event({ key: "t", ctrlKey: true, altKey: true, shiftKey: true }),
600 DEFAULT_BINDINGS,
601 { platform: "Win32" },
602 ),
603 "themeEditor.toggle",
604 );
605 });
606​
607 it("matches diff.toggle shortcut outside terminal focus", () => {
608 assert.isTrue(
609 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
610 platform: "MacIntel",
611 context: { terminalFocus: false },
612 }),
613 );
614 assert.isFalse(
615 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
616 platform: "MacIntel",
617 context: { terminalFocus: true },
618 }),
619 );
620 });
621});
622​
623describe("cross-command precedence", () => {
624 it("uses when + order so a later focused rule overrides a global rule", () => {
625 const keybindings = compile([
626 { shortcut: modShortcut("n"), command: "chat.new" },
627 {
628 shortcut: modShortcut("n"),
629 command: "terminal.new",
630 whenAst: whenIdentifier("terminalFocus"),
631 },
632 ]);
633​
634 assert.isTrue(
635 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
636 platform: "MacIntel",
637 context: { terminalFocus: true },
638 }),
639 );
640 assert.isFalse(
641 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
642 platform: "MacIntel",
643 context: { terminalFocus: true },
644 }),
645 );
646 assert.isFalse(
647 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
648 platform: "MacIntel",
649 context: { terminalFocus: false },
650 }),
651 );
652 assert.isTrue(
653 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
654 platform: "MacIntel",
655 context: { terminalFocus: false },
656 }),
657 );
658 });
659​
660 it("still lets a later global rule win when both rules match", () => {
661 const keybindings = compile([
662 {
663 shortcut: modShortcut("n"),
664 command: "terminal.new",
665 whenAst: whenIdentifier("terminalFocus"),
666 },
667 { shortcut: modShortcut("n"), command: "chat.new" },
668 ]);
669​
670 assert.isFalse(
671 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
672 platform: "Linux",
673 context: { terminalFocus: true },
674 }),
675 );
676 assert.isTrue(
677 isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
678 platform: "Linux",
679 context: { terminalFocus: true },
680 }),
681 );
682 });
683});
684​
685describe("resolveShortcutCommand", () => {
686 it("returns dynamic script commands", () => {
687 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
688​
689 assert.strictEqual(
690 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
691 platform: "Linux",
692 }),
693 "script.setup.run",
694 );
695 });
696​
697 it("resolves a custom right panel maximize binding", () => {
698 const keybindings = compile([
699 {
700 shortcut: modShortcut("m", { shiftKey: true }),
701 command: "rightPanel.toggleMaximized",
702 },
703 ]);
704​
705 assert.strictEqual(
706 resolveShortcutCommand(event({ key: "m", metaKey: true, shiftKey: true }), keybindings, {
707 platform: "MacIntel",
708 }),
709 "rightPanel.toggleMaximized",
710 );
711 });
712​
713 it("matches bracket shortcuts using the physical key code", () => {
714 assert.strictEqual(
715 resolveShortcutCommand(
716 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
717 DEFAULT_BINDINGS,
718 {
719 platform: "MacIntel",
720 },
721 ),
722 "thread.previous",
723 );
724 assert.strictEqual(
725 resolveShortcutCommand(
726 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
727 DEFAULT_BINDINGS,
728 {
729 platform: "Linux",
730 },
731 ),
732 "thread.next",
733 );
734 });
735​
736 it("matches Option-modified letters using the physical key code on macOS", () => {
737 assert.strictEqual(
738 resolveShortcutCommand(
739 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
740 DEFAULT_BINDINGS,
741 { platform: "MacIntel" },
742 ),
743 "rightPanel.toggle",
744 );
745 });
746​
747 it("matches non-Latin layout letters using the physical key code", () => {
748 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
749​
750 assert.strictEqual(
751 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
752 platform: "MacIntel",
753 }),
754 "diff.toggle",
755 );
756 });
757​
758 it("ignores the physical key code when the layout types a different Latin letter", () => {
759 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
760​
761 // On a remapped layout the physical D key types "a"; only the physical
762 // key whose layout output is "d" may trigger the shortcut.
763 assert.isNull(
764 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
765 platform: "MacIntel",
766 }),
767 );
768 assert.strictEqual(
769 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
770 platform: "MacIntel",
771 }),
772 "diff.toggle",
773 );
774 });
775});
776​
777describe("formatShortcutLabel", () => {
778 it("formats labels for macOS", () => {
779 assert.strictEqual(
780 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
781 "⇧⌘D",
782 );
783 });
784​
785 it("formats labels for non-macOS", () => {
786 assert.strictEqual(
787 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
788 "Ctrl+Shift+D",
789 );
790 });
791​
792 it("formats labels for plus key", () => {
793 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
794 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
795 });
796});
797​
798describe("isTerminalClearShortcut", () => {
799 it("matches Ctrl+L on all platforms", () => {
800 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
801 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
802 });
803​
804 it("matches Cmd+K on macOS", () => {
805 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
806 });
807​
808 it("ignores non-keydown events", () => {
809 assert.isFalse(
810 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
811 );
812 });
813});
814​
815describe("terminalDeleteShortcutData", () => {
816 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
817 assert.strictEqual(
818 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
819 "\u0015",
820 );
821 });
822​
823 it("ignores non-macOS platforms and modified variants", () => {
824 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
825 assert.isNull(
826 terminalDeleteShortcutData(
827 event({ key: "Backspace", metaKey: true, altKey: true }),
828 "MacIntel",
829 ),
830 );
831 });
832​
833 it("ignores non-keydown events", () => {
834 assert.isNull(
835 terminalDeleteShortcutData(
836 event({ type: "keyup", key: "Backspace", metaKey: true }),
837 "MacIntel",
838 ),
839 );
840 });
841});
842​
843describe("terminalNavigationShortcutData", () => {
844 it("maps Option+Arrow on macOS to word movement", () => {
845 assert.strictEqual(
846 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
847 "\u001bb",
848 );
849 assert.strictEqual(
850 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
851 "\u001bf",
852 );
853 });
854​
855 it("maps Cmd+Arrow on macOS to line movement", () => {
856 assert.strictEqual(
857 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
858 "\u0001",
859 );
860 assert.strictEqual(
861 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
862 "\u0005",
863 );
864 });
865​
866 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
867 assert.strictEqual(
868 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
869 "\u001bb",
870 );
871 assert.strictEqual(
872 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
873 "\u001bf",
874 );
875 });
876​
877 it("rejects unsupported combinations", () => {
878 assert.isNull(
879 terminalNavigationShortcutData(
880 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
881 "MacIntel",
882 ),
883 );
884 assert.isNull(
885 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
886 );
887 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
888 });
889​
890 it("ignores non-keydown events", () => {
891 assert.isNull(
892 terminalNavigationShortcutData(
893 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
894 "MacIntel",
895 ),
896 );
897 });
898});
899​
900describe("plus key parsing", () => {
901 it("matches the plus key shortcut", () => {
902 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
903 assert.isTrue(
904 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
905 platform: "MacIntel",
906 }),
907 );
908 assert.isTrue(
909 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
910 platform: "Linux",
911 }),
912 );
913 });
914});