From d8868c47e14444e3a60759a40037b02c761170f1 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 16 Dec 2020 23:45:28 +0100 Subject: [PATCH] test case for deletes + fix --- src/utils/updates.js | 7 ++++-- tests/updates.tests.js | 55 ++++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/utils/updates.js b/src/utils/updates.js index 4c524d45..ce46fa5d 100644 --- a/src/utils/updates.js +++ b/src/utils/updates.js @@ -96,11 +96,14 @@ export const logUpdate = update => logUpdateV2(update, UpdateDecoderV1) */ export const logUpdateV2 = (update, YDecoder = UpdateDecoderV2) => { const structs = [] - const lazyDecoder = new LazyStructReader(new YDecoder(decoding.createDecoder(update))) + const updateDecoder = new YDecoder(decoding.createDecoder(update)) + const lazyDecoder = new LazyStructReader(updateDecoder) for (let curr = lazyDecoder.curr; curr !== null; curr = lazyDecoder.next()) { structs.push(curr) } - logging.print(structs) + logging.print('Structs: ', structs) + const ds = readDeleteSet(updateDecoder) + logging.print('DeleteSet: ', ds) } export class LazyStructWriter { diff --git a/tests/updates.tests.js b/tests/updates.tests.js index e91401b7..dfa08420 100644 --- a/tests/updates.tests.js +++ b/tests/updates.tests.js @@ -37,19 +37,10 @@ export const testMergeUpdates = tc => { } /** - * @param {t.TestCase} tc + * @param {Y.Doc} ydoc + * @param {Array} updates - expecting at least 4 updates */ -export const testMergeUpdatesWrongOrder = tc => { - const ydoc = new Y.Doc() - const updates = /** @type {Array} */ ([]) - ydoc.on(updateEventName, update => { updates.push(update) }) - - const array = ydoc.getArray() - array.insert(0, [1]) - array.insert(0, [2]) - array.insert(0, [3]) - array.insert(0, [4]) - +const checkUpdateCases = (ydoc, updates) => { const cases = [] // Case 1: Simple case, simply merge everything @@ -71,7 +62,8 @@ export const testMergeUpdatesWrongOrder = tc => { // Case 4: Separated updates (containing skips) cases.push(mergeUpdates([ mergeUpdates([updates[0], updates[2]]), - mergeUpdates([updates[1], updates[3]]) + mergeUpdates([updates[1], updates[3]]), + mergeUpdates(updates.slice(4)) ])) // Case 5: overlapping with many duplicates @@ -86,11 +78,44 @@ export const testMergeUpdatesWrongOrder = tc => { logUpdate(updates) const merged = new Y.Doc() applyUpdate(merged, updates) - t.compareArrays(merged.getArray().toArray(), array.toArray()) - t.compare(updates, targetState) + t.compareArrays(merged.getArray().toArray(), ydoc.getArray().toArray()) }) } +/** + * @param {t.TestCase} tc + */ +export const testMergeUpdates1 = tc => { + const ydoc = new Y.Doc({ gc: false }) + const updates = /** @type {Array} */ ([]) + ydoc.on(updateEventName, update => { updates.push(update) }) + + const array = ydoc.getArray() + array.insert(0, [1]) + array.insert(0, [2]) + array.insert(0, [3]) + array.insert(0, [4]) + + checkUpdateCases(ydoc, updates) +} + +/** + * @param {t.TestCase} tc + */ +export const testMergeUpdates2 = tc => { + const ydoc = new Y.Doc({ gc: false }) + const updates = /** @type {Array} */ ([]) + ydoc.on(updateEventName, update => { updates.push(update) }) + + const array = ydoc.getArray() + array.insert(0, [1, 2]) + array.delete(1, 1) + array.insert(0, [3, 4]) + array.delete(1, 2) + + checkUpdateCases(ydoc, updates) +} + /** * @todo be able to apply Skip structs to Yjs docs */