import * as t from 'lib0/testing' import { contentRefs, readContentBinary, readContentDeleted, readContentString, readContentJSON, readContentEmbed, readContentType, readContentFormat, readContentAny, readContentDoc } from '../src/internals.js' import * as Y from '../src/index.js' /** * @param {t.TestCase} tc */ export const testStructReferences = tc => { t.assert(contentRefs.length === 11) t.assert(contentRefs[1] === readContentDeleted) t.assert(contentRefs[2] === readContentJSON) // TODO: deprecate content json? t.assert(contentRefs[3] === readContentBinary) t.assert(contentRefs[4] === readContentString) t.assert(contentRefs[5] === readContentEmbed) t.assert(contentRefs[6] === readContentFormat) t.assert(contentRefs[7] === readContentType) t.assert(contentRefs[8] === readContentAny) t.assert(contentRefs[9] === readContentDoc) // contentRefs[10] is reserved for Skip structs } /** * Reported here: https://github.com/yjs/yjs/issues/308 * @param {t.TestCase} tc */ export const testDiffStateVectorOfUpdateIsEmpty = tc => { const ydoc = new Y.Doc() /** * @type {any} */ let sv = null ydoc.getText().insert(0, 'a') ydoc.on('update', update => { sv = Y.encodeStateVectorFromUpdate(update) }) // should produce an update with an empty state vector (because previous ops are missing) ydoc.getText().insert(0, 'a') t.assert(sv !== null && sv.byteLength === 1 && sv[0] === 0) } /** * Reported here: https://github.com/yjs/yjs/issues/308 * @param {t.TestCase} tc */ export const testDiffStateVectorOfUpdateIgnoresSkips = tc => { const ydoc = new Y.Doc() /** * @type {Array} */ const updates = [] ydoc.on('update', update => { updates.push(update) }) ydoc.getText().insert(0, 'a') ydoc.getText().insert(0, 'b') ydoc.getText().insert(0, 'c') const update13 = Y.mergeUpdates([updates[0], updates[2]]) const sv = Y.encodeStateVectorFromUpdate(update13) const state = Y.decodeStateVector(sv) t.assert(state.get(ydoc.clientID) === 1) t.assert(state.size === 1) }