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

keybindings.test.ts

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