Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

KeybindingsSettings.logic.test.ts

412 lines11.7 KB

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 "composer.cycleHost",
307 "thread.stop",
308 "usage.open",
309 "script.setup-db.run",
310 ]),
311 );
312 for (const command of ["thread.stop", "composer.cycleHost"]) {
313 expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === command)).toBe(
314 false,
315 );
316 }
317 });
318​
319 it("reports unknown when variables without rejecting parseable expressions", () => {
320 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
321​
322 expect(parsed.ok).toBe(true);
323 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
324 });
325​
326 it("marks each default shortcut for multi-binding commands as default", () => {
327 const rows = buildKeybindingRows(
328 [
329 {
330 command: "chat.new",
331 shortcut: {
332 key: "n",
333 modKey: true,
334 metaKey: false,
335 ctrlKey: false,
336 altKey: false,
337 shiftKey: false,
338 },
339 whenAst: {
340 type: "not",
341 node: { type: "identifier", name: "terminalFocus" },
342 },
343 },
344 {
345 command: "chat.new",
346 shortcut: {
347 key: "o",
348 modKey: true,
349 metaKey: false,
350 ctrlKey: false,
351 altKey: false,
352 shiftKey: true,
353 },
354 whenAst: {
355 type: "not",
356 node: { type: "identifier", name: "terminalFocus" },
357 },
358 },
359 ] satisfies ResolvedKeybindingsConfig,
360 "",
361 );
362​
363 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
364 });
365​
366 it("reports conflicting shortcuts that share an active when context", () => {
367 const rows = buildKeybindingRows(
368 [
369 {
370 command: "chat.new",
371 shortcut: {
372 key: "n",
373 modKey: true,
374 metaKey: false,
375 ctrlKey: false,
376 altKey: false,
377 shiftKey: false,
378 },
379 whenAst: {
380 type: "not",
381 node: { type: "identifier", name: "terminalFocus" },
382 },
383 },
384 {
385 command: "chat.newLocal",
386 shortcut: {
387 key: "n",
388 modKey: true,
389 metaKey: false,
390 ctrlKey: false,
391 altKey: false,
392 shiftKey: false,
393 },
394 whenAst: {
395 type: "not",
396 node: { type: "identifier", name: "terminalFocus" },
397 },
398 },
399 ] satisfies ResolvedKeybindingsConfig,
400 "",
401 );
402​
403 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
404 expect(
405 keybindingConflictLabels(rows, {
406 rowId: rows[0]?.id ?? "",
407 key: "mod+n",
408 when: "",
409 }),
410 ).toEqual(["Chat: New Local"]);
411 });
412});