// fixtures/engines/undo.mjs — an undo or a redo made while a resync is // pending ends as the person saw it, in Chromium and in WebKit, through // the real editor (fix/undo-redo-while-diverged; Claude Fable's seventeen // cases on #29/#90, rv-undo-delete-probe.mjs, made into keys). // // Run by internal/docs/undo_engines_browser_test.go with the same // DOCS_ENGINES_CONFIG as first-reveal.mjs; docs.undo holds a fresh // document per engine and case ("First paragraph." / "Second // paragraph."). Slides has the same script. // // Each case: an "x" typed at the end of the second paragraph is refused // (its save answered 413) and the rebuild's fetch of the log held while // the person types, deletes, undoes and redoes; then it is let go. The // page must hold what the case wants — the tab's own text, the refused x // gone unless an undo or a redo put it back — and a line RESULT {...} // carries it for the Go test to hold the server to. Cmd/Ctrl-Z is undo, // Cmd/Ctrl-Shift-Z redo; a pause of 700 ms puts what follows in an undo // step of its own. 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 T = ['wait', 700] const on = (s) => ['type', 1, 'end', s] const back = (n) => [['select', 1, -n, 'end'], ['key', 'Backspace']] const U = ['undo'] const R = ['redo'] const F = 'First paragraph.' const cases = [ { name: 'A: "abc" on the x; Backspace; undo the Backspace', edits: [on('abc'), T, ...back(1), T, U], want: [F, 'Second paragraph.abc'] }, { name: 'B: "a" on the x; select it, type "b"; undo', edits: [on('a'), T, ['select', 1, -1, 'end'], ['keys', 'b'], T, U], want: [F, 'Second paragraph.a'] }, { name: 'C: "a" on the x; Backspace; undo', edits: [on('a'), T, ...back(1), T, U], want: [F, 'Second paragraph.a'] }, { name: 'D: "a" on the x; Backspace; undo; "b" typed', edits: [on('a'), T, ...back(1), T, U, T, on('b')], want: [F, 'Second paragraph.ab'] }, { name: 'E: "a" on the x; Backspace; undo; redo', edits: [on('a'), T, ...back(1), T, U, R], want: [F, 'Second paragraph.'] }, { name: 'F: "a" elsewhere; Backspace; undo', edits: [['type', 0, 'end', 'a'], T, ['select', 0, -1, 'end'], ['key', 'Backspace'], T, U], want: ['First paragraph.a', 'Second paragraph.'] }, { name: 'G: "a" on the x; undo; redo; undo; redo', edits: [on('a'), T, U, R, U, R], want: [F, 'Second paragraph.a'] }, { name: 'H: "a" on the x; "b"; undo; undo; redo; redo; "c"', edits: [on('a'), T, on('b'), T, U, U, R, R, T, on('c')], want: [F, 'Second paragraph.abc'] }, { name: 'I: "a" on the x; Backspace over "xa"; undo', edits: [on('a'), T, ...back(2), T, U], want: [F, 'Second paragraph.xa'] }, { name: 'J: "a" on the x; Backspace over ".xa"; undo', edits: [on('a'), T, ...back(3), T, U], want: [F, 'Second paragraph.xa'] }, { name: 'K: the x and "a" one undo step; undo; redo', quick: true, edits: [on('a'), T, U, R], want: [F, 'Second paragraph.xa'] }, { name: 'L: "a" on the x; undo; "b"; undo; redo', edits: [on('a'), T, U, T, on('b'), T, U, R], want: [F, 'Second paragraph.b'] }, { name: 'M: two words typed on the x; the first deleted; undo', edits: [on(' one two'), T, ['select', 1, -8, -4], ['key', 'Backspace'], T, U], want: [F, 'Second paragraph. one two'] }, { name: 'N: "a" on the x; undo', edits: [on('a'), T, U], want: [F, 'Second paragraph.'] }, { name: 'O: "a" on the x; undo; "b"', edits: [on('a'), T, U, T, on('b')], want: [F, 'Second paragraph.b'] }, { name: 'P: "a" on the x; "b" elsewhere; undo; undo', edits: [on('a'), T, ['type', 0, 'end', 'b'], T, U, U], want: [F, 'Second paragraph.'] }, { name: 'Q: "a" on the x; undo; redo; "c"', edits: [on('a'), T, U, R, T, on('c')], want: [F, 'Second paragraph.ac'] }, ] const instrument = () => { const f = window.fetch window.__posts = [] window.__inflight = 0 window.__lastPost = performance.now() 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; window.__lastPost = performance.now(); 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) { if (!window.__quick) 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 }) if (post) { window.__inflight++; window.__lastPost = performance.now() } try { const r = await f.call(this, u, o) if (post) window.__posts.push(r.status) return r } finally { if (post) { window.__inflight--; window.__lastPost = performance.now() } } } } 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 if (typeof off === 'number' && off < 0) off = p.textContent.length + off 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' || what === 'redo') { await page.keyboard.press(what === 'undo' ? 'ControlOrMeta+z' : 'ControlOrMeta+Shift+z') await pause(200) return } if (what === 'keys') { await page.keyboard.type(args[0]) await pause(80) 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.undo.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) if (c.quick) await page.evaluate(() => { window.__quick = true }) // The refused x. In the quick case its save is answered at once, so // what follows is typed after the refusal and inside the x's undo // step (500 ms). await act(page, ['type', 1, 'end', 'x']) await page.waitForFunction(() => window.refused === true, null, { timeout: 5000 }) if (!c.quick) await pause(700) 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()) // Until the page holds what it should and no save is on the wire. await page.waitForFunction((want) => JSON.stringify([...document.querySelectorAll('[docs-text] p')].map((p) => p.textContent)) === JSON.stringify(want), c.want, { timeout: 15000 }).catch(() => {}) // A carried save goes after the queue's debounce: quiet for 1.5 s // after the release or the last save, whichever was later. await page.waitForFunction(() => window.__inflight === 0 && performance.now() - window.__lastPost > 1500, null, { timeout: 15000 }).catch(() => {}) 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)}`) 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: c.want })) } 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 undo and redo made while the resync was pending ended as it was seen, in both engines')