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

keybindings.test.ts

960 lines28.7 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 assert.isFalse(
562 isOpenFavoriteEditorShortcut(
563 event({ key: "o", metaKey: true, repeat: true }),
564 DEFAULT_BINDINGS,
565 { platform: "MacIntel" },
566 ),
567 );
568 });
569​
570 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
571 assert.strictEqual(
572 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
573 platform: "MacIntel",
574 context: { terminalFocus: false },
575 }),
576 "commandPalette.toggle",
577 );
578 assert.notStrictEqual(
579 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
580 platform: "MacIntel",
581 context: { terminalFocus: true },
582 }),
583 "commandPalette.toggle",
584 );
585 });
586​
587 it("resolves a user-configured thread panel shortcut without assigning a default", () => {
588 const bindings = compile([
589 { shortcut: modShortcut("b", { shiftKey: true }), command: "threadPanel.toggle" },
590 ]);
591​
592 assert.strictEqual(
593 resolveShortcutCommand(event({ key: "b", metaKey: true, shiftKey: true }), bindings, {
594 platform: "MacIntel",
595 }),
596 "threadPanel.toggle",
597 );
598 });
599​
600 it("matches themeEditor.toggle on macOS and Windows", () => {
601 assert.strictEqual(
602 resolveShortcutCommand(
603 event({ key: "t", metaKey: true, altKey: true, shiftKey: true }),
604 DEFAULT_BINDINGS,
605 { platform: "MacIntel" },
606 ),
607 "themeEditor.toggle",
608 );
609 assert.strictEqual(
610 resolveShortcutCommand(
611 event({ key: "t", ctrlKey: true, altKey: true, shiftKey: true }),
612 DEFAULT_BINDINGS,
613 { platform: "Win32" },
614 ),
615 "themeEditor.toggle",
616 );
617 });
618​
619 it("matches filePicker.toggle shortcut outside terminal focus", () => {
620 assert.strictEqual(
621 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
622 platform: "MacIntel",
623 context: { terminalFocus: false },
624 }),
625 "filePicker.toggle",
626 );
627 assert.notStrictEqual(
628 resolveShortcutCommand(event({ key: "p", metaKey: true }), DEFAULT_BINDINGS, {
629 platform: "MacIntel",
630 context: { terminalFocus: true },
631 }),
632 "filePicker.toggle",
633 );
634 });
635​
636 it("matches projectSearch.toggle shortcut outside terminal focus", () => {
637 assert.strictEqual(
638 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
639 platform: "MacIntel",
640 context: { terminalFocus: false },
641 }),
642 "projectSearch.toggle",
643 );
644 assert.notStrictEqual(
645 resolveShortcutCommand(event({ key: "f", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
646 platform: "MacIntel",
647 context: { terminalFocus: true },
648 }),
649 "projectSearch.toggle",
650 );
651 });
652​
653 it("matches diff.toggle shortcut outside terminal focus", () => {
654 assert.isTrue(
655 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
656 platform: "MacIntel",
657 context: { terminalFocus: false },
658 }),
659 );
660 assert.isFalse(
661 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
662 platform: "MacIntel",
663 context: { terminalFocus: true },
664 }),
665 );
666 });
667});
668​
669describe("cross-command precedence", () => {
670 it("uses when + order so a later focused rule overrides a global rule", () => {
671 const keybindings = compile([
672 { shortcut: modShortcut("n"), command: "chat.new" },
673 {
674 shortcut: modShortcut("n"),
675 command: "terminal.new",
676 whenAst: whenIdentifier("terminalFocus"),
677 },
678 ]);
679​
680 assert.isTrue(
681 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
682 platform: "MacIntel",
683 context: { terminalFocus: true },
684 }),
685 );
686 assert.isFalse(
687 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
688 platform: "MacIntel",
689 context: { terminalFocus: true },
690 }),
691 );
692 assert.isFalse(
693 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
694 platform: "MacIntel",
695 context: { terminalFocus: false },
696 }),
697 );
698 assert.isTrue(
699 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
700 platform: "MacIntel",
701 context: { terminalFocus: false },
702 }),
703 );
704 });
705​
706 it("still lets a later global rule win when both rules match", () => {
707 const keybindings = compile([
708 {
709 shortcut: modShortcut("n"),
710 command: "terminal.new",
711 whenAst: whenIdentifier("terminalFocus"),
712 },
713 { shortcut: modShortcut("n"), command: "chat.new" },
714 ]);
715​
716 assert.isFalse(
717 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
718 platform: "Linux",
719 context: { terminalFocus: true },
720 }),
721 );
722 assert.isTrue(
723 isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
724 platform: "Linux",
725 context: { terminalFocus: true },
726 }),
727 );
728 });
729});
730​
731describe("resolveShortcutCommand", () => {
732 it("returns dynamic script commands", () => {
733 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
734​
735 assert.strictEqual(
736 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
737 platform: "Linux",
738 }),
739 "script.setup.run",
740 );
741 });
742​
743 it("resolves a custom right panel maximize binding", () => {
744 const keybindings = compile([
745 {
746 shortcut: modShortcut("m", { shiftKey: true }),
747 command: "rightPanel.toggleMaximized",
748 },
749 ]);
750​
751 assert.strictEqual(
752 resolveShortcutCommand(event({ key: "m", metaKey: true, shiftKey: true }), keybindings, {
753 platform: "MacIntel",
754 }),
755 "rightPanel.toggleMaximized",
756 );
757 });
758​
759 it("matches bracket shortcuts using the physical key code", () => {
760 assert.strictEqual(
761 resolveShortcutCommand(
762 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
763 DEFAULT_BINDINGS,
764 {
765 platform: "MacIntel",
766 },
767 ),
768 "thread.previous",
769 );
770 assert.strictEqual(
771 resolveShortcutCommand(
772 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
773 DEFAULT_BINDINGS,
774 {
775 platform: "Linux",
776 },
777 ),
778 "thread.next",
779 );
780 });
781​
782 it("matches Option-modified letters using the physical key code on macOS", () => {
783 assert.strictEqual(
784 resolveShortcutCommand(
785 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
786 DEFAULT_BINDINGS,
787 { platform: "MacIntel" },
788 ),
789 "rightPanel.toggle",
790 );
791 });
792​
793 it("matches non-Latin layout letters using the physical key code", () => {
794 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
795​
796 assert.strictEqual(
797 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
798 platform: "MacIntel",
799 }),
800 "diff.toggle",
801 );
802 });
803​
804 it("ignores the physical key code when the layout types a different Latin letter", () => {
805 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
806​
807 // On a remapped layout the physical D key types "a"; only the physical
808 // key whose layout output is "d" may trigger the shortcut.
809 assert.isNull(
810 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
811 platform: "MacIntel",
812 }),
813 );
814 assert.strictEqual(
815 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
816 platform: "MacIntel",
817 }),
818 "diff.toggle",
819 );
820 });
821});
822​
823describe("formatShortcutLabel", () => {
824 it("formats labels for macOS", () => {
825 assert.strictEqual(
826 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
827 "⇧⌘D",
828 );
829 });
830​
831 it("formats labels for non-macOS", () => {
832 assert.strictEqual(
833 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
834 "Ctrl+Shift+D",
835 );
836 });
837​
838 it("formats labels for plus key", () => {
839 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
840 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
841 });
842});
843​
844describe("isTerminalClearShortcut", () => {
845 it("matches Ctrl+L on all platforms", () => {
846 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
847 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
848 });
849​
850 it("matches Cmd+K on macOS", () => {
851 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
852 });
853​
854 it("ignores non-keydown events", () => {
855 assert.isFalse(
856 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
857 );
858 });
859});
860​
861describe("terminalDeleteShortcutData", () => {
862 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
863 assert.strictEqual(
864 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
865 "\u0015",
866 );
867 });
868​
869 it("ignores non-macOS platforms and modified variants", () => {
870 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
871 assert.isNull(
872 terminalDeleteShortcutData(
873 event({ key: "Backspace", metaKey: true, altKey: true }),
874 "MacIntel",
875 ),
876 );
877 });
878​
879 it("ignores non-keydown events", () => {
880 assert.isNull(
881 terminalDeleteShortcutData(
882 event({ type: "keyup", key: "Backspace", metaKey: true }),
883 "MacIntel",
884 ),
885 );
886 });
887});
888​
889describe("terminalNavigationShortcutData", () => {
890 it("maps Option+Arrow on macOS to word movement", () => {
891 assert.strictEqual(
892 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
893 "\u001bb",
894 );
895 assert.strictEqual(
896 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
897 "\u001bf",
898 );
899 });
900​
901 it("maps Cmd+Arrow on macOS to line movement", () => {
902 assert.strictEqual(
903 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
904 "\u0001",
905 );
906 assert.strictEqual(
907 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
908 "\u0005",
909 );
910 });
911​
912 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
913 assert.strictEqual(
914 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
915 "\u001bb",
916 );
917 assert.strictEqual(
918 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
919 "\u001bf",
920 );
921 });
922​
923 it("rejects unsupported combinations", () => {
924 assert.isNull(
925 terminalNavigationShortcutData(
926 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
927 "MacIntel",
928 ),
929 );
930 assert.isNull(
931 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
932 );
933 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
934 });
935​
936 it("ignores non-keydown events", () => {
937 assert.isNull(
938 terminalNavigationShortcutData(
939 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
940 "MacIntel",
941 ),
942 );
943 });
944});
945​
946describe("plus key parsing", () => {
947 it("matches the plus key shortcut", () => {
948 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
949 assert.isTrue(
950 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
951 platform: "MacIntel",
952 }),
953 );
954 assert.isTrue(
955 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
956 platform: "Linux",
957 }),
958 );
959 });
960});