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("rejects invalid command values", () =>
26 Effect.gen(function* () {
27 const result = yield* Effect.exit(
28 decode(KeybindingRule, {
29 key: "mod+j",
30 command: "script.Test.run",
31 }),
32 );
33 assert.strictEqual(result._tag, "Failure");
34 }),
35);
36
37it.effect("accepts dynamic script run commands", () =>
38 Effect.gen(function* () {
39 const parsed = yield* decode(KeybindingRule, {
40 key: "mod+r",
41 command: "script.setup.run",
42 });
43 assert.strictEqual(parsed.command, "script.setup.run");
44 }),
45);
46
47it.effect("parses keybindings array payload", () =>
48 Effect.gen(function* () {
49 const parsed = yield* decode(KeybindingsConfig, [
50 { key: "mod+j", command: "terminal.toggle" },
51 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
52 { key: "mod+shift+d", command: "terminal.splitVertical", when: "terminalFocus" },
53 ]);
54 assert.lengthOf(parsed, 3);
55 }),
56);
57
58it.effect("parses resolved keybinding rules", () =>
59 Effect.gen(function* () {
60 const parsed = yield* decode(ResolvedKeybindingRule, {
61 command: "terminal.split",
62 shortcut: {
63 key: "d",
64 metaKey: false,
65 ctrlKey: false,
66 shiftKey: false,
67 altKey: false,
68 modKey: true,
69 },
70 whenAst: {
71 type: "and",
72 left: { type: "identifier", name: "terminalOpen" },
73 right: {
74 type: "not",
75 node: { type: "identifier", name: "terminalFocus" },
76 },
77 },
78 });
79 assert.strictEqual(parsed.shortcut.key, "d");
80 }),
81);
82
83it.effect("parses resolved keybindings arrays", () =>
84 Effect.gen(function* () {
85 const parsed = yield* decode(ResolvedKeybindingsConfig, [
86 {
87 command: "terminal.toggle",
88 shortcut: {
89 key: "j",
90 metaKey: false,
91 ctrlKey: false,
92 shiftKey: false,
93 altKey: false,
94 modKey: true,
95 },
96 },
97 {
98 command: "thread.jump.3",
99 shortcut: {
100 key: "3",
101 metaKey: false,
102 ctrlKey: false,
103 shiftKey: false,
104 altKey: false,
105 modKey: true,
106 },
107 },
108 ]);
109 assert.lengthOf(parsed, 2);
110 }),
111);
112
113const shortcut = {
114 key: "p",
115 metaKey: false,
116 ctrlKey: false,
117 shiftKey: false,
118 altKey: false,
119 modKey: true,
120};
121
122it.effect("drops resolved rules with commands this build does not know", () =>
123 Effect.gen(function* () {
124 const parsed = yield* decode(ResolvedKeybindingsConfig, [
125 { command: "terminal.toggle", shortcut },
126 { command: "someFuture.toggle", shortcut },
127 { command: "filePicker.toggle", shortcut },
128 ]);
129 assert.deepEqual(
130 parsed.map((rule) => rule.command),
131 ["terminal.toggle", "filePicker.toggle"],
132 );
133 }),
134);
135
136it.effect("drops resolved rules with unknown when-node types", () =>
137 Effect.gen(function* () {
138 const parsed = yield* decode(ResolvedKeybindingsConfig, [
139 {
140 command: "terminal.toggle",
141 shortcut,
142 whenAst: { type: "xor", left: 1, right: 2 },
143 },
144 { command: "terminal.split", shortcut },
145 ]);
146 assert.deepEqual(
147 parsed.map((rule) => rule.command),
148 ["terminal.split"],
149 );
150 }),
151);
152
153it.effect("drops malformed resolved rule entries", () =>
154 Effect.gen(function* () {
155 const parsed = yield* decode(ResolvedKeybindingsConfig, [
156 "garbage",
157 { command: "terminal.toggle", shortcut },
158 null,
159 ]);
160 assert.deepEqual(
161 parsed.map((rule) => rule.command),
162 ["terminal.toggle"],
163 );
164 }),
165);
166
167it.effect("encodes resolved keybindings to the plain wire shape", () =>
168 Effect.gen(function* () {
169 const rules = [{ command: "terminal.toggle" as const, shortcut }];
170 const encoded = yield* encodeResolvedKeybindings(rules);
171 assert.deepEqual(encoded, rules);
172 const roundTripped = yield* decode(ResolvedKeybindingsConfig, encoded);
173 assert.deepEqual(roundTripped, rules);
174 }),
175);
176
177it.effect("drops unknown fields in resolved keybinding rules", () =>
178 decodeResolvedRule({
179 command: "terminal.toggle",
180 shortcut: {
181 key: "j",
182 metaKey: false,
183 ctrlKey: false,
184 shiftKey: false,
185 altKey: false,
186 modKey: true,
187 },
188 key: "mod+j",
189 }).pipe(
190 Effect.map((parsed) => {
191 const view = parsed as Record<string, unknown>;
192 assert.strictEqual("key" in view, false);
193 assert.strictEqual(view.command, "terminal.toggle");
194 }),
195 ),
196);