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

keybindings.test.ts

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