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