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

KeybindingsSettings.logic.test.ts

437 lines14.7 KB

1import { assert, describe, expect, it } from "vite-plus/test";
2import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
3import {
4 compileResolvedKeybindingsConfig,
5 mergeWithDefaultKeybindings,
6 parseKeybindingShortcut,
7} from "@t3tools/shared/keybindings";
8​
9import { resolveShortcutCommand } from "../../keybindings";
10​
11import {
12 buildKeybindingRows,
13 buildWhenVariableOptions,
14 keybindingConflictLabels,
15 keybindingFromKeyboardEvent,
16 parseWhenExpressionDraft,
17 shortcutToKeybindingInput,
18 shortcutToKeybindingParts,
19 unknownWhenVariables,
20 whenAstToExpression,
21} from "./KeybindingsSettings.logic";
22​
23describe("KeybindingsSettings.logic", () => {
24 it("keeps sibling row identities when a saved binding moves to the end", () => {
25 const bindings = compileResolvedKeybindingsConfig([
26 { command: "chat.new", key: "mod+n" },
27 { command: "terminal.toggle", key: "mod+j" },
28 { command: "terminal.toggle", key: "mod+k", when: "terminalFocus" },
29 ]);
30 const before = buildKeybindingRows(bindings);
31 const after = buildKeybindingRows([
32 ...bindings.slice(1),
33 ...compileResolvedKeybindingsConfig([{ command: "chat.new", key: "mod+shift+n" }]),
34 ]);
35​
36 expect(after.filter((row) => row.command === "terminal.toggle").map((row) => row.id)).toEqual(
37 before.filter((row) => row.command === "terminal.toggle").map((row) => row.id),
38 );
39 expect(after.find((row) => row.command === "chat.new")?.id).not.toBe(
40 before.find((row) => row.command === "chat.new")?.id,
41 );
42 });
43​
44 it("resets an edited shortcut to the built-in one its command no longer has", () => {
45 const rows = buildKeybindingRows(
46 mergeWithDefaultKeybindings(
47 compileResolvedKeybindingsConfig([
48 { command: "chat.new", key: "mod+n", when: "!terminalFocus" },
49 { command: "chat.new", key: "f8", when: "!terminalFocus" },
50 ]),
51 ),
52 );
53 expect(
54 rows
55 .filter((row) => row.command === "chat.new")
56 .map(({ key, defaultKey }) => ({ key, defaultKey })),
57 ).toEqual([
58 { key: "f8", defaultKey: "mod+shift+o" },
59 { key: "mod+n", defaultKey: "mod+n" },
60 ]);
61 const unassigned = buildKeybindingRows(
62 mergeWithDefaultKeybindings(
63 compileResolvedKeybindingsConfig([
64 { command: "chat.new", key: "mod+n", when: "!terminalFocus" },
65 { command: "chat.new", key: "mod+shift+o", when: "false" },
66 { command: "chat.new", key: "f8", when: "!terminalFocus" },
67 ]),
68 ),
69 );
70 expect(unassigned.find((row) => row.key === "f8")?.defaultKey).toBe("mod+n");
71 });
72​
73 it("shares a stable editor for resolved duplicates but separates commands, shortcuts, and conditions", () => {
74 const bindings = compileResolvedKeybindingsConfig([
75 { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },
76 { command: "terminal.toggle", key: "mod+escape", when: "(terminalFocus)" },
77 { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },
78 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
79 { command: "chat.new", key: "mod+esc", when: "terminalFocus" },
80 ]);
81 const rows = buildKeybindingRows(bindings);
82 expect(rows.map(({ command, key, when }) => ({ command, key, when }))).toEqual([
83 { command: "chat.new", key: "mod+esc", when: "terminalFocus" },
84 { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },
85 { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },
86 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
87 ]);
88 expect(new Set(rows.map((row) => row.id)).size).toBe(4);
89 expect(rows.find((row) => row.command === "chat.new")?.conflicts).toEqual(["Terminal: Toggle"]);
90 expect(buildKeybindingRows(bindings.slice(1)).map((row) => row.id)).toEqual(
91 rows.map((row) => row.id),
92 );
93 });
94​
95 it("marks only bindings shadowed by later unconditional or matching conditions", () => {
96 const bindings = compileResolvedKeybindingsConfig([
97 { command: "sidebar.toggle", key: "mod+b" },
98 { command: "terminal.toggle", key: "meta+b", when: "true" },
99 { command: "chat.new", key: "mod+n", when: "terminalFocus" },
100 { command: "terminal.new", key: "mod+n", when: "(terminalFocus)" },
101 { command: "chat.new", key: "mod+j" },
102 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
103 { command: "chat.new", key: "mod+k", when: "!terminalFocus" },
104 { command: "terminal.toggle", key: "mod+k", when: "terminalFocus" },
105 { command: "chat.new", key: "mod+l", when: "terminalFocus" },
106 { command: "terminal.toggle", key: "mod+l" },
107 ]);
108 const rows = buildKeybindingRows(bindings, "MacIntel");
109 expect(rows.filter((row) => row.previewLabels.length === 0).map((row) => row.binding)).toEqual([
110 bindings[8],
111 bindings[2],
112 bindings[0],
113 ]);
114 expect(rows.filter((row) => row.previewLabels.length > 0)).toHaveLength(7);
115 const sendBindings = compileResolvedKeybindingsConfig([
116 { command: "composer.sendAlternate", key: "mod+enter" },
117 { command: "chat.new", key: "mod+enter" },
118 ]);
119 for (const sendShortcut of ["enter", "mod-enter", "mod-enter-multiline"] as const) {
120 expect(
121 buildKeybindingRows(sendBindings, "MacIntel", sendShortcut).find(
122 (row) => row.command === "composer.sendAlternate",
123 )?.previewLabels.length === 0,
124 ).toBe(sendShortcut === "enter");
125 }
126 for (const command of ["composer.sendAlternate", "composer.sendBackground"] as const) {
127 const binding = compileResolvedKeybindingsConfig([{ command, key: "mod+enter" }]);
128 const labels = (mode: "enter" | "mod-enter" | "mod-enter-multiline") =>
129 buildKeybindingRows(binding, "Win32", mode)[0]?.previewLabels;
130 expect(labels("enter")).toEqual(["Ctrl+Enter"]);
131 expect(labels("mod-enter")).toEqual(["Ctrl+Shift+Enter"]);
132 expect(labels("mod-enter-multiline")).toEqual(["Ctrl+Enter", "Ctrl+Shift+Enter"]);
133 }
134 const shiftedBindings = compileResolvedKeybindingsConfig([
135 { command: "chat.new", key: "mod+shift+enter" },
136 { command: "composer.sendAlternate", key: "mod+enter" },
137 ]);
138 for (const sendShortcut of ["enter", "mod-enter", "mod-enter-multiline"] as const) {
139 expect(
140 buildKeybindingRows(shiftedBindings, "MacIntel", sendShortcut).find(
141 (row) => row.command === "chat.new",
142 )?.previewLabels.length === 0,
143 ).toBe(sendShortcut === "mod-enter");
144 }
145 expect(
146 buildKeybindingRows(bindings, "Win32").find((row) => row.command === "sidebar.toggle")
147 ?.previewLabels.length === 0,
148 ).toBe(false);
149 });
150​
151 it("captures platform-specific mod shortcuts", () => {
152 expect(
153 keybindingFromKeyboardEvent(
154 { key: "K", code: "KeyK", metaKey: true, ctrlKey: false, altKey: false, shiftKey: true },
155 "MacIntel",
156 ),
157 ).toBe("mod+shift+k");
158 expect(
159 keybindingFromKeyboardEvent(
160 { key: "K", code: "KeyK", metaKey: false, ctrlKey: true, altKey: false, shiftKey: true },
161 "Win32",
162 ),
163 ).toBe("mod+shift+k");
164 });
165​
166 it("waits for a key when only a modifier is pressed", () => {
167 expect(
168 keybindingFromKeyboardEvent(
169 {
170 key: "Meta",
171 code: "MetaLeft",
172 metaKey: true,
173 ctrlKey: false,
174 altKey: false,
175 shiftKey: false,
176 },
177 "MacIntel",
178 ),
179 ).toBeNull();
180 });
181​
182 it.each([
183 ["@", "Digit2", "mod+shift+2"],
184 ['"', "Digit2", "mod+shift+2"],
185 ["@", "Quote", "mod+shift+'"],
186 ])("captures %s at %s by physical key", (key, code, expected) => {
187 expect(
188 keybindingFromKeyboardEvent(
189 {
190 key,
191 code,
192 metaKey: true,
193 ctrlKey: false,
194 altKey: false,
195 shiftKey: true,
196 },
197 "MacIntel",
198 ),
199 ).toBe(expected);
200 });
201​
202 it("records layout punctuation while preserving physical Alt shortcuts; saved bindings still trigger", () => {
203 for (const { key, code, altKey = false, shiftKey, expected, defaultInput } of [
204 { key: "#", code: "Backslash", shiftKey: false, expected: "#", defaultInput: "\\" },
205 {
206 key: "'",
207 code: "Backslash",
208 shiftKey: true,
209 expected: "shift+'",
210 defaultInput: "shift+\\",
211 },
212 {
213 key: "¡",
214 code: "Digit1",
215 altKey: true,
216 shiftKey: false,
217 expected: "alt+1",
218 defaultInput: "alt+1",
219 },
220 {
221 key: "π",
222 code: "KeyP",
223 altKey: true,
224 shiftKey: false,
225 expected: "alt+p",
226 defaultInput: "alt+p",
227 },
228 // Dvorak Option+L and AZERTY Option+A: not the US glyph at its US key.
229 {
230 key: "¬",
231 code: "KeyP",
232 altKey: true,
233 shiftKey: false,
234 expected: "alt+¬",
235 defaultInput: "alt+¬",
236 },
237 {
238 key: "æ",
239 code: "KeyQ",
240 altKey: true,
241 shiftKey: false,
242 expected: "alt+æ",
243 defaultInput: "alt+æ",
244 },
245 ]) {
246 const pressed = {
247 key,
248 code,
249 metaKey: false,
250 ctrlKey: false,
251 altKey,
252 shiftKey,
253 };
254 expect(keybindingFromKeyboardEvent(pressed, "MacIntel")).toBe(defaultInput);
255 const input = keybindingFromKeyboardEvent(pressed, "MacIntel", { layoutCharacter: true });
256 expect(input).toBe(expected);
257 assert(input);
258 const shortcut = parseKeybindingShortcut(input);
259 assert(shortcut);
260 expect(
261 resolveShortcutCommand(pressed, [{ command: "chat.new", shortcut }], {
262 platform: "MacIntel",
263 }),
264 ).toBe("chat.new");
265 }
266 });
267​
268 it("captures Latin layout keys instead of their punctuation position", () => {
269 expect(
270 keybindingFromKeyboardEvent(
271 {
272 key: "m",
273 code: "Semicolon",
274 metaKey: true,
275 ctrlKey: false,
276 altKey: false,
277 shiftKey: false,
278 },
279 "MacIntel",
280 ),
281 ).toBe("mod+m");
282 });
283​
284 it("keeps a literal plus key as its own part", () => {
285 expect(shortcutToKeybindingParts(parseKeybindingShortcut("mod++")!)).toEqual(["mod", "+"]);
286 expect(shortcutToKeybindingParts(parseKeybindingShortcut("mod+shift++")!)).toEqual([
287 "mod",
288 "shift",
289 "+",
290 ]);
291 });
292​
293 it("serializes shortcuts and when expressions for upserts", () => {
294 expect(
295 shortcutToKeybindingInput({
296 key: " ",
297 modKey: true,
298 metaKey: false,
299 ctrlKey: false,
300 altKey: true,
301 shiftKey: false,
302 }),
303 ).toBe("mod+alt+space");
304​
305 expect(
306 whenAstToExpression({
307 type: "and",
308 left: { type: "identifier", name: "editorFocus" },
309 right: {
310 type: "not",
311 node: { type: "identifier", name: "terminalFocus" },
312 },
313 }),
314 ).toBe("editorFocus && !terminalFocus");
315​
316 expect(parseWhenExpressionDraft("editorFocus && (!terminalFocus || modelPickerOpen)")).toEqual({
317 ok: true,
318 value: {
319 type: "and",
320 left: { type: "identifier", name: "editorFocus" },
321 right: {
322 type: "or",
323 left: {
324 type: "not",
325 node: { type: "identifier", name: "terminalFocus" },
326 },
327 right: { type: "identifier", name: "modelPickerOpen" },
328 },
329 },
330 });
331 const previewOpenDraft = parseWhenExpressionDraft("previewOpen && !previewFocus");
332 assert(previewOpenDraft.ok);
333 expect(unknownWhenVariables(previewOpenDraft.value)).toEqual([]);
334 expect(buildWhenVariableOptions()).toContain("previewOpen");
335 expect(parseWhenExpressionDraft("editorFocus &&")).toEqual({
336 ok: false,
337 message: "Use variables with !, &&, ||, and parentheses.",
338 });
339​
340 expect(parseWhenExpressionDraft("!(terminalFocus || modelPickerOpen)")).toEqual({
341 ok: true,
342 value: {
343 type: "not",
344 node: {
345 type: "or",
346 left: { type: "identifier", name: "terminalFocus" },
347 right: { type: "identifier", name: "modelPickerOpen" },
348 },
349 },
350 });
351 });
352​
353 it("reports conflicting shortcuts that share an active when context", () => {
354 const rows = buildKeybindingRows([
355 {
356 command: "chat.new",
357 shortcut: {
358 key: "n",
359 modKey: true,
360 metaKey: false,
361 ctrlKey: false,
362 altKey: false,
363 shiftKey: false,
364 },
365 whenAst: {
366 type: "not",
367 node: { type: "identifier", name: "terminalFocus" },
368 },
369 },
370 {
371 command: "chat.newLocal",
372 shortcut: {
373 key: "n",
374 modKey: true,
375 metaKey: false,
376 ctrlKey: false,
377 altKey: false,
378 shiftKey: false,
379 },
380 whenAst: {
381 type: "not",
382 node: { type: "identifier", name: "terminalFocus" },
383 },
384 },
385 ] satisfies ResolvedKeybindingsConfig);
386​
387 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
388 const disabledRows = buildKeybindingRows([
389 rows[0]!.binding,
390 { ...rows[1]!.binding, whenAst: { type: "identifier", name: "false" } },
391 ]);
392 expect(disabledRows.find((row) => row.when === "false")?.previewLabels).toEqual([]);
393 expect(disabledRows.map((row) => row.conflicts)).toEqual([[], []]);
394​
395 expect(
396 keybindingConflictLabels(rows, {
397 rowId: rows[0]?.id ?? "",
398 key: "mod+n",
399 when: "",
400 }),
401 ).toEqual(["Chat: New Local"]);
402 });
403​
404 it.each([
405 { platform: "Linux x86_64", ctrl: ["Chat: New"], meta: [] },
406 { platform: "Win32", ctrl: ["Chat: New"], meta: [] },
407 { platform: "MacIntel", ctrl: [], meta: ["Chat: New"] },
408 ])("resolves mod to the $platform modifier when reporting conflicts", (expected) => {
409 const rows = buildKeybindingRows(
410 compileResolvedKeybindingsConfig([
411 { command: "chat.new", key: "mod+k" },
412 { command: "chat.newLocal", key: "ctrl+k" },
413 { command: "thread.copyReference", key: "meta+k" },
414 ]),
415 expected.platform,
416 );
417 const conflictsFor = (key: string) => rows.find((row) => row.key === key)?.conflicts;
418​
419 expect(conflictsFor("ctrl+k")).toEqual(expected.ctrl);
420 expect(conflictsFor("meta+k")).toEqual(expected.meta);
421 expect(
422 keybindingConflictLabels(rows, { rowId: "new", key: "ctrl+k", when: "" }, expected.platform),
423 ).toEqual(["Chat: New Local", ...expected.ctrl].toSorted());
424 });
425​
426 it("keeps mod and ctrl shortcuts apart when their when contexts differ", () => {
427 const rows = buildKeybindingRows(
428 compileResolvedKeybindingsConfig([
429 { command: "chat.new", key: "mod+k", when: "!terminalFocus" },
430 { command: "chat.newLocal", key: "ctrl+k", when: "terminalFocus" },
431 ]),
432 "Linux x86_64",
433 );
434​
435 expect(rows.map((row) => row.conflicts)).toEqual([[], []]);
436 });
437});