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

KeybindingsSettings.logic.test.ts

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