The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 229 lines 8.86 KB
Raw
1 import { RegistryProvider } from "@effect/atom-react";
2 import { describe, expect, it, vi } from "vite-plus/test";
3 import { page, userEvent, type Locator } from "vite-plus/test/browser";
4
5 import { render } from "../../../test/render";
6 import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
7 import * as atomCommands from "../../state/use-atom-command";
8
9 vi.mock(import("../../state/session"), async (original) => ({
10 ...(await original()),
11 useEnvironmentScope: () => true,
12 useEnvironmentsWithScope: ((environments: ReadonlyArray<{ environmentId: string }>) =>
13 new Set(environments.map(({ environmentId }) => environmentId))) as never,
14 readEnvironmentScope: () => true,
15 }));
16 vi.mock(import("../../state/use-atom-command"), async (original) => ({
17 ...(await original()),
18 useAtomCommand: vi.fn(() => vi.fn()) as never,
19 }));
20 vi.mock(import("./SettingsScopeContext"), async (original) => ({
21 ...(await original()),
22 useSettingsScope: (() => {
23 const environment = {
24 environmentId: "env",
25 label: "Build workstation",
26 serverConfig: {
27 keybindings: compileResolvedKeybindingsConfig([
28 { command: "composer.sendBackground", key: "mod+alt+enter", when: "false" },
29 ]),
30 keybindingsConfigPath: null,
31 availableEditors: [],
32 },
33 };
34 return {
35 scope: { kind: "environment" },
36 environment,
37 environments: [environment],
38 connectedEnvironments: [environment],
39 targets: [],
40 target: null,
41 };
42 }) as never,
43 }));
44
45 import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
46 import { KeybindingsSettingsPanel } from "./KeybindingsSettings";
47
48 // The modal dialog's inert layer swallows pointer events in this harness, so drive the DOM.
49 async function activate(locator: Locator, action: "click" | "focus") {
50 await expect.element(locator).toBeVisible();
51 (locator.element() as HTMLElement)[action]();
52 }
53
54 const existingField = page.getByRole("textbox", { name: "Keybinding for Sidebar: Toggle" });
55 const existingPill = page.getByRole("button", { name: /^Edit shortcut for Sidebar: Toggle:/ });
56 const newField = page.getByRole("textbox", { name: "Keybinding for new keybinding" });
57
58 describe("Esc in a shortcut recorder", () => {
59 it.each<{
60 name: string;
61 field: Locator;
62 open: () => Promise<void>;
63 expectReset: () => Promise<void>;
64 }>([
65 {
66 name: "existing binding",
67 field: existingField,
68 open: () => activate(existingPill, "click"),
69 expectReset: async () => {
70 await expect.element(existingField).not.toBeInTheDocument();
71 await expect.element(existingPill).toHaveFocus();
72 await expect.element(page.getByRole("button", { name: "Save" })).not.toBeInTheDocument();
73 },
74 },
75 {
76 name: "new binding",
77 field: newField,
78 open: async () => {
79 await activate(page.getByRole("button", { name: "Add keybinding" }), "click");
80 await activate(newField, "focus");
81 },
82 expectReset: async () => {
83 await expect.element(newField).toHaveValue("");
84 await expect.element(newField).toHaveAttribute("placeholder", "Unassigned");
85 },
86 },
87 ])("leaves the $name field without closing Settings", async ({ field, open, expectReset }) => {
88 const onOpenChange = vi.fn();
89 await using _screen = await render(
90 <Dialog open onOpenChange={onOpenChange}>
91 <DialogPopup>
92 <DialogTitle>Settings</DialogTitle>
93 <KeybindingsSettingsPanel />
94 </DialogPopup>
95 </Dialog>,
96 );
97
98 await open();
99 await userEvent.keyboard("{Control>}k{/Control}");
100 await expect.element(field).not.toHaveValue("");
101 await userEvent.tab();
102 await activate(field, "focus");
103 await expect.element(field).toHaveAttribute("placeholder", "Press shortcut");
104
105 await userEvent.keyboard("{Escape}");
106
107 expect(onOpenChange).not.toHaveBeenCalled();
108 expect(document.activeElement?.matches("[data-keybinding-capture]")).toBe(false);
109 await expectReset();
110
111 await userEvent.keyboard("{Escape}");
112 expect(onOpenChange).toHaveBeenCalledWith(false, expect.anything());
113 });
114 });
115
116 it("unassigns a default shortcut and restores its condition when one is assigned again", async () => {
117 const mutate = vi.fn(async () => ({ _tag: "Success", value: [] }));
118 await vi.mocked(atomCommands.useAtomCommand).withImplementation(
119 () => mutate as never,
120 async () => {
121 await using screen = await render(<KeybindingsSettingsPanel />);
122
123 await screen.getByRole("button", { name: "Actions for Sidebar: Toggle" }).click();
124 await screen.getByRole("menuitem", { name: "Unassign" }).click();
125 expect(mutate).toHaveBeenLastCalledWith({
126 environmentId: "env",
127 input: {
128 command: "sidebar.toggle",
129 key: "mod+b",
130 when: "false",
131 replace: { command: "sidebar.toggle", key: "mod+b" },
132 },
133 });
134
135 const unassigned = screen.getByRole("button", {
136 name: "Edit shortcut for Composer: Start in Background: Unassigned",
137 });
138 const save = screen.getByRole("button", { name: "Save" });
139 await unassigned.click();
140 await userEvent.keyboard("{Control>}{Alt>}j{/Alt}{/Control}");
141 await expect.element(save).toBeVisible();
142 await userEvent.keyboard("{Escape}");
143 await expect.element(save).not.toBeInTheDocument();
144
145 await unassigned.click();
146 await userEvent.keyboard("{Control>}{Alt>}j{/Alt}{/Control}");
147 await save.click();
148 expect(mutate).toHaveBeenLastCalledWith({
149 environmentId: "env",
150 input: expect.objectContaining({
151 command: "composer.sendBackground",
152 when: "composerFocus && draftThreadRoute",
153 replace: { command: "composer.sendBackground", key: "mod+alt+enter", when: "false" },
154 }),
155 });
156 },
157 );
158 });
159
160 it("Esc in the keybindings search clears the query without closing Settings", async () => {
161 const onOpenChange = vi.fn();
162 await using _screen = await render(
163 <Dialog open onOpenChange={onOpenChange}>
164 <DialogPopup>
165 <DialogTitle>Settings</DialogTitle>
166 <KeybindingsSettingsPanel />
167 </DialogPopup>
168 </Dialog>,
169 );
170 const search = page.getByRole("searchbox", { name: "Search keybindings" });
171 await search.fill("sidebar");
172
173 await userEvent.keyboard("{Escape}");
174
175 await expect.element(search).toHaveValue("");
176 expect(onOpenChange).not.toHaveBeenCalled();
177 });
178
179 it("re-enables Save when a When editor closes on an expression it could not parse", async () => {
180 await using screen = await render(
181 <RegistryProvider>
182 <KeybindingsSettingsPanel />
183 </RegistryProvider>,
184 );
185 const whenTrigger = screen.getByRole("button", { name: "Edit when clause for Terminal: Toggle" });
186 const expression = screen.getByRole("textbox", { name: "When expression" });
187
188 await whenTrigger.click();
189 await userEvent.type(expression, "terminalFocus");
190 await userEvent.type(expression, " &&");
191 await expect.element(screen.getByRole("button", { name: "Save" })).toBeDisabled();
192
193 await userEvent.keyboard("{Escape}");
194 await whenTrigger.click();
195
196 await expect.element(expression).toHaveValue("terminalFocus");
197 await expect.element(screen.getByRole("button", { name: "Save" })).toBeEnabled();
198 });
199
200 it("filters actions and allows additional shortcuts for assigned commands", async () => {
201 await using screen = await render(<KeybindingsSettingsPanel />);
202 await screen.getByRole("button", { name: "Add keybinding" }).click();
203 const dialog = page.getByRole("dialog", { name: "Command", exact: true });
204 const search = dialog.getByRole("combobox", { name: "Search", exact: true });
205 await expect.element(search).toHaveFocus();
206 await userEvent.keyboard("sidebar.toggle");
207 const assigned = page.getByRole("option", { name: /Sidebar: Toggle/ });
208 await expect.element(assigned).not.toHaveAttribute("aria-disabled", "true");
209 await expect.element(assigned.element().querySelector("kbd")!).toBeVisible();
210 await vi.waitFor(() => expect(page.getByRole("option").elements()).toHaveLength(1));
211 await assigned.click();
212 const command = screen.getByRole("combobox", { name: "Command", exact: true });
213 await expect.element(command).toHaveTextContent("Sidebar: Toggle");
214 await command.click();
215 await search.fill("Opposite Queue");
216 await expect
217 .element(page.getByRole("option", { name: /Composer: Opposite Queue or Steer Action/ }))
218 .toBeVisible();
219 await vi.waitFor(() => expect(page.getByRole("option").elements()).toHaveLength(1));
220 await search.fill("no-such-action");
221 await expect.element(page.getByText("No results found.")).toBeVisible();
222 await search.fill("composer.cycleHost");
223 const available = page.getByRole("option", { name: "Composer: Cycle Host" });
224 await expect.element(available).not.toHaveAttribute("aria-disabled", "true");
225 await available.click();
226 await expect
227 .element(screen.getByRole("combobox", { name: "Command", exact: true }))
228 .toHaveTextContent("Composer: Cycle Host");
229 });