From 9533672e091d3f1d7faa359e627918880489ee7c Mon Sep 17 00:00:00 2001 From: Miguel Palhas Date: Sat, 5 Sep 2026 06:49:48 +0100 Subject: [PATCH] fix(maestro): require real DOM focus before reconciliation re-focuses (#140) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ConnectionPool.reconcileViews re-focused a repaired connection whenever focusedId named it, but a same-tab sidebar preview moves the keyboard into the sidebar without ever calling setFocus (multiview.ts's syncFocus skips it while previewing) — so focusedId keeps naming the pane the operator was in before the preview, even though the DOM doesn't agree. Replacing that pane's connection during exactly that window called onMount({focus:true}) and yanked the keyboard back out of the sidebar mid-navigation. Capture existing.el.contains(document.activeElement) before dispose()/ el.remove() detach it (afterward the browser has already moved activeElement to , too late to tell), and require both that and focusedId===id to re-focus. document.body and a null activeElement both naturally fail .contains(), so "nothing focused" and "focus elsewhere" both correctly skip. Regression tests: 3 new (real-focus requirement, same-tab preview race, descendant focus, body-as-unfocused) — 498 tests total. --- web/src/connections.test.ts | 73 ++++++++++++++++++++++++++++++++++++- web/src/connections.ts | 17 ++++++--- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/web/src/connections.test.ts b/web/src/connections.test.ts index a9c10c6..5d3aff8 100644 --- a/web/src/connections.test.ts +++ b/web/src/connections.test.ts @@ -12,6 +12,7 @@ const disposed: string[] = []; const mounts: { id: string; focus: boolean | undefined }[] = []; function fakeConn(kind: string, id: string) { const el = document.createElement("div"); + el.tabIndex = -1; // focusable via .focus(), like a real input/terminal el.dataset.kind = kind; el.dataset.session = id; return { @@ -261,19 +262,89 @@ describe("ConnectionPool: view resolution races", () => { expect(disposed).toEqual([]); }); - it("re-focuses the fresh connection when the replaced pane was the focused session", () => { + it("re-focuses the fresh connection when the keyboard was actually inside the replaced pane", () => { mockFetch(); const { store, socket } = fakeStore(); const pool = new ConnectionPool({ store }); const pane = document.createElement("div"); + document.body.appendChild(pane); pool.mount(A, pane); pool.setFocus(A); + (pane.firstElementChild as HTMLElement).focus(); + expect(document.activeElement).toBe(pane.firstElementChild); mounts.length = 0; socket.push([session(A, "Structured")]); expect(disposed).toEqual(["attach:" + A]); expect(mounts).toEqual([{ id: A, focus: true }]); + pane.remove(); + }); + + it("does not steal focus from a same-tab sidebar preview even though focusedId still names the previous pane", () => { + // docs/PREVIEW.md: previewing a session already open in the current tab + // moves the keyboard into the sidebar without ever calling setFocus, so + // `focusedId` keeps naming the pane the operator was in before — but the + // DOM disagrees, and the DOM is what must decide. + mockFetch(); + const { store, socket } = fakeStore(); + const pool = new ConnectionPool({ store }); + const pane = document.createElement("div"); + document.body.appendChild(pane); + pool.mount(A, pane); + pool.setFocus(A); + (pane.firstElementChild as HTMLElement).focus(); + + const sidebarInput = document.createElement("input"); + document.body.appendChild(sidebarInput); + sidebarInput.focus(); + expect(document.activeElement).toBe(sidebarInput); + mounts.length = 0; + + socket.push([session(A, "Structured")]); + + expect(disposed).toEqual(["attach:" + A]); + expect(mounts).toEqual([{ id: A, focus: false }]); + pane.remove(); + sidebarInput.remove(); + }); + + it("counts focus on a descendant of the pane's element, not just the element itself", () => { + mockFetch(); + const { store, socket } = fakeStore(); + const pool = new ConnectionPool({ store }); + const pane = document.createElement("div"); + document.body.appendChild(pane); + pool.mount(A, pane); + pool.setFocus(A); + const textarea = document.createElement("textarea"); + (pane.firstElementChild as HTMLElement).appendChild(textarea); + textarea.focus(); + expect(document.activeElement).toBe(textarea); + mounts.length = 0; + + socket.push([session(A, "Structured")]); + + expect(mounts).toEqual([{ id: A, focus: true }]); + pane.remove(); + }); + + it("treats document.body (nothing meaningfully focused) as no active descendant", () => { + mockFetch(); + const { store, socket } = fakeStore(); + const pool = new ConnectionPool({ store }); + const pane = document.createElement("div"); + document.body.appendChild(pane); + pool.mount(A, pane); + pool.setFocus(A); + (document.activeElement as HTMLElement | null)?.blur(); + expect(document.activeElement).toBe(document.body); + mounts.length = 0; + + socket.push([session(A, "Structured")]); + + expect(mounts).toEqual([{ id: A, focus: false }]); + pane.remove(); }); it("does not steal focus for a merely-visible pane that was never actually focused", () => { diff --git a/web/src/connections.ts b/web/src/connections.ts index 426248a..6ca8870 100644 --- a/web/src/connections.ts +++ b/web/src/connections.ts @@ -161,6 +161,11 @@ export class ConnectionPool { const existing = this.cache.get(id)!; const parent = existing.el.parentElement; const wasMounted = this.mounted.has(id); + // Capture before anything below touches the DOM: dispose()/el.remove() + // detach `existing.el`, and once a focused descendant is detached the + // browser moves `document.activeElement` to `` — too late to + // tell whether the keyboard was actually in this pane. + const hadDomFocus = existing.el.contains(document.activeElement); const info: AttachInfo = { name: sessionName(session), command: session.command, view: session.view }; const fresh = this.replace(id, info); existing.el.remove(); // don't rely on dispose() to clear the DOM @@ -172,11 +177,13 @@ export class ConnectionPool { this.hidden.appendChild(fresh.el); } if (wasMounted) { - // `focusedId` only ever names a session the operator actually - // focused — never a merely-visible split neighbour or a preview - // (multiview.ts skips `setFocus` while previewing) — so this is - // exactly "was the keyboard actually in the pane being replaced." - fresh.onMount({ focus: this.focusedId === id }); + // `focusedId` names the session the pool was last told to focus — + // but a same-tab sidebar preview moves the keyboard into the + // sidebar without clearing it (multiview.ts skips `setFocus` while + // previewing), so `focusedId` alone can point at a pane the + // keyboard already left. Only re-focus when the DOM agrees it + // never did. + fresh.onMount({ focus: this.focusedId === id && hadDomFocus }); } else { fresh.onUnmount(); }