oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/engines/leaving.mjs — a document left before the editor has
2// drawn a frame lets go of its live stream, and gets it back, caught up,
3// when it is shown again from the back/forward cache (branch review on
4// #86). Chromium, with its back/forward cache on (Playwright turns it off
5// by default), and with requestAnimationFrame never firing, as in a
6// hidden page: the editor's own frame waits never finish, so nothing
7// registered after them can be relied on.
8//
9// Driven by internal/docs/leaving_browser_test.go over stdin and stdout:
10// this prints "LEAVING LEFT" once the page is left; the test checks the
11// server's streams, posts a collaborator's edit, and answers "BACK"; this
12// goes back and prints "LEAVING SHOWN {persisted, caughtUp}"; the test
13// checks the stream is back and answers "DONE".
14
15import { createInterface } from 'node:readline'
16
17const { chromium } = await import('playwright')
18const config = JSON.parse(process.env.DOCS_LEAVING_CONFIG)
19const lines = createInterface({ input: process.stdin })[Symbol.asyncIterator]()
20const next = async () => (await lines.next()).value
21
22const browser = await chromium.launch({ channel: 'chromium', headless: true, ignoreDefaultArgs: ['--disable-back-forward-cache'] })
23try {
24 const context = await browser.newContext()
25 await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })))
26 await context.addInitScript(() => {
27 // A hidden page runs no frames.
28 window.requestAnimationFrame = () => 0
29 window.__shown = []
30 addEventListener('pageshow', (e) => window.__shown.push(e.persisted))
31 // Leave the moment the editor opens its stream.
32 const ES = window.EventSource
33 window.EventSource = class extends ES {
34 constructor(...args) {
35 super(...args)
36 if (!sessionStorage.getItem('left')) {
37 sessionStorage.setItem('left', '1')
38 setTimeout(() => location.assign('/'), 0)
39 }
40 }
41 }
42 })
43 const page = await context.newPage()
44 await page.goto(config.origin + '/')
45 await page.goto(config.origin + '/d/' + config.doc)
46 await page.waitForURL(config.origin + '/', { timeout: 30000 })
47 await page.waitForLoadState('load')
48 console.log('LEAVING LEFT')
49 if ((await next()) !== 'BACK') throw new Error('expected BACK')
50 await page.goBack({ waitUntil: 'commit' })
51 const caughtUp = await page
52 .waitForFunction((text) => (document.querySelector('[docs-text]') || { textContent: '' }).textContent.includes(text), config.remote, { timeout: 15000, polling: 100 })
53 .then(() => true, () => false)
54 const persisted = await page.evaluate(() => window.__shown.includes(true))
55 console.log('LEAVING SHOWN ' + JSON.stringify({ persisted, caughtUp }))
56 await next()
57} finally {
58 await browser.close()
59}
60