// fixtures/engines/replay.mjs — a paste that waited for its font is kept // across a resync, in Chromium and in WebKit, through the real editor // (Claude Fable's eleven replay cases on #27/#88, pm-replay-probe.mjs, made // into the editor's own keys and clipboard). // // Run by internal/slidestest/replay_engines_browser_test.go with the same // SLIDES_ENGINES_CONFIG as first-reveal.mjs; decks.replay holds a fresh // deck per engine and case, its text block "First paragraph." / "Second // paragraph.". // // Each case: the editor's first save is answered 413 (the refused change, // made with real keys); at once a paste with code in it is made, which // waits for the code face (held 1.5 s); a colleague's words reach the // server meanwhile in (j) and (k), through a second editor, while the // rebuild's fetch is held. Afterwards the page must hold the paste once, in // the shape the case wants, and the pasted words must never have left the // page once there — no DOM change and no frame without them. A line // RESULT {...} per case carries what the Go test checks on the server. const { chromium, webkit } = await import('playwright') const config = JSON.parse(process.env.SLIDES_ENGINES_CONFIG) const pause = (ms) => new Promise((r) => setTimeout(r, ms)) const PASTE = '
REVIEW_RESYNC_PASTE codeword
ONE c1
TWO
' const KEPT = ['First paragraph.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] // Each step is [what, ...args] run in the page (see act below). const cases = [ { name: '(a) the paste right after the refused x (Fable\'s probe)', refuse: [['type', 1, 'end', 'x']], paste: [['caret', 1, 'end']], want: KEPT }, { name: '(b) the refused x elsewhere, before the paste', refuse: [['type', 0, 'end', 'x']], paste: [['caret', 1, 'end']], want: KEPT }, { name: '(g) the refused x after the paste point', refuse: [['type', 1, 'end', 'x']], paste: [['caret', 0, 'end']], want: ['First paragraph.REVIEW_RESYNC_PASTE codeword', 'Second paragraph.'] }, { name: '(c) two refused x, the paste after the second', refuse: [['type', 0, 'end', 'x'], ['type', 1, 'end', 'x']], paste: [['caret', 1, 'end']], want: KEPT }, { name: '(i) the refused x mid-paragraph, the paste at its end', refuse: [['type', 1, 7, 'x']], paste: [['caret', 1, 'end']], want: KEPT }, { name: '(d) the paste inside a paragraph the refused Enter made', refuse: [['press', 1, 'end', 'Enter']], paste: [['caret', 2, 0]], want: ['First paragraph.', 'Second paragraph.'], lost: true }, { name: '(e) the paste over "paragraph.x"', refuse: [['type', 1, 'end', 'x']], paste: [['select', 1, 7, 'end']], want: ['First paragraph.', 'Second REVIEW_RESYNC_PASTE codeword'] }, { name: '(f) a refused deletion elsewhere', refuse: [['select', 0, 6, 15], ['key', 'Backspace']], paste: [['caret', 1, 'end']], want: KEPT }, { name: '(h) two paragraphs pasted after the refused x', refuse: [['type', 1, 'end', 'x']], paste: [['caret', 1, 'end']], html: TWO, words: 'ONE', want: ['First paragraph.', 'Second paragraph.ONE c1', 'TWO'] }, { name: '(j) a colleague\'s words before, the refused x after', refuse: [['type', 1, 'end', 'x']], paste: [['caret', 1, 'end']], colleague: [0, ' More from a colleague.'], want: ['First paragraph. More from a colleague.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] }, { name: '(k) a colleague appended where the x was refused', refuse: [['type', 1, 'end', 'x']], paste: [['caret', 1, 'end']], colleague: [1, ' Colleague.'], want: ['First paragraph.', 'Second paragraph. Colleague.REVIEW_RESYNC_PASTE codeword'] }, ] // In the page: the first save refused, the rebuild's fetch held while // window.__hold is set, and a log of whether the pasted words are on the // stage at every DOM change and every frame. const instrument = (words) => { 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.__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 }) } if (window.refused && String(u).includes('since=0') && 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 } window.__log = [] const stage = document.querySelector('[slides-stage]') const rec = (how) => { const has = stage.textContent.includes(words) const last = window.__log[window.__log.length - 1] if (!last || last.has !== has) window.__log.push({ ms: Math.round(performance.now()), has, how }) } new MutationObserver(() => rec('dom')).observe(stage, { subtree: true, childList: true, characterData: true }) const frame = () => { rec('frame'); requestAnimationFrame(frame) } requestAnimationFrame(frame) } // place puts the DOM selection in paragraph i of the text block: a caret at // offset (a number or 'end'), or a range from a to b. const place = ([i, a, b]) => { const ps = document.querySelectorAll('[slides-stage] [data-block="text"] p') const p = ps[i] const el = p.closest('[contenteditable]') el.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 el = document.activeElement const d = new DataTransfer() d.setData('text/html', html) d.setData('text/plain', 'x') el.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 === 'caret') await page.evaluate(place, [args[0], args[1]]) else if (what === 'select') await page.evaluate(place, [args[0], args[1], args[2]]) else if (what === 'type' || what === 'press') { await page.evaluate(place, [args[0], args[1]]) await pause(40) if (what === 'type') await page.keyboard.type(args[2]) else await page.keyboard.press(args[2]) return } else if (what === 'key') { await page.keyboard.press(args[0]) return } await pause(40) } const paragraphs = () => [...document.querySelectorAll('[slides-stage] [data-block="text"] p')].map((p) => p.textContent) async function editor (context, deck) { const page = await context.newPage() await page.goto(config.origin + '/d/' + deck + '/edit') await page.waitForSelector('[slides-stage] [data-block="text"] [contenteditable="true"], [slides-stage] [data-block="text"][contenteditable="true"]', { timeout: 15000 }) await page.waitForFunction(() => document.documentElement.getAttribute('data-slides-fonts') === 'shown') await pause(300) return page } const failures = [] for (const [engineName, engine] of Object.entries({ chromium, webkit }).filter(([n]) => !process.env.SLIDES_ENGINES_ENGINE || n === process.env.SLIDES_ENGINES_ENGINE)) { const browser = await engine.launch(engineName === 'chromium' ? { headless: true, channel: 'chromium' } : { headless: true }) for (const c of cases) { if (process.env.SLIDES_REPLAY_CASE && !c.name.startsWith(process.env.SLIDES_REPLAY_CASE)) continue const label = `${engineName}, ${c.name}` const deck = config.decks.replay.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' }))) await context.route('**/*', async (route) => { if (/LiberationMono-/.test(route.request().url())) { const response = await route.fetch() await pause(1500) try { await route.fulfill({ response }) } catch {} return } return route.continue() }) try { const page = await editor(context, deck) const words = c.words || 'REVIEW_RESYNC_PASTE' await page.evaluate(instrument, words) if (c.colleague) await page.evaluate(() => { window.__hold = true }) // The refused change, and at once the paste: it is waiting for its // font when the refusal arrives (the save goes 250 ms after the // first key and is answered 150 ms later), and runs after it. for (const step of c.refuse) await act(page, step) for (const step of c.paste) await act(page, step) const early = await page.evaluate(() => !!window.refused) await page.evaluate(paste, c.html || PASTE) const waited = await page.evaluate(() => document.querySelector('[slides-fonts-status]').getAttribute('data-state') === 'loading') if (early || !waited) failures.push(`${label}: the paste did not wait for its font across the refusal (${early ? 'refused first' : 'no wait'}), so this proves nothing`) await page.waitForFunction(() => window.refused === true, null, { timeout: 5000 }) if (c.colleague) { const other = await editor(context, deck) await act(other, ['caret', c.colleague[0], 'end']) await other.keyboard.type(c.colleague[1]) await pause(1500) await other.close() await page.evaluate(() => window.__release()) } await pause(4500) const got = await page.evaluate(paragraphs) const state = await page.evaluate(() => ({ log: window.__log, posts: window.__posts, errors: window.__errors, status: document.querySelector('[estilo-save-status]') && document.querySelector('[estilo-save-status]').getAttribute('data-state') })) if (JSON.stringify(got) !== JSON.stringify(c.want)) failures.push(`${label}: the page holds ${JSON.stringify(got)}, want ${JSON.stringify(c.want)}`) if (state.errors.length) failures.push(`${label}: errors ${JSON.stringify(state.errors)}`) if (!c.lost) { const appeared = state.log.filter((e) => e.has).length const stayed = state.log.length && state.log[state.log.length - 1].has if (appeared !== 1 || !stayed) failures.push(`${label}: the pasted words appeared ${appeared} times and ${stayed ? 'stayed' : 'went'}, want once and to stay: ${JSON.stringify(state.log)}`) } else { if (state.posts.length !== 1) failures.push(`${label}: a paste that could not be kept was posted: ${JSON.stringify(state.posts)}`) if (state.status !== 'failed') failures.push(`${label}: the save line says ${state.status}, want failed`) } 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 case kept its paste, or refused it cleanly, in both engines')