Open History to see the last commit
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 parsedPullRequestCopyNumber = yield* decode(KeybindingRule, {
126 key: "mod+shift+k",
127 command: "pullRequest.copyNumber",
128 when: "!terminalFocus",
129 });
130 assert.strictEqual(parsedPullRequestCopyNumber.command, "pullRequest.copyNumber");
131
132 const parsedThreadStop = yield* decode(KeybindingRule, {
133 key: "mod+escape",
134 command: "thread.stop",
135 });
136 assert.strictEqual(parsedThreadStop.command, "thread.stop");
137 }),
138);
139
140it.effect("rejects invalid command values", () =>
141 Effect.gen(function* () {
142 const result = yield* Effect.exit(
143 decode(KeybindingRule, {
144 key: "mod+j",
145 command: "script.Test.run",
146 }),
147 );
148 assert.strictEqual(result._tag, "Failure");
149 }),
150);
151
152it.effect("accepts dynamic script run commands", () =>
153 Effect.gen(function* () {
154 const parsed = yield* decode(KeybindingRule, {
155 key: "mod+r",
156 command: "script.setup.run",
157 });
158 assert.strictEqual(parsed.command, "script.setup.run");
159 }),
160);
161
162it.effect("parses keybindings array payload", () =>
163 Effect.gen(function* () {
164 const parsed = yield* decode(KeybindingsConfig, [
165 { key: "mod+j", command: "terminal.toggle" },
166 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
167 { key: "mod+shift+d", command: "terminal.splitVertical", when: "terminalFocus" },
168 ]);
169 assert.lengthOf(parsed, 3);
170 }),
171);
172
173it.effect("parses resolved keybinding rules", () =>
174 Effect.gen(function* () {
175 const parsed = yield* decode(ResolvedKeybindingRule, {
176 command: "terminal.split",
177 shortcut: {
178 key: "d",
179 metaKey: false,
180 ctrlKey: false,
181 shiftKey: false,
182 altKey: false,
183 modKey: true,
184 },
185 whenAst: {
186 type: "and",
187 left: { type: "identifier", name: "terminalOpen" },
188 right: {
189 type: "not",
190 node: { type: "identifier", name: "terminalFocus" },
191 },
192 },
193 });
194 assert.strictEqual(parsed.shortcut.key, "d");
195 }),
196);
197
198it.effect("parses resolved keybindings arrays", () =>
199 Effect.gen(function* () {
200 const parsed = yield* decode(ResolvedKeybindingsConfig, [
201 {
202 command: "terminal.toggle",
203 shortcut: {
204 key: "j",
205 metaKey: false,
206 ctrlKey: false,
207 shiftKey: false,
208 altKey: false,
209 modKey: true,
210 },
211 },
212 {
213 command: "thread.jump.3",
214 shortcut: {
215 key: "3",
216 metaKey: false,
217 ctrlKey: false,
218 shiftKey: false,
219 altKey: false,
220 modKey: true,
221 },
222 },
223 ]);
224 assert.lengthOf(parsed, 2);
225 }),
226);
227
228const shortcut = {
229 key: "p",
230 metaKey: false,
231 ctrlKey: false,
232 shiftKey: false,
233 altKey: false,
234 modKey: true,
235};
236
237it.effect("drops resolved rules with commands this build does not know", () =>
238 Effect.gen(function* () {
239 const parsed = yield* decode(ResolvedKeybindingsConfig, [
240 { command: "terminal.toggle", shortcut },
241 { command: "someFuture.toggle", shortcut },
242 { command: "filePicker.toggle", shortcut },
243 ]);
244 assert.deepEqual(
245 parsed.map((rule) => rule.command),
246 ["terminal.toggle", "filePicker.toggle"],
247 );
248 }),
249);
250
251it.effect("drops resolved rules with unknown when-node types", () =>
252 Effect.gen(function* () {
253 const parsed = yield* decode(ResolvedKeybindingsConfig, [
254 {
255 command: "terminal.toggle",
256 shortcut,
257 whenAst: { type: "xor", left: 1, right: 2 },
258 },
259 { command: "terminal.split", shortcut },
260 ]);
261 assert.deepEqual(
262 parsed.map((rule) => rule.command),
263 ["terminal.split"],
264 );
265 }),
266);
267
268it.effect("drops malformed resolved rule entries", () =>
269 Effect.gen(function* () {
270 const parsed = yield* decode(ResolvedKeybindingsConfig, [
271 "garbage",
272 { command: "terminal.toggle", shortcut },
273 null,
274 ]);
275 assert.deepEqual(
276 parsed.map((rule) => rule.command),
277 ["terminal.toggle"],
278 );
279 }),
280);
281
282it.effect("encodes resolved keybindings to the plain wire shape", () =>
283 Effect.gen(function* () {
284 const rules = [{ command: "terminal.toggle" as const, shortcut }];
285 const encoded = yield* encodeResolvedKeybindings(rules);
286 assert.deepEqual(encoded, rules);
287 const roundTripped = yield* decode(ResolvedKeybindingsConfig, encoded);
288 assert.deepEqual(roundTripped, rules);
289 }),
290);
291
292it.effect("drops unknown fields in resolved keybinding rules", () =>
293 decodeResolvedRule({
294 command: "terminal.toggle",
295 shortcut: {
296 key: "j",
297 metaKey: false,
298 ctrlKey: false,
299 shiftKey: false,
300 altKey: false,
301 modKey: true,
302 },
303 key: "mod+j",
304 }).pipe(
305 Effect.map((parsed) => {
306 const view = parsed as Record<string, unknown>;
307 assert.strictEqual("key" in view, false);
308 assert.strictEqual(view.command, "terminal.toggle");
309 }),
310 ),
311);