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

keybindings.test.ts

487 lines14.0 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 shortcutLabelForCommand,
22 terminalNavigationShortcutData,
23 type ShortcutEventLike,
24} from "./keybindings";
25​
26function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
27 return {
28 key: "j",
29 metaKey: false,
30 ctrlKey: false,
31 shiftKey: false,
32 altKey: false,
33 ...overrides,
34 };
36​
37function modShortcut(
38 key: string,
39 overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
40): KeybindingShortcut {
41 return {
42 key,
43 metaKey: false,
44 ctrlKey: false,
45 shiftKey: false,
46 altKey: false,
47 modKey: true,
48 ...overrides,
49 };
51​
52function whenIdentifier(name: string): KeybindingWhenNode {
53 return { type: "identifier", name };
55​
56function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
57 return { type: "not", node };
59​
60function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
61 return { type: "and", left, right };
63​
64interface TestBinding {
65 shortcut: KeybindingShortcut;
66 command: KeybindingCommand;
67 whenAst?: KeybindingWhenNode;
69​
70function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
71 return bindings.map((binding) => ({
72 command: binding.command,
73 shortcut: binding.shortcut,
74 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
75 }));
77​
78const DEFAULT_BINDINGS = compile([
79 { shortcut: modShortcut("j"), command: "terminal.toggle" },
80 {
81 shortcut: modShortcut("d"),
82 command: "terminal.split",
83 whenAst: whenIdentifier("terminalFocus"),
84 },
85 {
86 shortcut: modShortcut("d", { shiftKey: true }),
87 command: "terminal.new",
88 whenAst: whenIdentifier("terminalFocus"),
89 },
90 {
91 shortcut: modShortcut("w"),
92 command: "terminal.close",
93 whenAst: whenIdentifier("terminalFocus"),
94 },
95 {
96 shortcut: modShortcut("d"),
97 command: "diff.toggle",
98 whenAst: whenNot(whenIdentifier("terminalFocus")),
99 },
100 { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
101 { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
102 { shortcut: modShortcut("o"), command: "editor.openFavorite" },
103]);
104​
105describe("isTerminalToggleShortcut", () => {
106 it("matches Cmd+J on macOS", () => {
107 assert.isTrue(
108 isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, {
109 platform: "MacIntel",
110 }),
111 );
112 });
113​
114 it("matches Ctrl+J on non-macOS", () => {
115 assert.isTrue(
116 isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }),
117 );
118 });
119});
120​
121describe("split/new/close terminal shortcuts", () => {
122 it("requires terminalFocus for default split/new/close bindings", () => {
123 assert.isFalse(
124 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
125 platform: "MacIntel",
126 context: { terminalFocus: false },
127 }),
128 );
129 assert.isFalse(
130 isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
131 platform: "Linux",
132 context: { terminalFocus: false },
133 }),
134 );
135 assert.isFalse(
136 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
137 platform: "Linux",
138 context: { terminalFocus: false },
139 }),
140 );
141 });
142​
143 it("matches split/new when terminalFocus is true", () => {
144 assert.isTrue(
145 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
146 platform: "MacIntel",
147 context: { terminalFocus: true },
148 }),
149 );
150 assert.isTrue(
151 isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
152 platform: "Linux",
153 context: { terminalFocus: true },
154 }),
155 );
156 assert.isTrue(
157 isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {
158 platform: "Linux",
159 context: { terminalFocus: true },
160 }),
161 );
162 });
163​
164 it("supports when expressions", () => {
165 const keybindings = compile([
166 {
167 shortcut: modShortcut("\\"),
168 command: "terminal.split",
169 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
170 },
171 {
172 shortcut: modShortcut("n", { shiftKey: true }),
173 command: "terminal.new",
174 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
175 },
176 { shortcut: modShortcut("j"), command: "terminal.toggle" },
177 ]);
178 assert.isTrue(
179 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
180 platform: "Win32",
181 context: { terminalOpen: true, terminalFocus: false },
182 }),
183 );
184 assert.isFalse(
185 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
186 platform: "Win32",
187 context: { terminalOpen: false, terminalFocus: false },
188 }),
189 );
190 assert.isTrue(
191 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
192 platform: "Win32",
193 context: { terminalOpen: true, terminalFocus: false },
194 }),
195 );
196 });
197​
198 it("supports when boolean literals", () => {
199 const keybindings = compile([
200 { shortcut: modShortcut("n"), command: "terminal.new", whenAst: whenIdentifier("true") },
201 { shortcut: modShortcut("m"), command: "terminal.new", whenAst: whenIdentifier("false") },
202 ]);
203​
204 assert.isTrue(
205 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
206 platform: "Linux",
207 }),
208 );
209 assert.isFalse(
210 isTerminalNewShortcut(event({ key: "m", ctrlKey: true }), keybindings, {
211 platform: "Linux",
212 }),
213 );
214 });
215});
216​
217describe("shortcutLabelForCommand", () => {
218 it("returns the most recent binding label", () => {
219 const bindings = compile([
220 {
221 shortcut: modShortcut("\\"),
222 command: "terminal.split",
223 whenAst: whenIdentifier("terminalFocus"),
224 },
225 {
226 shortcut: modShortcut("\\", { shiftKey: true }),
227 command: "terminal.split",
228 whenAst: whenNot(whenIdentifier("terminalFocus")),
229 },
230 ]);
231 assert.strictEqual(
232 shortcutLabelForCommand(bindings, "terminal.split", "Linux"),
233 "Ctrl+Shift+\\",
234 );
235 });
236​
237 it("returns labels for non-terminal commands", () => {
238 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
239 assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");
240 assert.strictEqual(
241 shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
242 "Ctrl+O",
243 );
244 });
245});
246​
247describe("chat/editor shortcuts", () => {
248 it("matches chat.new shortcut", () => {
249 assert.isTrue(
250 isChatNewShortcut(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
251 platform: "MacIntel",
252 }),
253 );
254 assert.isTrue(
255 isChatNewShortcut(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
256 platform: "Linux",
257 }),
258 );
259 });
260​
261 it("matches chat.newLocal shortcut", () => {
262 assert.isTrue(
263 isChatNewLocalShortcut(event({ key: "n", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
264 platform: "MacIntel",
265 }),
266 );
267 assert.isTrue(
268 isChatNewLocalShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
269 platform: "Linux",
270 }),
271 );
272 });
273​
274 it("matches editor.openFavorite shortcut", () => {
275 assert.isTrue(
276 isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
277 platform: "MacIntel",
278 }),
279 );
280 assert.isTrue(
281 isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
282 platform: "Linux",
283 }),
284 );
285 });
286​
287 it("matches diff.toggle shortcut outside terminal focus", () => {
288 assert.isTrue(
289 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
290 platform: "MacIntel",
291 context: { terminalFocus: false },
292 }),
293 );
294 assert.isFalse(
295 isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
296 platform: "MacIntel",
297 context: { terminalFocus: true },
298 }),
299 );
300 });
301});
302​
303describe("cross-command precedence", () => {
304 it("uses when + order so a later focused rule overrides a global rule", () => {
305 const keybindings = compile([
306 { shortcut: modShortcut("n"), command: "chat.new" },
307 {
308 shortcut: modShortcut("n"),
309 command: "terminal.new",
310 whenAst: whenIdentifier("terminalFocus"),
311 },
312 ]);
313​
314 assert.isTrue(
315 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
316 platform: "MacIntel",
317 context: { terminalFocus: true },
318 }),
319 );
320 assert.isFalse(
321 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
322 platform: "MacIntel",
323 context: { terminalFocus: true },
324 }),
325 );
326 assert.isFalse(
327 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
328 platform: "MacIntel",
329 context: { terminalFocus: false },
330 }),
331 );
332 assert.isTrue(
333 isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
334 platform: "MacIntel",
335 context: { terminalFocus: false },
336 }),
337 );
338 });
339​
340 it("still lets a later global rule win when both rules match", () => {
341 const keybindings = compile([
342 {
343 shortcut: modShortcut("n"),
344 command: "terminal.new",
345 whenAst: whenIdentifier("terminalFocus"),
346 },
347 { shortcut: modShortcut("n"), command: "chat.new" },
348 ]);
349​
350 assert.isFalse(
351 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
352 platform: "Linux",
353 context: { terminalFocus: true },
354 }),
355 );
356 assert.isTrue(
357 isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
358 platform: "Linux",
359 context: { terminalFocus: true },
360 }),
361 );
362 });
363});
364​
365describe("resolveShortcutCommand", () => {
366 it("returns dynamic script commands", () => {
367 const keybindings = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
368​
369 assert.strictEqual(
370 resolveShortcutCommand(event({ key: "r", ctrlKey: true }), keybindings, {
371 platform: "Linux",
372 }),
373 "script.setup.run",
374 );
375 });
376});
377​
378describe("formatShortcutLabel", () => {
379 it("formats labels for macOS", () => {
380 assert.strictEqual(
381 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"),
382 "⇧⌘D",
383 );
384 });
385​
386 it("formats labels for non-macOS", () => {
387 assert.strictEqual(
388 formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"),
389 "Ctrl+Shift+D",
390 );
391 });
392​
393 it("formats labels for plus key", () => {
394 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
395 assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
396 });
397});
398​
399describe("isTerminalClearShortcut", () => {
400 it("matches Ctrl+L on all platforms", () => {
401 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
402 assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
403 });
404​
405 it("matches Cmd+K on macOS", () => {
406 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
407 });
408​
409 it("ignores non-keydown events", () => {
410 assert.isFalse(
411 isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"),
412 );
413 });
414});
415​
416describe("terminalNavigationShortcutData", () => {
417 it("maps Option+Arrow on macOS to word movement", () => {
418 assert.strictEqual(
419 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), "MacIntel"),
420 "\u001bb",
421 );
422 assert.strictEqual(
423 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), "MacIntel"),
424 "\u001bf",
425 );
426 });
427​
428 it("maps Cmd+Arrow on macOS to line movement", () => {
429 assert.strictEqual(
430 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "MacIntel"),
431 "\u0001",
432 );
433 assert.strictEqual(
434 terminalNavigationShortcutData(event({ key: "ArrowRight", metaKey: true }), "MacIntel"),
435 "\u0005",
436 );
437 });
438​
439 it("maps Ctrl+Arrow on non-macOS to word movement", () => {
440 assert.strictEqual(
441 terminalNavigationShortcutData(event({ key: "ArrowLeft", ctrlKey: true }), "Win32"),
442 "\u001bb",
443 );
444 assert.strictEqual(
445 terminalNavigationShortcutData(event({ key: "ArrowRight", ctrlKey: true }), "Linux"),
446 "\u001bf",
447 );
448 });
449​
450 it("rejects unsupported combinations", () => {
451 assert.isNull(
452 terminalNavigationShortcutData(
453 event({ key: "ArrowLeft", shiftKey: true, altKey: true }),
454 "MacIntel",
455 ),
456 );
457 assert.isNull(
458 terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"),
459 );
460 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
461 });
462​
463 it("ignores non-keydown events", () => {
464 assert.isNull(
465 terminalNavigationShortcutData(
466 event({ type: "keyup", key: "ArrowLeft", altKey: true }),
467 "MacIntel",
468 ),
469 );
470 });
471});
472​
473describe("plus key parsing", () => {
474 it("matches the plus key shortcut", () => {
475 const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
476 assert.isTrue(
477 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
478 platform: "MacIntel",
479 }),
480 );
481 assert.isTrue(
482 isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
483 platform: "Linux",
484 }),
485 );
486 });
487});