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

keybindings.test.ts

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