1import { assert, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3import * as Effect from "effect/Effect";
4
5import { ResolvedKeybindingsConfig } from "./keybindings.ts";
6
7const decode = <S extends Schema.Top>(
8 schema: S,
9 input: unknown,
10): Effect.Effect<Schema.Schema.Type<S>, Schema.SchemaError, never> =>
11 Schema.decodeUnknownEffect(schema as never)(input) as Effect.Effect<
12 Schema.Schema.Type<S>,
13 Schema.SchemaError,
14 never
15 >;
16
17const shortcut = {
18 key: "p",
19 metaKey: false,
20 ctrlKey: false,
21 shiftKey: false,
22 altKey: false,
23 modKey: true,
24};
25
26it.effect("drops resolved rules with commands this build does not know", () =>
27 Effect.gen(function* () {
28 const parsed = yield* decode(ResolvedKeybindingsConfig, [
29 { command: "terminal.toggle", shortcut },
30 { command: "someFuture.toggle", shortcut },
31 { command: "filePicker.toggle", shortcut },
32 ]);
33 assert.deepEqual(
34 parsed.map((rule) => rule.command),
35 ["terminal.toggle", "filePicker.toggle"],
36 );
37 }),
38);
39
40it.effect("round-trips resolved Space shortcuts", () =>
41 Effect.gen(function* () {
42 const codec = Schema.fromJsonString(ResolvedKeybindingsConfig);
43 const rules = [
44 {
45 command: "composer.dictation" as const,
46 shortcut: { ...shortcut, key: " ", metaKey: true, modKey: false },
47 },
48 ];
49 const encoded = yield* Schema.encodeEffect(codec)(rules);
50 const parsed = yield* decode(codec, encoded);
51 assert.deepEqual(parsed, rules);
52 }),
53);
54
55it.effect("drops malformed resolved rule entries", () =>
56 Effect.gen(function* () {
57 const parsed = yield* decode(ResolvedKeybindingsConfig, [
58 "garbage",
59 { command: "terminal.toggle", shortcut },
60 null,
61 ]);
62 assert.deepEqual(
63 parsed.map((rule) => rule.command),
64 ["terminal.toggle"],
65 );
66 }),
67);