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