| 1 | // fixtures/engines/diverged.mjs — every change a person makes while a |
| 2 | // resync is pending is kept, in Chromium and in WebKit, through the real |
| 3 | // editor (feat/replay-while-diverged; Claude Fable's rv-general-probe.mjs |
| 4 | // made into keys and clipboard events). Paul ranks a lost edit worse than |
| 5 | // a flash. |
| 6 | // |
| 7 | // Run by internal/docs/diverged_engines_browser_test.go with the same |
| 8 | // DOCS_ENGINES_CONFIG as first-reveal.mjs; docs.diverged holds a fresh |
| 9 | // document per engine and case ("First paragraph." / "Second |
| 10 | // paragraph."). Slides has the same script. |
| 11 | // |
| 12 | // Each case: the editor's first save (the refused change, made with real |
| 13 | // keys) is answered 413, and the rebuild's fetch of the log is held while |
| 14 | // the person goes on editing. Then it is let go. The page must hold what |
| 15 | // the case wants, a line RESULT {...} carries it for the Go test to hold |
| 16 | // the server to, and one Cmd/Ctrl-Z must take back the last change only — |
| 17 | // the carried changes undo in the steps they were made in — the server |
| 18 | // following. |
| 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 cases = [ |
| 26 | { |
| 27 | name: 'mixed edits: on the refused text, a paste elsewhere, a delete across server, refused and new text, typing elsewhere', |
| 28 | refuse: [['type', 1, 'end', 'x']], |
| 29 | edits: [ |
| 30 | ['type', 1, 'end', 'a'], |
| 31 | ['paste', 0, 0, 'ELSEWHERE-'], |
| 32 | ['type', 1, 'end', 'b'], |
| 33 | ['select', 1, 7, 'end'], ['key', 'Backspace'], |
| 34 | // A second later, so it is its own undo step, as it was when made. |
| 35 | ['wait', 900], |
| 36 | ['type', 0, 'end', 'c'], |
| 37 | ], |
| 38 | want: ['ELSEWHERE-First paragraph.c', 'Second '], |
| 39 | undone: ['ELSEWHERE-First paragraph.', 'Second '], |
| 40 | }, |
| 41 | { |
| 42 | name: 'typing on the refused text, a paste elsewhere, more typing after it: all kept', |
| 43 | refuse: [['type', 1, 'end', 'x']], |
| 44 | edits: [ |
| 45 | ['type', 1, 'end', 'KEPT'], |
| 46 | ['paste', 0, 0, 'ELSEWHERE-'], |
| 47 | ['wait', 900], |
| 48 | ['type', 1, 'end', ' more'], |
| 49 | ], |
| 50 | want: ['ELSEWHERE-First paragraph.', 'Second paragraph.KEPT more'], |
| 51 | undone: ['ELSEWHERE-First paragraph.', 'Second paragraph.KEPT'], |
| 52 | }, |
| 53 | { |
| 54 | name: 'an undo in the middle: typing on the refused text, Cmd-Z, then typing elsewhere', |
| 55 | refuse: [['type', 1, 'end', 'x']], |
| 56 | edits: [ |
| 57 | ['wait', 700], |
| 58 | ['type', 1, 'end', 'a'], |
| 59 | ['undo'], |
| 60 | ['wait', 700], |
| 61 | ['type', 0, 'end', 'b'], |
| 62 | ], |
| 63 | want: ['First paragraph.b', 'Second paragraph.'], |
| 64 | undone: ['First paragraph.', 'Second paragraph.'], |
| 65 | }, |
| 66 | { |
| 67 | name: 'a change that cannot be kept in the middle: typing in the paragraph a refused Enter made, then elsewhere, then in it again', |
| 68 | refuse: [['press', 1, 'end', 'Enter']], |
| 69 | edits: [ |
| 70 | ['type', 2, 0, 'INSIDE'], |
| 71 | ['type', 0, 'end', ' after'], |
| 72 | ['type', 2, 'end', '!'], |
| 73 | ], |
| 74 | want: ['First paragraph. after', 'Second paragraph.'], |
| 75 | undone: ['First paragraph.', 'Second paragraph.'], |
| 76 | }, |
| 77 | ] |
| 78 | |
| 79 | const instrument = () => { |
| 80 | const f = window.fetch |
| 81 | window.__posts = [] |
| 82 | window.__errors = [] |
| 83 | addEventListener('error', (e) => window.__errors.push(String(e.message))) |
| 84 | addEventListener('unhandledrejection', (e) => window.__errors.push(String(e.reason))) |
| 85 | window.__hold = true |
| 86 | window.__release = () => { window.__hold = false; if (window.__go) window.__go() } |
| 87 | window.fetch = async function (u, o) { |
| 88 | const post = String(u).endsWith('/updates') && o && o.method === 'POST' |
| 89 | if (post && !window.refused) { |
| 90 | await new Promise((r) => setTimeout(r, 150)) |
| 91 | window.refused = true |
| 92 | window.__posts.push(413) |
| 93 | return new Response('', { status: 413 }) |
| 94 | } |
| 95 | // The rebuild's fetch of the whole log (the live cursor's carries since=). |
| 96 | if (window.refused && !post && String(u).includes('/updates') && !String(u).includes('since=') && window.__hold) await new Promise((r) => { window.__go = r }) |
| 97 | const r = await f.call(this, u, o) |
| 98 | if (post) window.__posts.push(r.status) |
| 99 | return r |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | const place = ([i, a, b]) => { |
| 104 | const p = document.querySelectorAll('[docs-text] p')[i] |
| 105 | document.querySelector('[docs-text]').focus() |
| 106 | const at = (off) => { |
| 107 | const w = document.createTreeWalker(p, NodeFilter.SHOW_TEXT) |
| 108 | let n |
| 109 | let last = null |
| 110 | let left = off === 'end' ? Infinity : off |
| 111 | while ((n = w.nextNode())) { |
| 112 | last = n |
| 113 | if (left <= n.data.length) return [n, left] |
| 114 | left -= n.data.length |
| 115 | } |
| 116 | return last ? [last, last.data.length] : [p, 0] |
| 117 | } |
| 118 | const r = document.createRange() |
| 119 | const [sn, so] = at(a) |
| 120 | r.setStart(sn, so) |
| 121 | if (b !== undefined) { |
| 122 | const [en, eo] = at(b) |
| 123 | r.setEnd(en, eo) |
| 124 | } else r.collapse(true) |
| 125 | getSelection().removeAllRanges() |
| 126 | getSelection().addRange(r) |
| 127 | } |
| 128 | |
| 129 | const paste = (html) => { |
| 130 | const d = new DataTransfer() |
| 131 | d.setData('text/html', html) |
| 132 | d.setData('text/plain', html) |
| 133 | document.activeElement.dispatchEvent(new ClipboardEvent('paste', { clipboardData: d, bubbles: true, cancelable: true })) |
| 134 | } |
| 135 | |
| 136 | // act runs one step. ProseMirror reads a moved caret on the next |
| 137 | // selectionchange, so every move is followed by a moment before the key. |
| 138 | async function act (page, [what, ...args]) { |
| 139 | if (what === 'wait') { |
| 140 | await pause(args[0]) |
| 141 | return |
| 142 | } |
| 143 | if (what === 'undo') { |
| 144 | await page.keyboard.press('ControlOrMeta+z') |
| 145 | await pause(200) |
| 146 | return |
| 147 | } |
| 148 | if (what === 'select') { |
| 149 | await page.evaluate(place, [args[0], args[1], args[2]]) |
| 150 | await pause(40) |
| 151 | } else if (what === 'key') { |
| 152 | await page.keyboard.press(args[0]) |
| 153 | } else { |
| 154 | await page.evaluate(place, [args[0], args[1]]) |
| 155 | await pause(40) |
| 156 | if (what === 'type') await page.keyboard.type(args[2]) |
| 157 | else if (what === 'press') await page.keyboard.press(args[2]) |
| 158 | else if (what === 'paste') await page.evaluate(paste, args[2]) |
| 159 | } |
| 160 | await pause(80) |
| 161 | } |
| 162 | |
| 163 | const paragraphs = () => [...document.querySelectorAll('[docs-text] p')].map((p) => p.textContent) |
| 164 | const same = (a, b) => JSON.stringify(a) === JSON.stringify(b) |
| 165 | |
| 166 | const failures = [] |
| 167 | for (const [engineName, engine] of Object.entries({ chromium, webkit }).filter(([n]) => !process.env.DOCS_ENGINES_ENGINE || n === process.env.DOCS_ENGINES_ENGINE)) { |
| 168 | const browser = await engine.launch(engineName === 'chromium' ? { headless: true, channel: 'chromium' } : { headless: true }) |
| 169 | for (const c of cases) { |
| 170 | const label = `${engineName}, ${c.name}` |
| 171 | const deck = config.docs.diverged.shift() |
| 172 | const context = await browser.newContext({ viewport: { width: 1280, height: 800 }, deviceScaleFactor: 1 }) |
| 173 | await context.addCookies([ |
| 174 | ...config.cookies.map((k) => ({ name: k.Name, value: k.Value, url: config.origin, httpOnly: k.HttpOnly, sameSite: 'Lax' })), |
| 175 | { name: 'docs-view', value: 'paged', url: config.origin }, |
| 176 | ]) |
| 177 | try { |
| 178 | const page = await context.newPage() |
| 179 | await page.goto(config.origin + '/d/' + deck) |
| 180 | await page.waitForSelector('html[data-docs-fonts="shown"] [docs-text][contenteditable="true"]', { timeout: 15000 }) |
| 181 | await pause(300) |
| 182 | await page.evaluate(instrument) |
| 183 | for (const step of c.refuse) await act(page, step) |
| 184 | await page.waitForFunction(() => window.refused === true, null, { timeout: 5000 }) |
| 185 | await pause(50) |
| 186 | for (const step of c.edits) await act(page, step) |
| 187 | const diverged = await page.evaluate(() => !!window.__go || window.__hold) |
| 188 | if (!diverged) failures.push(`${label}: the rebuild was not still pending during the edits, so this proves nothing`) |
| 189 | await page.evaluate(() => window.__release()) |
| 190 | await pause(3500) |
| 191 | const got = await page.evaluate(paragraphs) |
| 192 | if (!same(got, c.want)) failures.push(`${label}: the page holds ${JSON.stringify(got)}, want ${JSON.stringify(c.want)}`) |
| 193 | // One undo takes back the last change, and only it. |
| 194 | await act(page, ['type', 0, 'end', '']) |
| 195 | await page.keyboard.press('ControlOrMeta+z') |
| 196 | await pause(2000) |
| 197 | const undone = await page.evaluate(paragraphs) |
| 198 | if (!same(undone, c.undone)) failures.push(`${label}: after one undo the page holds ${JSON.stringify(undone)}, want ${JSON.stringify(c.undone)}`) |
| 199 | const errors = await page.evaluate(() => window.__errors) |
| 200 | if (errors.length) failures.push(`${label}: errors ${JSON.stringify(errors)}`) |
| 201 | console.log('RESULT ' + JSON.stringify({ label, deck, want: undone })) |
| 202 | } catch (err) { |
| 203 | failures.push(`${label}: ${err.message}`) |
| 204 | } |
| 205 | await context.close() |
| 206 | } |
| 207 | await browser.close() |
| 208 | } |
| 209 | |
| 210 | if (failures.length) { |
| 211 | console.error(failures.join('\n')) |
| 212 | process.exit(1) |
| 213 | } |
| 214 | console.log('every change made while the resync was pending was kept, in both engines') |
| 215 | |