| 1 | // fixtures/engines/undo.mjs — an undo or a redo made while a resync is |
| 2 | // pending ends as the person saw it, in Chromium and in WebKit, through |
| 3 | // the real editor (fix/undo-redo-while-diverged; Claude Fable's seventeen |
| 4 | // cases on #29/#90, rv-undo-delete-probe.mjs, made into keys). |
| 5 | // |
| 6 | // Run by internal/docs/undo_engines_browser_test.go with the same |
| 7 | // DOCS_ENGINES_CONFIG as first-reveal.mjs; docs.undo holds a fresh |
| 8 | // document per engine and case ("First paragraph." / "Second |
| 9 | // paragraph."). Slides has the same script. |
| 10 | // |
| 11 | // Each case: an "x" typed at the end of the second paragraph is refused |
| 12 | // (its save answered 413) and the rebuild's fetch of the log held while |
| 13 | // the person types, deletes, undoes and redoes; then it is let go. The |
| 14 | // page must hold what the case wants — the tab's own text, the refused x |
| 15 | // gone unless an undo or a redo put it back — and a line RESULT {...} |
| 16 | // carries it for the Go test to hold the server to. Cmd/Ctrl-Z is undo, |
| 17 | // Cmd/Ctrl-Shift-Z redo; a pause of 700 ms puts what follows in an undo |
| 18 | // step of its own. |
| 19 | |
| 20 | const { chromium, webkit } = await import('playwright') |
| 21 | |
| 22 | const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG) |
| 23 | const pause = (ms) => new Promise((r) => setTimeout(r, ms)) |
| 24 | |
| 25 | const T = ['wait', 700] |
| 26 | const on = (s) => ['type', 1, 'end', s] |
| 27 | const back = (n) => [['select', 1, -n, 'end'], ['key', 'Backspace']] |
| 28 | const U = ['undo'] |
| 29 | const R = ['redo'] |
| 30 | const F = 'First paragraph.' |
| 31 | const cases = [ |
| 32 | { name: 'A: "abc" on the x; Backspace; undo the Backspace', edits: [on('abc'), T, ...back(1), T, U], want: [F, 'Second paragraph.abc'] }, |
| 33 | { 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'] }, |
| 34 | { name: 'C: "a" on the x; Backspace; undo', edits: [on('a'), T, ...back(1), T, U], want: [F, 'Second paragraph.a'] }, |
| 35 | { 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'] }, |
| 36 | { name: 'E: "a" on the x; Backspace; undo; redo', edits: [on('a'), T, ...back(1), T, U, R], want: [F, 'Second paragraph.'] }, |
| 37 | { 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.'] }, |
| 38 | { name: 'G: "a" on the x; undo; redo; undo; redo', edits: [on('a'), T, U, R, U, R], want: [F, 'Second paragraph.a'] }, |
| 39 | { 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'] }, |
| 40 | { name: 'I: "a" on the x; Backspace over "xa"; undo', edits: [on('a'), T, ...back(2), T, U], want: [F, 'Second paragraph.xa'] }, |
| 41 | { name: 'J: "a" on the x; Backspace over ".xa"; undo', edits: [on('a'), T, ...back(3), T, U], want: [F, 'Second paragraph.xa'] }, |
| 42 | { name: 'K: the x and "a" one undo step; undo; redo', quick: true, edits: [on('a'), T, U, R], want: [F, 'Second paragraph.xa'] }, |
| 43 | { 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'] }, |
| 44 | { 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'] }, |
| 45 | { name: 'N: "a" on the x; undo', edits: [on('a'), T, U], want: [F, 'Second paragraph.'] }, |
| 46 | { name: 'O: "a" on the x; undo; "b"', edits: [on('a'), T, U, T, on('b')], want: [F, 'Second paragraph.b'] }, |
| 47 | { 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.'] }, |
| 48 | { name: 'Q: "a" on the x; undo; redo; "c"', edits: [on('a'), T, U, R, T, on('c')], want: [F, 'Second paragraph.ac'] }, |
| 49 | ] |
| 50 | |
| 51 | const instrument = () => { |
| 52 | const f = window.fetch |
| 53 | window.__posts = [] |
| 54 | window.__inflight = 0 |
| 55 | window.__lastPost = performance.now() |
| 56 | window.__errors = [] |
| 57 | addEventListener('error', (e) => window.__errors.push(String(e.message))) |
| 58 | addEventListener('unhandledrejection', (e) => window.__errors.push(String(e.reason))) |
| 59 | window.__hold = true |
| 60 | window.__release = () => { window.__hold = false; window.__lastPost = performance.now(); if (window.__go) window.__go() } |
| 61 | window.fetch = async function (u, o) { |
| 62 | const post = String(u).endsWith('/updates') && o && o.method === 'POST' |
| 63 | if (post && !window.refused) { |
| 64 | if (!window.__quick) await new Promise((r) => setTimeout(r, 150)) |
| 65 | window.refused = true |
| 66 | window.__posts.push(413) |
| 67 | return new Response('', { status: 413 }) |
| 68 | } |
| 69 | // The rebuild's fetch of the whole log (the live cursor's carries since=). |
| 70 | if (window.refused && !post && String(u).includes('/updates') && !String(u).includes('since=') && window.__hold) await new Promise((r) => { window.__go = r }) |
| 71 | if (post) { window.__inflight++; window.__lastPost = performance.now() } |
| 72 | try { |
| 73 | const r = await f.call(this, u, o) |
| 74 | if (post) window.__posts.push(r.status) |
| 75 | return r |
| 76 | } finally { |
| 77 | if (post) { window.__inflight--; window.__lastPost = performance.now() } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const place = ([i, a, b]) => { |
| 83 | const p = document.querySelectorAll('[docs-text] p')[i] |
| 84 | document.querySelector('[docs-text]').focus() |
| 85 | const at = (off) => { |
| 86 | const w = document.createTreeWalker(p, NodeFilter.SHOW_TEXT) |
| 87 | let n |
| 88 | let last = null |
| 89 | if (typeof off === 'number' && off < 0) off = p.textContent.length + off |
| 90 | let left = off === 'end' ? Infinity : off |
| 91 | while ((n = w.nextNode())) { |
| 92 | last = n |
| 93 | if (left <= n.data.length) return [n, left] |
| 94 | left -= n.data.length |
| 95 | } |
| 96 | return last ? [last, last.data.length] : [p, 0] |
| 97 | } |
| 98 | const r = document.createRange() |
| 99 | const [sn, so] = at(a) |
| 100 | r.setStart(sn, so) |
| 101 | if (b !== undefined) { |
| 102 | const [en, eo] = at(b) |
| 103 | r.setEnd(en, eo) |
| 104 | } else r.collapse(true) |
| 105 | getSelection().removeAllRanges() |
| 106 | getSelection().addRange(r) |
| 107 | } |
| 108 | |
| 109 | const paste = (html) => { |
| 110 | const d = new DataTransfer() |
| 111 | d.setData('text/html', html) |
| 112 | d.setData('text/plain', html) |
| 113 | document.activeElement.dispatchEvent(new ClipboardEvent('paste', { clipboardData: d, bubbles: true, cancelable: true })) |
| 114 | } |
| 115 | |
| 116 | // act runs one step. ProseMirror reads a moved caret on the next |
| 117 | // selectionchange, so every move is followed by a moment before the key. |
| 118 | async function act (page, [what, ...args]) { |
| 119 | if (what === 'wait') { |
| 120 | await pause(args[0]) |
| 121 | return |
| 122 | } |
| 123 | if (what === 'undo' || what === 'redo') { |
| 124 | await page.keyboard.press(what === 'undo' ? 'ControlOrMeta+z' : 'ControlOrMeta+Shift+z') |
| 125 | await pause(200) |
| 126 | return |
| 127 | } |
| 128 | if (what === 'keys') { |
| 129 | await page.keyboard.type(args[0]) |
| 130 | await pause(80) |
| 131 | return |
| 132 | } |
| 133 | if (what === 'select') { |
| 134 | await page.evaluate(place, [args[0], args[1], args[2]]) |
| 135 | await pause(40) |
| 136 | } else if (what === 'key') { |
| 137 | await page.keyboard.press(args[0]) |
| 138 | } else { |
| 139 | await page.evaluate(place, [args[0], args[1]]) |
| 140 | await pause(40) |
| 141 | if (what === 'type') await page.keyboard.type(args[2]) |
| 142 | else if (what === 'press') await page.keyboard.press(args[2]) |
| 143 | else if (what === 'paste') await page.evaluate(paste, args[2]) |
| 144 | } |
| 145 | await pause(80) |
| 146 | } |
| 147 | |
| 148 | const paragraphs = () => [...document.querySelectorAll('[docs-text] p')].map((p) => p.textContent) |
| 149 | const same = (a, b) => JSON.stringify(a) === JSON.stringify(b) |
| 150 | |
| 151 | const failures = [] |
| 152 | for (const [engineName, engine] of Object.entries({ chromium, webkit }).filter(([n]) => !process.env.DOCS_ENGINES_ENGINE || n === process.env.DOCS_ENGINES_ENGINE)) { |
| 153 | const browser = await engine.launch(engineName === 'chromium' ? { headless: true, channel: 'chromium' } : { headless: true }) |
| 154 | for (const c of cases) { |
| 155 | const label = `${engineName}, ${c.name}` |
| 156 | const deck = config.docs.undo.shift() |
| 157 | const context = await browser.newContext({ viewport: { width: 1280, height: 800 }, deviceScaleFactor: 1 }) |
| 158 | await context.addCookies([ |
| 159 | ...config.cookies.map((k) => ({ name: k.Name, value: k.Value, url: config.origin, httpOnly: k.HttpOnly, sameSite: 'Lax' })), |
| 160 | { name: 'docs-view', value: 'paged', url: config.origin }, |
| 161 | ]) |
| 162 | try { |
| 163 | const page = await context.newPage() |
| 164 | await page.goto(config.origin + '/d/' + deck) |
| 165 | await page.waitForSelector('html[data-docs-fonts="shown"] [docs-text][contenteditable="true"]', { timeout: 15000 }) |
| 166 | await pause(300) |
| 167 | await page.evaluate(instrument) |
| 168 | if (c.quick) await page.evaluate(() => { window.__quick = true }) |
| 169 | // The refused x. In the quick case its save is answered at once, so |
| 170 | // what follows is typed after the refusal and inside the x's undo |
| 171 | // step (500 ms). |
| 172 | await act(page, ['type', 1, 'end', 'x']) |
| 173 | await page.waitForFunction(() => window.refused === true, null, { timeout: 5000 }) |
| 174 | if (!c.quick) await pause(700) |
| 175 | for (const step of c.edits) await act(page, step) |
| 176 | const diverged = await page.evaluate(() => !!window.__go || window.__hold) |
| 177 | if (!diverged) failures.push(`${label}: the rebuild was not still pending during the edits, so this proves nothing`) |
| 178 | await page.evaluate(() => window.__release()) |
| 179 | // Until the page holds what it should and no save is on the wire. |
| 180 | await page.waitForFunction((want) => JSON.stringify([...document.querySelectorAll('[docs-text] p')].map((p) => p.textContent)) === JSON.stringify(want), c.want, { timeout: 15000 }).catch(() => {}) |
| 181 | // A carried save goes after the queue's debounce: quiet for 1.5 s |
| 182 | // after the release or the last save, whichever was later. |
| 183 | await page.waitForFunction(() => window.__inflight === 0 && performance.now() - window.__lastPost > 1500, null, { timeout: 15000 }).catch(() => {}) |
| 184 | const got = await page.evaluate(paragraphs) |
| 185 | if (!same(got, c.want)) failures.push(`${label}: the page holds ${JSON.stringify(got)}, want ${JSON.stringify(c.want)}`) |
| 186 | const errors = await page.evaluate(() => window.__errors) |
| 187 | if (errors.length) failures.push(`${label}: errors ${JSON.stringify(errors)}`) |
| 188 | console.log('RESULT ' + JSON.stringify({ label, deck, want: c.want })) |
| 189 | } catch (err) { |
| 190 | failures.push(`${label}: ${err.message}`) |
| 191 | } |
| 192 | await context.close() |
| 193 | } |
| 194 | await browser.close() |
| 195 | } |
| 196 | |
| 197 | if (failures.length) { |
| 198 | console.error(failures.join('\n')) |
| 199 | process.exit(1) |
| 200 | } |
| 201 | console.log('every undo and redo made while the resync was pending ended as it was seen, in both engines') |
| 202 | |