The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { userEvent } from "vite-plus/test/browser"; |
| 3 | import appCss from "~/index.css?inline"; |
| 4 | import { createWorkspaceFocusController } from "./workspaceFocus"; |
| 5 | |
| 6 | function 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 | |
| 36 | /** Mirrors MessagesTimeline's scroller, which takes focus so the keyboard scrolls it. */ |
| 37 | function 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; |
| 43 | } |
| 44 | |
| 45 | describe("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 | area.element.append(control); |
| 274 | control.focus(); |
| 275 | if (change === "disabled") control.disabled = true; |
| 276 | else control.remove(); |
| 277 | const expected = destination === "composer" ? composer.button : area.button; |
| 278 | await vi.waitFor(() => expect(document.activeElement).toBe(expected)); |
| 279 | }); |
| 280 | |
| 281 | it("falls back to the main content when the whole area disappears", async () => { |
| 282 | using fixture = setup(); |
| 283 | const composer = fixture.area("composer", 2); |
| 284 | const details = fixture.area("details", 3); |
| 285 | details.button.focus(); |
| 286 | details.element.hidden = true; |
| 287 | await vi.waitFor(() => expect(document.activeElement).toBe(composer.button)); |
| 288 | }); |
| 289 | |
| 290 | it("keeps focus on the control a panel tab switch came from", () => { |
| 291 | using fixture = setup(); |
| 292 | const panel = fixture.area("panel", 3); |
| 293 | const content = document.createElement("div"); |
| 294 | content.setAttribute("data-right-panel-surface-content", ""); |
| 295 | const row = (name: string) => { |
| 296 | const button = document.createElement("button"); |
| 297 | button.textContent = name; |
| 298 | content.append(button); |
| 299 | return button; |
| 300 | }; |
| 301 | const earlierRow = row("reference-analysis.md"); |
| 302 | const clickedRow = row("print-guide.md"); |
| 303 | panel.element.append(content); |
| 304 | panel.element.dataset.workspaceFocusKey = "panel:file:print-guide.md"; |
| 305 | earlierRow.focus(); |
| 306 | panel.element.dataset.workspaceFocusKey = "panel:file:reference-analysis.md"; |
| 307 | clickedRow.focus(); |
| 308 | panel.element.dataset.workspaceFocusKey = "panel:file:print-guide.md"; |
| 309 | expect(fixture.controller.focus("panel")).toBe(true); |
| 310 | expect(document.activeElement).toBe(clickedRow); |
| 311 | }); |
| 312 | |
| 313 | it("focuses a side chat's message input when the panel opens on it", () => { |
| 314 | using fixture = setup(); |
| 315 | const panel = fixture.area("panel", 3); |
| 316 | const content = document.createElement("div"); |
| 317 | content.setAttribute("data-right-panel-surface-content", ""); |
| 318 | const bringBack = document.createElement("button"); |
| 319 | bringBack.textContent = "Bring back"; |
| 320 | content.append(bringBack); |
| 321 | panel.element.append(content); |
| 322 | const timeline = conversationScroller(50); |
| 323 | timeline.append(Object.assign(document.createElement("div"), { style: "height: 400px" })); |
| 324 | fixture.area("conversation", 1, content).element.append(timeline); |
| 325 | const composer = fixture.area("composer", 2, content); |
| 326 | const editor = document.createElement("div"); |
| 327 | editor.contentEditable = "true"; |
| 328 | composer.element.append(editor); |
| 329 | expect(fixture.controller.focus("panel")).toBe(true); |
| 330 | expect(document.activeElement).toBe(editor); |
| 331 | // F6 from the message input moves on to the panel's own controls. |
| 332 | expect(fixture.controller.move(1)).toBe(true); |
| 333 | expect(document.activeElement).toBe(bringBack); |
| 334 | }); |
| 335 | |
| 336 | it("waits for a requested area to mount", async () => { |
| 337 | using fixture = setup(); |
| 338 | fixture.area("composer", 2); |
| 339 | fixture.controller.request({ area: "pull-requests" }); |
| 340 | const list = fixture.area("pull-requests", 1); |
| 341 | await vi.waitFor(() => expect(document.activeElement).toBe(list.button)); |
| 342 | }); |
| 343 | |
| 344 | it("keeps focus on a control that React remounted elsewhere", async () => { |
| 345 | using fixture = setup(); |
| 346 | fixture.area("composer", 2); |
| 347 | const header = fixture.append("<header></header>"); |
| 348 | const toggle = document.createElement("button"); |
| 349 | toggle.setAttribute("aria-label", "Toggle right panel"); |
| 350 | header.append(toggle); |
| 351 | toggle.focus(); |
| 352 | const moved = toggle.cloneNode() as HTMLButtonElement; |
| 353 | toggle.remove(); |
| 354 | header.append(moved); |
| 355 | await vi.waitFor(() => expect(document.activeElement).toBe(moved)); |
| 356 | }); |
| 357 | |
| 358 | it("keeps moving while a hover popup without focus is open", () => { |
| 359 | using fixture = setup(); |
| 360 | const composer = fixture.area("composer", 2); |
| 361 | const terminal = fixture.area("terminal", 4); |
| 362 | fixture.append('<div data-slot="popover-popup">New split terminal</div>'); |
| 363 | composer.button.focus(); |
| 364 | expect(fixture.controller.move(1)).toBe(true); |
| 365 | expect(document.activeElement).toBe(terminal.button); |
| 366 | }); |
| 367 | |
| 368 | it("rings an area reached with F6, but not a clicked one after a bare key press", async () => { |
| 369 | using fixture = setup(); |
| 370 | const style = document.createElement("style"); |
| 371 | style.textContent = appCss; |
| 372 | document.head.append(style); |
| 373 | using _style = { [Symbol.dispose]: () => style.remove() }; |
| 374 | const sidebar = fixture.area("sidebar", 0); |
| 375 | const home = fixture.append( |
| 376 | '<main data-workspace-area="home" data-workspace-order="1" tabindex="-1" style="height: 200px"></main>', |
| 377 | ); |
| 378 | const outline = () => getComputedStyle(home).outlineStyle; |
| 379 | |
| 380 | await userEvent.click(home); |
| 381 | expect(document.activeElement).toBe(home); |
| 382 | expect(outline()).toBe("none"); |
| 383 | // A window switch can deliver a lone modifier key without moving focus. |
| 384 | await userEvent.keyboard("{Shift}"); |
| 385 | expect(outline(), "a clicked area shows the focus ring after a bare key press").toBe("none"); |
| 386 | // Window activation refocuses the element that had focus, with nothing focused in between. |
| 387 | home.blur(); |
| 388 | home.focus(); |
| 389 | expect(outline(), "a clicked area rings when the window regains focus").toBe("none"); |
| 390 | |
| 391 | await userEvent.keyboard("{F6}"); |
| 392 | fixture.controller.move(-1); |
| 393 | expect(document.activeElement).toBe(sidebar.button); |
| 394 | await userEvent.keyboard("{F6}"); |
| 395 | fixture.controller.move(1); |
| 396 | expect(document.activeElement).toBe(home); |
| 397 | expect(outline()).toBe("solid"); |
| 398 | |
| 399 | // A dialog opened from the keyboard returns focus to the area when it closes. |
| 400 | await userEvent.keyboard("{Enter}"); |
| 401 | sidebar.button.focus(); |
| 402 | await userEvent.keyboard("{Escape}"); |
| 403 | home.focus(); |
| 404 | expect(outline(), "an area loses its ring when keyboard focus returns to it").toBe("solid"); |
| 405 | |
| 406 | await userEvent.click(home); |
| 407 | expect(outline(), "clicking the ringed area removes its ring").toBe("none"); |
| 408 | |
| 409 | // With no other area visible, F6 lands on the area that already has focus. |
| 410 | sidebar.element.hidden = true; |
| 411 | await userEvent.keyboard("{F6}"); |
| 412 | fixture.controller.move(1); |
| 413 | expect(document.activeElement).toBe(home); |
| 414 | expect(outline(), "F6 onto the only, already focused area shows no ring").toBe("solid"); |
| 415 | }); |
| 416 | }); |