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

keybindings.test.ts

663 lines19.7 KB

1import { assert, describe, it } from "vitest";
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 isOpenFavoriteEditorShortcut,
15 isTerminalClearShortcut,
16 isTerminalCloseShortcut,
17 isTerminalNewShortcut,
18 isTerminalSplitShortcut,
19 isTerminalToggleShortcut,
20 resolveShortcutCommand,
21 shouldShowThreadJumpHints,
22 shortcutLabelForCommand,
23 terminalDeleteShortcutData,
24 terminalNavigationShortcutData,
25 threadJumpCommandForIndex,
26 threadJumpIndexFromCommand,
27 threadTraversalDirectionFromCommand,
28 type ShortcutEventLike,
29} from "./keybindings";
30​
31function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
32 return {
33 key: "j",
34 metaKey: false,
35 ctrlKey: false,
36 shiftKey: false,
37 altKey: false,
38 ...overrides,
39 };
41​
42function modShortcut(
43 key: string,
44 overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
45): KeybindingShortcut {
46 return {
47 key,
48 metaKey: false,
49 ctrlKey: false,
50 shiftKey: false,
51 altKey: false,
52 modKey: true,
53 ...overrides,
54 };
56​
57function whenIdentifier(name: string): KeybindingWhenNode {
58 return { type: "identifier", name };
60​
61function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
62 return { type: "not", node };
64​
65function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
66 return { type: "and", left, right };
68​
69interface TestBinding {
70 shortcut: KeybindingShortcut;
71 command: KeybindingCommand;
72 whenAst?: KeybindingWhenNode;
74​
75function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
76 return bindings.map((binding) => ({
77 command: binding.command,
78 shortcut: binding.shortcut,
79 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
80 }));
82​
83const DEFAULT_BINDINGS = compile([
84 { shortcut: modShortcut("j"), command: "terminal.toggle" },
85 {
86 shortcut: modShortcut("d"),
87 command: "terminal.split",
88 whenAst: whenIdentifier("terminalFocus"),
89 },
90 {
91 shortcut: modShortcut("d", { shiftKey: true }),
92 command: "terminal.new",
93 whenAst: whenIdentifier("terminalFocus"),
94 },
95 {
96 shortcut: modShortcut("w"),
97 command: "terminal.close",
98 whenAst: whenIdentifier("terminalFocus"),
99 },
100 {
101 shortcut: modShortcut("d"),
102 command: "diff.toggle",
103 whenAst: whenNot(whenIdentifier("terminalFocus")),
104 },
105 {
106 shortcut: modShortcut("k"),
107 command: "commandPalette.toggle",
108 whenAst: whenNot(whenIdentifier("terminalFocus")),
109 },
110 { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
111 { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
112 { shortcut: modShortcut("o"), command: "editor.openFavorite" },
113 { shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },
114 { shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },
115 { shortcut: modShortcut("1"), command: "thread.jump.1" },
116 { shortcut: modShortcut("2"), command: "thread.jump.2" },
117 { shortcut: modShortcut("3"), command: "thread.jump.3" },
118]);
119​
120describe("isTerminalToggleShortcut", () => {
121 it("matches Cmd+J on macOS", () => {
122 assert.isTrue(
123 isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, {
124 platform: "MacIntel",
125 }),
126 );
127 });
128​
129 it("matches Ctrl+J on non-macOS", () => {
130 assert.isTrue(
131 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }),
132 );
133 });
134});
135​
136describe("split/new/close terminal shortcuts", () => {
137 it("requires terminalFocus for default split/new/close bindings", () => {
138 assert.isFalse(
139 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
140 platform: "MacIntel",
141 context: { terminalFocus: false },
142 }),
143 );
144 assert.isFalse(
145 isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
146 platform: "Linux",
147 context: { terminalFocus: false },
148 }),
149 );
150 assert.isFalse(
151 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
152 platform: "Linux",
153 context: { terminalFocus: false },
154 }),
155 );
156 });
157​
158 it("matches split/new when terminalFocus is true", () => {
159 assert.isTrue(
160 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
161 platform: "MacIntel",
162 context: { terminalFocus: true },
163 }),
164 );
165 assert.isTrue(
166 isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
167 platform: "Linux",
168 context: { terminalFocus: true },
169 }),
170 );
171 assert.isTrue(
172 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
173 platform: "Linux",
174 context: { terminalFocus: true },
175 }),
176 );
177 });
178​
179 it("supports when expressions", () => {
180 const keybindings = compile([
181 {
182 shortcut: modShortcut("\\"),
183 command: "terminal.split",
184 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
185 },
186 {
187 shortcut: modShortcut("n", { shiftKey: true }),
188 command: "terminal.new",
189 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
190 },
191 { shortcut: modShortcut("j"), command: "terminal.toggle" },
192 ]);
193 assert.isTrue(
194 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
195 platform: "Win32",
196 context: { terminalOpen: true, terminalFocus: false },
197 }),
198 );
199 assert.isFalse(
200 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
201 platform: "Win32",
202 context: { terminalOpen: false, terminalFocus: false },
203 }),
204 );
205 assert.isTrue(
206 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
207 platform: "Win32",
208 context: { terminalOpen: true, terminalFocus: false },
209 }),
210 );
211 });
212​
213 it("supports when boolean literals", () => {
214 const keybindings = compile([
215 { shortcut: modShortcut("n"), command: "terminal.new", whenAst: whenIdentifier("true") },
216 { shortcut: modShortcut("m"), command: "terminal.new", whenAst: whenIdentifier("false") },
217 ]);
218​
219 assert.isTrue(
220 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
221 platform: "Linux",
222 }),
223 );
224 assert.isFalse(
225 isTerminalNewShortcut(event({ key: "m", ctrlKey: true }), keybindings, {
226 platform: "Linux",
227 }),
228 );
229 });
230});
231​
232describe("shortcutLabelForCommand", () => {
233 it("returns the effective binding label", () => {
234 const bindings = compile([
235 {
236 shortcut: modShortcut("\\"),
237 command: "terminal.split",
238 whenAst: whenIdentifier("terminalFocus"),
239 },
240 {
241 shortcut: modShortcut("\\", { shiftKey: true }),
242 command: "terminal.split",
243 whenAst: whenNot(whenIdentifier("terminalFocus")),
244 },
245 ]);
246 assert.strictEqual(
247 shortcutLabelForCommand(bindings, "terminal.split", {
248 platform: "Linux",
249 context: { terminalFocus: false },
250 }),
251 "Ctrl+Shift+\\",
252 );
253 });
254​
255 it("returns effective labels for non-terminal commands", () => {
256 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
257 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");
258 assert.strictEqual(
259 shortcutLabelForCommand(DEFAULT_BINDINGS, "commandPalette.toggle", "MacIntel"),
260 "⌘K",
261 );
262 assert.strictEqual(
263 shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
264 "Ctrl+O",
265 );
266 assert.strictEqual(
267 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.jump.3", "MacIntel"),
268 "⌘3",
269 );
270 assert.strictEqual(
271 shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.previous", "Linux"),
272 "Ctrl+Shift+[",
273 );
274 });
275​
276 it("returns null for commands shadowed by a later conflicting shortcut", () => {
277 const bindings = compile([
278 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.1" },
279 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.7" },
280 ]);
281​
282 assert.isNull(shortcutLabelForCommand(bindings, "thread.jump.1", "MacIntel"));
283 assert.strictEqual(shortcutLabelForCommand(bindings, "thread.jump.7", "MacIntel"), "⇧⌘1");
284 });
285​
286 it("respects when-context while resolving labels", () => {
287 const bindings = compile([
288 { shortcut: modShortcut("d"), command: "diff.toggle" },
289 {
290 shortcut: modShortcut("d"),
291 command: "terminal.split",
292 whenAst: whenIdentifier("terminalFocus"),
293 },
294 ]);
295​
296 assert.strictEqual(
297 shortcutLabelForCommand(bindings, "diff.toggle", {
298 platform: "Linux",
299 context: { terminalFocus: false },
300 }),
301 "Ctrl+D",
302 );
303 assert.isNull(
304 shortcutLabelForCommand(bindings, "diff.toggle", {
305 platform: "Linux",
306 context: { terminalFocus: true },
307 }),
308 );
309 assert.strictEqual(
310 shortcutLabelForCommand(bindings, "terminal.split", {
311 platform: "Linux",
312 context: { terminalFocus: true },
313 }),
314 "Ctrl+D",
315 );
316 });
317});
318​
319describe("thread navigation helpers", () => {
320 it("maps jump commands to visible thread indices", () => {
321 assert.strictEqual(threadJumpCommandForIndex(0), "thread.jump.1");
322 assert.strictEqual(threadJumpCommandForIndex(2), "thread.jump.3");
323 assert.isNull(threadJumpCommandForIndex(9));
324 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.1"), 0);
325 assert.strictEqual(threadJumpIndexFromCommand("thread.jump.3"), 2);
326 assert.isNull(threadJumpIndexFromCommand("thread.next"));
327 });
328​
329 it("maps traversal commands to directions", () => {
330 assert.strictEqual(threadTraversalDirectionFromCommand("thread.previous"), "previous");
331 assert.strictEqual(threadTraversalDirectionFromCommand("thread.next"), "next");
332 assert.isNull(threadTraversalDirectionFromCommand("thread.jump.1"));
333 assert.isNull(threadTraversalDirectionFromCommand(null));
334 });
335​
336 it("shows jump hints only when configured modifiers match", () => {
337 assert.isTrue(
338 shouldShowThreadJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
339 platform: "MacIntel",
340 }),
341 );
342 assert.isFalse(
343 shouldShowThreadJumpHints(event({ metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
344 platform: "MacIntel",
345 }),
346 );
347 assert.isTrue(
348 shouldShowThreadJumpHints(event({ ctrlKey: true }), DEFAULT_BINDINGS, {
349 platform: "Linux",
350 }),
351 );
352 });
353});
354​
355describe("chat/editor shortcuts", () => {
356 it("matches chat.new shortcut", () => {
357 assert.isTrue(
358 isChatNewShortcut(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
359 platform: "MacIntel",
360 }),
361 );
362 assert.isTrue(
363 isChatNewShortcut(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
364 platform: "Linux",
365 }),
366 );
367 });
368​
369 it("matches chat.newLocal shortcut", () => {
370 assert.isTrue(
371 isChatNewLocalShortcut(event({ key: "n", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
372 platform: "MacIntel",
373 }),
374 );
375 assert.isTrue(
376 isChatNewLocalShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
377 platform: "Linux",
378 }),
379 );
380 });
381​
382 it("matches editor.openFavorite shortcut", () => {
383 assert.isTrue(
384 isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
385 platform: "MacIntel",
386 }),
387 );
388 assert.isTrue(
389 isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
390 platform: "Linux",
391 }),
392 );
393 });
394​
395 it("matches commandPalette.toggle shortcut outside terminal focus", () => {
396 assert.strictEqual(
397 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
398 platform: "MacIntel",
399 context: { terminalFocus: false },
400 }),
401 "commandPalette.toggle",
402 );
403 assert.notStrictEqual(
404 resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {
405 platform: "MacIntel",
406 context: { terminalFocus: true },
407 }),
408 "commandPalette.toggle",
409 );
410 });
411​
412 it("matches diff.toggle shortcut outside terminal focus", () => {
413 assert.isTrue(
414 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
415 platform: "MacIntel",
416 context: { terminalFocus: false },
417 }),
418 );
419 assert.isFalse(
420 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
421 platform: "MacIntel",
422 context: { terminalFocus: true },
423 }),
424 );
425 });
426});
427​
428describe("cross-command precedence", () => {
429 it("uses when + order so a later focused rule overrides a global rule", () => {
430 const keybindings = compile([
431 { shortcut: modShortcut("n"), command: "chat.new" },
432 {
433 shortcut: modShortcut("n"),
434 command: "terminal.new",
435 whenAst: whenIdentifier("terminalFocus"),
436 },
437 ]);
438​
439 assert.isTrue(
440 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
441 platform: "MacIntel",
442 context: { terminalFocus: true },
443 }),
444 );
445 assert.isFalse(
446 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
447 platform: "MacIntel",
448 context: { terminalFocus: true },
449 }),
450 );
451 assert.isFalse(
452 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
453 platform: "MacIntel",
454 context: { terminalFocus: false },
455 }),
456 );
457 assert.isTrue(
458 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
459 platform: "MacIntel",
460 context: { terminalFocus: false },
461 }),
462 );
463 });
464​
465 it("still lets a later global rule win when both rules match", () => {
466 const keybindings = compile([
467 {
468 shortcut: modShortcut("n"),
469 command: "terminal.new",
470 whenAst: whenIdentifier("terminalFocus"),
471 },
472 { shortcut: modShortcut("n"), command: "chat.new" },
473 ]);
474​
475 assert.isFalse(
476 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
477 platform: "Linux",
478 context: { terminalFocus: true },
479 }),
480 );
481 assert.isTrue(
482 isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
483 platform: "Linux",
484 context: { terminalFocus: true },
485 }),
486 );
487 });
488});
489​
490describe("resolveShortcutCommand", () => {
491 it("returns dynamic script commands", () => {
492 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
493​
494 assert.strictEqual(
495 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
496 platform: "Linux",
497 }),
498 "script.setup.run",
499 );
500 });
501​
502 it("matches bracket shortcuts using the physical key code", () => {
503 assert.strictEqual(
504 resolveShortcutCommand(
505 event({ key: "{", code: "BracketLeft", metaKey: true, shiftKey: true }),
506 DEFAULT_BINDINGS,
507 {
508 platform: "MacIntel",
509 },
510 ),
511 "thread.previous",
512 );
513 assert.strictEqual(
514 resolveShortcutCommand(
515 event({ key: "}", code: "BracketRight", ctrlKey: true, shiftKey: true }),
516 DEFAULT_BINDINGS,
517 {
518 platform: "Linux",
519 },
520 ),
521 "thread.next",
522 );
523 });
524});
525​
526describe("formatShortcutLabel", () => {
527 it("formats labels for macOS", () => {
528 assert.strictEqual(
529 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
530 "⇧⌘D",
531 );
532 });
533​
534 it("formats labels for non-macOS", () => {
535 assert.strictEqual(
536 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
537 "Ctrl+Shift+D",
538 );
539 });
540​
541 it("formats labels for plus key", () => {
542 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
543 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
544 });
545});
546​
547describe("isTerminalClearShortcut", () => {
548 it("matches Ctrl+L on all platforms", () => {
549 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
550 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
551 });
552​
553 it("matches Cmd+K on macOS", () => {
554 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
555 });
556​
557 it("ignores non-keydown events", () => {
558 assert.isFalse(
559 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
560 );
561 });
562});
563​
564describe("terminalDeleteShortcutData", () => {
565 it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
566 assert.strictEqual(
567 terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
568 "\u0015",
569 );
570 });
571​
572 it("ignores non-macOS platforms and modified variants", () => {
573 assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
574 assert.isNull(
575 terminalDeleteShortcutData(
576 event({ key: "Backspace", metaKey: true, altKey: true }),
577 "MacIntel",
578 ),
579 );
580 });
581​
582 it("ignores non-keydown events", () => {
583 assert.isNull(
584 terminalDeleteShortcutData(
585 event({ type: "keyup", key: "Backspace", metaKey: true }),
586 "MacIntel",
587 ),
588 );
589 });
590});
591​
592describe("terminalNavigationShortcutData", () => {
593 it("maps Option+Arrow on macOS to word movement", () => {
594 assert.strictEqual(
595 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
596 "\u001bb",
597 );
598 assert.strictEqual(
599 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
600 "\u001bf",
601 );
602 });
603​
604 it("maps Cmd+Arrow on macOS to line movement", () => {
605 assert.strictEqual(
606 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
607 "\u0001",
608 );
609 assert.strictEqual(
610 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
611 "\u0005",
612 );
613 });
614​
615 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
616 assert.strictEqual(
617 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
618 "\u001bb",
619 );
620 assert.strictEqual(
621 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
622 "\u001bf",
623 );
624 });
625​
626 it("rejects unsupported combinations", () => {
627 assert.isNull(
628 terminalNavigationShortcutData(
629 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
630 "MacIntel",
631 ),
632 );
633 assert.isNull(
634 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
635 );
636 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
637 });
638​
639 it("ignores non-keydown events", () => {
640 assert.isNull(
641 terminalNavigationShortcutData(
642 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
643 "MacIntel",
644 ),
645 );
646 });
647});
648​
649describe("plus key parsing", () => {
650 it("matches the plus key shortcut", () => {
651 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
652 assert.isTrue(
653 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
654 platform: "MacIntel",
655 }),
656 );
657 assert.isTrue(
658 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
659 platform: "Linux",
660 }),
661 );
662 });
663});