Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3import appCss from "~/index.css?inline";
4import { createWorkspaceFocusController } from "./workspaceFocus";
5
6function setup() {
7 const root = document.createElement("div");
8 document.body.append(root);
9 const controller = createWorkspaceFocusController(document);
10 const area = (name: string, order: number, parent: HTMLElement = root) => {
11 const element = document.createElement("section");
12 element.dataset.workspaceArea = name;
13 element.dataset.workspaceOrder = String(order);
14 element.tabIndex = -1;
15 const button = document.createElement("button");
16 button.textContent = name;
17 element.append(button);
18 parent.append(element);
19 return { element, button };
20 };
21 const append = (html: string) => {
22 root.insertAdjacentHTML("beforeend", html);
23 return root.lastElementChild as HTMLElement;
24 };
25 return {
26 area,
27 append,
28 controller,
29 [Symbol.dispose]() {
30 controller.dispose();
31 root.remove();
32 },
33 };
34}
35
36describe("workspace area focus", () => {
37 it("cycles in workspace order in both directions, independent of DOM order", () => {
38 using fixture = setup();
39 const panel = fixture.area("panel", 3);
40 const sidebar = fixture.area("sidebar", 0);
41 const conversation = fixture.area("conversation", 1);
42 const composer = fixture.area("composer", 2);
43 sidebar.button.focus();
44 fixture.controller.move(1);
45 // The conversation is a region; it takes focus itself rather than its first button.
46 expect(document.activeElement).toBe(conversation.element);
47 fixture.controller.move(1);
48 expect(document.activeElement).toBe(composer.button);
49 fixture.controller.move(1);
50 expect(document.activeElement).toBe(panel.button);
51 fixture.controller.move(1);
52 expect(document.activeElement).toBe(sidebar.button);
53 fixture.controller.move(-1);
54 expect(document.activeElement).toBe(panel.button);
55 });
56
57 it("skips hidden, inert, and maximized-away areas", () => {
58 using fixture = setup();
59 const sidebar = fixture.area("sidebar", 0);
60 const conversation = fixture.area("conversation", 1);
61 const composer = fixture.area("composer", 2);
62 const panel = fixture.area("panel", 3);
63 const terminal = fixture.area("terminal", 4);
64 conversation.element.dataset.chatColumnMaximizedAway = "true";
65 composer.element.hidden = true;
66 panel.element.dataset.workspaceUnavailable = "true";
67 terminal.element.inert = true;
68 sidebar.button.focus();
69 expect(fixture.controller.move(1)).toBe(true);
70 expect(document.activeElement).toBe(sidebar.button);
71 });
72
73 it("restores the last focused control and falls back when it is removed", () => {
74 using fixture = setup();
75 fixture.area("sidebar", 0);
76 const composer = fixture.area("composer", 2);
77 const remembered = document.createElement("button");
78 remembered.textContent = "remembered";
79 composer.element.append(remembered);
80 remembered.focus();
81 fixture.controller.move(-1);
82 fixture.controller.move(1);
83 expect(document.activeElement).toBe(remembered);
84 fixture.controller.move(-1);
85 remembered.remove();
86 fixture.controller.move(1);
87 expect(document.activeElement).toBe(composer.button);
88 });
89
90 it("keeps focus inside a dialog or an open popup", () => {
91 using fixture = setup();
92 fixture.area("sidebar", 0);
93 fixture.area("panel", 3);
94 const dialog = fixture.append('<div role="dialog" tabindex="-1">Confirm</div>');
95 dialog.focus();
96 expect(fixture.controller.move(1)).toBe(false);
97 expect(document.activeElement).toBe(dialog);
98 dialog.remove();
99
100 const popup = fixture.append(
101 '<div role="listbox" data-slot="select-popup"><button role="option">Choice</button></div>',
102 );
103 const choice = popup.querySelector("button")!;
104 choice.focus();
105 expect(fixture.controller.move(1)).toBe(false);
106 expect(document.activeElement).toBe(choice);
107 });
108
109 it("keeps moving while a non-modal notification is visible", () => {
110 using fixture = setup();
111 const composer = fixture.area("composer", 2);
112 const panel = fixture.area("panel", 4);
113 fixture.append(
114 '<div data-slot="toast-viewport"><div role="dialog" aria-modal="false" tabindex="0">Update available</div></div>',
115 );
116 composer.button.focus();
117 expect(fixture.controller.move(1)).toBe(true);
118 expect(document.activeElement).toBe(panel.button);
119 });
120
121 it("moves only between the areas of an open Settings modal", () => {
122 using fixture = setup();
123 fixture.area("sidebar", 0);
124 fixture.area("composer", 2);
125 const modal = fixture.append('<div role="dialog" data-app-modal=""></div>');
126 const nav = fixture.area("settings-nav", 0, modal);
127 const content = fixture.area("settings", 1, modal);
128 nav.button.focus();
129 expect(fixture.controller.move(1)).toBe(true);
130 expect(document.activeElement).toBe(content.button);
131 expect(fixture.controller.move(1)).toBe(true);
132 expect(document.activeElement).toBe(nav.button);
133 });
134
135 it("enters the Settings sections on the open section", () => {
136 using fixture = setup();
137 const nav = fixture.area("settings-nav", 0);
138 const content = fixture.area("settings", 1);
139 nav.element.insertAdjacentHTML(
140 "beforeend",
141 '<button data-settings-section="/settings/general">General</button><button data-settings-section="/settings/chat" aria-current="page">Chat</button>',
142 );
143 content.button.focus();
144 fixture.controller.move(-1);
145 expect(document.activeElement).toBe(nav.element.querySelector('[aria-current="page"]'));
146 });
147
148 it("keeps moving while a shortcut recorder is on the page but not focused", () => {
149 using fixture = setup();
150 const nav = fixture.area("settings-nav", 0);
151 const content = fixture.area("settings", 1);
152 fixture.append('<button data-keybinding-capture="">Record shortcut</button>');
153 nav.button.focus();
154 expect(fixture.controller.move(1)).toBe(true);
155 expect(document.activeElement).toBe(content.button);
156 });
157
158 it("brings a restored control back into view inside its own list", () => {
159 using fixture = setup();
160 const sidebar = fixture.area("sidebar", 0);
161 const list = document.createElement("div");
162 list.style.cssText = "height: 50px; overflow-y: auto";
163 const spacer = document.createElement("div");
164 spacer.style.height = "400px";
165 const row = document.createElement("button");
166 row.textContent = "Last thread";
167 list.append(spacer, row);
168 sidebar.element.append(list);
169 const composer = fixture.area("composer", 2);
170 row.focus();
171 fixture.controller.move(1);
172 expect(document.activeElement).toBe(composer.button);
173 list.scrollTop = 0;
174 fixture.controller.move(-1);
175 expect(document.activeElement).toBe(row);
176 expect(list.scrollTop).toBeGreaterThan(0);
177 });
178
179 it("reveals a restored control through nested scrolling lists", () => {
180 using fixture = setup();
181 const settings = fixture.area("settings", 1);
182 const page = document.createElement("div");
183 page.style.cssText = "height: 80px; overflow-y: auto";
184 const inner = document.createElement("div");
185 inner.style.cssText = "height: 60px; overflow-y: auto";
186 const field = document.createElement("button");
187 field.textContent = "Provider field";
188 inner.append(Object.assign(document.createElement("div"), { style: "height: 200px" }), field);
189 page.append(inner, Object.assign(document.createElement("div"), { style: "height: 400px" }));
190 settings.element.append(page);
191 const nav = fixture.area("settings-nav", 0);
192 field.focus();
193 fixture.controller.move(-1);
194 expect(document.activeElement).toBe(nav.button);
195 inner.scrollTop = 0;
196 page.scrollTop = 300;
197 fixture.controller.move(1);
198 expect(document.activeElement).toBe(field);
199 const box = page.getBoundingClientRect();
200 const rect = field.getBoundingClientRect();
201 expect(rect.bottom > box.top && rect.top < box.bottom).toBe(true);
202 });
203
204 it("does not scroll the conversation to an old control", () => {
205 using fixture = setup();
206 const conversation = fixture.area("conversation", 1);
207 const timeline = document.createElement("div");
208 timeline.style.cssText = "height: 50px; overflow-y: auto";
209 const copy = document.createElement("button");
210 copy.textContent = "Copy";
211 timeline.append(copy, Object.assign(document.createElement("div"), { style: "height: 400px" }));
212 conversation.element.append(timeline);
213 const composer = fixture.area("composer", 2);
214 copy.focus();
215 fixture.controller.move(1);
216 expect(document.activeElement).toBe(composer.button);
217 // Live follow moved the timeline to the newest output.
218 timeline.scrollTop = 300;
219 fixture.controller.move(-1);
220 expect(document.activeElement).toBe(conversation.element);
221 expect(timeline.scrollTop).toBe(300);
222 });
223
224 it.each([
225 ["panel", "panel", "removed"],
226 ["conversation", "composer", "removed"],
227 ["conversation", "composer", "disabled"],
228 ])("recovers focus from a %s control to the %s when %s", async (source, destination, change) => {
229 using fixture = setup();
230 const composer = fixture.area("composer", 2);
231 const area = fixture.area(source, 3);
232 const control = document.createElement("button");
233 control.id = "pending-request";
234 control.textContent = "Submit";
235 area.element.append(control);
236 control.focus();
237 if (change === "disabled") control.disabled = true;
238 else control.remove();
239 const expected = destination === "composer" ? composer.button : area.button;
240 await vi.waitFor(() => expect(document.activeElement).toBe(expected));
241 });
242
243 it("falls back to the main content when the whole area disappears", async () => {
244 using fixture = setup();
245 const composer = fixture.area("composer", 2);
246 const details = fixture.area("details", 3);
247 details.button.focus();
248 details.element.hidden = true;
249 await vi.waitFor(() => expect(document.activeElement).toBe(composer.button));
250 });
251
252 it("keeps focus on the control a panel tab switch came from", () => {
253 using fixture = setup();
254 const panel = fixture.area("panel", 3);
255 const content = document.createElement("div");
256 content.setAttribute("data-right-panel-surface-content", "");
257 const row = (name: string) => {
258 const button = document.createElement("button");
259 button.textContent = name;
260 content.append(button);
261 return button;
262 };
263 const earlierRow = row("reference-analysis.md");
264 const clickedRow = row("print-guide.md");
265 panel.element.append(content);
266 panel.element.dataset.workspaceFocusKey = "panel:file:print-guide.md";
267 earlierRow.focus();
268 panel.element.dataset.workspaceFocusKey = "panel:file:reference-analysis.md";
269 clickedRow.focus();
270 panel.element.dataset.workspaceFocusKey = "panel:file:print-guide.md";
271 expect(fixture.controller.focus("panel")).toBe(true);
272 expect(document.activeElement).toBe(clickedRow);
273 });
274
275 it("focuses a side chat's message input when the panel opens on it", () => {
276 using fixture = setup();
277 const panel = fixture.area("panel", 3);
278 const content = document.createElement("div");
279 content.setAttribute("data-right-panel-surface-content", "");
280 const bringBack = document.createElement("button");
281 bringBack.textContent = "Bring back";
282 content.append(bringBack);
283 panel.element.append(content);
284 fixture.area("conversation", 1, content);
285 const composer = fixture.area("composer", 2, content);
286 const editor = document.createElement("div");
287 editor.contentEditable = "true";
288 composer.element.append(editor);
289 expect(fixture.controller.focus("panel")).toBe(true);
290 expect(document.activeElement).toBe(editor);
291 // F6 from the message input moves on to the panel's own controls.
292 expect(fixture.controller.move(1)).toBe(true);
293 expect(document.activeElement).toBe(bringBack);
294 });
295
296 it("waits for a requested area to mount", async () => {
297 using fixture = setup();
298 fixture.area("composer", 2);
299 fixture.controller.request({ area: "pull-requests" });
300 const list = fixture.area("pull-requests", 1);
301 await vi.waitFor(() => expect(document.activeElement).toBe(list.button));
302 });
303
304 it("keeps focus on a control that React remounted elsewhere", async () => {
305 using fixture = setup();
306 fixture.area("composer", 2);
307 const header = fixture.append("<header></header>");
308 const toggle = document.createElement("button");
309 toggle.setAttribute("aria-label", "Toggle right panel");
310 header.append(toggle);
311 toggle.focus();
312 const moved = toggle.cloneNode() as HTMLButtonElement;
313 toggle.remove();
314 header.append(moved);
315 await vi.waitFor(() => expect(document.activeElement).toBe(moved));
316 });
317
318 it("keeps moving while a hover popup without focus is open", () => {
319 using fixture = setup();
320 const composer = fixture.area("composer", 2);
321 const terminal = fixture.area("terminal", 4);
322 fixture.append('<div data-slot="popover-popup">New split terminal</div>');
323 composer.button.focus();
324 expect(fixture.controller.move(1)).toBe(true);
325 expect(document.activeElement).toBe(terminal.button);
326 });
327
328 it("rings an area reached with F6, but not a clicked one after a bare key press", async () => {
329 using fixture = setup();
330 const style = document.createElement("style");
331 style.textContent = appCss;
332 document.head.append(style);
333 using _style = { [Symbol.dispose]: () => style.remove() };
334 const sidebar = fixture.area("sidebar", 0);
335 const home = fixture.append(
336 '<main data-workspace-area="home" data-workspace-order="1" tabindex="-1" style="height: 200px"></main>',
337 );
338 const outline = () => getComputedStyle(home).outlineStyle;
339
340 await userEvent.click(home);
341 expect(document.activeElement).toBe(home);
342 expect(outline()).toBe("none");
343 // A window switch can deliver a lone modifier key without moving focus.
344 await userEvent.keyboard("{Shift}");
345 expect(outline(), "a clicked area shows the focus ring after a bare key press").toBe("none");
346 // Window activation refocuses the element that had focus, with nothing focused in between.
347 home.blur();
348 home.focus();
349 expect(outline(), "a clicked area rings when the window regains focus").toBe("none");
350
351 await userEvent.keyboard("{F6}");
352 fixture.controller.move(-1);
353 expect(document.activeElement).toBe(sidebar.button);
354 await userEvent.keyboard("{F6}");
355 fixture.controller.move(1);
356 expect(document.activeElement).toBe(home);
357 expect(outline()).toBe("solid");
358
359 // A dialog opened from the keyboard returns focus to the area when it closes.
360 await userEvent.keyboard("{Enter}");
361 sidebar.button.focus();
362 await userEvent.keyboard("{Escape}");
363 home.focus();
364 expect(outline(), "an area loses its ring when keyboard focus returns to it").toBe("solid");
365
366 await userEvent.click(home);
367 expect(outline(), "clicking the ringed area removes its ring").toBe("none");
368
369 // With no other area visible, F6 lands on the area that already has focus.
370 sidebar.element.hidden = true;
371 await userEvent.keyboard("{F6}");
372 fixture.controller.move(1);
373 expect(document.activeElement).toBe(home);
374 expect(outline(), "F6 onto the only, already focused area shows no ring").toBe("solid");
375 });
376});