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

keybindings.test.ts

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