// fixtures/engines/diverged.mjs — every change a person makes while a // resync is pending is kept, in Chromium and in WebKit, through the real // editor (feat/replay-while-diverged; Claude Fable's rv-general-probe.mjs // made into keys and clipboard events). Paul ranks a lost edit worse than // a flash. // // Run by internal/docs/diverged_engines_browser_test.go with the same // DOCS_ENGINES_CONFIG as first-reveal.mjs; docs.diverged holds a fresh // document per engine and case ("First paragraph." / "Second // paragraph."). Slides has the same script. // // Each case: the editor's first save (the refused change, made with real // keys) is answered 413, and the rebuild's fetch of the log is held while // the person goes on editing. Then it is let go. The page must hold what // the case wants, a line RESULT {...} carries it for the Go test to hold // the server to, and one Cmd/Ctrl-Z must take back the last change only — // the carried changes undo in the steps they were made in — the server // following. const { chromium, webkit } = await import('playwright') const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG) const pause = (ms) => new Promise((r) => setTimeout(r, ms)) const cases = [ { name: 'mixed edits: on the refused text, a paste elsewhere, a delete across server, refused and new text, typing elsewhere', refuse: [['type', 1, 'end', 'x']], edits: [ ['type', 1, 'end', 'a'], ['paste', 0, 0, 'ELSEWHERE-'], ['type', 1, 'end', 'b'], ['select', 1, 7, 'end'], ['key', 'Backspace'], // A second later, so it is its own undo step, as it was when made. ['wait', 900], ['type', 0, 'end', 'c'], ], want: ['ELSEWHERE-First paragraph.c', 'Second '], undone: ['ELSEWHERE-First paragraph.', 'Second '], }, { name: 'typing on the refused text, a paste elsewhere, more typing after it: all kept', refuse: [['type', 1, 'end', 'x']], edits: [ ['type', 1, 'end', 'KEPT'], ['paste', 0, 0, 'ELSEWHERE-'], ['wait', 900], ['type', 1, 'end', ' more'], ], want: ['ELSEWHERE-First paragraph.', 'Second paragraph.KEPT more'], undone: ['ELSEWHERE-First paragraph.', 'Second paragraph.KEPT'], }, { name: 'an undo in the middle: typing on the refused text, Cmd-Z, then typing elsewhere', refuse: [['type', 1, 'end', 'x']], edits: [ ['wait', 700], ['type', 1, 'end', 'a'], ['undo'], ['wait', 700], ['type', 0, 'end', 'b'], ], want: ['First paragraph.b', 'Second paragraph.'], undone: ['First paragraph.', 'Second paragraph.'], }, { name: 'a change that cannot be kept in the middle: typing in the paragraph a refused Enter made, then elsewhere, then in it again', refuse: [['press', 1, 'end', 'Enter']], edits: [ ['type', 2, 0, 'INSIDE'], ['type', 0, 'end', ' after'], ['type', 2, 'end', '!'], ], want: ['First paragraph. after', 'Second paragraph.'], undone: ['First paragraph.', 'Second paragraph.'], }, ] const instrument = () => { const f = window.fetch window.__posts = [] window.__errors = [] addEventListener('error', (e) => window.__errors.push(String(e.message))) addEventListener('unhandledrejection', (e) => window.__errors.push(String(e.reason))) window.__hold = true window.__release = () => { window.__hold = false; if (window.__go) window.__go() } window.fetch = async function (u, o) { const post = String(u).endsWith('/updates') && o && o.method === 'POST' if (post && !window.refused) { await new Promise((r) => setTimeout(r, 150)) window.refused = true window.__posts.push(413) return new Response('', { status: 413 }) } // The rebuild's fetch of the whole log (the live cursor's carries since=). if (window.refused && !post && String(u).includes('/updates') && !String(u).includes('since=') && window.__hold) await new Promise((r) => { window.__go = r }) const r = await f.call(this, u, o) if (post) window.__posts.push(r.status) return r } } const place = ([i, a, b]) => { const p = document.querySelectorAll('[docs-text] p')[i] document.querySelector('[docs-text]').focus() const at = (off) => { const w = document.createTreeWalker(p, NodeFilter.SHOW_TEXT) let n let last = null let left = off === 'end' ? Infinity : off while ((n = w.nextNode())) { last = n if (left <= n.data.length) return [n, left] left -= n.data.length } return last ? [last, last.data.length] : [p, 0] } const r = document.createRange() const [sn, so] = at(a) r.setStart(sn, so) if (b !== undefined) { const [en, eo] = at(b) r.setEnd(en, eo) } else r.collapse(true) getSelection().removeAllRanges() getSelection().addRange(r) } const paste = (html) => { const d = new DataTransfer() d.setData('text/html', html) d.setData('text/plain', html) document.activeElement.dispatchEvent(new ClipboardEvent('paste', { clipboardData: d, bubbles: true, cancelable: true })) } // act runs one step. ProseMirror reads a moved caret on the next // selectionchange, so every move is followed by a moment before the key. async function act (page, [what, ...args]) { if (what === 'wait') { await pause(args[0]) return } if (what === 'undo') { await page.keyboard.press('ControlOrMeta+z') await pause(200) return } if (what === 'select') { await page.evaluate(place, [args[0], args[1], args[2]]) await pause(40) } else if (what === 'key') { await page.keyboard.press(args[0]) } else { await page.evaluate(place, [args[0], args[1]]) await pause(40) if (what === 'type') await page.keyboard.type(args[2]) else if (what === 'press') await page.keyboard.press(args[2]) else if (what === 'paste') await page.evaluate(paste, args[2]) } await pause(80) } const paragraphs = () => [...document.querySelectorAll('[docs-text] p')].map((p) => p.textContent) const same = (a, b) => JSON.stringify(a) === JSON.stringify(b) const failures = [] 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(engineName === 'chromium' ? { headless: true, channel: 'chromium' } : { headless: true }) for (const c of cases) { const label = `${engineName}, ${c.name}` const deck = config.docs.diverged.shift() const context = await browser.newContext({ viewport: { width: 1280, height: 800 }, deviceScaleFactor: 1 }) await context.addCookies([ ...config.cookies.map((k) => ({ name: k.Name, value: k.Value, url: config.origin, httpOnly: k.HttpOnly, sameSite: 'Lax' })), { name: 'docs-view', value: 'paged', url: config.origin }, ]) try { const page = await context.newPage() await page.goto(config.origin + '/d/' + deck) await page.waitForSelector('html[data-docs-fonts="shown"] [docs-text][contenteditable="true"]', { timeout: 15000 }) await pause(300) await page.evaluate(instrument) for (const step of c.refuse) await act(page, step) await page.waitForFunction(() => window.refused === true, null, { timeout: 5000 }) await pause(50) for (const step of c.edits) await act(page, step) const diverged = await page.evaluate(() => !!window.__go || window.__hold) if (!diverged) failures.push(`${label}: the rebuild was not still pending during the edits, so this proves nothing`) await page.evaluate(() => window.__release()) await pause(3500) const got = await page.evaluate(paragraphs) if (!same(got, c.want)) failures.push(`${label}: the page holds ${JSON.stringify(got)}, want ${JSON.stringify(c.want)}`) // One undo takes back the last change, and only it. await act(page, ['type', 0, 'end', '']) await page.keyboard.press('ControlOrMeta+z') await pause(2000) const undone = await page.evaluate(paragraphs) if (!same(undone, c.undone)) failures.push(`${label}: after one undo the page holds ${JSON.stringify(undone)}, want ${JSON.stringify(c.undone)}`) const errors = await page.evaluate(() => window.__errors) if (errors.length) failures.push(`${label}: errors ${JSON.stringify(errors)}`) console.log('RESULT ' + JSON.stringify({ label, deck, want: undone })) } catch (err) { failures.push(`${label}: ${err.message}`) } await context.close() } await browser.close() } if (failures.length) { console.error(failures.join('\n')) process.exit(1) } console.log('every change made while the resync was pending was kept, in both engines')