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

keybindings.test.ts

659 lines25.3 KB

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