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

KeybindingsSettings.logic.test.ts

249 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 defaults 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(expect.arrayContaining(["chat.new", "script.setup-db.run"]));
154 });
155​
156 it("reports unknown when variables without rejecting parseable expressions", () => {
157 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
158​
159 expect(parsed.ok).toBe(true);
160 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
161 });
162​
163 it("marks each default shortcut for multi-binding commands as default", () => {
164 const rows = buildKeybindingRows(
165 [
166 {
167 command: "chat.new",
168 shortcut: {
169 key: "n",
170 modKey: true,
171 metaKey: false,
172 ctrlKey: false,
173 altKey: false,
174 shiftKey: false,
175 },
176 whenAst: {
177 type: "not",
178 node: { type: "identifier", name: "terminalFocus" },
179 },
180 },
181 {
182 command: "chat.new",
183 shortcut: {
184 key: "o",
185 modKey: true,
186 metaKey: false,
187 ctrlKey: false,
188 altKey: false,
189 shiftKey: true,
190 },
191 whenAst: {
192 type: "not",
193 node: { type: "identifier", name: "terminalFocus" },
194 },
195 },
196 ] satisfies ResolvedKeybindingsConfig,
197 "",
198 );
199​
200 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
201 });
202​
203 it("reports conflicting shortcuts that share an active when context", () => {
204 const rows = buildKeybindingRows(
205 [
206 {
207 command: "chat.new",
208 shortcut: {
209 key: "n",
210 modKey: true,
211 metaKey: false,
212 ctrlKey: false,
213 altKey: false,
214 shiftKey: false,
215 },
216 whenAst: {
217 type: "not",
218 node: { type: "identifier", name: "terminalFocus" },
219 },
220 },
221 {
222 command: "chat.newLocal",
223 shortcut: {
224 key: "n",
225 modKey: true,
226 metaKey: false,
227 ctrlKey: false,
228 altKey: false,
229 shiftKey: false,
230 },
231 whenAst: {
232 type: "not",
233 node: { type: "identifier", name: "terminalFocus" },
234 },
235 },
236 ] satisfies ResolvedKeybindingsConfig,
237 "",
238 );
239​
240 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
241 expect(
242 keybindingConflictLabels(rows, {
243 rowId: rows[0]?.id ?? "",
244 key: "mod+n",
245 when: "",
246 }),
247 ).toEqual(["Chat: New Local"]);
248 });
249});