Open History to see the last commit
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 };
44}
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 };
59}
60
61function whenIdentifier(name: string): KeybindingWhenNode {
62 return { type: "identifier", name };
63}
64
65function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
66 return { type: "not", node };
67}
68
69function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
70 return { type: "and", left, right };
71}
72
73interface TestBinding {
74 shortcut: KeybindingShortcut;
75 command: KeybindingCommand;
76 whenAst?: KeybindingWhenNode;
77}
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 }));
85}
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("matches diff.toggle shortcut outside terminal focus", () => {
518 assert.isTrue(
519 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
520 platform: "MacIntel",
521 context: { terminalFocus: false },
522 }),
523 );
524 assert.isFalse(
525 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
526 platform: "MacIntel",
527 context: { terminalFocus: true },
528 }),
529 );
530 });
531});
532
533describe("cross-command precedence", () => {
534 it("uses when + order so a later focused rule overrides a global rule", () => {
535 const keybindings = compile([
536 { shortcut: modShortcut("n"), command: "chat.new" },
537 {
538 shortcut: modShortcut("n"),
539 command: "terminal.new",
540 whenAst: whenIdentifier("terminalFocus"),
541 },
542 ]);
543
544 assert.isTrue(
545 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
546 platform: "MacIntel",
547 context: { terminalFocus: true },
548 }),
549 );
550 assert.isFalse(
551 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
552 platform: "MacIntel",
553 context: { terminalFocus: true },
554 }),
555 );
556 assert.isFalse(
557 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
558 platform: "MacIntel",
559 context: { terminalFocus: false },
560 }),
561 );
562 assert.isTrue(
563 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
564 platform: "MacIntel",
565 context: { terminalFocus: false },
566 }),
567 );
568 });
569
570 it("still lets a later global rule win when both rules match", () => {
571 const keybindings = compile([
572 {
573 shortcut: modShortcut("n"),
574 command: "terminal.new",
575 whenAst: whenIdentifier("terminalFocus"),
576 },
577 { shortcut: modShortcut("n"), command: "chat.new" },
578 ]);
579
580 assert.isFalse(
581 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
582 platform: "Linux",
583 context: { terminalFocus: true },
584 }),
585 );
586 assert.isTrue(
587 isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
588 platform: "Linux",
589 context: { terminalFocus: true },
590 }),
591 );
592 });
593});
594
595describe("resolveShortcutCommand", () => {
596 it("returns dynamic script commands", () => {
597 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
598
599 assert.strictEqual(
600 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
601 platform: "Linux",
602 }),
603 "script.setup.run",
604 );
605 });
606
607 it("matches bracket shortcuts using the physical key code", () => {
608 assert.strictEqual(
609 resolveShortcutCommand(
610 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
611 DEFAULT_BINDINGS,
612 {
613 platform: "MacIntel",
614 },
615 ),
616 "thread.previous",
617 );
618 assert.strictEqual(
619 resolveShortcutCommand(
620 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
621 DEFAULT_BINDINGS,
622 {
623 platform: "Linux",
624 },
625 ),
626 "thread.next",
627 );
628 });
629
630 it("matches Option-modified letters using the physical key code on macOS", () => {
631 assert.strictEqual(
632 resolveShortcutCommand(
633 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
634 DEFAULT_BINDINGS,
635 { platform: "MacIntel" },
636 ),
637 "rightPanel.toggle",
638 );
639 });
640});
641
642describe("formatShortcutLabel", () => {
643 it("formats labels for macOS", () => {
644 assert.strictEqual(
645 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
646 "⇧⌘D",
647 );
648 });
649
650 it("formats labels for non-macOS", () => {
651 assert.strictEqual(
652 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
653 "Ctrl+Shift+D",
654 );
655 });
656
657 it("formats labels for plus key", () => {
658 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
659 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
660 });
661});
662
663describe("isTerminalClearShortcut", () => {
664 it("matches Ctrl+L on all platforms", () => {
665 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
666 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
667 });
668
669 it("matches Cmd+K on macOS", () => {
670 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
671 });
672
673 it("ignores non-keydown events", () => {
674 assert.isFalse(
675 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
676 );
677 });
678});
679
680describe("terminalDeleteShortcutData", () => {
681 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
682 assert.strictEqual(
683 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
684 "\u0015",
685 );
686 });
687
688 it("ignores non-macOS platforms and modified variants", () => {
689 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
690 assert.isNull(
691 terminalDeleteShortcutData(
692 event({ key: "Backspace", metaKey: true, altKey: true }),
693 "MacIntel",
694 ),
695 );
696 });
697
698 it("ignores non-keydown events", () => {
699 assert.isNull(
700 terminalDeleteShortcutData(
701 event({ type: "keyup", key: "Backspace", metaKey: true }),
702 "MacIntel",
703 ),
704 );
705 });
706});
707
708describe("terminalNavigationShortcutData", () => {
709 it("maps Option+Arrow on macOS to word movement", () => {
710 assert.strictEqual(
711 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
712 "\u001bb",
713 );
714 assert.strictEqual(
715 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
716 "\u001bf",
717 );
718 });
719
720 it("maps Cmd+Arrow on macOS to line movement", () => {
721 assert.strictEqual(
722 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
723 "\u0001",
724 );
725 assert.strictEqual(
726 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
727 "\u0005",
728 );
729 });
730
731 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
732 assert.strictEqual(
733 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
734 "\u001bb",
735 );
736 assert.strictEqual(
737 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
738 "\u001bf",
739 );
740 });
741
742 it("rejects unsupported combinations", () => {
743 assert.isNull(
744 terminalNavigationShortcutData(
745 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
746 "MacIntel",
747 ),
748 );
749 assert.isNull(
750 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
751 );
752 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
753 });
754
755 it("ignores non-keydown events", () => {
756 assert.isNull(
757 terminalNavigationShortcutData(
758 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
759 "MacIntel",
760 ),
761 );
762 });
763});
764
765describe("plus key parsing", () => {
766 it("matches the plus key shortcut", () => {
767 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
768 assert.isTrue(
769 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
770 platform: "MacIntel",
771 }),
772 );
773 assert.isTrue(
774 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
775 platform: "Linux",
776 }),
777 );
778 });
779});