| 1 | import assert from 'node:assert/strict' |
| 2 | import { spawnSync } from 'node:child_process' |
| 3 | import * as Y from 'yjs' |
| 4 | import { yXmlFragmentToProseMirrorRootNode } from 'y-prosemirror' |
| 5 | import { schema } from './schema.mjs' |
| 6 | |
| 7 | const browser = new Y.Doc() |
| 8 | const root = browser.getXmlFragment('prosemirror') |
| 9 | for (const [tag, text] of [['paragraph', 'Rich original'], ['paragraph', 'Replace me'], ['code_block', 'Untouched code']]) { |
| 10 | const block = new Y.XmlElement(tag), body = new Y.XmlText() |
| 11 | body.insert(0, text, tag === 'paragraph' ? {strong: {}} : {}) |
| 12 | block.insert(0, [body]); root.insert(root.length, [block]) |
| 13 | } |
| 14 | root.get(0).setAttribute('align', 'center') |
| 15 | root.get(0).setAttribute('indent', 2) |
| 16 | const untouched = root.get(0), code = root.get(2), base = Y.encodeStateAsUpdate(browser) |
| 17 | const offline = new Y.Doc(); Y.applyUpdate(offline, base) |
| 18 | const sv = Y.encodeStateVector(offline) |
| 19 | offline.getXmlFragment('prosemirror').get(0).get(0).insert(0, 'Offline ') |
| 20 | const concurrent = Y.encodeStateAsUpdate(offline, sv) |
| 21 | const run = spawnSync('cargo', ['run', '--quiet', '--locked', '--manifest-path', 'core/Cargo.toml', '--bin', 'emit_edit'], {input: base, maxBuffer: 8<<20}) |
| 22 | assert.equal(run.status, 0, run.stderr.toString()) |
| 23 | Y.applyUpdate(browser, run.stdout); Y.applyUpdate(browser, concurrent) |
| 24 | Y.applyUpdate(offline, run.stdout) |
| 25 | assert.equal(root.get(0), untouched) |
| 26 | assert.equal(root.get(2), code) |
| 27 | assert.equal(root.get(0).getAttribute('align'), 'center') |
| 28 | assert.equal(root.get(0).getAttribute('indent'), 2) |
| 29 | assert.deepEqual(root.get(0).get(0).toDelta().at(-1).attributes, {strong: {}}) |
| 30 | assert.deepEqual(root.toJSON(), offline.getXmlFragment('prosemirror').toJSON()) |
| 31 | assert.deepEqual(Y.decodeStateVector(Y.encodeStateVector(browser)), Y.decodeStateVector(Y.encodeStateVector(offline))) |
| 32 | const rendered = yXmlFragmentToProseMirrorRootNode(root, schema) |
| 33 | rendered.check() |
| 34 | assert.equal(rendered.child(1).type.name, 'heading') |
| 35 | assert.equal(rendered.child(1).attrs.level, 2) |
| 36 | assert.equal(rendered.child(1).textContent, 'Edited <script> đŸ˜€') |
| 37 | assert.equal(rendered.child(0).textContent, 'Offline Rich original') |
| 38 | root.get(1).get(0).insert(0, 'Browser after ') |
| 39 | Y.applyUpdate(offline, Y.encodeStateAsUpdate(browser, Y.encodeStateVector(offline))) |
| 40 | assert.deepEqual(root.toJSON(), offline.getXmlFragment('prosemirror').toJSON()) |
| 41 | yXmlFragmentToProseMirrorRootNode(root, schema).check() |
| 42 | console.log('semantic edits: production schema, rich-content identity and offline convergence passed') |
| 43 | |