From 2ec19defcb244fa32d0033f050c38c0f57a01e59 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Sat, 6 Nov 2021 15:55:59 +0100 Subject: [PATCH] export testHelper esm properly --- package.json | 2 ++ rollup.config.js | 17 +++++++++ src/index.js | 4 ++- src/utils/encoding.js | 2 +- tests/relativePositions.tests.js | 4 +-- tests/snapshot.tests.js | 62 ++++++++++++++++---------------- tests/testHelper.js | 24 ++++++------- tests/undo-redo.tests.js | 22 +++++------- tests/y-text.tests.js | 4 +-- 9 files changed, 79 insertions(+), 62 deletions(-) diff --git a/package.json b/package.json index 7bb17cc8..719a0269 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "./dist/yjs.cjs", "module": "./dist/yjs.mjs", "types": "./dist/src/index.d.ts", + "type": "module", "sideEffects": false, "funding": { "type": "GitHub Sponsors ❤", @@ -31,6 +32,7 @@ }, "./src/index.js": "./src/index.js", "./tests/testHelper.js": "./tests/testHelper.js", + "./testHelper": "./dist/testHelper.mjs", "./package.json": "./package.json" }, "files": [ diff --git a/rollup.config.js b/rollup.config.js index 8c63bcba..6bda7566 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -60,6 +60,23 @@ export default [{ sourcemap: true }, external: id => /^lib0\//.test(id) +}, { + input: './tests/testHelper.js', + output: { + name: 'Y', + file: 'dist/testHelper.mjs', + format: 'esm', + sourcemap: true + }, + external: id => /^lib0\//.test(id) || id === 'yjs', + plugins: [{ + resolveId (importee) { + if (importee === '../src/index.js') { + return 'yjs' + } + return null + } + }] }, { input: './tests/index.js', output: { diff --git a/src/index.js b/src/index.js index 93820e62..fe74a1d2 100644 --- a/src/index.js +++ b/src/index.js @@ -27,12 +27,13 @@ export { ContentString, ContentType, AbstractType, - RelativePosition, getTypeChildren, createRelativePositionFromTypeIndex, createRelativePositionFromJSON, createAbsolutePositionFromRelativePosition, compareRelativePositions, + AbsolutePosition, + RelativePosition, ID, createID, compareIDs, @@ -41,6 +42,7 @@ export { createSnapshot, createDeleteSet, createDeleteSetFromStructStore, + cleanupYTextFormatting, snapshot, emptySnapshot, findRootTypeKey, diff --git a/src/utils/encoding.js b/src/utils/encoding.js index 66536920..29af8bc6 100644 --- a/src/utils/encoding.js +++ b/src/utils/encoding.js @@ -601,7 +601,7 @@ export const decodeStateVector = decodedState => readStateVector(new DSDecoderV1 */ export const writeStateVector = (encoder, sv) => { encoding.writeVarUint(encoder.restEncoder, sv.size) - sv.forEach((clock, client) => { + Array.from(sv.entries()).sort((a, b) => b[0] - a[0]).forEach(([client, clock]) => { encoding.writeVarUint(encoder.restEncoder, client) // @todo use a special client decoder that is based on mapping encoding.writeVarUint(encoder.restEncoder, clock) }) diff --git a/tests/relativePositions.tests.js b/tests/relativePositions.tests.js index 48b000f3..817b9805 100644 --- a/tests/relativePositions.tests.js +++ b/tests/relativePositions.tests.js @@ -1,9 +1,9 @@ -import * as Y from '../src/internals' +import * as Y from '../src/index.js' import * as t from 'lib0/testing' /** - * @param {Y.YText} ytext + * @param {Y.Text} ytext */ const checkRelativePositions = ytext => { // test if all positions are encoded and restored correctly diff --git a/tests/snapshot.tests.js b/tests/snapshot.tests.js index dc66a763..7f26718d 100644 --- a/tests/snapshot.tests.js +++ b/tests/snapshot.tests.js @@ -1,17 +1,17 @@ -import { createDocFromSnapshot, Doc, snapshot, YMap } from '../src/internals' +import * as Y from '../src/index.js' import * as t from 'lib0/testing' -import { init } from './testHelper' +import { init } from './testHelper.js' /** * @param {t.TestCase} tc */ export const testBasicRestoreSnapshot = tc => { - const doc = new Doc({ gc: false }) + const doc = new Y.Doc({ gc: false }) doc.getArray('array').insert(0, ['hello']) - const snap = snapshot(doc) + const snap = Y.snapshot(doc) doc.getArray('array').insert(1, ['world']) - const docRestored = createDocFromSnapshot(doc, snap) + const docRestored = Y.createDocFromSnapshot(doc, snap) t.compare(docRestored.getArray('array').toArray(), ['hello']) t.compare(doc.getArray('array').toArray(), ['hello', 'world']) @@ -21,19 +21,19 @@ export const testBasicRestoreSnapshot = tc => { * @param {t.TestCase} tc */ export const testEmptyRestoreSnapshot = tc => { - const doc = new Doc({ gc: false }) - const snap = snapshot(doc) + const doc = new Y.Doc({ gc: false }) + const snap = Y.snapshot(doc) snap.sv.set(9999, 0) doc.getArray().insert(0, ['world']) - const docRestored = createDocFromSnapshot(doc, snap) + const docRestored = Y.createDocFromSnapshot(doc, snap) t.compare(docRestored.getArray().toArray(), []) t.compare(doc.getArray().toArray(), ['world']) // now this snapshot reflects the latest state. It shoult still work. - const snap2 = snapshot(doc) - const docRestored2 = createDocFromSnapshot(doc, snap2) + const snap2 = Y.snapshot(doc) + const docRestored2 = Y.createDocFromSnapshot(doc, snap2) t.compare(docRestored2.getArray().toArray(), ['world']) } @@ -41,15 +41,15 @@ export const testEmptyRestoreSnapshot = tc => { * @param {t.TestCase} tc */ export const testRestoreSnapshotWithSubType = tc => { - const doc = new Doc({ gc: false }) - doc.getArray('array').insert(0, [new YMap()]) + const doc = new Y.Doc({ gc: false }) + doc.getArray('array').insert(0, [new Y.Map()]) const subMap = doc.getArray('array').get(0) subMap.set('key1', 'value1') - const snap = snapshot(doc) + const snap = Y.snapshot(doc) subMap.set('key2', 'value2') - const docRestored = createDocFromSnapshot(doc, snap) + const docRestored = Y.createDocFromSnapshot(doc, snap) t.compare(docRestored.getArray('array').toJSON(), [{ key1: 'value1' @@ -64,13 +64,13 @@ export const testRestoreSnapshotWithSubType = tc => { * @param {t.TestCase} tc */ export const testRestoreDeletedItem1 = tc => { - const doc = new Doc({ gc: false }) + const doc = new Y.Doc({ gc: false }) doc.getArray('array').insert(0, ['item1', 'item2']) - const snap = snapshot(doc) + const snap = Y.snapshot(doc) doc.getArray('array').delete(0) - const docRestored = createDocFromSnapshot(doc, snap) + const docRestored = Y.createDocFromSnapshot(doc, snap) t.compare(docRestored.getArray('array').toArray(), ['item1', 'item2']) t.compare(doc.getArray('array').toArray(), ['item2']) @@ -80,15 +80,15 @@ export const testRestoreDeletedItem1 = tc => { * @param {t.TestCase} tc */ export const testRestoreLeftItem = tc => { - const doc = new Doc({ gc: false }) + const doc = new Y.Doc({ gc: false }) doc.getArray('array').insert(0, ['item1']) doc.getMap('map').set('test', 1) doc.getArray('array').insert(0, ['item0']) - const snap = snapshot(doc) + const snap = Y.snapshot(doc) doc.getArray('array').delete(1) - const docRestored = createDocFromSnapshot(doc, snap) + const docRestored = Y.createDocFromSnapshot(doc, snap) t.compare(docRestored.getArray('array').toArray(), ['item0', 'item1']) t.compare(doc.getArray('array').toArray(), ['item0']) @@ -98,13 +98,13 @@ export const testRestoreLeftItem = tc => { * @param {t.TestCase} tc */ export const testDeletedItemsBase = tc => { - const doc = new Doc({ gc: false }) + const doc = new Y.Doc({ gc: false }) doc.getArray('array').insert(0, ['item1']) doc.getArray('array').delete(0) - const snap = snapshot(doc) + const snap = Y.snapshot(doc) doc.getArray('array').insert(0, ['item0']) - const docRestored = createDocFromSnapshot(doc, snap) + const docRestored = Y.createDocFromSnapshot(doc, snap) t.compare(docRestored.getArray('array').toArray(), []) t.compare(doc.getArray('array').toArray(), ['item0']) @@ -114,13 +114,13 @@ export const testDeletedItemsBase = tc => { * @param {t.TestCase} tc */ export const testDeletedItems2 = tc => { - const doc = new Doc({ gc: false }) + const doc = new Y.Doc({ gc: false }) doc.getArray('array').insert(0, ['item1', 'item2', 'item3']) doc.getArray('array').delete(1) - const snap = snapshot(doc) + const snap = Y.snapshot(doc) doc.getArray('array').insert(0, ['item0']) - const docRestored = createDocFromSnapshot(doc, snap) + const docRestored = Y.createDocFromSnapshot(doc, snap) t.compare(docRestored.getArray('array').toArray(), ['item1', 'item3']) t.compare(doc.getArray('array').toArray(), ['item0', 'item1', 'item3']) @@ -140,11 +140,11 @@ export const testDependentChanges = tc => { } /** - * @type Doc + * @type {Y.Doc} */ const doc0 = array0.doc /** - * @type Doc + * @type {Y.Doc} */ const doc1 = array1.doc @@ -156,16 +156,16 @@ export const testDependentChanges = tc => { array1.insert(1, ['user2item1']) testConnector.syncAll() - const snap = snapshot(array0.doc) + const snap = Y.snapshot(array0.doc) array0.insert(2, ['user1item2']) testConnector.syncAll() array1.insert(3, ['user2item2']) testConnector.syncAll() - const docRestored0 = createDocFromSnapshot(array0.doc, snap) + const docRestored0 = Y.createDocFromSnapshot(array0.doc, snap) t.compare(docRestored0.getArray('array').toArray(), ['user1item1', 'user2item1']) - const docRestored1 = createDocFromSnapshot(array1.doc, snap) + const docRestored1 = Y.createDocFromSnapshot(array1.doc, snap) t.compare(docRestored1.getArray('array').toArray(), ['user1item1', 'user2item1']) } diff --git a/tests/testHelper.js b/tests/testHelper.js index 0edd0de2..4df1d11e 100644 --- a/tests/testHelper.js +++ b/tests/testHelper.js @@ -3,10 +3,10 @@ import * as t from 'lib0/testing' import * as prng from 'lib0/prng' import * as encoding from 'lib0/encoding' import * as decoding from 'lib0/decoding' -import * as syncProtocol from 'y-protocols/sync.js' +import * as syncProtocol from 'y-protocols/sync' import * as object from 'lib0/object' -import * as Y from '../src/internals.js' -export * from '../src/internals.js' +import * as Y from '../src/index.js' +export * from '../src/index.js' if (typeof window !== 'undefined') { // @ts-ignore @@ -279,7 +279,7 @@ export class TestConnector { * @param {t.TestCase} tc * @param {{users?:number}} conf * @param {InitTestObjectCallback} [initTestObject] - * @return {{testObjects:Array,testConnector:TestConnector,users:Array,array0:Y.YArray,array1:Y.YArray,array2:Y.YArray,map0:Y.YMap,map1:Y.YMap,map2:Y.YMap,map3:Y.YMap,text0:Y.YText,text1:Y.YText,text2:Y.YText,xml0:Y.YXmlElement,xml1:Y.YXmlElement,xml2:Y.YXmlElement}} + * @return {{testObjects:Array,testConnector:TestConnector,users:Array,array0:Y.Array,array1:Y.Array,array2:Y.Array,map0:Y.Map,map1:Y.Map,map2:Y.Map,map3:Y.Map,text0:Y.Text,text1:Y.Text,text2:Y.Text,xml0:Y.XmlElement,xml1:Y.XmlElement,xml2:Y.XmlElement}} */ export const init = (tc, { users = 5 } = {}, initTestObject) => { /** @@ -304,7 +304,7 @@ export const init = (tc, { users = 5 } = {}, initTestObject) => { result.users.push(y) result['array' + i] = y.getArray('array') result['map' + i] = y.getMap('map') - result['xml' + i] = y.get('xml', Y.YXmlElement) + result['xml' + i] = y.get('xml', Y.XmlElement) result['text' + i] = y.getText('text') } testConnector.syncAll() @@ -335,7 +335,7 @@ export const compare = users => { users.push(.../** @type {any} */(mergedDocs)) const userArrayValues = users.map(u => u.getArray('array').toJSON()) const userMapValues = users.map(u => u.getMap('map').toJSON()) - const userXmlValues = users.map(u => u.get('xml', Y.YXmlElement).toString()) + const userXmlValues = users.map(u => u.get('xml', Y.XmlElement).toString()) const userTextValues = users.map(u => u.getText('text').toDelta()) for (const u of users) { t.assert(u.store.pendingDs === null) @@ -370,7 +370,7 @@ export const compare = users => { } return true }) - t.compare(Y.getStateVector(users[i].store), Y.getStateVector(users[i + 1].store)) + t.compare(Y.encodeStateVector(users[i]), Y.encodeStateVector(users[i + 1])) compareDS(Y.createDeleteSetFromStructStore(users[i].store), Y.createDeleteSetFromStructStore(users[i + 1].store)) compareStructStores(users[i].store, users[i + 1].store) } @@ -385,8 +385,8 @@ export const compare = users => { export const compareItemIDs = (a, b) => a === b || (a !== null && b != null && Y.compareIDs(a.id, b.id)) /** - * @param {Y.StructStore} ss1 - * @param {Y.StructStore} ss2 + * @param {import('../src/internals').StructStore} ss1 + * @param {import('../src/internals').StructStore} ss2 */ export const compareStructStores = (ss1, ss2) => { t.assert(ss1.clients.size === ss2.clients.size) @@ -428,13 +428,13 @@ export const compareStructStores = (ss1, ss2) => { } /** - * @param {Y.DeleteSet} ds1 - * @param {Y.DeleteSet} ds2 + * @param {import('../src/internals').DeleteSet} ds1 + * @param {import('../src/internals').DeleteSet} ds2 */ export const compareDS = (ds1, ds2) => { t.assert(ds1.clients.size === ds2.clients.size) ds1.clients.forEach((deleteItems1, client) => { - const deleteItems2 = /** @type {Array} */ (ds2.clients.get(client)) + const deleteItems2 = /** @type {Array} */ (ds2.clients.get(client)) t.assert(deleteItems2 !== undefined && deleteItems1.length === deleteItems2.length) for (let i = 0; i < deleteItems1.length; i++) { const di1 = deleteItems1[i] diff --git a/tests/undo-redo.tests.js b/tests/undo-redo.tests.js index 813121b2..249a3505 100644 --- a/tests/undo-redo.tests.js +++ b/tests/undo-redo.tests.js @@ -1,9 +1,5 @@ import { init, compare, applyRandomTests, Doc } from './testHelper.js' // eslint-disable-line -import { - UndoManager -} from '../src/internals.js' - import * as Y from '../src/index.js' import * as t from 'lib0/testing' @@ -12,7 +8,7 @@ import * as t from 'lib0/testing' */ export const testUndoText = tc => { const { testConnector, text0, text1 } = init(tc, { users: 3 }) - const undoManager = new UndoManager(text0) + const undoManager = new Y.UndoManager(text0) // items that are added & deleted in the same transaction won't be undo text0.insert(0, 'test') @@ -81,7 +77,7 @@ export const testDoubleUndo = tc => { export const testUndoMap = tc => { const { testConnector, map0, map1 } = init(tc, { users: 2 }) map0.set('a', 0) - const undoManager = new UndoManager(map0) + const undoManager = new Y.UndoManager(map0) map0.set('a', 1) undoManager.undo() t.assert(map0.get('a') === 0) @@ -120,7 +116,7 @@ export const testUndoMap = tc => { */ export const testUndoArray = tc => { const { testConnector, array0, array1 } = init(tc, { users: 3 }) - const undoManager = new UndoManager(array0) + const undoManager = new Y.UndoManager(array0) array0.insert(0, [1, 2, 3]) array1.insert(0, [4, 5, 6]) testConnector.syncAll() @@ -171,7 +167,7 @@ export const testUndoArray = tc => { */ export const testUndoXml = tc => { const { xml0 } = init(tc, { users: 3 }) - const undoManager = new UndoManager(xml0) + const undoManager = new Y.UndoManager(xml0) const child = new Y.XmlElement('p') xml0.insert(0, [child]) const textchild = new Y.XmlText('content') @@ -196,7 +192,7 @@ export const testUndoXml = tc => { */ export const testUndoEvents = tc => { const { text0 } = init(tc, { users: 3 }) - const undoManager = new UndoManager(text0) + const undoManager = new Y.UndoManager(text0) let counter = 0 let receivedMetadata = -1 undoManager.on('stack-item-added', /** @param {any} event */ event => { @@ -222,7 +218,7 @@ export const testUndoEvents = tc => { export const testTrackClass = tc => { const { users, text0 } = init(tc, { users: 3 }) // only track origins that are numbers - const undoManager = new UndoManager(text0, { trackedOrigins: new Set([Number]) }) + const undoManager = new Y.UndoManager(text0, { trackedOrigins: new Set([Number]) }) users[0].transact(() => { text0.insert(0, 'abc') }, 42) @@ -240,8 +236,8 @@ export const testTypeScope = tc => { const text0 = new Y.Text() const text1 = new Y.Text() array0.insert(0, [text0, text1]) - const undoManager = new UndoManager(text0) - const undoManagerBoth = new UndoManager([text0, text1]) + const undoManager = new Y.UndoManager(text0) + const undoManagerBoth = new Y.UndoManager([text0, text1]) text1.insert(0, 'abc') t.assert(undoManager.undoStack.length === 0) t.assert(undoManagerBoth.undoStack.length === 1) @@ -260,7 +256,7 @@ export const testUndoDeleteFilter = tc => { * @type {Array>} */ const array0 = /** @type {any} */ (init(tc, { users: 3 }).array0) - const undoManager = new UndoManager(array0, { deleteFilter: item => !(item instanceof Y.Item) || (item.content instanceof Y.ContentType && item.content.type._map.size === 0) }) + const undoManager = new Y.UndoManager(array0, { deleteFilter: item => !(item instanceof Y.Item) || (item.content instanceof Y.ContentType && item.content.type._map.size === 0) }) const map0 = new Y.Map() map0.set('hi', 1) const map1 = new Y.Map() diff --git a/tests/y-text.tests.js b/tests/y-text.tests.js index d16bc0a7..2a555237 100644 --- a/tests/y-text.tests.js +++ b/tests/y-text.tests.js @@ -157,7 +157,7 @@ export const testGetDeltaWithEmbeds = tc => { export const testTypesAsEmbed = tc => { const { text0, text1, testConnector } = init(tc, { users: 2 }) text0.applyDelta([{ - insert: new Y.YMap([['key', 'val']]) + insert: new Y.Map([['key', 'val']]) }]) t.compare(text0.toDelta()[0].insert.toJSON(), { key: 'val' }) let firedEvent = false @@ -714,7 +714,7 @@ const qChanges = [ if (prng.bool(gen)) { ytext.insertEmbed(insertPos, { image: 'https://user-images.githubusercontent.com/5553757/48975307-61efb100-f06d-11e8-9177-ee895e5916e5.png' }) } else { - ytext.insertEmbed(insertPos, new Y.YMap([[prng.word(gen), prng.word(gen)]])) + ytext.insertEmbed(insertPos, new Y.Map([[prng.word(gen), prng.word(gen)]])) } }, /**