// fixtures/engines/leaving.mjs — a document left before the editor has // drawn a frame lets go of its live stream, and gets it back, caught up, // when it is shown again from the back/forward cache (branch review on // #86). Chromium, with its back/forward cache on (Playwright turns it off // by default), and with requestAnimationFrame never firing, as in a // hidden page: the editor's own frame waits never finish, so nothing // registered after them can be relied on. // // Driven by internal/docs/leaving_browser_test.go over stdin and stdout: // this prints "LEAVING LEFT" once the page is left; the test checks the // server's streams, posts a collaborator's edit, 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.DOCS_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)) // Leave the moment the editor opens its stream. const ES = window.EventSource window.EventSource = class extends ES { constructor(...args) { super(...args) if (!sessionStorage.getItem('left')) { sessionStorage.setItem('left', '1') setTimeout(() => location.assign('/'), 0) } } } }) const page = await context.newPage() await page.goto(config.origin + '/') await page.goto(config.origin + '/d/' + config.doc) 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((text) => (document.querySelector('[docs-text]') || { textContent: '' }).textContent.includes(text), config.remote, { timeout: 15000, polling: 100 }) .then(() => true, () => false) const persisted = await page.evaluate(() => window.__shown.includes(true)) console.log('LEAVING SHOWN ' + JSON.stringify({ persisted, caughtUp })) await next() } finally { await browser.close() }