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

keybindings.test.ts

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