Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
3import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
4
5import {
6 buildKeybindingRows,
7 buildKeybindingCommandOptions,
8 buildWhenVariableOptions,
9 commandLabel,
10 filterKeybindingRows,
11 groupKeybindingRows,
12 keybindingConflictLabels,
13 keybindingFromKeyboardEvent,
14 parseWhenExpressionDraft,
15 shortcutToKeybindingInput,
16 unknownWhenVariables,
17 whenAstToExpression,
18 whenNodeRemoveLabel,
19} from "./KeybindingsSettings.logic";
20
21describe("KeybindingsSettings.logic", () => {
22 it("lists composer, provider, and pull request commands with editable defaults", () => {
23 const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS);
24 for (const command of [
25 "composer.sendAlternate",
26 "composer.sendBackground",
27 "thread.steerQueuedMessage",
28 "thread.editQueuedMessage",
29 "composer.host",
30 "composer.effort",
31 "composer.mode",
32 "composer.workspace",
33 "composer.branch",
34 "composer.previousWorktree",
35 "modelPicker.previousProvider",
36 "modelPicker.nextProvider",
37 "thread.copyReference",
38 "pullRequest.copyNumber",
39 ]) {
40 expect(rows.find((row) => row.command === command)).toMatchObject({
41 source: "Default",
42 conflicts: [],
43 });
44 }
45 });
46 it("finds the editable shortcut for sending the first queued message", () => {
47 expect(
48 filterKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS), "first queued"),
49 ).toContainEqual(
50 expect.objectContaining({
51 command: "thread.steerQueuedMessage",
52 key: "mod+shift+enter",
53 }),
54 );
55 });
56 it("filtering keeps the row objects so unchanged rows skip rendering", () => {
57 const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS);
58 const filtered = filterKeybindingRows(rows, "terminal");
59 expect(filtered.length).toBeGreaterThan(0);
60 expect(filtered.length).toBeLessThan(rows.length);
61 for (const row of filtered) expect(rows).toContain(row);
62 expect(filterKeybindingRows(rows, " ")).toBe(rows);
63 });
64 it.each(["pu", "pull request", "copy link", "thread id"])(
65 "finds the copy link shortcut with %s",
66 (query) => {
67 const rows = filterKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS), query);
68 expect(rows).toContainEqual(
69 expect.objectContaining({ command: "thread.copyReference", key: "mod+shift+c" }),
70 );
71 },
72 );
73 it("groups rows by command area in page order and drops empty groups", () => {
74 const groups = groupKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS));
75 expect(groups.map((group) => group.title)).toEqual([
76 "Navigation",
77 "Threads",
78 "Composer",
79 "Terminal",
80 "Preview & diff",
81 "Appearance",
82 "Other",
83 ]);
84 const composer = groups.find((group) => group.id === "composer");
85 expect(composer?.rows.map((row) => row.command)).toEqual(
86 expect.arrayContaining(["composer.host", "modelPicker.toggle"]),
87 );
88 expect(
89 groupKeybindingRows(
90 filterKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS), "split"),
91 ),
92 ).toEqual([expect.objectContaining({ id: "terminal" })]);
93 });
94 it("orders Usage bindings and command choices like the page", () => {
95 const expected = [
96 "usage.open",
97 "usage.cost",
98 "usage.tokens",
99 "usage.limits",
100 "usage.period.day",
101 "usage.period.week",
102 "usage.period.month",
103 "usage.period.quarter",
104 ];
105 for (const bindings of [
106 DEFAULT_RESOLVED_KEYBINDINGS,
107 DEFAULT_RESOLVED_KEYBINDINGS.toReversed(),
108 ]) {
109 expect(
110 filterKeybindingRows(buildKeybindingRows(bindings), "usage").map((row) => row.command),
111 ).toEqual(expected);
112 expect(
113 buildKeybindingCommandOptions(bindings).filter((command) => command.startsWith("usage.")),
114 ).toEqual(expected);
115 }
116 });
117
118 it("builds searchable rows with readable key and when values", () => {
119 const rows = filterKeybindingRows(
120 buildKeybindingRows([
121 {
122 command: "terminal.toggle",
123 shortcut: {
124 key: "j",
125 modKey: true,
126 metaKey: false,
127 ctrlKey: false,
128 altKey: false,
129 shiftKey: false,
130 },
131 whenAst: {
132 type: "not",
133 node: { type: "identifier", name: "terminalFocus" },
134 },
135 },
136 ] satisfies ResolvedKeybindingsConfig),
137 "terminal",
138 );
139
140 expect(rows).toEqual([
141 expect.objectContaining({
142 command: "terminal.toggle",
143 key: "mod+j",
144 when: "!terminalFocus",
145 defaultKey: "mod+j",
146 defaultWhen: "",
147 source: "Custom",
148 }),
149 ]);
150 });
151
152 it("captures platform-specific mod shortcuts", () => {
153 expect(
154 keybindingFromKeyboardEvent(
155 { key: "K", code: "KeyK", metaKey: true, ctrlKey: false, altKey: false, shiftKey: true },
156 "MacIntel",
157 ),
158 ).toBe("mod+shift+k");
159 expect(
160 keybindingFromKeyboardEvent(
161 { key: "K", code: "KeyK", metaKey: false, ctrlKey: true, altKey: false, shiftKey: true },
162 "Win32",
163 ),
164 ).toBe("mod+shift+k");
165 });
166
167 it.each([
168 ["k", "KeyK", "k"],
169 ["Tab", "Tab", "tab"],
170 ["F5", "F5", "f5"],
171 ])("captures %s without a modifier", (key, code, expected) => {
172 const noModifiers = { metaKey: false, ctrlKey: false, altKey: false, shiftKey: false };
173 expect(keybindingFromKeyboardEvent({ key, code, ...noModifiers }, "MacIntel")).toBe(expected);
174 });
175
176 it("waits for a key when only a modifier is pressed", () => {
177 expect(
178 keybindingFromKeyboardEvent(
179 {
180 key: "Meta",
181 code: "MetaLeft",
182 metaKey: true,
183 ctrlKey: false,
184 altKey: false,
185 shiftKey: false,
186 },
187 "MacIntel",
188 ),
189 ).toBeNull();
190 });
191
192 it.each([
193 ["@", "Digit2", "mod+shift+2"],
194 ['"', "Digit2", "mod+shift+2"],
195 ["@", "Quote", "mod+shift+'"],
196 ])("captures %s at %s by physical key", (key, code, expected) => {
197 expect(
198 keybindingFromKeyboardEvent(
199 {
200 key,
201 code,
202 metaKey: true,
203 ctrlKey: false,
204 altKey: false,
205 shiftKey: true,
206 },
207 "MacIntel",
208 ),
209 ).toBe(expected);
210 });
211
212 it("captures Latin layout keys instead of their punctuation position", () => {
213 expect(
214 keybindingFromKeyboardEvent(
215 {
216 key: "m",
217 code: "Semicolon",
218 metaKey: true,
219 ctrlKey: false,
220 altKey: false,
221 shiftKey: false,
222 },
223 "MacIntel",
224 ),
225 ).toBe("mod+m");
226 });
227
228 it("serializes shortcuts and when expressions for upserts", () => {
229 expect(
230 shortcutToKeybindingInput({
231 key: " ",
232 modKey: true,
233 metaKey: false,
234 ctrlKey: false,
235 altKey: true,
236 shiftKey: false,
237 }),
238 ).toBe("mod+alt+space");
239
240 expect(
241 whenAstToExpression({
242 type: "and",
243 left: { type: "identifier", name: "editorFocus" },
244 right: {
245 type: "not",
246 node: { type: "identifier", name: "terminalFocus" },
247 },
248 }),
249 ).toBe("editorFocus && !terminalFocus");
250
251 expect(parseWhenExpressionDraft("editorFocus && (!terminalFocus || modelPickerOpen)")).toEqual({
252 ok: true,
253 value: {
254 type: "and",
255 left: { type: "identifier", name: "editorFocus" },
256 right: {
257 type: "or",
258 left: {
259 type: "not",
260 node: { type: "identifier", name: "terminalFocus" },
261 },
262 right: { type: "identifier", name: "modelPickerOpen" },
263 },
264 },
265 });
266 expect(parseWhenExpressionDraft("editorFocus &&")).toEqual({
267 ok: false,
268 message: "Use variables with !, &&, ||, and parentheses.",
269 });
270
271 expect(parseWhenExpressionDraft("!(terminalFocus || modelPickerOpen)")).toEqual({
272 ok: true,
273 value: {
274 type: "not",
275 node: {
276 type: "or",
277 left: { type: "identifier", name: "terminalFocus" },
278 right: { type: "identifier", name: "modelPickerOpen" },
279 },
280 },
281 });
282 });
283
284 it("describes the scope of each visual expression removal", () => {
285 const condition = { type: "identifier", name: "terminalFocus" } as const;
286 const negatedCondition = { type: "not", node: condition } as const;
287 const group = { type: "and", left: condition, right: negatedCondition } as const;
288 const negatedGroup = { type: "not", node: group } as const;
289
290 expect(whenNodeRemoveLabel(group, 0)).toBe("Clear all conditions");
291 expect(whenNodeRemoveLabel(condition, 1)).toBe("Remove condition");
292 expect(whenNodeRemoveLabel(negatedCondition, 1)).toBe("Remove condition");
293 expect(whenNodeRemoveLabel(group, 1)).toBe("Remove group and its conditions");
294 expect(whenNodeRemoveLabel(negatedGroup, 1)).toBe("Remove group and its conditions");
295 });
296
297 it("formats static and project script command labels", () => {
298 expect(commandLabel("commandPalette.toggle")).toBe("Command Palette: Toggle");
299 expect(commandLabel("themeEditor.toggle")).toBe("Theme Editor: Toggle");
300 expect(commandLabel("view.reopenClosed")).toBe("Reopen Closed Tab");
301 expect(commandLabel("preview.focusUrl")).toBe("Preview: Focus URL");
302 expect(commandLabel("script.setup-db.run")).toBe("Run Script: Setup Db");
303 });
304
305 it("builds known when variable options from defaults without frontend labels", () => {
306 const options = buildWhenVariableOptions();
307
308 expect(options).toEqual(
309 expect.arrayContaining([
310 "terminalFocus",
311 "terminalOpen",
312 "isWeb",
313 "isDesktop",
314 "modelPickerOpen",
315 "true",
316 "false",
317 ]),
318 );
319 expect(options).not.toContain("customModeActive");
320 });
321
322 it("builds command options from all static commands and resolved project bindings", () => {
323 const options = buildKeybindingCommandOptions([
324 {
325 command: "script.setup-db.run",
326 shortcut: {
327 key: "r",
328 modKey: true,
329 metaKey: false,
330 ctrlKey: false,
331 altKey: false,
332 shiftKey: false,
333 },
334 },
335 ] satisfies ResolvedKeybindingsConfig);
336
337 expect(options).toEqual(
338 expect.arrayContaining([
339 "chat.new",
340 "threadPanel.toggle",
341 "rightPanel.toggleMaximized",
342 "composer.cycleHost",
343 "thread.stop",
344 "usage.open",
345 "script.setup-db.run",
346 ]),
347 );
348 for (const command of ["thread.stop", "composer.cycleHost"]) {
349 expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === command)).toBe(
350 false,
351 );
352 }
353 });
354
355 it("reports unknown when variables without rejecting parseable expressions", () => {
356 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
357
358 expect(parsed.ok).toBe(true);
359 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
360 });
361
362 it("marks each default shortcut for multi-binding commands as default", () => {
363 const rows = buildKeybindingRows([
364 {
365 command: "chat.new",
366 shortcut: {
367 key: "n",
368 modKey: true,
369 metaKey: false,
370 ctrlKey: false,
371 altKey: false,
372 shiftKey: false,
373 },
374 whenAst: {
375 type: "not",
376 node: { type: "identifier", name: "terminalFocus" },
377 },
378 },
379 {
380 command: "chat.new",
381 shortcut: {
382 key: "o",
383 modKey: true,
384 metaKey: false,
385 ctrlKey: false,
386 altKey: false,
387 shiftKey: true,
388 },
389 whenAst: {
390 type: "not",
391 node: { type: "identifier", name: "terminalFocus" },
392 },
393 },
394 ] satisfies ResolvedKeybindingsConfig);
395
396 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
397 });
398
399 it("reports conflicting shortcuts that share an active when context", () => {
400 const rows = buildKeybindingRows([
401 {
402 command: "chat.new",
403 shortcut: {
404 key: "n",
405 modKey: true,
406 metaKey: false,
407 ctrlKey: false,
408 altKey: false,
409 shiftKey: false,
410 },
411 whenAst: {
412 type: "not",
413 node: { type: "identifier", name: "terminalFocus" },
414 },
415 },
416 {
417 command: "chat.newLocal",
418 shortcut: {
419 key: "n",
420 modKey: true,
421 metaKey: false,
422 ctrlKey: false,
423 altKey: false,
424 shiftKey: false,
425 },
426 whenAst: {
427 type: "not",
428 node: { type: "identifier", name: "terminalFocus" },
429 },
430 },
431 ] satisfies ResolvedKeybindingsConfig);
432
433 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
434 expect(
435 keybindingConflictLabels(rows, {
436 rowId: rows[0]?.id ?? "",
437 key: "mod+n",
438 when: "",
439 }),
440 ).toEqual(["Chat: New Local"]);
441 });
442});