// fixtures/engines/saving.mjs — a document keeps saving while a link it // opened is still loading, in Chromium and in WebKit. // // WebKit refuses every ordinary fetch() and XMLHttpRequest a page starts // while its main frame has a navigation waiting for an answer: the promise // rejects at once with "TypeError: Load failed" and nothing leaves the // browser. A navigation that is never answered, or answered with a // download, leaves the page on screen and editable the whole time. Before // the fix, every save the editor tried in that state was refused that way, // so it said "Trouble saving. Retrying…" and retried four times a second // with requests the server never saw (Paul, Safari, 2026-10-26: the edge // logged no POSTs at all, and a reload cleared it). // // Run by internal/docs/saving_browser_test.go, which serves the link's // target and holds it unanswered, counts the saves that reach it while it // does, and reads the document back from the server afterwards. This // prints, per scenario, one "SAVING RESULT" line with the save states the // page showed and whether what the scenario waited for came, and one // "SAVING TRACE" line with the page's requests (the config's trace // script), which the test prints if the scenario fails. // // Nothing here waits a fixed time to decide anything. Each scenario // watches the page at least as long as it used to (WATCH_MS), then until // the server's view of the document (config.state) holds what it waits // for, for at most CEILING_MS. A fixed three seconds could not tell a slow // pass from a stall (Claude Fable on #94, round 3). const { chromium, webkit } = await import('playwright') const config = JSON.parse(process.env.DOCS_SAVING_CONFIG) const pause = (ms) => new Promise((r) => setTimeout(r, ms)) // WATCH_MS is how long a scenario watches the save line at least: the // fixed pause it replaces, so no state it could see is missed now. // CEILING_MS is how long it waits for the server at most. const WATCH_MS = 3000 const CEILING_MS = 20000 // view is the test server's side of a document: its text, and every save // that reached it. Asked from here, not from the page, which is the one // whose requests are being refused. // A poll that fails is "not yet", as in the WKWebView harness: throwing // here would end every scenario left, with nothing explained, at the one // time the explanation is wanted (Claude Fable on Docs #98). const view = async (sqid) => { try { const r = await fetch(config.origin + config.state + sqid) return r.ok ? await r.json() : null } catch { return null } } const holds = (text, want) => want.split(/\s+/).every((w) => text.includes(w)) // retried is whether the page tried a save again after one was refused: // the moment it decides whether to say "Trouble saving". const retried = (trace) => { const refused = new Set(trace.filter((e) => e.ev === 'refused').map((e) => e.id)) const posts = trace.filter((e) => e.ev === 'start' && e.m === 'POST' && e.u.endsWith('/updates')) return posts.some((p, i) => refused.has(p.id) && i < posts.length - 1) } // settle waits for done(view) from since, at least watchMs and at most // CEILING_MS, and says whether it came and when. async function settle (sqid, since, watchMs, done) { for (;;) { const ms = Date.now() - since const v = ms >= watchMs ? await view(sqid) : null if (v && done(v)) { // The page's console lines from just before are still on their way. await pause(250) return { settled: true, ms } } if (ms >= CEILING_MS) return { settled: false, ms } await pause(100) } } // open is a document's page, ready to type in, with its save states and // requests read from its console from the start: Playwright's own // evaluate waits for a pending navigation to finish, and the held link // never does. async function open (context, sqid) { const page = await context.newPage() const seen = { states: [], trace: [], typed: false } page.on('console', (m) => { const t = m.text() if (t.startsWith('SAVE-STATE ')) seen.states.push(t.slice('SAVE-STATE '.length)) else if (t.startsWith('SAVE-TRACE ')) seen.trace.push(JSON.parse(t.slice('SAVE-TRACE '.length))) else if (t === 'SAVE-TYPED') seen.typed = true }) // A real close asks first only while a resync holds the page; yes. page.on('dialog', (d) => d.accept().catch(() => {})) await page.goto(config.origin + '/d/' + sqid) await page.waitForSelector('html[data-docs-fonts="shown"] [docs-text][contenteditable="true"]', { timeout: 30000 }) await pause(500) return { page, seen } } const report = (engine, seen, got, waiting) => { console.log('SAVING RESULT ' + JSON.stringify({ engine, states: seen.states, settled: got.settled, ms: got.ms, waiting })) console.log('SAVING TRACE ' + JSON.stringify({ engine, trace: seen.trace })) } // heldScenario types into a document with the held link loading, and // waits for the server to hold want: saved while the link loads, given // link, or, given orRetried, a refused save tried again will do. async function heldScenario (context, engine, sqid, typing, { want, link, orRetried }) { const { page, seen } = await open(context, sqid) const since = Date.now() await page.evaluate(`(${config.script})(${JSON.stringify(typing)})`) const waiting = `the server to hold ${JSON.stringify(want.slice(0, 40))}` + (link ? ', saved while the link loaded' : '') + (orRetried ? ', or a refused save tried again' : '') const got = await settle(sqid, since, WATCH_MS, (v) => (holds(v.text, want) && (!link || v.held > 0)) || (orRetried && retried(seen.trace))) report(engine, seen, got, waiting) // Each page done with is closed, and its held link and stream with it: // left open they take the host's connections (six, on HTTP/1.1), and a // later page's unload beacon queues behind them. Playwright's close // skips the unload handlers, so this is harsher than a tab closing: // what the scenario waited for has to have been saved already. await page.close() } for (const [engineName, engine] of Object.entries({ chromium, webkit }).filter(([n]) => !process.env.DOCS_ENGINES_ENGINE || n === process.env.DOCS_ENGINES_ENGINE)) { const browser = await engine.launch({ headless: true }) 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(config.trace) // Three words typed while the link loads: saved while it does. await heldScenario(context, engineName, config.docs[engineName], config.typing, { want: config.want, link: true }) // The same, closed the way a person closes a tab, as soon as the last // word is in: the unload handlers run, and the beacon carries what the // queue has not yet sent. if (config.closed) { const sqid = config.closed[engineName] const { page, seen } = await open(context, sqid) const since = Date.now() await page.evaluate(`(${config.script})(${JSON.stringify(config.typing)})`) const until = Date.now() + CEILING_MS while (!seen.typed && Date.now() < until) await pause(25) const gone = new Promise((resolve) => page.once('close', resolve)) await page.close({ runBeforeUnload: true }) await Promise.race([gone, pause(CEILING_MS)]) const got = await settle(sqid, since, 0, (v) => holds(v.text, config.want)) report(engineName + '-closed', seen, got, `the server to hold ${JSON.stringify(config.want)} after the tab closed`) } // The same with other edits (config.held: [{ name, docs, typing, want, // link, retried }]): a large paste of prose, which goes keepalive once // gzipped, and one too large for a keepalive save even compressed, // which WebKit refuses until the link is answered. Nothing is lost, and // the page must not say a save failed, because none did. for (const held of config.held || []) { await heldScenario(context, engineName + '-' + held.name, held.docs[engineName], held.typing, { want: held.want, link: !!held.link, orRetried: !!held.retried }) } // Typed with no link loading (config.typed: [{ name, docs, text }]): // the server's side is the case, such as a hop that strips a gzipped // save's Content-Encoding. Each text is saved before the next is typed. for (const typed of config.typed || []) { const sqid = typed.docs[engineName] const { page, seen } = await open(context, sqid) await page.evaluate(`(() => { const line = document.querySelector('[estilo-save-status~="floating"]'); new MutationObserver(() => console.log('SAVE-STATE ' + line.getAttribute('data-state'))).observe(line, { attributes: true, attributeFilter: ['data-state'] }) })()`) const got = { settled: true, ms: 0 } let waiting = '' for (const text of typed.text) { const since = Date.now() await page.evaluate(`(${config.type})(${JSON.stringify(text)})`) const one = await settle(sqid, since, 1500, (v) => holds(v.text, text)) got.ms += one.ms if (!one.settled) { got.settled = false waiting = `the server to hold ${JSON.stringify(text.slice(0, 40))}` break } } report(engineName + '-' + typed.name, seen, got, waiting || 'the server to hold each text') await page.close() } // Closing with a large save on the wire: the server holds it, the page // types more behind it and leaves before that is sent, and the unload // beacon must carry what was typed. Keepalive requests and beacons // share one 64KiB quota, so a beacon that repeats the save already on // the wire is refused (Claude Fable on #94). Done once the large save // has been answered and the server holds both. if (config.closing) { const sqid = config.closing[engineName] const { page, seen } = await open(context, sqid) const since = Date.now() await page.evaluate(`(${config.closingScript})(${JSON.stringify(config.closingTyping)})`) await page.waitForURL(config.origin + '/healthz', { timeout: 30000 }) const got = await settle(sqid, since, 0, (v) => !v.largeHeld && holds(v.text, 'Bigpaste Queuedbehind')) report(engineName + '-closing', seen, got, 'the large save to be answered and the server to hold "Bigpaste Queuedbehind"') } } finally { await browser.close() } }