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 parsedCommandPalette = yield* decode(KeybindingRule, {
45 key: "mod+k",
46 command: "commandPalette.toggle",
47 });
48 assert.strictEqual(parsedCommandPalette.command, "commandPalette.toggle");
49
50 const parsedLocal = yield* decode(KeybindingRule, {
51 key: "mod+shift+n",
52 command: "chat.newLocal",
53 });
54 assert.strictEqual(parsedLocal.command, "chat.newLocal");
55
56 const parsedThreadPrevious = yield* decode(KeybindingRule, {
57 key: "mod+shift+[",
58 command: "thread.previous",
59 });
60 assert.strictEqual(parsedThreadPrevious.command, "thread.previous");
61 }),
62);
63
64it.effect("rejects invalid command values", () =>
65 Effect.gen(function* () {
66 const result = yield* Effect.exit(
67 decode(KeybindingRule, {
68 key: "mod+j",
69 command: "script.Test.run",
70 }),
71 );
72 assert.strictEqual(result._tag, "Failure");
73 }),
74);
75
76it.effect("accepts dynamic script run commands", () =>
77 Effect.gen(function* () {
78 const parsed = yield* decode(KeybindingRule, {
79 key: "mod+r",
80 command: "script.setup.run",
81 });
82 assert.strictEqual(parsed.command, "script.setup.run");
83 }),
84);
85
86it.effect("parses keybindings array payload", () =>
87 Effect.gen(function* () {
88 const parsed = yield* decode(KeybindingsConfig, [
89 { key: "mod+j", command: "terminal.toggle" },
90 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
91 ]);
92 assert.lengthOf(parsed, 2);
93 }),
94);
95
96it.effect("parses resolved keybinding rules", () =>
97 Effect.gen(function* () {
98 const parsed = yield* decode(ResolvedKeybindingRule, {
99 command: "terminal.split",
100 shortcut: {
101 key: "d",
102 metaKey: false,
103 ctrlKey: false,
104 shiftKey: false,
105 altKey: false,
106 modKey: true,
107 },
108 whenAst: {
109 type: "and",
110 left: { type: "identifier", name: "terminalOpen" },
111 right: {
112 type: "not",
113 node: { type: "identifier", name: "terminalFocus" },
114 },
115 },
116 });
117 assert.strictEqual(parsed.shortcut.key, "d");
118 }),
119);
120
121it.effect("parses resolved keybindings arrays", () =>
122 Effect.gen(function* () {
123 const parsed = yield* decode(ResolvedKeybindingsConfig, [
124 {
125 command: "terminal.toggle",
126 shortcut: {
127 key: "j",
128 metaKey: false,
129 ctrlKey: false,
130 shiftKey: false,
131 altKey: false,
132 modKey: true,
133 },
134 },
135 {
136 command: "thread.jump.3",
137 shortcut: {
138 key: "3",
139 metaKey: false,
140 ctrlKey: false,
141 shiftKey: false,
142 altKey: false,
143 modKey: true,
144 },
145 },
146 ]);
147 assert.lengthOf(parsed, 2);
148 }),
149);
150
151it.effect("drops unknown fields in resolved keybinding rules", () =>
152 decodeResolvedRule({
153 command: "terminal.toggle",
154 shortcut: {
155 key: "j",
156 metaKey: false,
157 ctrlKey: false,
158 shiftKey: false,
159 altKey: false,
160 modKey: true,
161 },
162 key: "mod+j",
163 }).pipe(
164 Effect.map((parsed) => {
165 const view = parsed as Record<string, unknown>;
166 assert.strictEqual("key" in view, false);
167 assert.strictEqual(view.command, "terminal.toggle");
168 }),
169 ),
170);