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

KeybindingsSettings.logic.test.ts

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