Open History to see the last commit
1import { Schema } from "effect";
2import { assert, it } from "@effect/vitest";
3import { Effect } from "effect";
4
5import {
6 KeybindingsConfig,
7 KeybindingRule,
8 ResolvedKeybindingRule,
9 ResolvedKeybindingsConfig,
10} from "./keybindings";
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);
23
24it.effect("parses keybinding rules", () =>
25 Effect.gen(function* () {
26 const parsed = yield* decode(KeybindingRule, {
27 key: "mod+j",
28 command: "terminal.toggle",
29 });
30 assert.strictEqual(parsed.command, "terminal.toggle");
31
32 const parsedClose = yield* decode(KeybindingRule, {
33 key: "mod+w",
34 command: "terminal.close",
35 });
36 assert.strictEqual(parsedClose.command, "terminal.close");
37
38 const parsedDiffToggle = yield* decode(KeybindingRule, {
39 key: "mod+d",
40 command: "diff.toggle",
41 });
42 assert.strictEqual(parsedDiffToggle.command, "diff.toggle");
43
44 const parsedLocal = yield* decode(KeybindingRule, {
45 key: "mod+shift+n",
46 command: "chat.newLocal",
47 });
48 assert.strictEqual(parsedLocal.command, "chat.newLocal");
49 }),
50);
51
52it.effect("rejects invalid command values", () =>
53 Effect.gen(function* () {
54 const result = yield* Effect.exit(
55 decode(KeybindingRule, {
56 key: "mod+j",
57 command: "script.Test.run",
58 }),
59 );
60 assert.strictEqual(result._tag, "Failure");
61 }),
62);
63
64it.effect("accepts dynamic script run commands", () =>
65 Effect.gen(function* () {
66 const parsed = yield* decode(KeybindingRule, {
67 key: "mod+r",
68 command: "script.setup.run",
69 });
70 assert.strictEqual(parsed.command, "script.setup.run");
71 }),
72);
73
74it.effect("parses keybindings array payload", () =>
75 Effect.gen(function* () {
76 const parsed = yield* decode(KeybindingsConfig, [
77 { key: "mod+j", command: "terminal.toggle" },
78 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
79 ]);
80 assert.lengthOf(parsed, 2);
81 }),
82);
83
84it.effect("parses resolved keybinding rules", () =>
85 Effect.gen(function* () {
86 const parsed = yield* decode(ResolvedKeybindingRule, {
87 command: "terminal.split",
88 shortcut: {
89 key: "d",
90 metaKey: false,
91 ctrlKey: false,
92 shiftKey: false,
93 altKey: false,
94 modKey: true,
95 },
96 whenAst: {
97 type: "and",
98 left: { type: "identifier", name: "terminalOpen" },
99 right: {
100 type: "not",
101 node: { type: "identifier", name: "terminalFocus" },
102 },
103 },
104 });
105 assert.strictEqual(parsed.shortcut.key, "d");
106 }),
107);
108
109it.effect("parses resolved keybindings arrays", () =>
110 Effect.gen(function* () {
111 const parsed = yield* decode(ResolvedKeybindingsConfig, [
112 {
113 command: "terminal.toggle",
114 shortcut: {
115 key: "j",
116 metaKey: false,
117 ctrlKey: false,
118 shiftKey: false,
119 altKey: false,
120 modKey: true,
121 },
122 },
123 ]);
124 assert.lengthOf(parsed, 1);
125 }),
126);
127
128it.effect("drops unknown fields in resolved keybinding rules", () =>
129 decodeResolvedRule({
130 command: "terminal.toggle",
131 shortcut: {
132 key: "j",
133 metaKey: false,
134 ctrlKey: false,
135 shiftKey: false,
136 altKey: false,
137 modKey: true,
138 },
139 key: "mod+j",
140 }).pipe(
141 Effect.map((parsed) => {
142 const view = parsed as Record<string, unknown>;
143 assert.strictEqual("key" in view, false);
144 assert.strictEqual(view.command, "terminal.toggle");
145 }),
146 ),
147);