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

KeybindingsSettings.logic.test.ts

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