Skip to content

Commit 0379df3

Browse files
committed
test(desktop): instrument the Escape-on-Leave-dialog step (temporary)
1 parent 51205e9 commit 0379df3

1 file changed

Lines changed: 85 additions & 19 deletions

File tree

‎apps/desktop/e2e/browser-page-dialogs.spec.ts‎

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ import { useBrowserSessionStore as store } from ${JSON.stringify(join(SIM_DIR, '
5151
import { BrowserPageDialogModal } from ${JSON.stringify(join(BROWSER_SESSION_COMPONENTS, 'browser-page-dialog.tsx'))};
5252
import { useBrowserPanelOcclusion } from ${JSON.stringify(join(BROWSER_SESSION_COMPONENTS, 'browser-panel-occlusion.ts'))};
5353
const scope = ${JSON.stringify(SCOPE)};
54+
const diag = (globalThis.escapeDiag = []);
55+
const stamp = (what, extra) => diag.push({ what, t: Math.round(performance.now()), active: document.activeElement?.textContent?.slice(0, 20) ?? null, hasFocus: document.hasFocus(), ...extra });
56+
const origAdd = window.addEventListener.bind(window);
57+
const origRemove = window.removeEventListener.bind(window);
58+
window.addEventListener = (type, fn, opts) => { if (type === 'keydown' && opts === true) stamp('lock-on'); return origAdd(type, fn, opts) };
59+
window.removeEventListener = (type, fn, opts) => { if (type === 'keydown' && opts === true) stamp('lock-off'); return origRemove(type, fn, opts) };
60+
origAdd('keydown', (e) => { if (e.key === 'Escape') stamp('esc-window-capture') }, true);
61+
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') stamp('esc-document-capture') }, true);
62+
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') stamp('esc-document-bubble', { prevented: e.defaultPrevented }) });
63+
document.addEventListener('focusin', () => stamp('focusin'), true);
5464
const api = globalThis.simDesktop.browserAgent;
5565
api.onPageState(state => store.getState().setPageState(state));
5666
store.subscribe(state => {
@@ -288,26 +298,82 @@ createRoot(document.getElementById('root')).render(createElement(Fixture));`,
288298
})
289299

290300
await test.step('Escape keeps the draft and returns typing to the page', async () => {
291-
await userInput(app, form, '#draft')
292-
// The click is queued input: reloading before the page takes focus lets that focus land
293-
// after the modal's, leaving the Sim window without keyboard focus.
294-
await expect.poll(() => focusedPageUrl(app)).toBe(form)
295-
await act({ action: 'reload' })
296-
await expect(shell.getByRole('button', { name: 'Stay', exact: true })).toBeFocused()
297-
const shown = await pageDialog()
298-
await shell.keyboard.press('Escape')
299-
await expect
300-
.poll(pageDialog, {
301-
message: `dialog ${shown?.requestId} was showing when Escape was pressed`,
301+
const results: unknown[] = []
302+
await app.evaluate(({ webContents }, url) => {
303+
const g = globalThis as unknown as { mainEsc: unknown[] }
304+
g.mainEsc = []
305+
for (const contents of webContents.getAllWebContents()) {
306+
contents.on('before-input-event', (_event, input) => {
307+
if (input.key === 'Escape')
308+
g.mainEsc.push({
309+
url: contents.getURL().slice(0, 40),
310+
type: input.type,
311+
loading: contents.isLoadingMainFrame(),
312+
t: Date.now(),
313+
})
314+
})
315+
}
316+
void url
317+
}, form)
318+
for (let attempt = 0; attempt < 8; attempt++) {
319+
await shell.evaluate(() => {
320+
;(globalThis as unknown as { escapeDiag: unknown[] }).escapeDiag.length = 0
321+
})
322+
await userInput(app, form, '#draft')
323+
await expect.poll(() => focusedPageUrl(app)).toBe(form)
324+
await act({ action: 'reload' })
325+
await expect(shell.getByRole('button', { name: 'Stay', exact: true })).toBeFocused()
326+
const shown = await pageDialog()
327+
const before = await app.evaluate(({ webContents, BrowserWindow }, url) => {
328+
const page = webContents.getAllWebContents().find((c) => c.getURL() === url)
329+
;(globalThis as unknown as { mainEsc: unknown[] }).mainEsc.length = 0
330+
return {
331+
focusedContents: webContents.getFocusedWebContents()?.getURL()?.slice(0, 40) ?? null,
332+
focusedWindow: Boolean(BrowserWindow.getFocusedWindow()),
333+
pageLoading: page?.isLoading(),
334+
pageLoadingMain: page?.isLoadingMainFrame(),
335+
t: Date.now(),
336+
}
337+
}, form)
338+
await shell.evaluate(() => {
339+
;(globalThis as unknown as { escapeDiag: { what: string }[] }).escapeDiag.push({
340+
what: 'pre-press',
341+
} as never)
302342
})
303-
.toBeNull()
304-
await expect.poll(() => focusedPageUrl(app)).toBe(form)
305-
await app.evaluate(({ webContents }) => {
306-
const contents = webContents.getFocusedWebContents()
307-
if (!contents) throw new Error('Missing focused page')
308-
contents.sendInputEvent({ type: 'char', keyCode: 'x' })
309-
})
310-
await expect.poll(() => draft()).toBe('xdraft')
343+
await shell.keyboard.press('Escape')
344+
let cleared = true
345+
try {
346+
await expect.poll(pageDialog, { timeout: 5000 }).toBeNull()
347+
} catch {
348+
cleared = false
349+
}
350+
const after = await pageDialog()
351+
const renderer = await shell.evaluate(
352+
() => (globalThis as unknown as { escapeDiag: unknown[] }).escapeDiag
353+
)
354+
const main = await app.evaluate(
355+
() => (globalThis as unknown as { mainEsc: unknown[] }).mainEsc
356+
)
357+
results.push({
358+
attempt,
359+
cleared,
360+
shown: shown?.requestId,
361+
after: after?.requestId ?? null,
362+
before,
363+
renderer,
364+
main,
365+
})
366+
if (!cleared) {
367+
await shell.getByRole('button', { name: 'Stay', exact: true }).click()
368+
await expect.poll(pageDialog).toBeNull()
369+
}
370+
await expect.poll(() => focusedPageUrl(app)).toBe(form)
371+
}
372+
console.log(`ESCAPE_DIAG ${JSON.stringify(results)}`)
373+
const { writeFileSync, mkdirSync } = await import('node:fs')
374+
mkdirSync('test-results', { recursive: true })
375+
writeFileSync('test-results/escape-diag.json', JSON.stringify(results, null, 2))
376+
expect(results.filter((r) => !(r as { cleared: boolean }).cleared)).toEqual([])
311377
})
312378

313379
await test.step('Leave lets the navigation through without asking again', async () => {

0 commit comments

Comments
 (0)