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

keybindings.test.ts

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