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