Open History to see the last commit
1import { RegistryProvider } from "@effect/atom-react";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { page, userEvent, type Locator } from "vite-plus/test/browser";
4
5import { act, render } from "../../../test/render";
6import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
7import * as atomCommands from "../../state/use-atom-command";
8
9vi.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}));
16vi.mock(import("../../state/use-atom-command"), async (original) => ({
17 ...(await original()),
18 useAtomCommand: vi.fn(() => vi.fn()) as never,
19}));
20vi.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
45import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
46import { KeybindingsSettingsPanel } from "./KeybindingsSettings";
47
48// The modal dialog's inert layer swallows pointer events in this harness, so drive the DOM.
49async function activate(locator: Locator, action: "click" | "focus") {
50 await expect.element(locator).toBeVisible();
51 (locator.element() as HTMLElement)[action]();
52}
53
54const existingField = page.getByRole("textbox", { name: "Keybinding for Sidebar: Toggle" });
55const existingPill = page.getByRole("button", { name: /^Edit shortcut for Sidebar: Toggle:/ });
56const newField = page.getByRole("textbox", { name: "Keybinding for new keybinding" });
57
58describe("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 it.each([
116 { name: "existing binding", field: existingField, open: () => activate(existingPill, "click") },
117 {
118 name: "new binding",
119 field: newField,
120 open: async () => {
121 await activate(page.getByRole("button", { name: "Add keybinding" }), "click");
122 await activate(newField, "focus");
123 },
124 },
125 ])("records Shift+Esc in the $name field", async ({ field, open }) => {
126 const onOpenChange = vi.fn();
127 await using _screen = await render(
128 <Dialog open onOpenChange={onOpenChange}>
129 <DialogPopup>
130 <DialogTitle>Settings</DialogTitle>
131 <KeybindingsSettingsPanel />
132 </DialogPopup>
133 </Dialog>,
134 );
135
136 await open();
137 await userEvent.keyboard("{Shift>}{Escape}{/Shift}");
138
139 await expect.element(field).toHaveValue("shift+esc");
140 expect(onOpenChange).not.toHaveBeenCalled();
141 });
142});
143
144it("unassigns a default shortcut and restores its condition when one is assigned again", async () => {
145 const mutate = vi.fn(async () => ({ _tag: "Success", value: [] }));
146 await vi.mocked(atomCommands.useAtomCommand).withImplementation(
147 () => mutate as never,
148 async () => {
149 await using screen = await render(<KeybindingsSettingsPanel />);
150
151 await screen.getByRole("button", { name: "Actions for Sidebar: Toggle" }).click();
152 await screen.getByRole("menuitem", { name: "Unassign" }).click();
153 expect(mutate).toHaveBeenLastCalledWith({
154 environmentId: "env",
155 input: {
156 command: "sidebar.toggle",
157 key: "mod+b",
158 when: "false",
159 replace: { command: "sidebar.toggle", key: "mod+b" },
160 },
161 });
162
163 const unassigned = screen.getByRole("button", {
164 name: "Edit shortcut for Composer: Start in Background: Unassigned",
165 });
166 const save = screen.getByRole("button", { name: "Save" });
167 await unassigned.click();
168 await userEvent.keyboard("{Control>}{Alt>}j{/Alt}{/Control}");
169 await expect.element(save).toBeVisible();
170 await userEvent.keyboard("{Escape}");
171 await expect.element(save).not.toBeInTheDocument();
172
173 await unassigned.click();
174 await userEvent.keyboard("{Control>}{Alt>}j{/Alt}{/Control}");
175 await save.click();
176 expect(mutate).toHaveBeenLastCalledWith({
177 environmentId: "env",
178 input: expect.objectContaining({
179 command: "composer.sendBackground",
180 when: "composerFocus && draftThreadRoute",
181 replace: { command: "composer.sendBackground", key: "mod+alt+enter", when: "false" },
182 }),
183 });
184 },
185 );
186});
187
188it("Esc in the keybindings search clears the query without closing Settings", async () => {
189 const onOpenChange = vi.fn();
190 await using _screen = await render(
191 <Dialog open onOpenChange={onOpenChange}>
192 <DialogPopup>
193 <DialogTitle>Settings</DialogTitle>
194 <KeybindingsSettingsPanel />
195 </DialogPopup>
196 </Dialog>,
197 );
198 const search = page.getByRole("searchbox", { name: "Search keybindings" });
199 await search.fill("sidebar");
200 for (const composition of [
201 { key: "Escape", isComposing: true, keyCode: 27 },
202 { key: "Escape", isComposing: false, keyCode: 229 },
203 ]) {
204 const keydown = new KeyboardEvent("keydown", {
205 ...composition,
206 bubbles: true,
207 cancelable: true,
208 });
209 await act(() => {
210 // Safari ends the composition just before the Escape keydown.
211 if (!composition.isComposing)
212 search.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
213 search.element().dispatchEvent(keydown);
214 });
215 expect(keydown.defaultPrevented).toBe(false);
216 await expect.element(search).toHaveValue("sidebar");
217 await expect.element(search).toHaveFocus();
218 }
219 expect(search.element().getAttribute("type")).toBe("text");
220
221 await userEvent.keyboard("{Escape}");
222 await expect.element(search).toHaveValue("");
223 await expect.element(search).toHaveFocus();
224
225 await act(() =>
226 search.element().dispatchEvent(
227 new KeyboardEvent("keydown", {
228 key: "Escape",
229 repeat: true,
230 bubbles: true,
231 cancelable: true,
232 }),
233 ),
234 );
235 await expect.element(search).toHaveValue("");
236 await expect.element(search).toHaveFocus();
237 expect(onOpenChange).not.toHaveBeenCalled();
238
239 await search.fill("sidebar");
240 await userEvent.keyboard("{Shift>}{Escape}{/Shift}");
241 await expect.element(search).toHaveValue("sidebar");
242 await expect.element(search).toHaveFocus();
243 expect(onOpenChange).toHaveBeenCalledWith(false, expect.anything());
244});
245
246it("re-enables Save when a When editor closes on an expression it could not parse", async () => {
247 await using screen = await render(
248 <RegistryProvider>
249 <KeybindingsSettingsPanel />
250 </RegistryProvider>,
251 );
252 const whenTrigger = screen.getByRole("button", { name: "Edit when clause for Terminal: Toggle" });
253 const expression = screen.getByRole("textbox", { name: "When expression" });
254
255 await whenTrigger.click();
256 await userEvent.type(expression, "terminalFocus");
257 await userEvent.type(expression, " &&");
258 await expect.element(screen.getByRole("button", { name: "Save" })).toBeDisabled();
259
260 await userEvent.keyboard("{Escape}");
261 await whenTrigger.click();
262
263 await expect.element(expression).toHaveValue("terminalFocus");
264 await expect.element(screen.getByRole("button", { name: "Save" })).toBeEnabled();
265});
266
267it("filters actions and allows additional shortcuts for assigned commands", async () => {
268 await using screen = await render(<KeybindingsSettingsPanel />);
269 await screen.getByRole("button", { name: "Add keybinding" }).click();
270 const dialog = page.getByRole("dialog", { name: "Command", exact: true });
271 const search = dialog.getByRole("combobox", { name: "Search", exact: true });
272 await expect.element(search).toHaveFocus();
273 await userEvent.keyboard("sidebar.toggle");
274 const assigned = page.getByRole("option", { name: /Sidebar: Toggle/ });
275 await expect.element(assigned).not.toHaveAttribute("aria-disabled", "true");
276 await expect.element(assigned.element().querySelector("kbd")!).toBeVisible();
277 await vi.waitFor(() => expect(page.getByRole("option").elements()).toHaveLength(1));
278 await assigned.click();
279 const command = screen.getByRole("combobox", { name: "Command", exact: true });
280 await expect.element(command).toHaveTextContent("Sidebar: Toggle");
281 await command.click();
282 await search.fill("Opposite Queue");
283 await expect
284 .element(page.getByRole("option", { name: /Composer: Opposite Queue or Steer Action/ }))
285 .toBeVisible();
286 await vi.waitFor(() => expect(page.getByRole("option").elements()).toHaveLength(1));
287 await search.fill("no-such-action");
288 await expect.element(page.getByText("No results found.")).toBeVisible();
289 await search.fill("composer.cycleHost");
290 const available = page.getByRole("option", { name: "Composer: Cycle Host" });
291 await expect.element(available).not.toHaveAttribute("aria-disabled", "true");
292 await available.click();
293 await expect
294 .element(screen.getByRole("combobox", { name: "Command", exact: true }))
295 .toHaveTextContent("Composer: Cycle Host");
296});