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

keybindings.test.ts

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