Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

workspaceFocus.browser.test.ts

467 lines19.0 KB

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 };
35​
36/** Mirrors MessagesTimeline's scroller, which takes focus so the keyboard scrolls it. */
37function conversationScroller(height: number) {
38 const timeline = document.createElement("div");
39 timeline.style.cssText = `height: ${height}px; overflow-y: auto`;
40 timeline.tabIndex = -1;
41 timeline.dataset.workspaceFocusDefault = "";
42 return timeline;
44​
45describe("workspace area focus", () => {
46 it("cycles in workspace order in both directions, independent of DOM order", () => {
47 using fixture = setup();
48 const panel = fixture.area("panel", 3);
49 const sidebar = fixture.area("sidebar", 0);
50 const conversation = fixture.area("conversation", 1);
51 const composer = fixture.area("composer", 2);
52 sidebar.button.focus();
53 fixture.controller.move(1);
54 // Without a timeline (an empty thread), the region takes focus rather than its first button.
55 expect(document.activeElement).toBe(conversation.element);
56 fixture.controller.move(1);
57 expect(document.activeElement).toBe(composer.button);
58 fixture.controller.move(1);
59 expect(document.activeElement).toBe(panel.button);
60 fixture.controller.move(1);
61 expect(document.activeElement).toBe(sidebar.button);
62 fixture.controller.move(-1);
63 expect(document.activeElement).toBe(panel.button);
64 });
65​
66 it("moves back out of a side chat whose areas reuse the main chat's names", () => {
67 using fixture = setup();
68 const composer = fixture.area("composer", 2);
69 const panel = fixture.area("panel", 4);
70 const sideComposer = fixture.area("composer", 2, panel.element);
71 composer.button.focus();
72 fixture.controller.move(1);
73 expect(document.activeElement).toBe(sideComposer.button);
74 fixture.controller.move(-1);
75 expect(document.activeElement).toBe(composer.button);
76 });
77​
78 it("skips hidden, inert, and maximized-away areas", () => {
79 using fixture = setup();
80 const sidebar = fixture.area("sidebar", 0);
81 const conversation = fixture.area("conversation", 1);
82 const composer = fixture.area("composer", 2);
83 const panel = fixture.area("panel", 3);
84 const terminal = fixture.area("terminal", 4);
85 conversation.element.dataset.chatColumnMaximizedAway = "true";
86 composer.element.hidden = true;
87 panel.element.dataset.workspaceUnavailable = "true";
88 terminal.element.inert = true;
89 sidebar.button.focus();
90 expect(fixture.controller.move(1)).toBe(true);
91 expect(document.activeElement).toBe(sidebar.button);
92 });
93​
94 it("restores the last focused control and falls back when it is removed", () => {
95 using fixture = setup();
96 fixture.area("sidebar", 0);
97 const composer = fixture.area("composer", 2);
98 const remembered = document.createElement("button");
99 remembered.textContent = "remembered";
100 composer.element.append(remembered);
101 remembered.focus();
102 fixture.controller.move(-1);
103 fixture.controller.move(1);
104 expect(document.activeElement).toBe(remembered);
105 fixture.controller.move(-1);
106 remembered.remove();
107 fixture.controller.move(1);
108 expect(document.activeElement).toBe(composer.button);
109 });
110​
111 it("keeps focus inside a dialog or an open popup", () => {
112 using fixture = setup();
113 fixture.area("sidebar", 0);
114 fixture.area("panel", 3);
115 const dialog = fixture.append('<div role="dialog" tabindex="-1">Confirm</div>');
116 dialog.focus();
117 expect(fixture.controller.move(1)).toBe(false);
118 expect(document.activeElement).toBe(dialog);
119 dialog.remove();
120​
121 const popup = fixture.append(
122 '<div role="listbox" data-slot="select-popup"><button role="option">Choice</button></div>',
123 );
124 const choice = popup.querySelector("button")!;
125 choice.focus();
126 expect(fixture.controller.move(1)).toBe(false);
127 expect(document.activeElement).toBe(choice);
128 });
129​
130 it("keeps moving while a non-modal notification is visible", () => {
131 using fixture = setup();
132 const composer = fixture.area("composer", 2);
133 const panel = fixture.area("panel", 4);
134 fixture.append(
135 '<div data-slot="toast-viewport"><div role="dialog" aria-modal="false" tabindex="0">Update available</div></div>',
136 );
137 composer.button.focus();
138 expect(fixture.controller.move(1)).toBe(true);
139 expect(document.activeElement).toBe(panel.button);
140 });
141​
142 it("moves only between the areas of an open Settings modal", () => {
143 using fixture = setup();
144 fixture.area("sidebar", 0);
145 fixture.area("composer", 2);
146 const modal = fixture.append('<div role="dialog" data-app-modal=""></div>');
147 const nav = fixture.area("settings-nav", 0, modal);
148 const content = fixture.area("settings", 1, modal);
149 nav.button.focus();
150 expect(fixture.controller.move(1)).toBe(true);
151 expect(document.activeElement).toBe(content.button);
152 expect(fixture.controller.move(1)).toBe(true);
153 expect(document.activeElement).toBe(nav.button);
154 });
155​
156 it("enters the Settings sections on the open section", () => {
157 using fixture = setup();
158 const nav = fixture.area("settings-nav", 0);
159 const content = fixture.area("settings", 1);
160 nav.element.insertAdjacentHTML(
161 "beforeend",
162 '<button data-settings-section="/settings/general">General</button><button data-settings-section="/settings/chat" aria-current="page">Chat</button>',
163 );
164 content.button.focus();
165 fixture.controller.move(-1);
166 expect(document.activeElement).toBe(nav.element.querySelector('[aria-current="page"]'));
167 });
168​
169 it("keeps moving while a shortcut recorder is on the page but not focused", () => {
170 using fixture = setup();
171 const nav = fixture.area("settings-nav", 0);
172 const content = fixture.area("settings", 1);
173 fixture.append('<button data-keybinding-capture="">Record shortcut</button>');
174 nav.button.focus();
175 expect(fixture.controller.move(1)).toBe(true);
176 expect(document.activeElement).toBe(content.button);
177 });
178​
179 it("brings a restored control back into view inside its own list", () => {
180 using fixture = setup();
181 const sidebar = fixture.area("sidebar", 0);
182 const list = document.createElement("div");
183 list.style.cssText = "height: 50px; overflow-y: auto";
184 const spacer = document.createElement("div");
185 spacer.style.height = "400px";
186 const row = document.createElement("button");
187 row.textContent = "Last thread";
188 list.append(spacer, row);
189 sidebar.element.append(list);
190 const composer = fixture.area("composer", 2);
191 row.focus();
192 fixture.controller.move(1);
193 expect(document.activeElement).toBe(composer.button);
194 list.scrollTop = 0;
195 fixture.controller.move(-1);
196 expect(document.activeElement).toBe(row);
197 expect(list.scrollTop).toBeGreaterThan(0);
198 });
199​
200 it("reveals a restored control through nested scrolling lists", () => {
201 using fixture = setup();
202 const settings = fixture.area("settings", 1);
203 const page = document.createElement("div");
204 page.style.cssText = "height: 80px; overflow-y: auto";
205 const inner = document.createElement("div");
206 inner.style.cssText = "height: 60px; overflow-y: auto";
207 const field = document.createElement("button");
208 field.textContent = "Provider field";
209 inner.append(Object.assign(document.createElement("div"), { style: "height: 200px" }), field);
210 page.append(inner, Object.assign(document.createElement("div"), { style: "height: 400px" }));
211 settings.element.append(page);
212 const nav = fixture.area("settings-nav", 0);
213 field.focus();
214 fixture.controller.move(-1);
215 expect(document.activeElement).toBe(nav.button);
216 inner.scrollTop = 0;
217 page.scrollTop = 300;
218 fixture.controller.move(1);
219 expect(document.activeElement).toBe(field);
220 const box = page.getBoundingClientRect();
221 const rect = field.getBoundingClientRect();
222 expect(rect.bottom > box.top && rect.top < box.bottom).toBe(true);
223 });
224​
225 it("does not scroll the conversation to an old control", () => {
226 using fixture = setup();
227 const conversation = fixture.area("conversation", 1);
228 const timeline = conversationScroller(50);
229 const copy = document.createElement("button");
230 copy.textContent = "Copy";
231 timeline.append(copy, Object.assign(document.createElement("div"), { style: "height: 400px" }));
232 conversation.element.append(timeline);
233 const composer = fixture.area("composer", 2);
234 copy.focus();
235 fixture.controller.move(1);
236 expect(document.activeElement).toBe(composer.button);
237 // Live follow moved the timeline to the newest output.
238 timeline.scrollTop = 300;
239 fixture.controller.move(-1);
240 expect(document.activeElement).toBe(timeline);
241 expect(timeline.scrollTop).toBe(300);
242 });
243​
244 it("scrolls the conversation with the keyboard once an empty thread gets a timeline", async () => {
245 using fixture = setup();
246 const conversation = fixture.area("conversation", 1);
247 conversation.button.remove();
248 conversation.element.style.height = "200px";
249 const composer = fixture.area("composer", 2);
250 composer.button.focus();
251 fixture.controller.move(-1);
252 expect(document.activeElement).toBe(conversation.element);
253 fixture.controller.move(1);
254 const timeline = conversationScroller(200);
255 timeline.append(Object.assign(document.createElement("div"), { style: "height: 2000px" }));
256 conversation.element.append(timeline);
257 fixture.controller.move(-1);
258 await userEvent.keyboard("{ArrowDown}{PageDown}");
259 await vi.waitFor(() => expect(timeline.scrollTop).toBeGreaterThan(0));
260 });
261​
262 it.each([
263 ["panel", "panel", "removed"],
264 ["conversation", "composer", "removed"],
265 ["conversation", "composer", "disabled"],
266 ])("recovers focus from a %s control to the %s when %s", async (source, destination, change) => {
267 using fixture = setup();
268 const composer = fixture.area("composer", 2);
269 const area = fixture.area(source, 3);
270 const control = document.createElement("button");
271 control.id = "pending-request";
272 control.textContent = "Submit";
273 if (source === "conversation") {
274 // Timeline prompts sit in the scrolling timeline among other controls.
275 const timeline = conversationScroller(50);
276 const other = document.createElement("button");
277 other.style.cssText = "display: block; height: 100px";
278 timeline.append(control, other);
279 area.element.append(timeline);
280 } else area.element.append(control);
281 control.focus();
282 if (change === "disabled") control.disabled = true;
283 else control.remove();
284 const expected = destination === "composer" ? composer.button : area.button;
285 await vi.waitFor(() => expect(document.activeElement).toBe(expected));
286 });
287​
288 it("recovers focus from a removed list row to its neighbour without scrolling the list", async () => {
289 using fixture = setup();
290 fixture.area("composer", 2);
291 const panel = fixture.area("panel", 3);
292 const content = document.createElement("div");
293 content.setAttribute("data-right-panel-surface-content", "");
294 const list = document.createElement("div");
295 list.style.cssText = "height: 100px; overflow-y: auto";
296 const rows = Array.from({ length: 20 }, (_, index) => {
297 const row = document.createElement("button");
298 row.textContent = `Row ${index}`;
299 row.style.cssText = "display: block; height: 30px";
300 list.append(row);
301 return row;
302 });
303 content.append(list);
304 panel.element.append(content);
305 list.scrollTop = list.scrollHeight;
306 const removed = rows[18]!;
307 removed.focus({ preventScroll: true });
308 removed.remove();
309 const scrolled = list.scrollTop;
310 await vi.waitFor(() => expect(document.activeElement).not.toBe(document.body));
311 expect(list.scrollTop).toBe(scrolled);
312 expect([rows[17], rows[19]]).toContain(document.activeElement);
313 });
314​
315 it("falls back to the main content when the whole area disappears", async () => {
316 using fixture = setup();
317 const composer = fixture.area("composer", 2);
318 const details = fixture.area("details", 3);
319 details.button.focus();
320 details.element.hidden = true;
321 await vi.waitFor(() => expect(document.activeElement).toBe(composer.button));
322 });
323​
324 it("keeps focus inside an open dialog when a focused button disables", async () => {
325 using fixture = setup();
326 const composer = fixture.area("composer", 2);
327 const dialog = fixture.append('<div role="dialog" tabindex="-1"><button>Save</button></div>');
328 const button = dialog.querySelector("button")!;
329 button.focus();
330 button.disabled = true;
331 await vi.waitFor(() => expect(document.activeElement).toBe(dialog));
332​
333 // A dialog that cannot take focus falls back to the workspace instead of the body.
334 dialog.removeAttribute("tabindex");
335 button.disabled = false;
336 button.focus();
337 button.disabled = true;
338 await vi.waitFor(() => expect(document.activeElement).toBe(composer.button));
339 });
340​
341 it("keeps focus on the control a panel tab switch came from", () => {
342 using fixture = setup();
343 const panel = fixture.area("panel", 3);
344 const content = document.createElement("div");
345 content.setAttribute("data-right-panel-surface-content", "");
346 const row = (name: string) => {
347 const button = document.createElement("button");
348 button.textContent = name;
349 content.append(button);
350 return button;
351 };
352 const earlierRow = row("reference-analysis.md");
353 const clickedRow = row("print-guide.md");
354 panel.element.append(content);
355 panel.element.dataset.workspaceFocusKey = "panel:file:print-guide.md";
356 earlierRow.focus();
357 panel.element.dataset.workspaceFocusKey = "panel:file:reference-analysis.md";
358 clickedRow.focus();
359 panel.element.dataset.workspaceFocusKey = "panel:file:print-guide.md";
360 expect(fixture.controller.focus("panel")).toBe(true);
361 expect(document.activeElement).toBe(clickedRow);
362 });
363​
364 it("focuses a side chat's message input when the panel opens on it", () => {
365 using fixture = setup();
366 const panel = fixture.area("panel", 3);
367 const content = document.createElement("div");
368 content.setAttribute("data-right-panel-surface-content", "");
369 const bringBack = document.createElement("button");
370 bringBack.textContent = "Bring back";
371 content.append(bringBack);
372 panel.element.append(content);
373 const timeline = conversationScroller(50);
374 timeline.append(Object.assign(document.createElement("div"), { style: "height: 400px" }));
375 fixture.area("conversation", 1, content).element.append(timeline);
376 const composer = fixture.area("composer", 2, content);
377 const editor = document.createElement("div");
378 editor.contentEditable = "true";
379 composer.element.append(editor);
380 expect(fixture.controller.focus("panel")).toBe(true);
381 expect(document.activeElement).toBe(editor);
382 // F6 from the message input moves on to the panel's own controls.
383 expect(fixture.controller.move(1)).toBe(true);
384 expect(document.activeElement).toBe(bringBack);
385 });
386​
387 it("waits for a requested area to mount", async () => {
388 using fixture = setup();
389 fixture.area("composer", 2);
390 fixture.controller.request({ area: "pull-requests" });
391 const list = fixture.area("pull-requests", 1);
392 await vi.waitFor(() => expect(document.activeElement).toBe(list.button));
393 });
394​
395 it("keeps focus on a control that React remounted elsewhere", async () => {
396 using fixture = setup();
397 fixture.area("composer", 2);
398 const header = fixture.append("<header></header>");
399 const toggle = document.createElement("button");
400 toggle.setAttribute("aria-label", "Toggle right panel");
401 header.append(toggle);
402 toggle.focus();
403 const moved = toggle.cloneNode() as HTMLButtonElement;
404 toggle.remove();
405 header.append(moved);
406 await vi.waitFor(() => expect(document.activeElement).toBe(moved));
407 });
408​
409 it("keeps moving while a hover popup without focus is open", () => {
410 using fixture = setup();
411 const composer = fixture.area("composer", 2);
412 const terminal = fixture.area("terminal", 4);
413 fixture.append('<div data-slot="popover-popup">New split terminal</div>');
414 composer.button.focus();
415 expect(fixture.controller.move(1)).toBe(true);
416 expect(document.activeElement).toBe(terminal.button);
417 });
418​
419 it("rings an area reached with F6, but not a clicked one after a bare key press", async () => {
420 using fixture = setup();
421 const style = document.createElement("style");
422 style.textContent = appCss;
423 document.head.append(style);
424 using _style = { [Symbol.dispose]: () => style.remove() };
425 const sidebar = fixture.area("sidebar", 0);
426 const home = fixture.append(
427 '<main data-workspace-area="home" data-workspace-order="1" tabindex="-1" style="height: 200px"></main>',
428 );
429 const outline = () => getComputedStyle(home).outlineStyle;
430​
431 await userEvent.click(home);
432 expect(document.activeElement).toBe(home);
433 expect(outline()).toBe("none");
434 // A window switch can deliver a lone modifier key without moving focus.
435 await userEvent.keyboard("{Shift}");
436 expect(outline(), "a clicked area shows the focus ring after a bare key press").toBe("none");
437 // Window activation refocuses the element that had focus, with nothing focused in between.
438 home.blur();
439 home.focus();
440 expect(outline(), "a clicked area rings when the window regains focus").toBe("none");
441​
442 await userEvent.keyboard("{F6}");
443 fixture.controller.move(-1);
444 expect(document.activeElement).toBe(sidebar.button);
445 await userEvent.keyboard("{F6}");
446 fixture.controller.move(1);
447 expect(document.activeElement).toBe(home);
448 expect(outline()).toBe("solid");
449​
450 // A dialog opened from the keyboard returns focus to the area when it closes.
451 await userEvent.keyboard("{Enter}");
452 sidebar.button.focus();
453 await userEvent.keyboard("{Escape}");
454 home.focus();
455 expect(outline(), "an area loses its ring when keyboard focus returns to it").toBe("solid");
456​
457 await userEvent.click(home);
458 expect(outline(), "clicking the ringed area removes its ring").toBe("none");
459​
460 // With no other area visible, F6 lands on the area that already has focus.
461 sidebar.element.hidden = true;
462 await userEvent.keyboard("{F6}");
463 fixture.controller.move(1);
464 expect(document.activeElement).toBe(home);
465 expect(outline(), "F6 onto the only, already focused area shows no ring").toBe("solid");
466 });
467});