| 1 | // fixtures/engines/leaving.mjs — a page left the moment it has opened its |
| 2 | // live stream lets go of it, and gets it back, caught up, when it is |
| 3 | // shown again from the back/forward cache (Docs #86, dadf9c1, adopted: |
| 4 | // each page kept in that cache held its EventSource open, and after five |
| 5 | // the next page stalled on HTTP/1.1's per-host connection limit). |
| 6 | // Chromium, with its back/forward cache on (Playwright turns it off by |
| 7 | // default), and with requestAnimationFrame never firing, as in a hidden |
| 8 | // page. |
| 9 | // |
| 10 | // Driven by internal/slidestest/leaving_browser_test.go over stdin and |
| 11 | // stdout, once per surface that streams (the editor, the projector, the |
| 12 | // follow view, the presenter view, the remote): this prints "LEAVING |
| 13 | // LEFT" once the page is left; the test checks the server's streams, |
| 14 | // changes something while the page is away, and answers "BACK"; this goes |
| 15 | // back and prints "LEAVING SHOWN {persisted, caughtUp}"; the test checks |
| 16 | // the stream is back and answers "DONE". |
| 17 | |
| 18 | import { createInterface } from 'node:readline' |
| 19 | |
| 20 | const { chromium } = await import('playwright') |
| 21 | const config = JSON.parse(process.env.SLIDES_LEAVING_CONFIG) |
| 22 | const lines = createInterface({ input: process.stdin })[Symbol.asyncIterator]() |
| 23 | const next = async () => (await lines.next()).value |
| 24 | |
| 25 | const browser = await chromium.launch({ channel: 'chromium', headless: true, ignoreDefaultArgs: ['--disable-back-forward-cache'] }) |
| 26 | try { |
| 27 | const context = await browser.newContext() |
| 28 | await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) |
| 29 | await context.addInitScript(() => { |
| 30 | // A hidden page runs no frames. |
| 31 | window.requestAnimationFrame = () => 0 |
| 32 | window.__shown = [] |
| 33 | addEventListener('pageshow', (e) => { |
| 34 | window.__shown.push(e.persisted) |
| 35 | if (e.persisted) sessionStorage.setItem('persisted', '1') |
| 36 | }) |
| 37 | // Leave the moment the page opens its stream. |
| 38 | const ES = window.EventSource |
| 39 | window.EventSource = class extends ES { |
| 40 | constructor(...args) { |
| 41 | super(...args) |
| 42 | if (!sessionStorage.getItem('left')) { |
| 43 | sessionStorage.setItem('left', '1') |
| 44 | // Once the load event is over: a navigation a script starts |
| 45 | // before it is a replacement in Chromium, and there would be |
| 46 | // no entry to go back to (the presenter and the remote open |
| 47 | // their streams before their pictures have loaded). |
| 48 | const leave = () => setTimeout(() => location.assign('/'), 0) |
| 49 | if (document.readyState === 'complete') leave() |
| 50 | else addEventListener('load', leave, { once: true }) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | }) |
| 55 | const page = await context.newPage() |
| 56 | await page.goto(config.origin + '/') |
| 57 | await page.goto(config.origin + config.path) |
| 58 | await page.waitForURL(config.origin + '/', { timeout: 30000 }) |
| 59 | await page.waitForLoadState('load') |
| 60 | console.log('LEAVING LEFT') |
| 61 | if ((await next()) !== 'BACK') throw new Error('expected BACK') |
| 62 | await page.goBack({ waitUntil: 'commit' }) |
| 63 | const caughtUp = await page |
| 64 | .waitForFunction(config.caughtUp, null, { timeout: 15000, polling: 100 }) |
| 65 | .then(() => true, () => false) |
| 66 | // A surface that follows by reloading has a new document by now; the |
| 67 | // one shown from the cache recorded itself before it reloaded. |
| 68 | const persisted = await page.evaluate(() => window.__shown.includes(true) || sessionStorage.getItem('persisted') === '1').catch(() => false) |
| 69 | if (!caughtUp) { |
| 70 | 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)) |
| 71 | console.log('LEAVING STATE ' + JSON.stringify(state)) |
| 72 | } |
| 73 | console.log('LEAVING SHOWN ' + JSON.stringify({ persisted, caughtUp })) |
| 74 | await next() |
| 75 | } finally { |
| 76 | await browser.close() |
| 77 | } |
| 78 | |