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("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});