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

keybindings.test.ts

304 lines8.6 KB

1import { assert, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3import * as Effect from "effect/Effect";
4​
5import {
6 KeybindingsConfig,
7 KeybindingRule,
8 ResolvedKeybindingRule,
9 ResolvedKeybindingsConfig,
10} from "./keybindings.ts";
11​
12const decode = <S extends Schema.Top>(
13 schema: S,
14 input: unknown,
15): Effect.Effect<Schema.Schema.Type<S>, Schema.SchemaError, never> =>
16 Schema.decodeUnknownEffect(schema as never)(input) as Effect.Effect<
17 Schema.Schema.Type<S>,
18 Schema.SchemaError,
19 never
20 >;
21​
22const decodeResolvedRule = Schema.decodeUnknownEffect(ResolvedKeybindingRule as never);
23const encodeResolvedKeybindings = Schema.encodeEffect(ResolvedKeybindingsConfig);
24​
25it.effect("parses keybinding rules", () =>
26 Effect.gen(function* () {
27 const parsed = yield* decode(KeybindingRule, {
28 key: "mod+j",
29 command: "terminal.toggle",
30 });
31 assert.strictEqual(parsed.command, "terminal.toggle");
32​
33 const parsedSidebarToggle = yield* decode(KeybindingRule, {
34 key: "mod+b",
35 command: "sidebar.toggle",
36 });
37 assert.strictEqual(parsedSidebarToggle.command, "sidebar.toggle");
38​
39 const parsedRightPanelToggle = yield* decode(KeybindingRule, {
40 key: "mod+alt+b",
41 command: "rightPanel.toggle",
42 });
43 assert.strictEqual(parsedRightPanelToggle.command, "rightPanel.toggle");
44​
45 const parsedRightPanelToggleMaximized = yield* decode(KeybindingRule, {
46 key: "mod+shift+m",
47 command: "rightPanel.toggleMaximized",
48 });
49 assert.strictEqual(parsedRightPanelToggleMaximized.command, "rightPanel.toggleMaximized");
50​
51 const parsedClose = yield* decode(KeybindingRule, {
52 key: "mod+w",
53 command: "terminal.close",
54 });
55 assert.strictEqual(parsedClose.command, "terminal.close");
56​
57 const parsedDiffToggle = yield* decode(KeybindingRule, {
58 key: "mod+d",
59 command: "diff.toggle",
60 });
61 assert.strictEqual(parsedDiffToggle.command, "diff.toggle");
62​
63 const parsedCommandPalette = yield* decode(KeybindingRule, {
64 key: "mod+k",
65 command: "commandPalette.toggle",
66 });
67 assert.strictEqual(parsedCommandPalette.command, "commandPalette.toggle");
68​
69 const parsedFilePicker = yield* decode(KeybindingRule, {
70 key: "mod+p",
71 command: "filePicker.toggle",
72 });
73 assert.strictEqual(parsedFilePicker.command, "filePicker.toggle");
74​
75 const parsedProjectSearch = yield* decode(KeybindingRule, {
76 key: "mod+shift+f",
77 command: "projectSearch.toggle",
78 });
79 assert.strictEqual(parsedProjectSearch.command, "projectSearch.toggle");
80​
81 const parsedThemeEditor = yield* decode(KeybindingRule, {
82 key: "mod+alt+shift+t",
83 command: "themeEditor.toggle",
84 });
85 assert.strictEqual(parsedThemeEditor.command, "themeEditor.toggle");
86​
87 const parsedLocal = yield* decode(KeybindingRule, {
88 key: "mod+shift+n",
89 command: "chat.newLocal",
90 });
91 assert.strictEqual(parsedLocal.command, "chat.newLocal");
92​
93 const parsedModelPickerToggle = yield* decode(KeybindingRule, {
94 key: "mod+shift+m",
95 command: "modelPicker.toggle",
96 });
97 assert.strictEqual(parsedModelPickerToggle.command, "modelPicker.toggle");
98​
99 const parsedModelPickerJump = yield* decode(KeybindingRule, {
100 key: "mod+1",
101 command: "modelPicker.jump.1",
102 });
103 assert.strictEqual(parsedModelPickerJump.command, "modelPicker.jump.1");
104​
105 const parsedThreadPrevious = yield* decode(KeybindingRule, {
106 key: "mod+shift+[",
107 command: "thread.previous",
108 });
109 assert.strictEqual(parsedThreadPrevious.command, "thread.previous");
110​
111 const parsedThreadSettle = yield* decode(KeybindingRule, {
112 key: "mod+shift+s",
113 command: "thread.settle",
114 when: "!terminalFocus",
115 });
116 assert.strictEqual(parsedThreadSettle.command, "thread.settle");
117​
118 const parsedThreadCopyReference = yield* decode(KeybindingRule, {
119 key: "mod+shift+c",
120 command: "thread.copyReference",
121 when: "!terminalFocus",
122 });
123 assert.strictEqual(parsedThreadCopyReference.command, "thread.copyReference");
124​
125 const parsedThreadStop = yield* decode(KeybindingRule, {
126 key: "mod+escape",
127 command: "thread.stop",
128 });
129 assert.strictEqual(parsedThreadStop.command, "thread.stop");
130 }),
131);
132​
133it.effect("rejects invalid command values", () =>
134 Effect.gen(function* () {
135 const result = yield* Effect.exit(
136 decode(KeybindingRule, {
137 key: "mod+j",
138 command: "script.Test.run",
139 }),
140 );
141 assert.strictEqual(result._tag, "Failure");
142 }),
143);
144​
145it.effect("accepts dynamic script run commands", () =>
146 Effect.gen(function* () {
147 const parsed = yield* decode(KeybindingRule, {
148 key: "mod+r",
149 command: "script.setup.run",
150 });
151 assert.strictEqual(parsed.command, "script.setup.run");
152 }),
153);
154​
155it.effect("parses keybindings array payload", () =>
156 Effect.gen(function* () {
157 const parsed = yield* decode(KeybindingsConfig, [
158 { key: "mod+j", command: "terminal.toggle" },
159 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
160 { key: "mod+shift+d", command: "terminal.splitVertical", when: "terminalFocus" },
161 ]);
162 assert.lengthOf(parsed, 3);
163 }),
164);
165​
166it.effect("parses resolved keybinding rules", () =>
167 Effect.gen(function* () {
168 const parsed = yield* decode(ResolvedKeybindingRule, {
169 command: "terminal.split",
170 shortcut: {
171 key: "d",
172 metaKey: false,
173 ctrlKey: false,
174 shiftKey: false,
175 altKey: false,
176 modKey: true,
177 },
178 whenAst: {
179 type: "and",
180 left: { type: "identifier", name: "terminalOpen" },
181 right: {
182 type: "not",
183 node: { type: "identifier", name: "terminalFocus" },
184 },
185 },
186 });
187 assert.strictEqual(parsed.shortcut.key, "d");
188 }),
189);
190​
191it.effect("parses resolved keybindings arrays", () =>
192 Effect.gen(function* () {
193 const parsed = yield* decode(ResolvedKeybindingsConfig, [
194 {
195 command: "terminal.toggle",
196 shortcut: {
197 key: "j",
198 metaKey: false,
199 ctrlKey: false,
200 shiftKey: false,
201 altKey: false,
202 modKey: true,
203 },
204 },
205 {
206 command: "thread.jump.3",
207 shortcut: {
208 key: "3",
209 metaKey: false,
210 ctrlKey: false,
211 shiftKey: false,
212 altKey: false,
213 modKey: true,
214 },
215 },
216 ]);
217 assert.lengthOf(parsed, 2);
218 }),
219);
220​
221const shortcut = {
222 key: "p",
223 metaKey: false,
224 ctrlKey: false,
225 shiftKey: false,
226 altKey: false,
227 modKey: true,
228};
229​
230it.effect("drops resolved rules with commands this build does not know", () =>
231 Effect.gen(function* () {
232 const parsed = yield* decode(ResolvedKeybindingsConfig, [
233 { command: "terminal.toggle", shortcut },
234 { command: "someFuture.toggle", shortcut },
235 { command: "filePicker.toggle", shortcut },
236 ]);
237 assert.deepEqual(
238 parsed.map((rule) => rule.command),
239 ["terminal.toggle", "filePicker.toggle"],
240 );
241 }),
242);
243​
244it.effect("drops resolved rules with unknown when-node types", () =>
245 Effect.gen(function* () {
246 const parsed = yield* decode(ResolvedKeybindingsConfig, [
247 {
248 command: "terminal.toggle",
249 shortcut,
250 whenAst: { type: "xor", left: 1, right: 2 },
251 },
252 { command: "terminal.split", shortcut },
253 ]);
254 assert.deepEqual(
255 parsed.map((rule) => rule.command),
256 ["terminal.split"],
257 );
258 }),
259);
260​
261it.effect("drops malformed resolved rule entries", () =>
262 Effect.gen(function* () {
263 const parsed = yield* decode(ResolvedKeybindingsConfig, [
264 "garbage",
265 { command: "terminal.toggle", shortcut },
266 null,
267 ]);
268 assert.deepEqual(
269 parsed.map((rule) => rule.command),
270 ["terminal.toggle"],
271 );
272 }),
273);
274​
275it.effect("encodes resolved keybindings to the plain wire shape", () =>
276 Effect.gen(function* () {
277 const rules = [{ command: "terminal.toggle" as const, shortcut }];
278 const encoded = yield* encodeResolvedKeybindings(rules);
279 assert.deepEqual(encoded, rules);
280 const roundTripped = yield* decode(ResolvedKeybindingsConfig, encoded);
281 assert.deepEqual(roundTripped, rules);
282 }),
283);
284​
285it.effect("drops unknown fields in resolved keybinding rules", () =>
286 decodeResolvedRule({
287 command: "terminal.toggle",
288 shortcut: {
289 key: "j",
290 metaKey: false,
291 ctrlKey: false,
292 shiftKey: false,
293 altKey: false,
294 modKey: true,
295 },
296 key: "mod+j",
297 }).pipe(
298 Effect.map((parsed) => {
299 const view = parsed as Record<string, unknown>;
300 assert.strictEqual("key" in view, false);
301 assert.strictEqual(view.command, "terminal.toggle");
302 }),
303 ),
304);