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

keybindings.test.ts

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