diff --git a/packages/webui/src/public/bus.js b/packages/webui/src/public/bus.js index b546af884..b2d498976 100644 --- a/packages/webui/src/public/bus.js +++ b/packages/webui/src/public/bus.js @@ -2246,5 +2246,9 @@ window.addEventListener('hashchange', e => render(!agentOnly(e.oldURL, e.newURL))); $('refresh').addEventListener('click', () => render(false)); $('pause').addEventListener('click', () => setTimeout(schedule)); // after app.js flips aria-pressed - render(false).then(() => { if (!$('bus-view').hidden) { const h1 = $('bus-view').querySelector('h1'); if (h1 && location.hash.startsWith('#/')) h1.focus(); } }); + // #1873 triage-followups: the first render ends by focusing the page's heading. A drawer or dialog the + // reader opened before then keeps its focus: the Triage drawer, an agent drawer, the narrow-screen + // menu or any open dialog. + const holdsFocus = () => { const a = document.activeElement; return !!a && (!!a.closest('.ws-drawer:not([hidden]), [data-ws-drawer], dialog[open], [role="dialog"], [role="alertdialog"]') || (drawerOpen && nav.contains(a))); }; + render(false).then(() => { if (!$('bus-view').hidden && !holdsFocus()) { const h1 = $('bus-view').querySelector('h1'); if (h1 && location.hash.startsWith('#/')) h1.focus(); } }); })(); diff --git a/packages/webui/tests/agent-drawer-browser.test.mjs b/packages/webui/tests/agent-drawer-browser.test.mjs index 31b6d93c3..ea2564ef6 100644 --- a/packages/webui/tests/agent-drawer-browser.test.mjs +++ b/packages/webui/tests/agent-drawer-browser.test.mjs @@ -577,3 +577,21 @@ test('at 390 wide the drawer is a full-width sheet', async t => { assert.equal(box[1], box[2]); assert.ok(box[3] <= box[2], `no horizontal page scroll: ${box[3]} > ${box[2]}`); }); + +// #1873 triage-followups: the page focuses its heading when its first render ends. On load the page +// starts a second render that reads GET /api/workspace again, so when the first render's read is slow +// the second draws the rows first and a reader can open a drawer before the first render ends. Its late +// end must not take the drawer's focus. The page holds the first GET /api/workspace until __go. +const HOLD_FIRST = `(() => { const f = window.fetch; let first = true; window.__firstDone = false; window.fetch = (u, o) => { if (first && String(u?.url ?? u).includes('/api/workspace')) { first = false; return new Promise(r => { const t = setInterval(() => { if (window.__go) { clearInterval(t); r(f(u, o).finally(() => setTimeout(() => { window.__firstDone = true; }, 0))); } }, 20); }); } return f(u, o); }; })()`; + +test('triage-followups: a drawer opened before the first render ends keeps focus when that render ends', async t => { + const { b, wait } = await setup(t, { scripts: [HOLD_FIRST] }); + await wait(`document.querySelector(${JSON.stringify(rowLink('acme:coder'))})`); + assert.notEqual(await b.evaluate('document.activeElement?.tagName'), 'H1', 'the first render has not ended'); + await b.evaluate(`document.querySelector(${JSON.stringify(rowLink('acme:coder'))}).click()`); + await wait(`${open('acme:coder')} && ${drawer('acme:coder')}.contains(document.activeElement)`); + await b.evaluate('window.__go = true'); + await wait('window.__firstDone'); + await new Promise(r => setTimeout(r, 400)); + assert.equal(await b.evaluate(`${drawer('acme:coder')}.contains(document.activeElement)`), true, `focus stays in the drawer, not on ${await b.evaluate('document.activeElement?.tagName')}`); +}); diff --git a/packages/webui/tests/triage-drawer-browser.test.mjs b/packages/webui/tests/triage-drawer-browser.test.mjs index dca0f8965..65f899237 100644 --- a/packages/webui/tests/triage-drawer-browser.test.mjs +++ b/packages/webui/tests/triage-drawer-browser.test.mjs @@ -409,3 +409,31 @@ test('item 5: markup and control characters in every field stay text: no element await sleep(500); assert.equal(await f.b.evaluate('window.__pwned ?? null'), null); }); + +// #1873 triage-followups: the page focuses its heading when its first render ends. A reader who opened +// the drawer before then keeps focus in the drawer. The page holds GET /api/workspace, which the first +// render waits on, until the drawer has opened and taken focus. +const HOLD_WORKSPACE = `(() => { const f = window.fetch; window.__held = 0; window.fetch = (u, o) => { if (String(u?.url ?? u).includes('/api/workspace') && !window.__go) { window.__held += 1; return new Promise(r => { const t = setInterval(() => { if (window.__go) { clearInterval(t); r(f(u, o)); } }, 20); }); } return f(u, o); }; })()`; + +test('triage-followups: a drawer opened before the first render ends keeps focus when the render ends', async t => { + const f = await setup(t, { init: HOLD_WORKSPACE }); + await f.wait('document.querySelector("#triage-open") && window.__held > 0'); + const heading = () => f.b.evaluate('document.querySelector("#bus-view h1")?.textContent ?? null'); + await sleep(300); + assert.equal(await heading(), 'Reading…', 'the first render has not ended'); + await f.click('#triage-open'); + await f.wait('document.querySelector("#triage-drawer")?.contains(document.activeElement)'); + await f.b.evaluate('window.__go = true'); + await f.wait('document.querySelector("#bus-view h1")?.textContent === "Dashboard"'); + await sleep(400); + assert.equal(await f.b.evaluate('document.querySelector("#triage-drawer").contains(document.activeElement)'), true, `focus stays in the drawer, not on ${await f.b.evaluate('document.activeElement?.tagName')}`); + // Escape still closes it and puts focus back on the entry. + await f.b.call('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 }); + await f.wait('document.querySelector("#triage-drawer").hidden'); + assert.equal(await f.b.evaluate('document.activeElement?.id'), 'triage-open'); +}); + +test('triage-followups: with no drawer open the first render still focuses the heading', async t => { + const f = await setup(t); + await f.wait('document.activeElement?.tagName === "H1"'); +});