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