Files
yjs/tests/encoding.tests.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-05-14 18:53:24 +02:00
import * as t from 'lib0/testing'
2019-04-07 23:08:08 +02:00
import {
contentRefs,
readContentBinary,
readContentDeleted,
readContentString,
readContentJSON,
readContentEmbed,
readContentType,
2019-08-09 01:15:33 +02:00
readContentFormat,
readContentAny,
2025-10-20 02:14:02 +02:00
readContentDoc
2019-04-07 23:08:08 +02:00
} from '../src/internals.js'
import * as Y from '../src/index.js'
2019-04-07 23:08:08 +02:00
/**
* @param {t.TestCase} tc
*/
export const testStructReferences = tc => {
2020-12-16 22:58:22 +01:00
t.assert(contentRefs.length === 11)
t.assert(contentRefs[1] === readContentDeleted)
2019-08-09 01:15:33 +02:00
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)
2019-08-09 01:15:33 +02:00
t.assert(contentRefs[8] === readContentAny)
t.assert(contentRefs[9] === readContentDoc)
2020-12-16 22:58:22 +01:00
// contentRefs[10] is reserved for Skip structs
2019-04-07 23:08:08 +02:00
}
/**
* Reported here: https://github.com/yjs/yjs/issues/308
* @param {t.TestCase} tc
*/
export const testDiffStateVectorOfUpdateIsEmpty = tc => {
const ydoc = new Y.Doc()
/**
2022-03-02 13:23:18 +01:00
* @type {any}
*/
2022-03-02 13:23:18 +01:00
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<Uint8Array>}
*/
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)
}