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

KeybindingsSettings.logic.test.ts

251 lines6.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
3​
4import {
5 buildKeybindingRows,
6 buildKeybindingCommandOptions,
7 buildWhenVariableOptions,
8 commandLabel,
9 keybindingConflictLabels,
10 keybindingFromKeyboardEvent,
11 parseWhenExpressionDraft,
12 shortcutToKeybindingInput,
13 unknownWhenVariables,
14 whenAstToExpression,
15} from "./KeybindingsSettings.logic";
16​
17describe("KeybindingsSettings.logic", () => {
18 it("builds searchable rows with readable key and when values", () => {
19 const rows = buildKeybindingRows(
20 [
21 {
22 command: "terminal.toggle",
23 shortcut: {
24 key: "j",
25 modKey: true,
26 metaKey: false,
27 ctrlKey: false,
28 altKey: false,
29 shiftKey: false,
30 },
31 whenAst: {
32 type: "not",
33 node: { type: "identifier", name: "terminalFocus" },
34 },
35 },
36 ] satisfies ResolvedKeybindingsConfig,
37 "terminal",
38 );
39​
40 expect(rows).toEqual([
41 expect.objectContaining({
42 command: "terminal.toggle",
43 key: "mod+j",
44 when: "!terminalFocus",
45 defaultKey: "mod+j",
46 defaultWhen: "",
47 source: "Custom",
48 }),
49 ]);
50 });
51​
52 it("captures platform-specific mod shortcuts", () => {
53 expect(
54 keybindingFromKeyboardEvent(
55 { key: "K", metaKey: true, ctrlKey: false, altKey: false, shiftKey: true },
56 "MacIntel",
57 ),
58 ).toBe("mod+shift+k");
59 expect(
60 keybindingFromKeyboardEvent(
61 { key: "K", metaKey: false, ctrlKey: true, altKey: false, shiftKey: true },
62 "Win32",
63 ),
64 ).toBe("mod+shift+k");
65 });
66​
67 it("serializes shortcuts and when expressions for upserts", () => {
68 expect(
69 shortcutToKeybindingInput({
70 key: " ",
71 modKey: true,
72 metaKey: false,
73 ctrlKey: false,
74 altKey: true,
75 shiftKey: false,
76 }),
77 ).toBe("mod+alt+space");
78​
79 expect(
80 whenAstToExpression({
81 type: "and",
82 left: { type: "identifier", name: "editorFocus" },
83 right: {
84 type: "not",
85 node: { type: "identifier", name: "terminalFocus" },
86 },
87 }),
88 ).toBe("editorFocus && !terminalFocus");
89​
90 expect(parseWhenExpressionDraft("editorFocus && (!terminalFocus || modelPickerOpen)")).toEqual({
91 ok: true,
92 value: {
93 type: "and",
94 left: { type: "identifier", name: "editorFocus" },
95 right: {
96 type: "or",
97 left: {
98 type: "not",
99 node: { type: "identifier", name: "terminalFocus" },
100 },
101 right: { type: "identifier", name: "modelPickerOpen" },
102 },
103 },
104 });
105 expect(parseWhenExpressionDraft("editorFocus &&")).toEqual({
106 ok: false,
107 message: "Use variables with !, &&, ||, and parentheses.",
108 });
109​
110 expect(parseWhenExpressionDraft("!(terminalFocus || modelPickerOpen)")).toEqual({
111 ok: true,
112 value: {
113 type: "not",
114 node: {
115 type: "or",
116 left: { type: "identifier", name: "terminalFocus" },
117 right: { type: "identifier", name: "modelPickerOpen" },
118 },
119 },
120 });
121 });
122​
123 it("formats static and project script command labels", () => {
124 expect(commandLabel("commandPalette.toggle")).toBe("Command Palette: Toggle");
125 expect(commandLabel("themeEditor.toggle")).toBe("Theme Editor: Toggle");
126 expect(commandLabel("script.setup-db.run")).toBe("Run Script: Setup Db");
127 });
128​
129 it("builds known when variable options from defaults without frontend labels", () => {
130 const options = buildWhenVariableOptions();
131​
132 expect(options).toEqual(
133 expect.arrayContaining(["terminalFocus", "terminalOpen", "modelPickerOpen", "true", "false"]),
134 );
135 expect(options).not.toContain("customModeActive");
136 });
137​
138 it("builds command options from built-in commands and resolved project bindings", () => {
139 const options = buildKeybindingCommandOptions([
140 {
141 command: "script.setup-db.run",
142 shortcut: {
143 key: "r",
144 modKey: true,
145 metaKey: false,
146 ctrlKey: false,
147 altKey: false,
148 shiftKey: false,
149 },
150 },
151 ] satisfies ResolvedKeybindingsConfig);
152​
153 expect(options).toEqual(
154 expect.arrayContaining(["chat.new", "rightPanel.toggleMaximized", "script.setup-db.run"]),
155 );
156 });
157​
158 it("reports unknown when variables without rejecting parseable expressions", () => {
159 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
160​
161 expect(parsed.ok).toBe(true);
162 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
163 });
164​
165 it("marks each default shortcut for multi-binding commands as default", () => {
166 const rows = buildKeybindingRows(
167 [
168 {
169 command: "chat.new",
170 shortcut: {
171 key: "n",
172 modKey: true,
173 metaKey: false,
174 ctrlKey: false,
175 altKey: false,
176 shiftKey: false,
177 },
178 whenAst: {
179 type: "not",
180 node: { type: "identifier", name: "terminalFocus" },
181 },
182 },
183 {
184 command: "chat.new",
185 shortcut: {
186 key: "o",
187 modKey: true,
188 metaKey: false,
189 ctrlKey: false,
190 altKey: false,
191 shiftKey: true,
192 },
193 whenAst: {
194 type: "not",
195 node: { type: "identifier", name: "terminalFocus" },
196 },
197 },
198 ] satisfies ResolvedKeybindingsConfig,
199 "",
200 );
201​
202 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
203 });
204​
205 it("reports conflicting shortcuts that share an active when context", () => {
206 const rows = buildKeybindingRows(
207 [
208 {
209 command: "chat.new",
210 shortcut: {
211 key: "n",
212 modKey: true,
213 metaKey: false,
214 ctrlKey: false,
215 altKey: false,
216 shiftKey: false,
217 },
218 whenAst: {
219 type: "not",
220 node: { type: "identifier", name: "terminalFocus" },
221 },
222 },
223 {
224 command: "chat.newLocal",
225 shortcut: {
226 key: "n",
227 modKey: true,
228 metaKey: false,
229 ctrlKey: false,
230 altKey: false,
231 shiftKey: false,
232 },
233 whenAst: {
234 type: "not",
235 node: { type: "identifier", name: "terminalFocus" },
236 },
237 },
238 ] satisfies ResolvedKeybindingsConfig,
239 "",
240 );
241​
242 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
243 expect(
244 keybindingConflictLabels(rows, {
245 rowId: rows[0]?.id ?? "",
246 key: "mod+n",
247 when: "",
248 }),
249 ).toEqual(["Chat: New Local"]);
250 });
251});