// Browser replay of gc_undo_kept_probe (oficina/docs #145): the native update // stream and snapshots of the undo-kept GC repro, read by Yjs with gc on and // off. Each native snapshot must integrate whole, show what the replayed // stream shows and, with gc on, hold the same live, deleted and collected // units as Yjs; the paragraph's still-kept "hello" must be collected, as Yjs // itself does (checked first, with Yjs's own UndoManager). Also checks Yjs // deletes a local insert into a type a peer deleted (the vendored integrate // patch). Writes the Yjs states back for `gc_undo_kept_probe DIR --check`. import assert from 'node:assert/strict' import fs from 'node:fs' import path from 'node:path' import * as Y from '../../fixtures/node_modules/yjs/dist/yjs.mjs' const directory = process.argv[2] assert.ok(directory, 'usage: node native/scripts/check-gc-undo-kept-wire.mjs OUTPUT_DIRECTORY') const read = (name) => new Uint8Array(fs.readFileSync(path.join(directory, name))) const steps = Number(fs.readFileSync(path.join(directory, 'steps'), 'utf8')) const body = (doc) => doc.getXmlFragment('prosemirror').toString() const whole = (doc) => doc.store.pendingStructs === null && doc.store.pendingDs === null // Every unit (client:clock) of an update: collected, deleted or live. function units (update) { const { structs, ds } = Y.decodeUpdate(update) const out = {} for (const struct of structs) { if (struct instanceof Y.Skip) continue for (let i = 0; i < struct.length; i++) { const id = Y.createID(struct.id.client, struct.id.clock + i) out[`${id.client}:${id.clock}`] = struct instanceof Y.GC ? 'gc' : Y.isDeleted(ds, id) ? 'deleted' : 'live' } } return out } // Yjs's own semantics, the reference: ContentType.gc collects a kept child. { const author = new Y.Doc() const text = new Y.XmlText() text.insert(0, 'hello world') const paragraph = new Y.XmlElement('paragraph') paragraph.insert(0, [text]) author.getXmlFragment('prosemirror').insert(0, [paragraph]) const word = text._start.id const editor = new Y.Doc() Y.applyUpdate(editor, Y.encodeStateAsUpdate(author), 'remote') const undo = new Y.UndoManager(editor.getXmlFragment('prosemirror'), { captureTimeout: 0 }) const typed = editor.getXmlFragment('prosemirror').get(0).get(0) typed.delete(0, 5) typed.insert(1, 'Q') undo.undo() typed.insert(0, 'x') // clears the redo stack Y.applyUpdate(author, Y.encodeStateAsUpdate(editor, Y.encodeStateVector(author)), 'remote') author.getXmlFragment('prosemirror').delete(0, 1) Y.applyUpdate(editor, Y.encodeStateAsUpdate(author, Y.encodeStateVector(editor)), 'remote') const structs = editor.store.clients.get(word.client) const hello = structs[Y.findIndexSS(structs, word.clock)] assert.ok(hello instanceof Y.GC, 'Yjs collects the kept "hello" with its paragraph') } // Yjs's own semantics for the vendored integrate patch: an item inserted into // a type a peer has deleted is deleted at once (Item.integrate), locally too. { const local = new Y.Doc({ gc: false }) const text = new Y.XmlText() text.insert(0, 'ab') const paragraph = new Y.XmlElement('paragraph') paragraph.insert(0, [text]) local.getXmlFragment('prosemirror').insert(0, [paragraph]) const peer = new Y.Doc({ gc: false }) Y.applyUpdate(peer, Y.encodeStateAsUpdate(local)) peer.getXmlFragment('prosemirror').delete(0, 1) Y.applyUpdate(local, Y.encodeStateAsUpdate(peer, Y.encodeStateVector(local))) text.insert(1, 'X') const inserted = local.store.clients.get(local.clientID).at(-1) assert.ok(inserted.deleted, 'Yjs deletes a local insert into a deleted parent') Y.applyUpdate(peer, Y.encodeStateAsUpdate(local, Y.encodeStateVector(peer))) assert.ok(Y.equalDeleteSets(Y.createDeleteSetFromStructStore(local.store), Y.createDeleteSetFromStructStore(peer.store))) } // The kept word in the native stream: "hello" of "hello world" in the base. const base = Y.decodeUpdate(read('0.bin')).structs.find((s) => s.content?.str?.includes('hello world')) const hello = Array.from({ length: 5 }, (_, i) => `${base.id.client}:${base.id.clock + base.content.str.indexOf('hello') + i}`) let comparisons = 0 for (const gc of [true, false]) { const replay = new Y.Doc({ gc }) for (let step = 0; step <= steps; step++) { Y.applyUpdate(replay, read(`${step}.bin`)) const snapshot = read(`${step}.snapshot`) const fresh = new Y.Doc({ gc }) Y.applyUpdate(fresh, snapshot) assert.ok(whole(fresh), `gc=${gc} step=${step}: Yjs parks part of the native snapshot`) assert.ok(whole(replay), `gc=${gc} step=${step}: Yjs parks part of the native stream`) assert.equal(body(fresh), body(replay), `gc=${gc} step=${step}`) if (gc) { const ours = units(snapshot) assert.deepEqual(ours, units(Y.encodeStateAsUpdate(replay)), `step=${step}: collected/deleted/live units differ from Yjs`) if (step >= 5) assert.deepEqual(hello.map((u) => ours[u]), Array(5).fill('gc'), `step=${step}: the kept "hello" was not collected`) } fs.writeFileSync(path.join(directory, `browser-${gc}-${step}.bin`), Y.encodeStateAsUpdate(replay)) fresh.destroy() comparisons++ } assert.equal(body(replay), 'xaz') replay.destroy() } console.log(`GC undo-kept wire replay passed ${comparisons} snapshot comparisons, Yjs's two references and the collected kept word.`)