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

KeybindingsSettings.logic.test.ts

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