Open History to see the last commit
1import { KeybindingCommand, KeybindingRule, KeybindingsConfig } from "@t3tools/contracts";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import { assertFailure } from "@effect/vitest/utils";
5import * as Cause from "effect/Cause";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Logger from "effect/Logger";
10import * as Path from "effect/Path";
11import * as Schema from "effect/Schema";
12import * as ServerConfig from "./config.ts";
13import * as Keybindings from "./keybindings.ts";
14import { KeybindingsConfigError, MAX_KEYBINDINGS_COUNT } from "@t3tools/contracts";
15import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
16
17const KeybindingsConfigJson = Schema.fromJsonString(KeybindingsConfig);
18const encodeKeybindingsConfigJson = Schema.encodeEffect(KeybindingsConfigJson);
19const decodeKeybindingsConfigJson = Schema.decodeUnknownEffect(KeybindingsConfigJson);
20const encodeResolvedKeybindingFromConfig = Schema.encodeEffect(
21 Keybindings.ResolvedKeybindingFromConfig,
22);
23const decodeResolvedKeybindingFromConfigExit = Schema.decodeUnknownExit(
24 Keybindings.ResolvedKeybindingFromConfig,
25);
26const layerKeybindings = () => {
27 return Keybindings.layer.pipe(
28 Layer.provideMerge(
29 Layer.fresh(
30 ServerConfig.layerTest(process.cwd(), {
31 prefix: "t3code-keybindings-test-",
32 }),
33 ),
34 ),
35 );
36};
37
38const toDetailResult = <A, R>(effect: Effect.Effect<A, KeybindingsConfigError, R>) =>
39 effect.pipe(
40 Effect.mapError((error) => error.detail),
41 Effect.result,
42 );
43
44const writeKeybindingsConfig = (configPath: string, rules: readonly KeybindingRule[]) =>
45 Effect.gen(function* () {
46 const fileSystem = yield* FileSystem.FileSystem;
47 const path = yield* Path.Path;
48 const encoded = yield* encodeKeybindingsConfigJson(rules);
49 yield* fileSystem.makeDirectory(path.dirname(configPath), { recursive: true });
50 yield* fileSystem.writeFileString(configPath, encoded);
51 });
52
53const readKeybindingsConfig = (configPath: string) =>
54 Effect.gen(function* () {
55 const fileSystem = yield* FileSystem.FileSystem;
56 const rawConfig = yield* fileSystem.readFileString(configPath);
57 return yield* decodeKeybindingsConfigJson(rawConfig);
58 });
59
60it.layer(NodeServices.layer)("keybindings", (it) => {
61 it.effect("parses shortcuts including plus key", () =>
62 Effect.sync(() => {
63 assert.deepEqual(Keybindings.parseKeybindingShortcut("mod+j"), {
64 key: "j",
65 metaKey: false,
66 ctrlKey: false,
67 shiftKey: false,
68 altKey: false,
69 modKey: true,
70 });
71 assert.deepEqual(Keybindings.parseKeybindingShortcut("mod++"), {
72 key: "+",
73 metaKey: false,
74 ctrlKey: false,
75 shiftKey: false,
76 altKey: false,
77 modKey: true,
78 });
79 }),
80 );
81
82 it.effect("compiles valid rule with parsed when AST", () =>
83 Effect.sync(() => {
84 const compiled = Keybindings.compileResolvedKeybindingRule({
85 key: "mod+d",
86 command: "terminal.split",
87 when: "terminalOpen && !terminalFocus",
88 });
89
90 assert.deepEqual(compiled, {
91 command: "terminal.split",
92 shortcut: {
93 key: "d",
94 metaKey: false,
95 ctrlKey: false,
96 shiftKey: false,
97 altKey: false,
98 modKey: true,
99 },
100 whenAst: {
101 type: "and",
102 left: { type: "identifier", name: "terminalOpen" },
103 right: {
104 type: "not",
105 node: { type: "identifier", name: "terminalFocus" },
106 },
107 },
108 });
109 }),
110 );
111
112 it.effect("encodes resolved plus-key shortcuts", () =>
113 Effect.gen(function* () {
114 const encoded = yield* encodeResolvedKeybindingFromConfig({
115 command: "terminal.toggle",
116 shortcut: {
117 key: "+",
118 metaKey: false,
119 ctrlKey: false,
120 shiftKey: false,
121 altKey: false,
122 modKey: true,
123 },
124 });
125
126 assert.equal(encoded.key, "mod++");
127 assert.equal(encoded.command, "terminal.toggle");
128 }),
129 );
130
131 it.effect("rejects invalid rules", () =>
132 Effect.sync(() => {
133 assert.isNull(
134 Keybindings.compileResolvedKeybindingRule({
135 key: "mod+shift+d+o",
136 command: "terminal.new",
137 }),
138 );
139
140 assert.isNull(
141 Keybindings.compileResolvedKeybindingRule({
142 key: "mod+d",
143 command: "terminal.split",
144 when: "terminalFocus && (",
145 }),
146 );
147
148 assert.isNull(
149 Keybindings.compileResolvedKeybindingRule({
150 key: "mod+d",
151 command: "terminal.split",
152 when: `${"!".repeat(300)}terminalFocus`,
153 }),
154 );
155 }),
156 );
157
158 it.effect("formats invalid resolved keybinding rules with the custom message", () =>
159 Effect.sync(() => {
160 const result = decodeResolvedKeybindingFromConfigExit({
161 key: "mod+shift+d+o",
162 command: "terminal.new",
163 });
164
165 if (result._tag !== "Failure") {
166 assert.fail("Expected invalid keybinding decode to fail");
167 }
168
169 const detail = Cause.pretty(result.cause);
170 assert.isTrue(detail.includes("Invalid keybinding rule"));
171 assert.isFalse(detail.includes("Invalid data"));
172 }),
173 );
174
175 it.effect("bootstraps default keybindings when config file is missing", () =>
176 Effect.gen(function* () {
177 const fs = yield* FileSystem.FileSystem;
178 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
179 assert.isFalse(yield* fs.exists(keybindingsConfigPath));
180
181 yield* Effect.gen(function* () {
182 const keybindings = yield* Keybindings.Keybindings;
183 yield* keybindings.syncDefaultKeybindingsOnStartup;
184 });
185
186 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
187 assert.deepEqual(persisted, Keybindings.DEFAULT_KEYBINDINGS);
188 }).pipe(Effect.provide(layerKeybindings())),
189 );
190
191 it.effect("uses defaults in runtime when config is malformed without overriding file", () =>
192 Effect.gen(function* () {
193 const fs = yield* FileSystem.FileSystem;
194 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
195 yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");
196
197 const configState = yield* Effect.gen(function* () {
198 const keybindings = yield* Keybindings.Keybindings;
199 return yield* keybindings.loadConfigState;
200 });
201
202 assert.deepEqual(
203 configState.keybindings,
204 Keybindings.compileResolvedKeybindingsConfig(Keybindings.DEFAULT_KEYBINDINGS),
205 );
206 assert.deepEqual(configState.issues, [
207 {
208 kind: "keybindings.malformed-config",
209 message: configState.issues[0]?.message ?? "",
210 },
211 ]);
212 assert.equal(yield* fs.readFileString(keybindingsConfigPath), "{ not-json");
213 }).pipe(Effect.provide(layerKeybindings())),
214 );
215
216 it.effect("ignores invalid entries in runtime and reports them as issues", () =>
217 Effect.gen(function* () {
218 const fs = yield* FileSystem.FileSystem;
219 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
220 yield* fs.writeFileString(
221 keybindingsConfigPath,
222 // @effect-diagnostics-next-line preferSchemaOverJson:off
223 JSON.stringify([
224 { key: "mod+j", command: "terminal.toggle" },
225 { key: "mod+shift+d+o", command: "terminal.new" },
226 { key: "mod+x", command: "invalid.command" },
227 ]),
228 );
229
230 const configState = yield* Effect.gen(function* () {
231 const keybindings = yield* Keybindings.Keybindings;
232 return yield* keybindings.loadConfigState;
233 });
234
235 assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
236 assert.isFalse(
237 configState.keybindings.some((entry) => String(entry.command) === "invalid.command"),
238 );
239 assert.deepEqual(configState.issues, [
240 {
241 kind: "keybindings.invalid-entry",
242 index: 1,
243 message: configState.issues[0]?.message ?? "",
244 },
245 {
246 kind: "keybindings.invalid-entry",
247 index: 2,
248 message: configState.issues[1]?.message ?? "",
249 },
250 ]);
251 }).pipe(Effect.provide(layerKeybindings())),
252 );
253
254 it.effect(
255 "upserts missing default keybindings on startup without overriding existing command rules",
256 () =>
257 Effect.gen(function* () {
258 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
259 yield* writeKeybindingsConfig(keybindingsConfigPath, [
260 { key: "mod+shift+t", command: "terminal.toggle" },
261 { key: "mod+shift+r", command: "script.run-tests.run" },
262 ]);
263
264 yield* Effect.gen(function* () {
265 const keybindings = yield* Keybindings.Keybindings;
266 yield* keybindings.syncDefaultKeybindingsOnStartup;
267 });
268
269 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
270 const byCommand = new Map(persisted.map((entry) => [entry.command, entry]));
271
272 const persistedToggle = byCommand.get("terminal.toggle");
273 assert.isNotNull(persistedToggle);
274 assert.equal(persistedToggle?.key, "mod+shift+t");
275 assert.isFalse(
276 persisted.some((entry) => entry.command === "terminal.toggle" && entry.key === "mod+j"),
277 );
278
279 for (const defaultRule of Keybindings.DEFAULT_KEYBINDINGS) {
280 assert.isTrue(byCommand.has(defaultRule.command), `expected ${defaultRule.command}`);
281 }
282 assert.isTrue(byCommand.has("script.run-tests.run"));
283 }).pipe(Effect.provide(layerKeybindings())),
284 );
285
286 it.effect("adds a late default to an existing command once", () =>
287 Effect.gen(function* () {
288 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
289 const keybindings = yield* Keybindings.Keybindings;
290 const when = "composerFocus && draftThreadRoute";
291 const existing = { key: "mod+alt+enter", command: "composer.sendBackground", when } as const;
292 const backgroundRules = Effect.map(readKeybindingsConfig(keybindingsConfigPath), (rules) =>
293 rules.filter((entry) => entry.command === "composer.sendBackground"),
294 );
295 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
296
297 yield* keybindings.syncDefaultKeybindingsOnStartup;
298 assert.deepStrictEqual(yield* backgroundRules, [
299 existing,
300 { key: "mod+enter", command: "composer.sendBackground", when },
301 ]);
302
303 // Removing the added rule later must survive the next startup.
304 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
305 yield* keybindings.syncDefaultKeybindingsOnStartup;
306 assert.deepStrictEqual(yield* backgroundRules, [existing]);
307 }).pipe(Effect.provide(layerKeybindings())),
308 );
309
310 it.effect("leaves a customized command without the late default", () =>
311 Effect.gen(function* () {
312 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
313 const keybindings = yield* Keybindings.Keybindings;
314 const custom = {
315 key: "alt+b",
316 command: "composer.sendBackground",
317 when: "composerFocus && draftThreadRoute",
318 } as const;
319 yield* writeKeybindingsConfig(keybindingsConfigPath, [custom]);
320
321 yield* keybindings.syncDefaultKeybindingsOnStartup;
322 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
323 assert.deepStrictEqual(
324 persisted.filter((entry) => entry.command === "composer.sendBackground"),
325 [custom],
326 );
327 }).pipe(Effect.provide(layerKeybindings())),
328 );
329
330 it.effect("keeps a late default pending while the config is full", () =>
331 Effect.gen(function* () {
332 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
333 const keybindings = yield* Keybindings.Keybindings;
334 const when = "composerFocus && draftThreadRoute";
335 const existing = { key: "mod+alt+enter", command: "composer.sendBackground", when } as const;
336 const fillers = Array.from({ length: MAX_KEYBINDINGS_COUNT - 1 }, (_, index) => ({
337 key: "mod+alt+f1",
338 command: `script.filler-${index}.run` as const,
339 }));
340 const hasModEnter = Effect.map(readKeybindingsConfig(keybindingsConfigPath), (rules) =>
341 rules.some(
342 (entry) => entry.command === "composer.sendBackground" && entry.key === "mod+enter",
343 ),
344 );
345
346 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing, ...fillers]);
347 yield* keybindings.syncDefaultKeybindingsOnStartup;
348 assert.isFalse(yield* hasModEnter);
349
350 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
351 yield* keybindings.syncDefaultKeybindingsOnStartup;
352 assert.isTrue(yield* hasModEnter);
353 }).pipe(Effect.provide(layerKeybindings())),
354 );
355
356 it.effect("skips conflicting default keybindings on startup and logs a detailed warning", () => {
357 const messages: string[] = [];
358 const logger = Logger.make(({ message }) => {
359 messages.push(String(message));
360 });
361
362 return Effect.gen(function* () {
363 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
364 yield* writeKeybindingsConfig(keybindingsConfigPath, [
365 { key: "mod+j", command: "script.custom-action.run" },
366 ]);
367
368 yield* Effect.gen(function* () {
369 const keybindings = yield* Keybindings.Keybindings;
370 yield* keybindings.syncDefaultKeybindingsOnStartup;
371 });
372
373 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
374 assert.isFalse(persisted.some((entry) => entry.command === "terminal.toggle"));
375 assert.isTrue(persisted.some((entry) => entry.command === "script.custom-action.run"));
376
377 assert.isTrue(
378 messages.some((message) =>
379 message.includes("skipping default keybinding due to shortcut conflict"),
380 ),
381 );
382 }).pipe(
383 Effect.provide(
384 Layer.mergeAll(layerKeybindings(), Logger.layer([logger], { mergeWithExisting: false })),
385 ),
386 );
387 });
388
389 it.effect("upserts custom keybindings to configured path", () =>
390 Effect.gen(function* () {
391 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
392 yield* writeKeybindingsConfig(keybindingsConfigPath, [
393 { key: "mod+j", command: "terminal.toggle" },
394 ]);
395
396 const resolved = yield* Effect.gen(function* () {
397 const keybindings = yield* Keybindings.Keybindings;
398 return yield* keybindings.upsertKeybindingRule({
399 key: "mod+shift+r",
400 command: "script.run-tests.run",
401 });
402 });
403
404 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
405 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
406
407 assert.deepEqual(persistedView, [
408 { key: "mod+j", command: "terminal.toggle" },
409 { key: "mod+shift+r", command: "script.run-tests.run" },
410 ]);
411 assert.isTrue(resolved.some((entry) => entry.command === "script.run-tests.run"));
412 }).pipe(Effect.provide(layerKeybindings())),
413 );
414
415 it.effect("appends additional custom keybindings for the same command", () =>
416 Effect.gen(function* () {
417 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
418 yield* writeKeybindingsConfig(keybindingsConfigPath, [
419 { key: "mod+r", command: "script.run-tests.run" },
420 ]);
421 yield* Effect.gen(function* () {
422 const keybindings = yield* Keybindings.Keybindings;
423 return yield* keybindings.upsertKeybindingRule({
424 key: "mod+shift+r",
425 command: "script.run-tests.run",
426 });
427 });
428
429 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
430 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
431 assert.deepEqual(persistedView, [
432 { key: "mod+r", command: "script.run-tests.run" },
433 { key: "mod+shift+r", command: "script.run-tests.run" },
434 ]);
435 }).pipe(Effect.provide(layerKeybindings())),
436 );
437
438 it.effect("replaces only the targeted custom keybinding", () =>
439 Effect.gen(function* () {
440 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
441 yield* writeKeybindingsConfig(keybindingsConfigPath, [
442 { key: "mod+r", command: "script.run-tests.run" },
443 { key: "mod+shift+r", command: "script.run-tests.run" },
444 ]);
445 yield* Effect.gen(function* () {
446 const keybindings = yield* Keybindings.Keybindings;
447 return yield* keybindings.upsertKeybindingRule({
448 key: "mod+alt+r",
449 command: "script.run-tests.run",
450 replace: { key: "mod+r", command: "script.run-tests.run" },
451 });
452 });
453
454 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
455 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
456 assert.deepEqual(persistedView, [
457 { key: "mod+shift+r", command: "script.run-tests.run" },
458 { key: "mod+alt+r", command: "script.run-tests.run" },
459 ]);
460 }).pipe(Effect.provide(layerKeybindings())),
461 );
462
463 it.effect("replacing with a rule that already exists elsewhere does not duplicate it", () =>
464 Effect.gen(function* () {
465 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
466 yield* writeKeybindingsConfig(keybindingsConfigPath, [
467 { key: "mod+r", command: "script.run-tests.run" },
468 { key: "mod+alt+r", command: "script.run-tests.run" },
469 ]);
470 yield* Effect.gen(function* () {
471 const keybindings = yield* Keybindings.Keybindings;
472 return yield* keybindings.upsertKeybindingRule({
473 key: "mod+alt+r",
474 command: "script.run-tests.run",
475 replace: { key: "mod+r", command: "script.run-tests.run" },
476 });
477 });
478
479 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
480 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
481 assert.deepEqual(persistedView, [{ key: "mod+alt+r", command: "script.run-tests.run" }]);
482 }).pipe(Effect.provide(layerKeybindings())),
483 );
484
485 it.effect("removes only the targeted custom keybinding", () =>
486 Effect.gen(function* () {
487 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
488 yield* writeKeybindingsConfig(keybindingsConfigPath, [
489 { key: "mod+r", command: "script.run-tests.run" },
490 { key: "mod+shift+r", command: "script.run-tests.run" },
491 ]);
492 yield* Effect.gen(function* () {
493 const keybindings = yield* Keybindings.Keybindings;
494 return yield* keybindings.removeKeybindingRule({
495 key: "mod+r",
496 command: "script.run-tests.run",
497 });
498 });
499
500 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
501 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
502 assert.deepEqual(persistedView, [{ key: "mod+shift+r", command: "script.run-tests.run" }]);
503 }).pipe(Effect.provide(layerKeybindings())),
504 );
505
506 it.effect("refuses to overwrite malformed keybindings config", () =>
507 Effect.gen(function* () {
508 const fs = yield* FileSystem.FileSystem;
509 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
510 yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");
511
512 const result = yield* Effect.gen(function* () {
513 const keybindings = yield* Keybindings.Keybindings;
514 return yield* keybindings.upsertKeybindingRule({
515 key: "mod+shift+r",
516 command: "script.run-tests.run",
517 });
518 }).pipe(toDetailResult);
519 assertFailure(result, "expected JSON array");
520
521 const persistedRaw = yield* fs.readFileString(keybindingsConfigPath);
522 assert.equal(persistedRaw, "{ not-json");
523 }).pipe(Effect.provide(layerKeybindings())),
524 );
525
526 it.effect("reports non-array config parse errors without duplicate prefix", () =>
527 Effect.gen(function* () {
528 const fs = yield* FileSystem.FileSystem;
529 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
530 yield* fs.writeFileString(
531 keybindingsConfigPath,
532 '{"key":"mod+j","command":"terminal.toggle"}',
533 );
534
535 const firstResult = yield* Effect.gen(function* () {
536 const keybindings = yield* Keybindings.Keybindings;
537 return yield* keybindings.upsertKeybindingRule({
538 key: "mod+shift+r",
539 command: "script.run-tests.run",
540 });
541 }).pipe(toDetailResult);
542 assertFailure(firstResult, "expected JSON array");
543
544 const secondResult = yield* Effect.gen(function* () {
545 const keybindings = yield* Keybindings.Keybindings;
546 return yield* keybindings.upsertKeybindingRule({
547 key: "mod+shift+r",
548 command: "script.run-tests.run",
549 });
550 }).pipe(toDetailResult);
551 assertFailure(secondResult, "expected JSON array");
552 }).pipe(Effect.provide(layerKeybindings())),
553 );
554
555 // chmod cannot make a directory unwritable on Windows, so the write succeeds.
556 it.effect.skipIf(HostProcessPlatform.defaultValue() === "win32")(
557 "fails when config directory is not writable",
558 () =>
559 Effect.gen(function* () {
560 const fs = yield* FileSystem.FileSystem;
561 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
562 const { dirname } = yield* Path.Path;
563 yield* writeKeybindingsConfig(keybindingsConfigPath, [
564 { key: "mod+j", command: "terminal.toggle" },
565 ]);
566 yield* fs.chmod(dirname(keybindingsConfigPath), 0o500);
567
568 const result = yield* Effect.gen(function* () {
569 const keybindings = yield* Keybindings.Keybindings;
570 return yield* keybindings.upsertKeybindingRule({
571 key: "mod+shift+r",
572 command: "script.run-tests.run",
573 });
574 }).pipe(toDetailResult);
575 assertFailure(result, "failed to write keybindings config");
576
577 yield* fs.chmod(dirname(keybindingsConfigPath), 0o700);
578
579 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
580 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
581 assert.deepEqual(persistedView, [{ key: "mod+j", command: "terminal.toggle" }]);
582 }).pipe(Effect.provide(layerKeybindings())),
583 );
584
585 it.effect("caches loaded resolved config across repeated reads", () =>
586 Effect.gen(function* () {
587 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
588 yield* writeKeybindingsConfig(keybindingsConfigPath, [
589 { key: "mod+j", command: "terminal.toggle" },
590 ]);
591
592 const [first, second] = yield* Effect.gen(function* () {
593 const keybindings = yield* Keybindings.Keybindings;
594 const firstLoad = (yield* keybindings.loadConfigState).keybindings;
595 const secondLoad = (yield* keybindings.loadConfigState).keybindings;
596 return [firstLoad, secondLoad] as const;
597 });
598
599 assert.deepEqual(first, second);
600 assert.isTrue(second.some((entry) => entry.command === "terminal.toggle"));
601 }).pipe(Effect.provide(layerKeybindings())),
602 );
603
604 it.effect("retries a failed config read instead of keeping the failure", () =>
605 Effect.gen(function* () {
606 const fileSystem = yield* FileSystem.FileSystem;
607 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
608 // A directory where the file should be makes the read itself fail.
609 yield* fileSystem.makeDirectory(keybindingsConfigPath, { recursive: true });
610
611 const keybindings = yield* Keybindings.Keybindings;
612 const failed = yield* toDetailResult(keybindings.loadConfigState);
613 assertFailure(failed, "failed to read keybindings config");
614
615 yield* fileSystem.remove(keybindingsConfigPath, { recursive: true });
616 yield* writeKeybindingsConfig(keybindingsConfigPath, [
617 { key: "mod+j", command: "terminal.toggle" },
618 ]);
619
620 const configState = yield* keybindings.loadConfigState;
621 assert.deepEqual(configState.issues, []);
622 assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
623 }).pipe(Effect.provide(layerKeybindings())),
624 );
625
626 it.effect("updates cached resolved config after upsert", () =>
627 Effect.gen(function* () {
628 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
629 yield* writeKeybindingsConfig(keybindingsConfigPath, [
630 { key: "mod+j", command: "terminal.toggle" },
631 ]);
632
633 const loadedAfterUpsert = yield* Effect.gen(function* () {
634 const keybindings = yield* Keybindings.Keybindings;
635 yield* keybindings.loadConfigState;
636 yield* keybindings.upsertKeybindingRule({
637 key: "mod+shift+r",
638 command: "script.run-tests.run",
639 });
640 return (yield* keybindings.loadConfigState).keybindings;
641 });
642
643 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "script.run-tests.run"));
644 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "terminal.toggle"));
645 }).pipe(Effect.provide(layerKeybindings())),
646 );
647
648 it.effect("serializes concurrent upserts to avoid lost updates", () =>
649 Effect.gen(function* () {
650 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
651 yield* writeKeybindingsConfig(keybindingsConfigPath, []);
652
653 const commands = Array.from(
654 { length: 20 },
655 (_, index): KeybindingCommand => `script.concurrent-${index}.run`,
656 );
657 yield* Effect.gen(function* () {
658 const keybindings = yield* Keybindings.Keybindings;
659 yield* Effect.forEach(
660 commands,
661 (command, index) =>
662 keybindings.upsertKeybindingRule({
663 key: `mod+${String.fromCharCode(97 + index)}`,
664 command,
665 }),
666 { concurrency: "unbounded", discard: true },
667 );
668 });
669
670 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
671 const persistedCommands = new Set(persisted.map((entry) => entry.command));
672 for (const command of commands) {
673 assert.isTrue(persistedCommands.has(command), `expected persisted command ${command}`);
674 }
675 }).pipe(Effect.provide(layerKeybindings())),
676 );
677});