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 for (const bindings of [
93 DEFAULT_RESOLVED_KEYBINDINGS,
94 DEFAULT_RESOLVED_KEYBINDINGS.toReversed(),
95 ]) {
96 expect(buildKeybindingRows(bindings, "usage").map((row) => row.command)).toEqual(expected);
97 expect(
98 buildKeybindingCommandOptions(bindings).filter((command) => command.startsWith("usage.")),
99 ).toEqual(expected);
100 }
101 });
102
103 it("builds searchable rows with readable key and when values", () => {
104 const rows = buildKeybindingRows(
105 [
106 {
107 command: "terminal.toggle",
108 shortcut: {
109 key: "j",
110 modKey: true,
111 metaKey: false,
112 ctrlKey: false,
113 altKey: false,
114 shiftKey: false,
115 },
116 whenAst: {
117 type: "not",
118 node: { type: "identifier", name: "terminalFocus" },
119 },
120 },
121 ] satisfies ResolvedKeybindingsConfig,
122 "terminal",
123 );
124
125 expect(rows).toEqual([
126 expect.objectContaining({
127 command: "terminal.toggle",
128 key: "mod+j",
129 when: "!terminalFocus",
130 defaultKey: "mod+j",
131 defaultWhen: "",
132 source: "Custom",
133 }),
134 ]);
135 });
136
137 it("captures platform-specific mod shortcuts", () => {
138 expect(
139 keybindingFromKeyboardEvent(
140 { key: "K", code: "KeyK", metaKey: true, ctrlKey: false, altKey: false, shiftKey: true },
141 "MacIntel",
142 ),
143 ).toBe("mod+shift+k");
144 expect(
145 keybindingFromKeyboardEvent(
146 { key: "K", code: "KeyK", metaKey: false, ctrlKey: true, altKey: false, shiftKey: true },
147 "Win32",
148 ),
149 ).toBe("mod+shift+k");
150 });
151
152 it.each([
153 ["k", "KeyK", "k"],
154 ["Tab", "Tab", "tab"],
155 ["F5", "F5", "f5"],
156 ])("captures %s without a modifier", (key, code, expected) => {
157 const noModifiers = { metaKey: false, ctrlKey: false, altKey: false, shiftKey: false };
158 expect(keybindingFromKeyboardEvent({ key, code, ...noModifiers }, "MacIntel")).toBe(expected);
159 });
160
161 it("waits for a key when only a modifier is pressed", () => {
162 expect(
163 keybindingFromKeyboardEvent(
164 {
165 key: "Meta",
166 code: "MetaLeft",
167 metaKey: true,
168 ctrlKey: false,
169 altKey: false,
170 shiftKey: false,
171 },
172 "MacIntel",
173 ),
174 ).toBeNull();
175 });
176
177 it.each([
178 ["@", "Digit2", "mod+shift+2"],
179 ['"', "Digit2", "mod+shift+2"],
180 ["@", "Quote", "mod+shift+'"],
181 ])("captures %s at %s by physical key", (key, code, expected) => {
182 expect(
183 keybindingFromKeyboardEvent(
184 {
185 key,
186 code,
187 metaKey: true,
188 ctrlKey: false,
189 altKey: false,
190 shiftKey: true,
191 },
192 "MacIntel",
193 ),
194 ).toBe(expected);
195 });
196
197 it("captures Latin layout keys instead of their punctuation position", () => {
198 expect(
199 keybindingFromKeyboardEvent(
200 {
201 key: "m",
202 code: "Semicolon",
203 metaKey: true,
204 ctrlKey: false,
205 altKey: false,
206 shiftKey: false,
207 },
208 "MacIntel",
209 ),
210 ).toBe("mod+m");
211 });
212
213 it("serializes shortcuts and when expressions for upserts", () => {
214 expect(
215 shortcutToKeybindingInput({
216 key: " ",
217 modKey: true,
218 metaKey: false,
219 ctrlKey: false,
220 altKey: true,
221 shiftKey: false,
222 }),
223 ).toBe("mod+alt+space");
224
225 expect(
226 whenAstToExpression({
227 type: "and",
228 left: { type: "identifier", name: "editorFocus" },
229 right: {
230 type: "not",
231 node: { type: "identifier", name: "terminalFocus" },
232 },
233 }),
234 ).toBe("editorFocus && !terminalFocus");
235
236 expect(parseWhenExpressionDraft("editorFocus && (!terminalFocus || modelPickerOpen)")).toEqual({
237 ok: true,
238 value: {
239 type: "and",
240 left: { type: "identifier", name: "editorFocus" },
241 right: {
242 type: "or",
243 left: {
244 type: "not",
245 node: { type: "identifier", name: "terminalFocus" },
246 },
247 right: { type: "identifier", name: "modelPickerOpen" },
248 },
249 },
250 });
251 expect(parseWhenExpressionDraft("editorFocus &&")).toEqual({
252 ok: false,
253 message: "Use variables with !, &&, ||, and parentheses.",
254 });
255
256 expect(parseWhenExpressionDraft("!(terminalFocus || modelPickerOpen)")).toEqual({
257 ok: true,
258 value: {
259 type: "not",
260 node: {
261 type: "or",
262 left: { type: "identifier", name: "terminalFocus" },
263 right: { type: "identifier", name: "modelPickerOpen" },
264 },
265 },
266 });
267 });
268
269 it("describes the scope of each visual expression removal", () => {
270 const condition = { type: "identifier", name: "terminalFocus" } as const;
271 const negatedCondition = { type: "not", node: condition } as const;
272 const group = { type: "and", left: condition, right: negatedCondition } as const;
273 const negatedGroup = { type: "not", node: group } as const;
274
275 expect(whenNodeRemoveLabel(group, 0)).toBe("Clear all conditions");
276 expect(whenNodeRemoveLabel(condition, 1)).toBe("Remove condition");
277 expect(whenNodeRemoveLabel(negatedCondition, 1)).toBe("Remove condition");
278 expect(whenNodeRemoveLabel(group, 1)).toBe("Remove group and its conditions");
279 expect(whenNodeRemoveLabel(negatedGroup, 1)).toBe("Remove group and its conditions");
280 });
281
282 it("formats static and project script command labels", () => {
283 expect(commandLabel("commandPalette.toggle")).toBe("Command Palette: Toggle");
284 expect(commandLabel("themeEditor.toggle")).toBe("Theme Editor: Toggle");
285 expect(commandLabel("view.reopenClosed")).toBe("Reopen Closed Tab");
286 expect(commandLabel("preview.focusUrl")).toBe("Preview: Focus URL");
287 expect(commandLabel("script.setup-db.run")).toBe("Run Script: Setup Db");
288 });
289
290 it("builds known when variable options from defaults without frontend labels", () => {
291 const options = buildWhenVariableOptions();
292
293 expect(options).toEqual(
294 expect.arrayContaining([
295 "terminalFocus",
296 "terminalOpen",
297 "isWeb",
298 "isDesktop",
299 "modelPickerOpen",
300 "true",
301 "false",
302 ]),
303 );
304 expect(options).not.toContain("customModeActive");
305 });
306
307 it("builds command options from all static commands and resolved project bindings", () => {
308 const options = buildKeybindingCommandOptions([
309 {
310 command: "script.setup-db.run",
311 shortcut: {
312 key: "r",
313 modKey: true,
314 metaKey: false,
315 ctrlKey: false,
316 altKey: false,
317 shiftKey: false,
318 },
319 },
320 ] satisfies ResolvedKeybindingsConfig);
321
322 expect(options).toEqual(
323 expect.arrayContaining([
324 "chat.new",
325 "threadPanel.toggle",
326 "rightPanel.toggleMaximized",
327 "composer.cycleHost",
328 "thread.stop",
329 "usage.open",
330 "script.setup-db.run",
331 ]),
332 );
333 for (const command of ["thread.stop", "composer.cycleHost"]) {
334 expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === command)).toBe(
335 false,
336 );
337 }
338 });
339
340 it("reports unknown when variables without rejecting parseable expressions", () => {
341 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
342
343 expect(parsed.ok).toBe(true);
344 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
345 });
346
347 it("marks each default shortcut for multi-binding commands as default", () => {
348 const rows = buildKeybindingRows(
349 [
350 {
351 command: "chat.new",
352 shortcut: {
353 key: "n",
354 modKey: true,
355 metaKey: false,
356 ctrlKey: false,
357 altKey: false,
358 shiftKey: false,
359 },
360 whenAst: {
361 type: "not",
362 node: { type: "identifier", name: "terminalFocus" },
363 },
364 },
365 {
366 command: "chat.new",
367 shortcut: {
368 key: "o",
369 modKey: true,
370 metaKey: false,
371 ctrlKey: false,
372 altKey: false,
373 shiftKey: true,
374 },
375 whenAst: {
376 type: "not",
377 node: { type: "identifier", name: "terminalFocus" },
378 },
379 },
380 ] satisfies ResolvedKeybindingsConfig,
381 "",
382 );
383
384 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
385 });
386
387 it("reports conflicting shortcuts that share an active when context", () => {
388 const rows = buildKeybindingRows(
389 [
390 {
391 command: "chat.new",
392 shortcut: {
393 key: "n",
394 modKey: true,
395 metaKey: false,
396 ctrlKey: false,
397 altKey: false,
398 shiftKey: false,
399 },
400 whenAst: {
401 type: "not",
402 node: { type: "identifier", name: "terminalFocus" },
403 },
404 },
405 {
406 command: "chat.newLocal",
407 shortcut: {
408 key: "n",
409 modKey: true,
410 metaKey: false,
411 ctrlKey: false,
412 altKey: false,
413 shiftKey: false,
414 },
415 whenAst: {
416 type: "not",
417 node: { type: "identifier", name: "terminalFocus" },
418 },
419 },
420 ] satisfies ResolvedKeybindingsConfig,
421 "",
422 );
423
424 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
425 expect(
426 keybindingConflictLabels(rows, {
427 rowId: rows[0]?.id ?? "",
428 key: "mod+n",
429 when: "",
430 }),
431 ).toEqual(["Chat: New Local"]);
432 });
433});