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 makeKeybindingsLayer = () => {
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(makeKeybindingsLayer())),
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(makeKeybindingsLayer())),
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(makeKeybindingsLayer())),
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(makeKeybindingsLayer())),
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(makeKeybindingsLayer())),
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(makeKeybindingsLayer())),
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(makeKeybindingsLayer())),
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(
385 makeKeybindingsLayer(),
386 Logger.layer([logger], { mergeWithExisting: false }),
387 ),
388 ),
389 );
390 });
391
392 it.effect("upserts custom keybindings to configured path", () =>
393 Effect.gen(function* () {
394 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
395 yield* writeKeybindingsConfig(keybindingsConfigPath, [
396 { key: "mod+j", command: "terminal.toggle" },
397 ]);
398
399 const resolved = yield* Effect.gen(function* () {
400 const keybindings = yield* Keybindings.Keybindings;
401 return yield* keybindings.upsertKeybindingRule({
402 key: "mod+shift+r",
403 command: "script.run-tests.run",
404 });
405 });
406
407 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
408 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
409
410 assert.deepEqual(persistedView, [
411 { key: "mod+j", command: "terminal.toggle" },
412 { key: "mod+shift+r", command: "script.run-tests.run" },
413 ]);
414 assert.isTrue(resolved.some((entry) => entry.command === "script.run-tests.run"));
415 }).pipe(Effect.provide(makeKeybindingsLayer())),
416 );
417
418 it.effect("appends additional custom keybindings for the same command", () =>
419 Effect.gen(function* () {
420 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
421 yield* writeKeybindingsConfig(keybindingsConfigPath, [
422 { key: "mod+r", command: "script.run-tests.run" },
423 ]);
424 yield* Effect.gen(function* () {
425 const keybindings = yield* Keybindings.Keybindings;
426 return yield* keybindings.upsertKeybindingRule({
427 key: "mod+shift+r",
428 command: "script.run-tests.run",
429 });
430 });
431
432 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
433 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
434 assert.deepEqual(persistedView, [
435 { key: "mod+r", command: "script.run-tests.run" },
436 { key: "mod+shift+r", command: "script.run-tests.run" },
437 ]);
438 }).pipe(Effect.provide(makeKeybindingsLayer())),
439 );
440
441 it.effect("replaces only the targeted custom keybinding", () =>
442 Effect.gen(function* () {
443 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
444 yield* writeKeybindingsConfig(keybindingsConfigPath, [
445 { key: "mod+r", command: "script.run-tests.run" },
446 { key: "mod+shift+r", command: "script.run-tests.run" },
447 ]);
448 yield* Effect.gen(function* () {
449 const keybindings = yield* Keybindings.Keybindings;
450 return yield* keybindings.upsertKeybindingRule({
451 key: "mod+alt+r",
452 command: "script.run-tests.run",
453 replace: { key: "mod+r", command: "script.run-tests.run" },
454 });
455 });
456
457 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
458 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
459 assert.deepEqual(persistedView, [
460 { key: "mod+shift+r", command: "script.run-tests.run" },
461 { key: "mod+alt+r", command: "script.run-tests.run" },
462 ]);
463 }).pipe(Effect.provide(makeKeybindingsLayer())),
464 );
465
466 it.effect("replacing with a rule that already exists elsewhere does not duplicate it", () =>
467 Effect.gen(function* () {
468 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
469 yield* writeKeybindingsConfig(keybindingsConfigPath, [
470 { key: "mod+r", command: "script.run-tests.run" },
471 { key: "mod+alt+r", command: "script.run-tests.run" },
472 ]);
473 yield* Effect.gen(function* () {
474 const keybindings = yield* Keybindings.Keybindings;
475 return yield* keybindings.upsertKeybindingRule({
476 key: "mod+alt+r",
477 command: "script.run-tests.run",
478 replace: { key: "mod+r", command: "script.run-tests.run" },
479 });
480 });
481
482 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
483 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
484 assert.deepEqual(persistedView, [{ key: "mod+alt+r", command: "script.run-tests.run" }]);
485 }).pipe(Effect.provide(makeKeybindingsLayer())),
486 );
487
488 it.effect("removes only the targeted custom keybinding", () =>
489 Effect.gen(function* () {
490 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
491 yield* writeKeybindingsConfig(keybindingsConfigPath, [
492 { key: "mod+r", command: "script.run-tests.run" },
493 { key: "mod+shift+r", command: "script.run-tests.run" },
494 ]);
495 yield* Effect.gen(function* () {
496 const keybindings = yield* Keybindings.Keybindings;
497 return yield* keybindings.removeKeybindingRule({
498 key: "mod+r",
499 command: "script.run-tests.run",
500 });
501 });
502
503 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
504 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
505 assert.deepEqual(persistedView, [{ key: "mod+shift+r", command: "script.run-tests.run" }]);
506 }).pipe(Effect.provide(makeKeybindingsLayer())),
507 );
508
509 it.effect("refuses to overwrite malformed keybindings config", () =>
510 Effect.gen(function* () {
511 const fs = yield* FileSystem.FileSystem;
512 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
513 yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");
514
515 const result = yield* Effect.gen(function* () {
516 const keybindings = yield* Keybindings.Keybindings;
517 return yield* keybindings.upsertKeybindingRule({
518 key: "mod+shift+r",
519 command: "script.run-tests.run",
520 });
521 }).pipe(toDetailResult);
522 assertFailure(result, "expected JSON array");
523
524 const persistedRaw = yield* fs.readFileString(keybindingsConfigPath);
525 assert.equal(persistedRaw, "{ not-json");
526 }).pipe(Effect.provide(makeKeybindingsLayer())),
527 );
528
529 it.effect("reports non-array config parse errors without duplicate prefix", () =>
530 Effect.gen(function* () {
531 const fs = yield* FileSystem.FileSystem;
532 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
533 yield* fs.writeFileString(
534 keybindingsConfigPath,
535 '{"key":"mod+j","command":"terminal.toggle"}',
536 );
537
538 const firstResult = yield* Effect.gen(function* () {
539 const keybindings = yield* Keybindings.Keybindings;
540 return yield* keybindings.upsertKeybindingRule({
541 key: "mod+shift+r",
542 command: "script.run-tests.run",
543 });
544 }).pipe(toDetailResult);
545 assertFailure(firstResult, "expected JSON array");
546
547 const secondResult = yield* Effect.gen(function* () {
548 const keybindings = yield* Keybindings.Keybindings;
549 return yield* keybindings.upsertKeybindingRule({
550 key: "mod+shift+r",
551 command: "script.run-tests.run",
552 });
553 }).pipe(toDetailResult);
554 assertFailure(secondResult, "expected JSON array");
555 }).pipe(Effect.provide(makeKeybindingsLayer())),
556 );
557
558 // chmod cannot make a directory unwritable on Windows, so the write succeeds.
559 it.effect.skipIf(HostProcessPlatform.defaultValue() === "win32")(
560 "fails when config directory is not writable",
561 () =>
562 Effect.gen(function* () {
563 const fs = yield* FileSystem.FileSystem;
564 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
565 const { dirname } = yield* Path.Path;
566 yield* writeKeybindingsConfig(keybindingsConfigPath, [
567 { key: "mod+j", command: "terminal.toggle" },
568 ]);
569 yield* fs.chmod(dirname(keybindingsConfigPath), 0o500);
570
571 const result = yield* Effect.gen(function* () {
572 const keybindings = yield* Keybindings.Keybindings;
573 return yield* keybindings.upsertKeybindingRule({
574 key: "mod+shift+r",
575 command: "script.run-tests.run",
576 });
577 }).pipe(toDetailResult);
578 assertFailure(result, "failed to write keybindings config");
579
580 yield* fs.chmod(dirname(keybindingsConfigPath), 0o700);
581
582 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
583 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
584 assert.deepEqual(persistedView, [{ key: "mod+j", command: "terminal.toggle" }]);
585 }).pipe(Effect.provide(makeKeybindingsLayer())),
586 );
587
588 it.effect("caches loaded resolved config across repeated reads", () =>
589 Effect.gen(function* () {
590 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
591 yield* writeKeybindingsConfig(keybindingsConfigPath, [
592 { key: "mod+j", command: "terminal.toggle" },
593 ]);
594
595 const [first, second] = yield* Effect.gen(function* () {
596 const keybindings = yield* Keybindings.Keybindings;
597 const firstLoad = (yield* keybindings.loadConfigState).keybindings;
598 const secondLoad = (yield* keybindings.loadConfigState).keybindings;
599 return [firstLoad, secondLoad] as const;
600 });
601
602 assert.deepEqual(first, second);
603 assert.isTrue(second.some((entry) => entry.command === "terminal.toggle"));
604 }).pipe(Effect.provide(makeKeybindingsLayer())),
605 );
606
607 it.effect("updates cached resolved config after upsert", () =>
608 Effect.gen(function* () {
609 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
610 yield* writeKeybindingsConfig(keybindingsConfigPath, [
611 { key: "mod+j", command: "terminal.toggle" },
612 ]);
613
614 const loadedAfterUpsert = yield* Effect.gen(function* () {
615 const keybindings = yield* Keybindings.Keybindings;
616 yield* keybindings.loadConfigState;
617 yield* keybindings.upsertKeybindingRule({
618 key: "mod+shift+r",
619 command: "script.run-tests.run",
620 });
621 return (yield* keybindings.loadConfigState).keybindings;
622 });
623
624 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "script.run-tests.run"));
625 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "terminal.toggle"));
626 }).pipe(Effect.provide(makeKeybindingsLayer())),
627 );
628
629 it.effect("serializes concurrent upserts to avoid lost updates", () =>
630 Effect.gen(function* () {
631 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
632 yield* writeKeybindingsConfig(keybindingsConfigPath, []);
633
634 const commands = Array.from(
635 { length: 20 },
636 (_, index): KeybindingCommand => `script.concurrent-${index}.run`,
637 );
638 yield* Effect.gen(function* () {
639 const keybindings = yield* Keybindings.Keybindings;
640 yield* Effect.forEach(
641 commands,
642 (command, index) =>
643 keybindings.upsertKeybindingRule({
644 key: `mod+${String.fromCharCode(97 + index)}`,
645 command,
646 }),
647 { concurrency: "unbounded", discard: true },
648 );
649 });
650
651 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
652 const persistedCommands = new Set(persisted.map((entry) => entry.command));
653 for (const command of commands) {
654 assert.isTrue(persistedCommands.has(command), `expected persisted command ${command}`);
655 }
656 }).pipe(Effect.provide(makeKeybindingsLayer())),
657 );
658});