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

KeybindingsSettings.logic.test.ts

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