| 1 | // fixtures/carry.test.mjs — what a resync carries onto the rebuilt |
| 2 | // document (carry.mjs), with the real Yjs, ProseMirror and y-prosemirror. |
| 3 | // |
| 4 | // The server holds two paragraphs. The tab made a change the server |
| 5 | // refused (client 2), took a fresh client id (3), and then wrote more; the |
| 6 | // rebuilt document is the server's, sometimes with a colleague's words that |
| 7 | // arrived meanwhile. Claude Fable's probes on #27 and #88: carry-probe.mjs |
| 8 | // (which updates took) and pm-replay-probe.mjs (the eleven replay cases). |
| 9 | import { test } from 'node:test' |
| 10 | import assert from 'node:assert/strict' |
| 11 | import * as Y from 'yjs' |
| 12 | import { EditorState, TextSelection } from 'prosemirror-state' |
| 13 | import { Slice, Fragment } from 'prosemirror-model' |
| 14 | import { yXmlFragmentToProseMirrorRootNode, prosemirrorToYXmlFragment } from 'y-prosemirror' |
| 15 | import { schema } from './schema.mjs' |
| 16 | import { carry, took, createKeeper, freshClientID, replaySteps, derivedStep } from './carry.mjs' |
| 17 | |
| 18 | const CARRIED = 'carried' |
| 19 | const P = schema.nodes.paragraph |
| 20 | const mk = (...paras) => schema.nodes.doc.create(null, paras.map(t => P.create(null, t ? schema.text(t) : null))) |
| 21 | const paras = doc => { const out = []; doc.forEach(n => out.push(n.textContent)); return out } |
| 22 | const frag = d => d.getXmlFragment('t') |
| 23 | const pmOf = d => yXmlFragmentToProseMirrorRootNode(frag(d), schema) |
| 24 | const endOf = (doc, i) => { let pos = 0; for (let k = 0; k <= i; k++) { if (k === i) return pos + 1 + doc.child(k).content.size; pos += doc.child(k).nodeSize } } |
| 25 | const code = schema.marks.code.create() |
| 26 | const pasteSlice = new Slice(Fragment.from(P.create(null, [schema.text('REVIEW_RESYNC_PASTE '), schema.text('codeword', [code])])), 1, 1) |
| 27 | const twoParas = new Slice(Fragment.from([P.create(null, [schema.text('ONE '), schema.text('c1', [code])]), P.create(null, schema.text('TWO'))]), 1, 1) |
| 28 | |
| 29 | // write puts pm into d's text as one transaction and returns its updates. |
| 30 | function write (d, pm) { |
| 31 | const out = [] |
| 32 | const grab = u => out.push(u) |
| 33 | d.on('update', grab) |
| 34 | d.transact(() => prosemirrorToYXmlFragment(pm, frag(d))) |
| 35 | d.off('update', grab) |
| 36 | return out |
| 37 | } |
| 38 | |
| 39 | // world builds the server, the tab after its refused change, and the |
| 40 | // rebuilt document (the server's, with colleague's words when given). |
| 41 | function world (refused, colleague) { |
| 42 | const server = new Y.Doc(); server.clientID = 1 |
| 43 | write(server, mk('First paragraph.', 'Second paragraph.')) |
| 44 | const tab = new Y.Doc(); tab.clientID = 2 |
| 45 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 46 | write(tab, refused) |
| 47 | tab.clientID = 3 |
| 48 | if (colleague) { |
| 49 | const peer = new Y.Doc(); peer.clientID = 5 |
| 50 | Y.applyUpdate(peer, Y.encodeStateAsUpdate(server)) |
| 51 | for (const u of write(peer, colleague)) Y.applyUpdate(server, u) |
| 52 | } |
| 53 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 54 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 55 | return { server, tab, rebuilt } |
| 56 | } |
| 57 | |
| 58 | // pasteOn makes the paste on the tab as the editor would — a ProseMirror |
| 59 | // transaction on its text — and returns the entry the resync keeps. |
| 60 | function pasteOn (tab, from, to, slice = pasteSlice) { |
| 61 | const before = pmOf(tab) |
| 62 | const tr = EditorState.create({ doc: before, schema }).tr |
| 63 | tr.setSelection(TextSelection.create(before, from(before), (to || from)(before))).replaceSelection(slice) |
| 64 | const updates = write(tab, tr.doc) |
| 65 | return { updates, replay: { tr, locate: frag, schema } } |
| 66 | } |
| 67 | |
| 68 | // posted applies what carry would post to a copy of the server: it must |
| 69 | // integrate there whole, and give the page's text. |
| 70 | function posted (server, post) { |
| 71 | const copy = new Y.Doc() |
| 72 | Y.applyUpdate(copy, Y.encodeStateAsUpdate(server)) |
| 73 | for (const u of post) Y.applyUpdate(copy, u) |
| 74 | return copy |
| 75 | } |
| 76 | |
| 77 | const replayCases = [ |
| 78 | { name: '(a) the paste right after the refused x (Fable\'s probe)', refused: mk('First paragraph.', 'Second paragraph.x'), at: d => endOf(d, 1), want: ['First paragraph.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] }, |
| 79 | { name: '(b) the refused x elsewhere, before the paste', refused: mk('First paragraph.x', 'Second paragraph.'), at: d => endOf(d, 1), want: ['First paragraph.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] }, |
| 80 | { name: '(g) the refused x after the paste point', refused: mk('First paragraph.', 'Second paragraph.x'), at: d => endOf(d, 0), want: ['First paragraph.REVIEW_RESYNC_PASTE codeword', 'Second paragraph.'] }, |
| 81 | { name: '(c) two refused x, the paste after the second', refused: mk('First paragraph.x', 'Second paragraph.x'), at: d => endOf(d, 1), want: ['First paragraph.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] }, |
| 82 | { name: '(i) the refused x mid-paragraph, the paste at its end', refused: mk('First paragraph.', 'Second xparagraph.'), at: d => endOf(d, 1), want: ['First paragraph.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] }, |
| 83 | { name: '(e) the paste over "paragraph.x"', refused: mk('First paragraph.', 'Second paragraph.x'), at: d => endOf(d, 1) - 11, to: d => endOf(d, 1), want: ['First paragraph.', 'Second REVIEW_RESYNC_PASTE codeword'] }, |
| 84 | { name: '(f) a refused deletion elsewhere', refused: mk('First .', 'Second paragraph.'), at: d => endOf(d, 1), want: ['First paragraph.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] }, |
| 85 | { name: '(h) two paragraphs pasted after the refused x', refused: mk('First paragraph.', 'Second paragraph.x'), at: d => endOf(d, 1), slice: twoParas, want: ['First paragraph.', 'Second paragraph.ONE c1', 'TWO'] }, |
| 86 | { name: '(j) a colleague\'s words before, the refused x after', refused: mk('First paragraph.', 'Second paragraph.x'), colleague: mk('First paragraph. More from a colleague.', 'Second paragraph.'), at: d => endOf(d, 1), want: ['First paragraph. More from a colleague.', 'Second paragraph.REVIEW_RESYNC_PASTE codeword'] }, |
| 87 | { name: '(k) a colleague appended where the x was refused', refused: mk('First paragraph.', 'Second paragraph.x'), colleague: mk('First paragraph.', 'Second paragraph. Colleague.'), at: d => endOf(d, 1), want: ['First paragraph.', 'Second paragraph. Colleague.REVIEW_RESYNC_PASTE codeword'] } |
| 88 | ] |
| 89 | |
| 90 | for (const c of replayCases) { |
| 91 | test(`a paste that waited for its font is kept: ${c.name}`, () => { |
| 92 | const { server, tab, rebuilt } = world(c.refused, c.colleague) |
| 93 | const entry = pasteOn(tab, c.at, c.to, c.slice) |
| 94 | const { post, lost } = carry(rebuilt, [entry], CARRIED) |
| 95 | assert.equal(lost, 0) |
| 96 | assert.deepEqual(paras(pmOf(rebuilt)), c.want, 'the rebuilt page') |
| 97 | assert.equal(rebuilt.store.pendingStructs, null, 'nothing held pending on the page') |
| 98 | const there = posted(server, post) |
| 99 | assert.deepEqual(paras(pmOf(there)), c.want, 'the server, once it has what was posted') |
| 100 | assert.equal(there.store.pendingStructs, null, 'nothing held pending on the server') |
| 101 | assert.equal(there.store.pendingDs, null) |
| 102 | // The code mark came through. |
| 103 | let marked = false |
| 104 | pmOf(rebuilt).descendants(n => { if (n.isText && code.isInSet(n.marks)) marked = true }) |
| 105 | assert.ok(marked, 'the pasted code is still code') |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | test('a paste into a paragraph the refused change made cannot be kept: nothing is posted, the page is the server\'s', () => { |
| 110 | const { server, tab, rebuilt } = world(mk('First paragraph.', 'Second paragraph.', '')) |
| 111 | const entry = pasteOn(tab, d => endOf(d, 2)) |
| 112 | const { post, lost } = carry(rebuilt, [entry], CARRIED) |
| 113 | assert.equal(lost, 1) |
| 114 | assert.deepEqual(post, []) |
| 115 | assert.deepEqual(paras(pmOf(rebuilt)), paras(pmOf(server))) |
| 116 | assert.equal(rebuilt.store.pendingStructs, null, 'its updates were never applied') |
| 117 | }) |
| 118 | |
| 119 | test('a paste made elsewhere carries as its own updates, not a replay', () => { |
| 120 | const { tab, rebuilt } = world(mk('First paragraph.x', 'Second paragraph.')) |
| 121 | const entry = pasteOn(tab, d => endOf(d, 1)) |
| 122 | // Not anchored to the refused x: its own updates integrate. |
| 123 | const { post } = carry(rebuilt, [{ ...entry, replay: { ...entry.replay, locate: () => { throw new Error('replayed') } } }], CARRIED) |
| 124 | assert.deepEqual(post, entry.updates) |
| 125 | }) |
| 126 | |
| 127 | test('the replay is one change for an undo manager tracking the carried origin', () => { |
| 128 | const { tab, rebuilt } = world(mk('First paragraph.', 'Second paragraph.x')) |
| 129 | const undo = new Y.UndoManager(frag(rebuilt), { trackedOrigins: new Set([CARRIED]) }) |
| 130 | carry(rebuilt, [pasteOn(tab, d => endOf(d, 1))], CARRIED) |
| 131 | assert.equal(undo.undoStack.length, 1) |
| 132 | undo.undo() |
| 133 | assert.deepEqual(paras(pmOf(rebuilt)), ['First paragraph.', 'Second paragraph.']) |
| 134 | }) |
| 135 | |
| 136 | // Which updates took (Claude Fable's carry-probe.mjs). The server holds |
| 137 | // "hello"; the tab typed "x" (refused), took a fresh id and wrote more. |
| 138 | function textWorld () { |
| 139 | const server = new Y.Doc(); server.clientID = 1 |
| 140 | server.getText('t').insert(0, 'hello') |
| 141 | const tab = new Y.Doc(); tab.clientID = 2 |
| 142 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 143 | tab.getText('t').insert(5, 'x') |
| 144 | tab.clientID = 3 |
| 145 | const unsaved = [] |
| 146 | tab.on('update', u => unsaved.push(u)) |
| 147 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 148 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 149 | return { server, tab, rebuilt, unsaved } |
| 150 | } |
| 151 | const entries = us => us.map(u => ({ updates: [u] })) |
| 152 | |
| 153 | test('every update written on top of the refused text is held back, not only the first', () => { |
| 154 | const { tab, rebuilt, unsaved } = textWorld() |
| 155 | tab.getText('t').insert(6, 'a') |
| 156 | tab.getText('t').insert(7, 'b') |
| 157 | const { post, lost } = carry(rebuilt, entries(unsaved), CARRIED) |
| 158 | assert.deepEqual(post, [], 'the second keystroke was posted') |
| 159 | assert.equal(lost, 2) |
| 160 | }) |
| 161 | |
| 162 | test('a Backspace over the refused text is held back', () => { |
| 163 | const { tab, rebuilt, unsaved } = textWorld() |
| 164 | tab.getText('t').delete(5, 1) |
| 165 | const { post } = carry(rebuilt, entries(unsaved), CARRIED) |
| 166 | assert.deepEqual(post, [], 'a delete-only update over refused text was posted') |
| 167 | }) |
| 168 | |
| 169 | test('of an update that partly took, the part that took is posted, and nothing the server would hold pending', () => { |
| 170 | const { server, tab, rebuilt, unsaved } = textWorld() |
| 171 | tab.getText('t').delete(3, 3) // "lo" and the refused x together |
| 172 | const { post, lost } = carry(rebuilt, entries(unsaved), CARRIED) |
| 173 | assert.equal(post.length, 1) |
| 174 | assert.equal(lost, 1) |
| 175 | assert.equal(rebuilt.getText('t').toString(), 'hel') |
| 176 | const there = posted(server, post) |
| 177 | assert.equal(there.getText('t').toString(), 'hel') |
| 178 | assert.equal(there.store.pendingDs, null, 'the delete of the refused x was posted') |
| 179 | }) |
| 180 | |
| 181 | test('an update that rewrites formatting and writes on top of the refused text posts the formatting, not the words', () => { |
| 182 | // What the editor's binding writes for a paste into marked text: the |
| 183 | // formatting around the server's words rewritten (it integrates) and |
| 184 | // the words after the refused ones (they cannot) in one update. |
| 185 | const { server, tab, rebuilt, unsaved } = textWorld() |
| 186 | tab.transact(() => { |
| 187 | tab.getText('t').format(0, 5, { b: true }) |
| 188 | tab.getText('t').insert(6, 'ONTOP') |
| 189 | }) |
| 190 | const { post } = carry(rebuilt, entries(unsaved), CARRIED) |
| 191 | assert.equal(post.length, 1) |
| 192 | const there = posted(server, post) |
| 193 | assert.equal(there.store.pendingStructs, null, 'the words were posted, to sit pending on the server') |
| 194 | assert.deepEqual(there.getText('t').toDelta(), [{ insert: 'hello', attributes: { b: true } }]) |
| 195 | }) |
| 196 | |
| 197 | test('a paste elsewhere is posted and a keystroke after the refused x is not', () => { |
| 198 | const { tab, rebuilt, unsaved } = textWorld() |
| 199 | tab.getText('t').insert(0, 'PASTE ') |
| 200 | tab.getText('t').insert(12, 'a') |
| 201 | const { post } = carry(rebuilt, entries(unsaved), CARRIED) |
| 202 | assert.deepEqual(post, [unsaved[0]]) |
| 203 | assert.equal(rebuilt.getText('t').toString(), 'PASTE hello') |
| 204 | }) |
| 205 | |
| 206 | test('took reads all, some and none from the update itself', () => { |
| 207 | const { tab, rebuilt, unsaved } = textWorld() |
| 208 | tab.getText('t').insert(0, 'A') |
| 209 | tab.getText('t').insert(7, 'B') // after the refused x |
| 210 | tab.getText('t').delete(4, 3) // "o", the refused x, and B |
| 211 | for (const u of unsaved) Y.applyUpdate(rebuilt, u) |
| 212 | assert.deepEqual(unsaved.map(u => took(rebuilt, u)), ['all', 'none', 'some']) |
| 213 | }) |
| 214 | |
| 215 | // Claude Fable's review of #28/#89 (rv-carry-review.test.mjs). |
| 216 | |
| 217 | test('a Backspace over this tab\'s own saved words and its refused ones together posts the saved part (one delete range, one client)', () => { |
| 218 | // The tab (client 2) typed "llo" (saved) and "x" (refused), then deleted |
| 219 | // "ox": one range of one client, which Yjs applies up to the doc's state |
| 220 | // and holds pending past it. |
| 221 | const server = new Y.Doc(); server.clientID = 1 |
| 222 | server.getText('t').insert(0, 'he') |
| 223 | const tab = new Y.Doc(); tab.clientID = 2 |
| 224 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 225 | const saved = []; tab.on('update', u => saved.push(u)) |
| 226 | tab.getText('t').insert(2, 'llo') |
| 227 | for (const u of saved) Y.applyUpdate(server, u) |
| 228 | tab.getText('t').insert(5, 'x') |
| 229 | tab.clientID = 3 |
| 230 | const unsaved = []; tab.on('update', u => unsaved.push({ updates: [u] })) |
| 231 | tab.getText('t').delete(4, 2) |
| 232 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 233 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 234 | const { post } = carry(rebuilt, unsaved, CARRIED) |
| 235 | assert.equal(took(rebuilt, unsaved[0].updates[0]), 'some') |
| 236 | assert.equal(rebuilt.getText('t').toString(), 'hell') |
| 237 | assert.equal(posted(server, post).getText('t').toString(), 'hell', 'the server has what the page shows') |
| 238 | }) |
| 239 | |
| 240 | test('a paste right after a refused repeated letter lands where it was made', () => { |
| 241 | // "hell|o" + a refused "l": the paste after that l belongs before the o. |
| 242 | const server = new Y.Doc(); server.clientID = 1 |
| 243 | write(server, mk('hello world')) |
| 244 | const tab = new Y.Doc(); tab.clientID = 2 |
| 245 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 246 | write(tab, mk('helllo world')) |
| 247 | tab.clientID = 3 |
| 248 | const before = pmOf(tab) |
| 249 | const tr = EditorState.create({ doc: before, schema }).tr |
| 250 | tr.setSelection(TextSelection.create(before, 6)).replaceSelection(new Slice(Fragment.from(P.create(null, schema.text('PASTE'))), 1, 1)) |
| 251 | const updates = write(tab, tr.doc) |
| 252 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 253 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 254 | const { post, lost } = carry(rebuilt, [{ updates, replay: { tr, locate: frag, schema } }], CARRIED) |
| 255 | assert.equal(lost, 0) |
| 256 | assert.deepEqual(paras(pmOf(rebuilt)), ['hellPASTEo world']) |
| 257 | assert.deepEqual(paras(pmOf(posted(server, post))), ['hellPASTEo world']) |
| 258 | }) |
| 259 | |
| 260 | // What the editor keeps while diverged goes through a keeper, which gives |
| 261 | // the document a fresh client id after every change: Yjs integrates a |
| 262 | // client's structs in clock order, so one change held back would |
| 263 | // otherwise hold back every later one, wherever it was made. |
| 264 | for (const order of ['on the refused text, then elsewhere', 'elsewhere, then on the refused text']) { |
| 265 | test(`a change made in a clean spot is not lost with one held back on the refused text (${order})`, () => { |
| 266 | const server = new Y.Doc(); server.clientID = 1 |
| 267 | server.getText('t').insert(0, 'hello') |
| 268 | const tab = new Y.Doc(); tab.clientID = 2 |
| 269 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 270 | tab.getText('t').insert(5, 'x') |
| 271 | tab.clientID = 3 |
| 272 | const keeper = createKeeper() |
| 273 | // Raw Yjs edits, as an inspector change or an undo is: each takes a |
| 274 | // fresh id before it is made. |
| 275 | tab.on('beforeTransaction', () => keeper.before(tab, 'other')) |
| 276 | tab.on('update', u => keeper.keep(tab, u)) |
| 277 | if (order.startsWith('on')) { |
| 278 | tab.getText('t').insert(6, 'a') // on the refused x |
| 279 | tab.getText('t').insert(0, 'PASTE ') // elsewhere, afterwards |
| 280 | } else { |
| 281 | tab.getText('t').insert(0, 'PASTE ') |
| 282 | tab.getText('t').insert(12, 'a') |
| 283 | } |
| 284 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 285 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 286 | const { post, lost } = carry(rebuilt, keeper.take(), CARRIED) |
| 287 | assert.equal(lost, 1) |
| 288 | assert.equal(rebuilt.getText('t').toString(), 'PASTE hello', 'the paste elsewhere is on the page') |
| 289 | assert.equal(posted(server, post).getText('t').toString(), 'PASTE hello') |
| 290 | }) |
| 291 | } |
| 292 | |
| 293 | test('the keeper groups the updates of one held change into one entry, and hands the entries over once', () => { |
| 294 | const doc = new Y.Doc() |
| 295 | const keeper = createKeeper() |
| 296 | const held = { tr: null } |
| 297 | doc.on('update', u => keeper.keep(doc, u, held)) |
| 298 | doc.getText('t').insert(0, 'a') |
| 299 | doc.getText('t').insert(1, 'b') |
| 300 | doc.getText('t').insert(2, 'c') |
| 301 | assert.equal(keeper.entries.length, 1) |
| 302 | assert.equal(keeper.entries[0].updates.length, 3) |
| 303 | assert.equal(keeper.entries[0].replay, held) |
| 304 | assert.equal(keeper.take().length, 1) |
| 305 | assert.deepEqual(keeper.take(), []) |
| 306 | }) |
| 307 | |
| 308 | // The general replay (feat/replay-while-diverged): every local ProseMirror |
| 309 | // change made while diverged is kept with its transaction (Claude Fable's |
| 310 | // rv-general-probe.mjs). Each runs on the tab under its own client id, as |
| 311 | // the keeper gives it. |
| 312 | const startOf = (doc, i) => endOf(doc, i) - doc.child(i).content.size |
| 313 | const text = s => new Slice(Fragment.from(P.create(null, schema.text(s))), 1, 1) |
| 314 | function change (tab, keeper, make) { |
| 315 | const before = pmOf(tab) |
| 316 | const tr = EditorState.create({ doc: before, schema }).tr |
| 317 | make(tr, before) |
| 318 | const held = { tr, locate: frag, schema } |
| 319 | const grab = u => keeper.keep(tab, u, held) |
| 320 | keeper.before(tab, 'pm') |
| 321 | tab.on('update', grab) |
| 322 | tab.transact(() => prosemirrorToYXmlFragment(tr.doc, frag(tab))) |
| 323 | tab.off('update', grab) |
| 324 | } |
| 325 | |
| 326 | test('every change made while diverged is kept, whatever it was written on', () => { |
| 327 | const { server, tab, rebuilt } = world(mk('First paragraph.', 'Second paragraph.x')) |
| 328 | const keeper = createKeeper() |
| 329 | // On the refused x, elsewhere, on the a, across the server's words, the |
| 330 | // refused x and the new ones, and elsewhere again. |
| 331 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 1))).replaceSelection(text('a'))) |
| 332 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, startOf(d, 0))).replaceSelection(text('ELSEWHERE '))) |
| 333 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 1))).replaceSelection(text('b'))) |
| 334 | change(tab, keeper, (tr, d) => tr.delete(endOf(d, 1) - 13, endOf(d, 1))) |
| 335 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 0))).replaceSelection(text('c'))) |
| 336 | const { post, lost } = carry(rebuilt, keeper.take(), CARRIED) |
| 337 | assert.equal(lost, 0) |
| 338 | const want = ['ELSEWHERE First paragraph.c', 'Second '] |
| 339 | assert.deepEqual(paras(pmOf(rebuilt)), want) |
| 340 | const there = posted(server, post) |
| 341 | assert.deepEqual(paras(pmOf(there)), want) |
| 342 | assert.equal(there.store.pendingStructs, null) |
| 343 | assert.equal(there.store.pendingDs, null) |
| 344 | }) |
| 345 | |
| 346 | test('a change that cannot be kept in the middle loses nothing after it', () => { |
| 347 | // A refused Enter made a third paragraph; typing inside it cannot be |
| 348 | // kept, typing elsewhere afterwards is, and inside again is not. |
| 349 | const { server, tab, rebuilt } = world(mk('First paragraph.', 'Second paragraph.', '')) |
| 350 | const keeper = createKeeper() |
| 351 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 2))).replaceSelection(text('INSIDE'))) |
| 352 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 0))).replaceSelection(text(' after'))) |
| 353 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 2))).replaceSelection(text('!'))) |
| 354 | const { post, lost } = carry(rebuilt, keeper.take(), CARRIED) |
| 355 | assert.equal(lost, 2) |
| 356 | assert.deepEqual(paras(pmOf(rebuilt)), ['First paragraph. after', 'Second paragraph.']) |
| 357 | assert.deepEqual(paras(pmOf(posted(server, post))), ['First paragraph. after', 'Second paragraph.']) |
| 358 | assert.equal(rebuilt.store.pendingStructs, null) |
| 359 | }) |
| 360 | |
| 361 | test("a replay that cannot be made falls back to what of the change's own updates integrates", () => { |
| 362 | const { server, tab, rebuilt, unsaved } = textWorld() |
| 363 | tab.getText('t').delete(3, 3) // "lo" and the refused x together |
| 364 | const entries = unsaved.map(u => ({ updates: [u], replay: { tr: null, locate: () => null, schema } })) |
| 365 | const { post } = carry(rebuilt, entries, CARRIED) |
| 366 | assert.equal(rebuilt.getText('t').toString(), 'hel') |
| 367 | assert.equal(posted(server, post).getText('t').toString(), 'hel') |
| 368 | }) |
| 369 | |
| 370 | test('the carried changes undo in the steps they were made in', () => { |
| 371 | const { tab, rebuilt } = world(mk('First paragraph.', 'Second paragraph.x')) |
| 372 | let clock = 0 |
| 373 | const keeper = createKeeper({ now: () => clock }) |
| 374 | // "a" and "b" a moment apart; "c" a second later. |
| 375 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 1))).replaceSelection(text('a'))) |
| 376 | clock = 100 |
| 377 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 1))).replaceSelection(text('b'))) |
| 378 | clock = 1100 |
| 379 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 0))).replaceSelection(text('c'))) |
| 380 | const undo = new Y.UndoManager(frag(rebuilt), { trackedOrigins: new Set([CARRIED]), captureTimeout: 500 }) |
| 381 | carry(rebuilt, keeper.take(), CARRIED, (e, p) => { if (!p || e.at - p.at >= 500) undo.stopCapturing() }) |
| 382 | assert.deepEqual(paras(pmOf(rebuilt)), ['First paragraph.c', 'Second paragraph.ab']) |
| 383 | undo.undo() |
| 384 | assert.deepEqual(paras(pmOf(rebuilt)), ['First paragraph.', 'Second paragraph.ab'], 'one undo took back more than the last change') |
| 385 | undo.undo() |
| 386 | assert.deepEqual(paras(pmOf(rebuilt)), ['First paragraph.', 'Second paragraph.'], 'a and b, made a moment apart, are one step') |
| 387 | }) |
| 388 | |
| 389 | // Claude Fable's review of #29/#90 (feat/replay-while-diverged at 72f74b0). |
| 390 | |
| 391 | // A multi-step transaction: each later step is in the coordinates of the |
| 392 | // document the earlier steps made, so it is mapped through their inverses, |
| 393 | // the span, and their replayed forms (prosemirror-collab's rebase), not |
| 394 | // through the one span map (rv-multistep-probe.mjs). |
| 395 | const multi = [ |
| 396 | ['an insert, then an insert just before the refused text', 'abcdefghxij', 'abcdefghij', tr => { tr.insertText('ZZZ', 3); tr.insertText('Q', 10) }, ['abZZZcdefQghij']], |
| 397 | ['an insert, then a mark over it, the refused text two letters on', 'abcdefxgh', 'abcdefgh', tr => { tr.insertText('ZZZ', 5); tr.addMark(5, 8, code) }, ['abcdZZZefgh']], |
| 398 | ['a delete, then an insert past the refused text', 'abcdefghxij', 'abcdefghij', tr => { tr.delete(2, 5); tr.insertText('Q', 8) }, ['aefghiQj']] |
| 399 | ] |
| 400 | for (const [name, had, rebuiltText, build, want] of multi) { |
| 401 | test(`a multi-step change replays each step where it was made: ${name}`, () => { |
| 402 | const tr = EditorState.create({ doc: mk(had), schema }).tr |
| 403 | build(tr) |
| 404 | const got = replaySteps(tr, mk(rebuiltText)) |
| 405 | assert.ok(got, 'the replay was refused') |
| 406 | assert.deepEqual(paras(got), want) |
| 407 | }) |
| 408 | } |
| 409 | |
| 410 | test('a mark made over an insertion in the same change covers all of it', () => { |
| 411 | const tr = EditorState.create({ doc: mk('abcdefxgh'), schema }).tr |
| 412 | tr.insertText('ZZZ', 5); tr.addMark(5, 8, code) |
| 413 | const marked = d => { const out = []; d.descendants(n => { if (n.isText && n.marks.length) out.push(n.text) }); return out.join('|') } |
| 414 | assert.equal(marked(replaySteps(tr, mk('abcdefgh'))), 'ZZZ') |
| 415 | }) |
| 416 | |
| 417 | test('a multi-step change on the refused text is replayed where it was made, on the page and the server', () => { |
| 418 | const server = new Y.Doc(); server.clientID = 1 |
| 419 | write(server, mk('abcdefghij')) |
| 420 | const tab = new Y.Doc(); tab.clientID = 2 |
| 421 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 422 | write(tab, mk('abcdefghxij')) |
| 423 | tab.clientID = 3 |
| 424 | const before = pmOf(tab) |
| 425 | const tr = EditorState.create({ doc: before, schema }).tr |
| 426 | tr.insertText('ZZZ', 3); tr.insertText('Q', 12) // the Q right before the refused x |
| 427 | const updates = write(tab, tr.doc) |
| 428 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 429 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 430 | const { post } = carry(rebuilt, [{ updates, replay: { tr, locate: frag, schema } }], CARRIED) |
| 431 | assert.deepEqual(paras(pmOf(rebuilt)), ['abZZZcdefghQij']) |
| 432 | assert.deepEqual(paras(pmOf(posted(server, post))), ['abZZZcdefghQij']) |
| 433 | }) |
| 434 | |
| 435 | // Undo and redo while diverged (Claude Fable's rv-undo-delete-probe.mjs, |
| 436 | // #29/#90). Each is the undo manager's Yjs change; the editor keeps it |
| 437 | // with a replay derived from ySync's mirror of it (derivedStep), so the |
| 438 | // resync makes it again on the rebuilt document, taking text away or |
| 439 | // putting it back, whatever ids the change it undoes was replayed under. |
| 440 | // The refused x is gone afterwards unless an undo or a redo put it back, |
| 441 | // as the person saw. Item matching (watchUndo, before this) lost an undo |
| 442 | // that put text back — a Backspace undone — when that text was typed on |
| 443 | // the refused x: Yjs anchors the re-inserted text to the original, which |
| 444 | // the rebuilt document never holds. |
| 445 | function undoWorld (oneStep = false) { |
| 446 | const server = new Y.Doc(); server.clientID = 1 |
| 447 | write(server, mk('First paragraph.', 'Second paragraph.')) |
| 448 | const tab = new Y.Doc(); tab.clientID = 2 |
| 449 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 450 | let clock = 0 |
| 451 | // The editor's undo manager; the refused x is in its own step unless |
| 452 | // oneStep, where the x and what follows are one. |
| 453 | const um = new Y.UndoManager(frag(tab), { captureTimeout: oneStep ? 100000 : 0, trackedOrigins: new Set([null]) }) |
| 454 | write(tab, mk('First paragraph.', 'Second paragraph.x')) |
| 455 | tab.clientID = 3 |
| 456 | const keeper = createKeeper({ now: () => clock }) |
| 457 | const pm = make => change(tab, keeper, make) |
| 458 | // An undo or a redo, kept as the editor keeps it: its update with the |
| 459 | // step derived from ySync's mirror (before, and the document after). |
| 460 | const other = fn => { |
| 461 | keeper.before(tab, 'other') |
| 462 | const before = pmOf(tab) |
| 463 | const got = [] |
| 464 | const grab = u => got.push(u) |
| 465 | tab.on('update', grab) |
| 466 | fn() |
| 467 | tab.off('update', grab) |
| 468 | const step = derivedStep(before, pmOf(tab)) |
| 469 | const held = step ? { tr: { before, steps: [step] }, locate: frag, schema } : undefined |
| 470 | for (const u of got) keeper.keep(tab, u, held) |
| 471 | } |
| 472 | const resync = () => { |
| 473 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 474 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 475 | const { post, lost } = carry(rebuilt, keeper.take(), CARRIED) |
| 476 | assert.equal(rebuilt.store.pendingStructs, null, 'something was left pending on the page') |
| 477 | return { page: paras(pmOf(rebuilt)), server: paras(pmOf(posted(server, post))), lost } |
| 478 | } |
| 479 | return { tab, pm, undo: () => other(() => um.undo()), redo: () => other(() => um.redo()), tick: (ms = 600) => { clock += ms }, resync } |
| 480 | } |
| 481 | const on1 = (tr, d, back = 0) => tr.setSelection(TextSelection.create(d, endOf(d, 1) - back)) |
| 482 | const del1 = n => (tr, d) => tr.delete(endOf(d, 1) - n, endOf(d, 1)) |
| 483 | |
| 484 | // Each case: what is done, and what the page and the server must hold |
| 485 | // afterwards — the tab's own text, the refused x gone unless an undo or a |
| 486 | // redo put it back. |
| 487 | const undoCases = [ |
| 488 | ['A: "abc" on the x; Backspace; undo the Backspace', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('abc'))); w.tick(); w.pm(del1(1)); w.tick(); w.undo() }, 'Second paragraph.abc'], |
| 489 | ['B: "a" on the x; select it, type "b"; undo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm((tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 1) - 1, endOf(d, 1))).replaceSelection(text('b'))); w.tick(); w.undo() }, 'Second paragraph.a'], |
| 490 | ['C: "a" on the x; Backspace; undo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm(del1(1)); w.tick(); w.undo() }, 'Second paragraph.a'], |
| 491 | ['D: "a" on the x; Backspace; undo; "b" typed', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm(del1(1)); w.tick(); w.undo(); w.tick(); w.pm((tr, d) => on1(tr, d).replaceSelection(text('b'))) }, 'Second paragraph.ab'], |
| 492 | ['E: "a" on the x; Backspace; undo; redo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm(del1(1)); w.tick(); w.undo(); w.redo() }, 'Second paragraph.'], |
| 493 | ['F: "a" elsewhere; Backspace; undo', w => { w.pm((tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 0))).replaceSelection(text('a'))); w.tick(); w.pm((tr, d) => tr.delete(endOf(d, 0) - 1, endOf(d, 0))); w.tick(); w.undo() }, 'Second paragraph.', 'First paragraph.a'], |
| 494 | ['G: "a" on the x; undo; redo; undo; redo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.undo(); w.redo(); w.undo(); w.redo() }, 'Second paragraph.a'], |
| 495 | ['H: "a" on the x; "b"; undo; undo; redo; redo; "c"', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm((tr, d) => on1(tr, d).replaceSelection(text('b'))); w.tick(); w.undo(); w.undo(); w.redo(); w.redo(); w.tick(); w.pm((tr, d) => on1(tr, d).replaceSelection(text('c'))) }, 'Second paragraph.abc'], |
| 496 | ['I: "a" on the x; Backspace over "xa"; undo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm(del1(2)); w.tick(); w.undo() }, 'Second paragraph.xa'], |
| 497 | ['J: "a" on the x; Backspace over ".xa"; undo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm(del1(3)); w.tick(); w.undo() }, 'Second paragraph.xa'], |
| 498 | ['K: the x and "a" one undo step; undo; redo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.undo(); w.redo() }, 'Second paragraph.xa', undefined, true], |
| 499 | ['L: "a" on the x; undo; "b"; undo; redo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.undo(); w.tick(); w.pm((tr, d) => on1(tr, d).replaceSelection(text('b'))); w.tick(); w.undo(); w.redo() }, 'Second paragraph.b'], |
| 500 | ['M: two words pasted on the x; the first deleted; undo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text(' one two'))); w.tick(); w.pm((tr, d) => tr.delete(endOf(d, 1) - 8, endOf(d, 1) - 4)); w.tick(); w.undo() }, 'Second paragraph. one two'], |
| 501 | ['N: "a" on the x; undo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.undo() }, 'Second paragraph.'], |
| 502 | ['O: "a" on the x; undo; "b"', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.undo(); w.tick(); w.pm((tr, d) => on1(tr, d).replaceSelection(text('b'))) }, 'Second paragraph.b'], |
| 503 | ['P: "a" on the x; "b" elsewhere; undo; undo', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.pm((tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 0))).replaceSelection(text('b'))); w.tick(); w.undo(); w.undo() }, 'Second paragraph.'], |
| 504 | ['Q: "a" on the x; undo; redo; "c"', w => { w.pm((tr, d) => on1(tr, d).replaceSelection(text('a'))); w.tick(); w.undo(); w.redo(); w.tick(); w.pm((tr, d) => on1(tr, d).replaceSelection(text('c'))) }, 'Second paragraph.ac'] |
| 505 | ] |
| 506 | for (const [name, run, second, first = 'First paragraph.', oneStep = false] of undoCases) { |
| 507 | test(`an undo or a redo while diverged ends as the person saw it: ${name}`, () => { |
| 508 | const w = undoWorld(oneStep) |
| 509 | run(w) |
| 510 | const want = [first, second] |
| 511 | const r = w.resync() |
| 512 | assert.deepEqual(r.page, want, 'the page') |
| 513 | assert.deepEqual(r.server, want, 'the server') |
| 514 | }) |
| 515 | } |
| 516 | |
| 517 | test('an undo of a change saved before the refusal is still carried', () => { |
| 518 | const server = new Y.Doc(); server.clientID = 1 |
| 519 | write(server, mk('First paragraph.', 'Second paragraph.')) |
| 520 | const tab = new Y.Doc(); tab.clientID = 2 |
| 521 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 522 | const um = new Y.UndoManager(frag(tab), { captureTimeout: 0, trackedOrigins: new Set([null]) }) |
| 523 | // "s" typed and saved, then "x" refused, then the x and the s undone. |
| 524 | for (const u of write(tab, mk('First paragraph.s', 'Second paragraph.'))) Y.applyUpdate(server, u) |
| 525 | um.stopCapturing() |
| 526 | write(tab, mk('First paragraph.s', 'Second paragraph.x')) |
| 527 | tab.clientID = 3 |
| 528 | const keeper = createKeeper() |
| 529 | for (let i = 0; i < 2; i++) { |
| 530 | keeper.before(tab, 'other') |
| 531 | const before = pmOf(tab) |
| 532 | const got = [] |
| 533 | const grab = u => got.push(u) |
| 534 | tab.on('update', grab) |
| 535 | um.undo() |
| 536 | tab.off('update', grab) |
| 537 | const step = derivedStep(before, pmOf(tab)) |
| 538 | for (const u of got) keeper.keep(tab, u, step ? { tr: { before, steps: [step] }, locate: frag, schema } : undefined) |
| 539 | } |
| 540 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 541 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 542 | const { post, lost } = carry(rebuilt, keeper.take(), CARRIED) |
| 543 | assert.deepEqual(paras(pmOf(rebuilt)), ['First paragraph.', 'Second paragraph.']) |
| 544 | assert.deepEqual(paras(pmOf(posted(server, post))), ['First paragraph.', 'Second paragraph.']) |
| 545 | assert.equal(lost, 0, 'an undo of only the refused x counted as a loss') |
| 546 | }) |
| 547 | |
| 548 | test('derivedStep turns one document into the other, and is null for the same one', () => { |
| 549 | const a = mk('First paragraph.', 'Second paragraph.') |
| 550 | const b = mk('First paragraph.', 'Second paragraph.ab') |
| 551 | const step = derivedStep(a, b) |
| 552 | assert.deepEqual(paras(step.apply(a).doc), paras(b)) |
| 553 | assert.equal(derivedStep(a, a), null) |
| 554 | // A repeated letter: the overlap rule keeps the step's size right. |
| 555 | const c = mk('hello'); const d = mk('helllo') |
| 556 | assert.deepEqual(paras(derivedStep(c, d).apply(c).doc), ['helllo']) |
| 557 | }) |
| 558 | |
| 559 | // The cost of a long divergence: ProseMirror changes share one client id |
| 560 | // per 500 ms window, and carry reads clocks directly and keeps one running |
| 561 | // copy, so it is linear (rv-cost-probe.mjs: 2000 changes took 6.1 s and |
| 562 | // 4000 took 27 s, every change leaving a client in the deck for good). |
| 563 | function longWorld () { |
| 564 | const body = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '.repeat(4) |
| 565 | const doc0 = schema.nodes.doc.create(null, Array.from({ length: 60 }, (_, i) => P.create(null, schema.text(`${i} ${body}`)))) |
| 566 | const server = new Y.Doc(); server.clientID = 1 |
| 567 | write(server, doc0) |
| 568 | const tab = new Y.Doc(); tab.clientID = 2 |
| 569 | Y.applyUpdate(tab, Y.encodeStateAsUpdate(server)) |
| 570 | let d = pmOf(tab) |
| 571 | write(tab, EditorState.create({ doc: d, schema }).tr.insertText('x', endOf(d, 59)).doc) |
| 572 | tab.clientID = 3 |
| 573 | return { server, tab } |
| 574 | } |
| 575 | // carryTime types n changes (elsewhere, or on the refused text) into a |
| 576 | // long document and returns how long carrying them took, with the deck's |
| 577 | // client count, checking page and server agree. |
| 578 | function carryTime (where, n) { |
| 579 | const { server, tab } = longWorld() |
| 580 | let clock = 0 |
| 581 | const keeper = createKeeper({ now: () => clock }) |
| 582 | // Typing straight into the tab's Yjs text, as a keystroke reaches it; |
| 583 | // the transaction is kept as the editor keeps it. |
| 584 | let pm = pmOf(tab) |
| 585 | for (let i = 0; i < n; i++) { |
| 586 | clock += 25 |
| 587 | const at = where === 'elsewhere' ? endOf(pm, 0) : endOf(pm, 59) |
| 588 | const tr = EditorState.create({ doc: pm, schema }).tr.insertText(String.fromCharCode(97 + (i % 26)), at) |
| 589 | const held = { tr, locate: frag, schema } |
| 590 | keeper.before(tab, 'pm') |
| 591 | const grab = u => keeper.keep(tab, u, held) |
| 592 | tab.on('update', grab) |
| 593 | tab.transact(() => prosemirrorToYXmlFragment(tr.doc, frag(tab))) |
| 594 | tab.off('update', grab) |
| 595 | pm = tr.doc |
| 596 | } |
| 597 | const rebuilt = new Y.Doc(); rebuilt.clientID = 4 |
| 598 | Y.applyUpdate(rebuilt, Y.encodeStateAsUpdate(server)) |
| 599 | const entries = keeper.take() |
| 600 | // CPU time, not wall time: a loaded machine stretches the second and |
| 601 | // barely the first. |
| 602 | const t0 = process.cpuUsage() |
| 603 | const { post, lost } = carry(rebuilt, entries, CARRIED) |
| 604 | const used = process.cpuUsage(t0) |
| 605 | const ms = (used.user + used.system) / 1000 |
| 606 | assert.equal(lost, 0) |
| 607 | const there = posted(server, post) |
| 608 | assert.equal(pmOf(there).textContent, pmOf(rebuilt).textContent) |
| 609 | return { ms, clients: new Set(Y.decodeStateVector(Y.encodeStateVector(there)).keys()).size } |
| 610 | } |
| 611 | // The bound is on growth, not on a time, so a loaded runner does not fail |
| 612 | // it: four times the changes may take at most eight times the CPU time |
| 613 | // (linear is four; the quadratic version this replaced took about twenty, |
| 614 | // 6.1 s for 2000 and 27 s for 4000), with a generous ceiling as well. The |
| 615 | // best of three runs of the smaller size and two of the larger is compared, against noise. |
| 616 | for (const [where, small, big, ceiling] of [['elsewhere', 1000, 4000, 10000], ['on the refused text', 500, 2000, 20000]]) { |
| 617 | test(`carrying grows linearly over a long divergence: changes typed ${where}, ${small} against ${big}, under few client ids`, () => { |
| 618 | const a = Math.min(...[0, 1, 2].map(() => carryTime(where, small).ms)) |
| 619 | const bigs = [0, 1].map(() => carryTime(where, big)) |
| 620 | const b = Math.min(...bigs.map(r => r.ms)) |
| 621 | assert.ok(b < Math.max(a, 40) * 8, `${small} changes took ${Math.round(a)} ms of CPU and ${big} took ${Math.round(b)} ms`) |
| 622 | assert.ok(b < ceiling, `${big} changes took ${Math.round(b)} ms`) |
| 623 | // One id per 500 ms window: changes 25 ms apart, so n / 20 of them. |
| 624 | const { clients } = bigs[0] |
| 625 | assert.ok(clients <= Math.ceil(big * 25 / 500) + 3, `${clients} client ids in the deck`) |
| 626 | }) |
| 627 | } |
| 628 | |
| 629 | test('a client id never collides with one the document already holds', () => { |
| 630 | const doc = new Y.Doc(); doc.clientID = 7 |
| 631 | doc.getText('t').insert(0, 'a') |
| 632 | const draws = [7, 7, 9] |
| 633 | assert.equal(freshClientID(doc, () => draws.shift()), 9) |
| 634 | }) |
| 635 | |
| 636 | test('ProseMirror changes inside one window share an id; one held back there does not strand the next', () => { |
| 637 | const { server, tab, rebuilt } = world(mk('First paragraph.', 'Second paragraph.x')) |
| 638 | let clock = 0 |
| 639 | const keeper = createKeeper({ now: () => clock }) |
| 640 | change(tab, keeper, (tr, d) => on1(tr, d).replaceSelection(text('a'))) // on the refused x |
| 641 | const first = tab.clientID |
| 642 | clock += 100 |
| 643 | change(tab, keeper, (tr, d) => tr.setSelection(TextSelection.create(d, endOf(d, 0))).replaceSelection(text('c'))) // elsewhere |
| 644 | assert.equal(tab.clientID, first, 'two changes in one window took two ids') |
| 645 | const { post, lost } = carry(rebuilt, keeper.take(), CARRIED) |
| 646 | assert.equal(lost, 0) |
| 647 | const want = ['First paragraph.c', 'Second paragraph.a'] |
| 648 | assert.deepEqual(paras(pmOf(rebuilt)), want) |
| 649 | assert.deepEqual(paras(pmOf(posted(server, post))), want) |
| 650 | }) |
| 651 | |