| 1 | // Browser replay of gc_undo_kept_probe (oficina/docs #145): the native update |
| 2 | // stream and snapshots of the undo-kept GC repro, read by Yjs with gc on and |
| 3 | // off. Each native snapshot must integrate whole, show what the replayed |
| 4 | // stream shows and, with gc on, hold the same live, deleted and collected |
| 5 | // units as Yjs; the paragraph's still-kept "hello" must be collected, as Yjs |
| 6 | // itself does (checked first, with Yjs's own UndoManager). Also checks Yjs |
| 7 | // deletes a local insert into a type a peer deleted (the vendored integrate |
| 8 | // patch). Writes the Yjs states back for `gc_undo_kept_probe DIR --check`. |
| 9 | import assert from 'node:assert/strict' |
| 10 | import fs from 'node:fs' |
| 11 | import path from 'node:path' |
| 12 | import * as Y from '../../fixtures/node_modules/yjs/dist/yjs.mjs' |
| 13 | const directory = process.argv[2] |
| 14 | assert.ok(directory, 'usage: node native/scripts/check-gc-undo-kept-wire.mjs OUTPUT_DIRECTORY') |
| 15 | const read = (name) => new Uint8Array(fs.readFileSync(path.join(directory, name))) |
| 16 | const steps = Number(fs.readFileSync(path.join(directory, 'steps'), 'utf8')) |
| 17 | const body = (doc) => doc.getXmlFragment('prosemirror').toString() |
| 18 | const whole = (doc) => doc.store.pendingStructs === null && doc.store.pendingDs === null |
| 19 | |
| 20 | // Every unit (client:clock) of an update: collected, deleted or live. |
| 21 | function units (update) { |
| 22 | const { structs, ds } = Y.decodeUpdate(update) |
| 23 | const out = {} |
| 24 | for (const struct of structs) { |
| 25 | if (struct instanceof Y.Skip) continue |
| 26 | for (let i = 0; i < struct.length; i++) { |
| 27 | const id = Y.createID(struct.id.client, struct.id.clock + i) |
| 28 | out[`${id.client}:${id.clock}`] = struct instanceof Y.GC ? 'gc' : Y.isDeleted(ds, id) ? 'deleted' : 'live' |
| 29 | } |
| 30 | } |
| 31 | return out |
| 32 | } |
| 33 | |
| 34 | // Yjs's own semantics, the reference: ContentType.gc collects a kept child. |
| 35 | { |
| 36 | const author = new Y.Doc() |
| 37 | const text = new Y.XmlText() |
| 38 | text.insert(0, 'hello world') |
| 39 | const paragraph = new Y.XmlElement('paragraph') |
| 40 | paragraph.insert(0, [text]) |
| 41 | author.getXmlFragment('prosemirror').insert(0, [paragraph]) |
| 42 | const word = text._start.id |
| 43 | const editor = new Y.Doc() |
| 44 | Y.applyUpdate(editor, Y.encodeStateAsUpdate(author), 'remote') |
| 45 | const undo = new Y.UndoManager(editor.getXmlFragment('prosemirror'), { captureTimeout: 0 }) |
| 46 | const typed = editor.getXmlFragment('prosemirror').get(0).get(0) |
| 47 | typed.delete(0, 5) |
| 48 | typed.insert(1, 'Q') |
| 49 | undo.undo() |
| 50 | typed.insert(0, 'x') // clears the redo stack |
| 51 | Y.applyUpdate(author, Y.encodeStateAsUpdate(editor, Y.encodeStateVector(author)), 'remote') |
| 52 | author.getXmlFragment('prosemirror').delete(0, 1) |
| 53 | Y.applyUpdate(editor, Y.encodeStateAsUpdate(author, Y.encodeStateVector(editor)), 'remote') |
| 54 | const structs = editor.store.clients.get(word.client) |
| 55 | const hello = structs[Y.findIndexSS(structs, word.clock)] |
| 56 | assert.ok(hello instanceof Y.GC, 'Yjs collects the kept "hello" with its paragraph') |
| 57 | } |
| 58 | |
| 59 | // Yjs's own semantics for the vendored integrate patch: an item inserted into |
| 60 | // a type a peer has deleted is deleted at once (Item.integrate), locally too. |
| 61 | { |
| 62 | const local = new Y.Doc({ gc: false }) |
| 63 | const text = new Y.XmlText() |
| 64 | text.insert(0, 'ab') |
| 65 | const paragraph = new Y.XmlElement('paragraph') |
| 66 | paragraph.insert(0, [text]) |
| 67 | local.getXmlFragment('prosemirror').insert(0, [paragraph]) |
| 68 | const peer = new Y.Doc({ gc: false }) |
| 69 | Y.applyUpdate(peer, Y.encodeStateAsUpdate(local)) |
| 70 | peer.getXmlFragment('prosemirror').delete(0, 1) |
| 71 | Y.applyUpdate(local, Y.encodeStateAsUpdate(peer, Y.encodeStateVector(local))) |
| 72 | text.insert(1, 'X') |
| 73 | const inserted = local.store.clients.get(local.clientID).at(-1) |
| 74 | assert.ok(inserted.deleted, 'Yjs deletes a local insert into a deleted parent') |
| 75 | Y.applyUpdate(peer, Y.encodeStateAsUpdate(local, Y.encodeStateVector(peer))) |
| 76 | assert.ok(Y.equalDeleteSets(Y.createDeleteSetFromStructStore(local.store), Y.createDeleteSetFromStructStore(peer.store))) |
| 77 | } |
| 78 | |
| 79 | // The kept word in the native stream: "hello" of "hello world" in the base. |
| 80 | const base = Y.decodeUpdate(read('0.bin')).structs.find((s) => s.content?.str?.includes('hello world')) |
| 81 | const hello = Array.from({ length: 5 }, (_, i) => `${base.id.client}:${base.id.clock + base.content.str.indexOf('hello') + i}`) |
| 82 | |
| 83 | let comparisons = 0 |
| 84 | for (const gc of [true, false]) { |
| 85 | const replay = new Y.Doc({ gc }) |
| 86 | for (let step = 0; step <= steps; step++) { |
| 87 | Y.applyUpdate(replay, read(`${step}.bin`)) |
| 88 | const snapshot = read(`${step}.snapshot`) |
| 89 | const fresh = new Y.Doc({ gc }) |
| 90 | Y.applyUpdate(fresh, snapshot) |
| 91 | assert.ok(whole(fresh), `gc=${gc} step=${step}: Yjs parks part of the native snapshot`) |
| 92 | assert.ok(whole(replay), `gc=${gc} step=${step}: Yjs parks part of the native stream`) |
| 93 | assert.equal(body(fresh), body(replay), `gc=${gc} step=${step}`) |
| 94 | if (gc) { |
| 95 | const ours = units(snapshot) |
| 96 | assert.deepEqual(ours, units(Y.encodeStateAsUpdate(replay)), `step=${step}: collected/deleted/live units differ from Yjs`) |
| 97 | if (step >= 5) assert.deepEqual(hello.map((u) => ours[u]), Array(5).fill('gc'), `step=${step}: the kept "hello" was not collected`) |
| 98 | } |
| 99 | fs.writeFileSync(path.join(directory, `browser-${gc}-${step}.bin`), Y.encodeStateAsUpdate(replay)) |
| 100 | fresh.destroy() |
| 101 | comparisons++ |
| 102 | } |
| 103 | assert.equal(body(replay), '<paragraph>xaz</paragraph>') |
| 104 | replay.destroy() |
| 105 | } |
| 106 | console.log(`GC undo-kept wire replay passed ${comparisons} snapshot comparisons, Yjs's two references and the collected kept word.`) |
| 107 | |