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