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

keybindings.test.ts

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