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