// fixtures/engines/leaving.mjs — a page left the moment it has opened its // live stream lets go of it, and gets it back, caught up, when it is // shown again from the back/forward cache (Docs #86, dadf9c1, adopted: // each page kept in that cache held its EventSource open, and after five // the next page stalled on HTTP/1.1's per-host connection limit). // Chromium, with its back/forward cache on (Playwright turns it off by // default), and with requestAnimationFrame never firing, as in a hidden // page. // // Driven by internal/slidestest/leaving_browser_test.go over stdin and // stdout, once per surface that streams (the editor, the projector, the // follow view, the presenter view, the remote): this prints "LEAVING // LEFT" once the page is left; the test checks the server's streams, // changes something while the page is away, and answers "BACK"; this goes // back and prints "LEAVING SHOWN {persisted, caughtUp}"; the test checks // the stream is back and answers "DONE". import { createInterface } from 'node:readline' const { chromium } = await import('playwright') const config = JSON.parse(process.env.SLIDES_LEAVING_CONFIG) const lines = createInterface({ input: process.stdin })[Symbol.asyncIterator]() const next = async () => (await lines.next()).value const browser = await chromium.launch({ channel: 'chromium', headless: true, ignoreDefaultArgs: ['--disable-back-forward-cache'] }) try { const context = await browser.newContext() await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) await context.addInitScript(() => { // A hidden page runs no frames. window.requestAnimationFrame = () => 0 window.__shown = [] addEventListener('pageshow', (e) => { window.__shown.push(e.persisted) if (e.persisted) sessionStorage.setItem('persisted', '1') }) // Leave the moment the page opens its stream. const ES = window.EventSource window.EventSource = class extends ES { constructor(...args) { super(...args) if (!sessionStorage.getItem('left')) { sessionStorage.setItem('left', '1') // Once the load event is over: a navigation a script starts // before it is a replacement in Chromium, and there would be // no entry to go back to (the presenter and the remote open // their streams before their pictures have loaded). const leave = () => setTimeout(() => location.assign('/'), 0) if (document.readyState === 'complete') leave() else addEventListener('load', leave, { once: true }) } } } }) const page = await context.newPage() await page.goto(config.origin + '/') await page.goto(config.origin + config.path) await page.waitForURL(config.origin + '/', { timeout: 30000 }) await page.waitForLoadState('load') console.log('LEAVING LEFT') if ((await next()) !== 'BACK') throw new Error('expected BACK') await page.goBack({ waitUntil: 'commit' }) const caughtUp = await page .waitForFunction(config.caughtUp, null, { timeout: 15000, polling: 100 }) .then(() => true, () => false) // A surface that follows by reloading has a new document by now; the // one shown from the cache recorded itself before it reloaded. const persisted = await page.evaluate(() => window.__shown.includes(true) || sessionStorage.getItem('persisted') === '1').catch(() => false) if (!caughtUp) { const state = await page.evaluate(() => ({ href: location.href, shown: window.__shown, storage: { ...sessionStorage }, html: document.body ? document.body.innerHTML.slice(0, 300) : '' })).catch((e) => String(e)) console.log('LEAVING STATE ' + JSON.stringify(state)) } console.log('LEAVING SHOWN ' + JSON.stringify({ persisted, caughtUp })) await next() } finally { await browser.close() }