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

KeybindingsSettings.logic.test.ts

303 lines9.4 KB

1import { assert, describe, expect, it } from "vite-plus/test";
2import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
3import {
4 compileResolvedKeybindingsConfig,
5 parseKeybindingShortcut,
6} from "@t3tools/shared/keybindings";
7​
8import { resolveShortcutCommand } from "../../keybindings";
9​
10import {
11 buildKeybindingRows,
12 keybindingConflictLabels,
13 keybindingFromKeyboardEvent,
14 parseWhenExpressionDraft,
15 shortcutToKeybindingInput,
16 whenAstToExpression,
17} from "./KeybindingsSettings.logic";
18​
19describe("KeybindingsSettings.logic", () => {
20 it("keeps sibling row identities when a saved binding moves to the end", () => {
21 const bindings = compileResolvedKeybindingsConfig([
22 { command: "chat.new", key: "mod+n" },
23 { command: "terminal.toggle", key: "mod+j" },
24 { command: "terminal.toggle", key: "mod+k", when: "terminalFocus" },
25 ]);
26 const before = buildKeybindingRows(bindings);
27 const after = buildKeybindingRows([
28 ...bindings.slice(1),
29 ...compileResolvedKeybindingsConfig([{ command: "chat.new", key: "mod+shift+n" }]),
30 ]);
31​
32 expect(after.filter((row) => row.command === "terminal.toggle").map((row) => row.id)).toEqual(
33 before.filter((row) => row.command === "terminal.toggle").map((row) => row.id),
34 );
35 expect(after.find((row) => row.command === "chat.new")?.id).not.toBe(
36 before.find((row) => row.command === "chat.new")?.id,
37 );
38 });
39​
40 it("shares a stable editor for resolved duplicates but separates commands, shortcuts, and conditions", () => {
41 const bindings = compileResolvedKeybindingsConfig([
42 { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },
43 { command: "terminal.toggle", key: "mod+escape", when: "(terminalFocus)" },
44 { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },
45 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
46 { command: "chat.new", key: "mod+esc", when: "terminalFocus" },
47 ]);
48 const rows = buildKeybindingRows(bindings);
49 expect(rows.map(({ command, key, when }) => ({ command, key, when }))).toEqual([
50 { command: "chat.new", key: "mod+esc", when: "terminalFocus" },
51 { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },
52 { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },
53 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
54 ]);
55 expect(new Set(rows.map((row) => row.id)).size).toBe(4);
56 expect(rows.find((row) => row.command === "chat.new")?.conflicts).toEqual(["Terminal: Toggle"]);
57 expect(buildKeybindingRows(bindings.slice(1)).map((row) => row.id)).toEqual(
58 rows.map((row) => row.id),
59 );
60 });
61​
62 it("captures platform-specific mod shortcuts", () => {
63 expect(
64 keybindingFromKeyboardEvent(
65 { key: "K", code: "KeyK", metaKey: true, ctrlKey: false, altKey: false, shiftKey: true },
66 "MacIntel",
67 ),
68 ).toBe("mod+shift+k");
69 expect(
70 keybindingFromKeyboardEvent(
71 { key: "K", code: "KeyK", metaKey: false, ctrlKey: true, altKey: false, shiftKey: true },
72 "Win32",
73 ),
74 ).toBe("mod+shift+k");
75 });
76​
77 it("waits for a key when only a modifier is pressed", () => {
78 expect(
79 keybindingFromKeyboardEvent(
80 {
81 key: "Meta",
82 code: "MetaLeft",
83 metaKey: true,
84 ctrlKey: false,
85 altKey: false,
86 shiftKey: false,
87 },
88 "MacIntel",
89 ),
90 ).toBeNull();
91 });
92​
93 it.each([
94 ["@", "Digit2", "mod+shift+2"],
95 ['"', "Digit2", "mod+shift+2"],
96 ["@", "Quote", "mod+shift+'"],
97 ])("captures %s at %s by physical key", (key, code, expected) => {
98 expect(
99 keybindingFromKeyboardEvent(
100 {
101 key,
102 code,
103 metaKey: true,
104 ctrlKey: false,
105 altKey: false,
106 shiftKey: true,
107 },
108 "MacIntel",
109 ),
110 ).toBe(expected);
111 });
112​
113 it("records layout punctuation while preserving physical Alt shortcuts; saved bindings still trigger", () => {
114 for (const { key, code, altKey = false, shiftKey, expected, defaultInput } of [
115 { key: "#", code: "Backslash", shiftKey: false, expected: "#", defaultInput: "\\" },
116 {
117 key: "'",
118 code: "Backslash",
119 shiftKey: true,
120 expected: "shift+'",
121 defaultInput: "shift+\\",
122 },
123 {
124 key: "¡",
125 code: "Digit1",
126 altKey: true,
127 shiftKey: false,
128 expected: "alt+1",
129 defaultInput: "alt+1",
130 },
131 ]) {
132 const pressed = {
133 key,
134 code,
135 metaKey: false,
136 ctrlKey: false,
137 altKey,
138 shiftKey,
139 };
140 expect(keybindingFromKeyboardEvent(pressed, "MacIntel")).toBe(defaultInput);
141 const input = keybindingFromKeyboardEvent(pressed, "MacIntel", { layoutCharacter: true });
142 expect(input).toBe(expected);
143 assert(input);
144 const shortcut = parseKeybindingShortcut(input);
145 assert(shortcut);
146 expect(
147 resolveShortcutCommand(pressed, [{ command: "chat.new", shortcut }], {
148 platform: "MacIntel",
149 }),
150 ).toBe("chat.new");
151 }
152 });
153​
154 it("captures Latin layout keys instead of their punctuation position", () => {
155 expect(
156 keybindingFromKeyboardEvent(
157 {
158 key: "m",
159 code: "Semicolon",
160 metaKey: true,
161 ctrlKey: false,
162 altKey: false,
163 shiftKey: false,
164 },
165 "MacIntel",
166 ),
167 ).toBe("mod+m");
168 });
169​
170 it("serializes shortcuts and when expressions for upserts", () => {
171 expect(
172 shortcutToKeybindingInput({
173 key: " ",
174 modKey: true,
175 metaKey: false,
176 ctrlKey: false,
177 altKey: true,
178 shiftKey: false,
179 }),
180 ).toBe("mod+alt+space");
181​
182 expect(
183 whenAstToExpression({
184 type: "and",
185 left: { type: "identifier", name: "editorFocus" },
186 right: {
187 type: "not",
188 node: { type: "identifier", name: "terminalFocus" },
189 },
190 }),
191 ).toBe("editorFocus && !terminalFocus");
192​
193 expect(parseWhenExpressionDraft("editorFocus && (!terminalFocus || modelPickerOpen)")).toEqual({
194 ok: true,
195 value: {
196 type: "and",
197 left: { type: "identifier", name: "editorFocus" },
198 right: {
199 type: "or",
200 left: {
201 type: "not",
202 node: { type: "identifier", name: "terminalFocus" },
203 },
204 right: { type: "identifier", name: "modelPickerOpen" },
205 },
206 },
207 });
208 expect(parseWhenExpressionDraft("editorFocus &&")).toEqual({
209 ok: false,
210 message: "Use variables with !, &&, ||, and parentheses.",
211 });
212​
213 expect(parseWhenExpressionDraft("!(terminalFocus || modelPickerOpen)")).toEqual({
214 ok: true,
215 value: {
216 type: "not",
217 node: {
218 type: "or",
219 left: { type: "identifier", name: "terminalFocus" },
220 right: { type: "identifier", name: "modelPickerOpen" },
221 },
222 },
223 });
224 });
225​
226 it("reports conflicting shortcuts that share an active when context", () => {
227 const rows = buildKeybindingRows([
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.newLocal",
245 shortcut: {
246 key: "n",
247 modKey: true,
248 metaKey: false,
249 ctrlKey: false,
250 altKey: false,
251 shiftKey: false,
252 },
253 whenAst: {
254 type: "not",
255 node: { type: "identifier", name: "terminalFocus" },
256 },
257 },
258 ] satisfies ResolvedKeybindingsConfig);
259​
260 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
261 expect(
262 keybindingConflictLabels(rows, {
263 rowId: rows[0]?.id ?? "",
264 key: "mod+n",
265 when: "",
266 }),
267 ).toEqual(["Chat: New Local"]);
268 });
269​
270 it.each([
271 { platform: "Linux x86_64", ctrl: ["Chat: New"], meta: [] },
272 { platform: "Win32", ctrl: ["Chat: New"], meta: [] },
273 { platform: "MacIntel", ctrl: [], meta: ["Chat: New"] },
274 ])("resolves mod to the $platform modifier when reporting conflicts", (expected) => {
275 const rows = buildKeybindingRows(
276 compileResolvedKeybindingsConfig([
277 { command: "chat.new", key: "mod+k" },
278 { command: "chat.newLocal", key: "ctrl+k" },
279 { command: "thread.copyReference", key: "meta+k" },
280 ]),
281 expected.platform,
282 );
283 const conflictsFor = (key: string) => rows.find((row) => row.key === key)?.conflicts;
284​
285 expect(conflictsFor("ctrl+k")).toEqual(expected.ctrl);
286 expect(conflictsFor("meta+k")).toEqual(expected.meta);
287 expect(
288 keybindingConflictLabels(rows, { rowId: "new", key: "ctrl+k", when: "" }, expected.platform),
289 ).toEqual(["Chat: New Local", ...expected.ctrl].toSorted());
290 });
291​
292 it("keeps mod and ctrl shortcuts apart when their when contexts differ", () => {
293 const rows = buildKeybindingRows(
294 compileResolvedKeybindingsConfig([
295 { command: "chat.new", key: "mod+k", when: "!terminalFocus" },
296 { command: "chat.newLocal", key: "ctrl+k", when: "terminalFocus" },
297 ]),
298 "Linux x86_64",
299 );
300​
301 expect(rows.map((row) => row.conflicts)).toEqual([[], []]);
302 });
303});