Files
yjs/tests/y-map.tests.js

721 lines
20 KiB
JavaScript
Raw Normal View History

import * as Y from '../src/index.js'
import { init, compare, applyRandomTests, Doc } from './testHelper.js' // eslint-disable-line
2026-03-27 01:29:21 +01:00
import { noAttributionsManager, TwosetAttributionManager } from '../src/utils/AttributionManager.js'
import { createIdMapFromIdSet } from '../src/utils/ids.js'
2021-05-14 18:53:24 +02:00
import * as t from 'lib0/testing'
import * as prng from 'lib0/prng'
import * as delta from 'lib0/delta'
import * as s from 'lib0/schema'
2025-10-28 17:51:10 +01:00
import * as object from 'lib0/object'
2017-08-09 02:21:17 +02:00
2023-11-20 12:53:58 +01:00
/**
* @param {t.TestCase} _tc
*/
export const testIterators = _tc => {
const ydoc = new Y.Doc()
/**
2025-12-29 11:37:01 +01:00
* @type {Y.Type<{attrs: { [k:string]: number} }>}
2023-11-20 12:53:58 +01:00
*/
2025-12-29 11:37:01 +01:00
const ymap = ydoc.get()
2023-11-20 12:53:58 +01:00
// we are only checking if the type assumptions are correct
/**
* @type {Array<number>}
*/
2025-12-29 11:37:01 +01:00
const vals = Array.from(ymap.attrValues())
2023-11-20 12:53:58 +01:00
/**
* @type {Array<[string,number]>}
*/
2025-12-29 11:37:01 +01:00
const entries = Array.from(ymap.attrEntries())
2023-11-20 12:53:58 +01:00
/**
* @type {Array<string>}
*/
2025-12-29 11:37:01 +01:00
const keys = Array.from(ymap.attrKeys())
2023-11-20 12:53:58 +01:00
console.log(vals, entries, keys)
}
export const testNestedMapEvent = () => {
const ydoc = new Y.Doc()
2025-12-29 11:37:01 +01:00
const ymap = ydoc.get()
const ymapNested = ymap.setAttr('nested', new Y.Type())
let called = 0
2025-12-29 11:37:01 +01:00
ymap.observeDeep(event => {
const d = event.deltaDeep
called++
2026-02-25 15:55:42 +01:00
t.compare(d, delta.create().modifyAttr('nested', delta.create().setAttr('k', 'v')).done())
})
2026-01-10 00:57:43 +01:00
ymapNested.setAttr('k', 'v')
t.assert(called === 1)
}
export const testNestedMapEvent2 = () => {
const ydoc = new Y.Doc()
2025-12-29 11:37:01 +01:00
const yarr = ydoc.get()
const ymapNested = new Y.Type()
yarr.insert(0, [ymapNested])
let called = 0
2025-12-29 11:37:01 +01:00
yarr.observeDeep(event => {
const d = event.deltaDeep
called++
2025-12-29 11:37:01 +01:00
t.compare(d, delta.create().modify(delta.create().setAttr('k', 'v')))
})
2025-12-29 11:37:01 +01:00
ymapNested.setAttr('k', 'v')
t.assert(called === 1)
}
/**
* Computing event changes after transaction should result in an error. See yjs#539
*
* @param {t.TestCase} _tc
*/
export const testMapEventError = _tc => {
const doc = new Y.Doc()
2025-12-29 11:37:01 +01:00
const ymap = doc.get()
/**
* @type {any}
*/
let event = null
ymap.observe((e) => {
event = e
})
t.fails(() => {
t.info(event.keys)
})
t.fails(() => {
t.info(event.keys)
})
}
/**
* @param {t.TestCase} tc
*/
export const testMapHavingIterableAsConstructorParamTests = tc => {
const { map0 } = init(tc, { users: 1 })
2025-12-29 11:37:01 +01:00
const m1 = Y.Type.from(delta.create().setAttr('number', 1).setAttr('string', 'hello'))
map0.setAttr('m1', m1)
t.assert(m1.getAttr('number') === 1)
t.assert(m1.getAttr('string') === 'hello')
const m2 = Y.Type.from(delta.create(delta.$deltaAny).setAttrs({ object: { x: 1 }, boolean: true }).done())
map0.setAttr('m2', m2)
t.assert(m2.getAttr('object')?.x === 1)
t.assert(m2.getAttr('boolean') === true)
2026-01-10 17:32:29 +01:00
const m3 = new Y.Type().applyDelta(m1.toDelta()).applyDelta(m2.toDelta())
2025-12-29 11:37:01 +01:00
map0.setAttr('m3', m3)
t.assert(m3.getAttr('number') === 1)
t.assert(m3.getAttr('string') === 'hello')
t.assert(m3.getAttr('object')?.x === 1)
t.assert(m3.getAttr('boolean') === true)
}
/**
* @param {t.TestCase} tc
*/
export const testBasicMapTests = tc => {
const { testConnector, users, map0, map1, map2 } = init(tc, { users: 3 })
2017-08-09 02:21:17 +02:00
users[2].disconnect()
2025-12-29 11:37:01 +01:00
map0.setAttr('null', null)
map0.setAttr('number', 1)
map0.setAttr('string', 'hello Y')
map0.setAttr('object', { key: { key2: 'value' } })
map0.setAttr('y-map', new Y.Type())
map0.setAttr('boolean1', true)
map0.setAttr('boolean0', false)
const map = map0.getAttr('y-map')
2026-01-10 00:57:43 +01:00
map.setAttr('y-array', new Y.Type())
const array = map.getAttr('y-array')
2017-08-09 02:21:17 +02:00
array.insert(0, [0])
array.insert(0, [-1])
2025-12-29 11:37:01 +01:00
t.assert(map0.getAttr('null') === null, 'client 0 computed the change (null)')
t.assert(map0.getAttr('number') === 1, 'client 0 computed the change (number)')
t.assert(map0.getAttr('string') === 'hello Y', 'client 0 computed the change (string)')
t.assert(map0.getAttr('boolean0') === false, 'client 0 computed the change (boolean)')
t.assert(map0.getAttr('boolean1') === true, 'client 0 computed the change (boolean)')
t.compare(map0.getAttr('object'), { key: { key2: 'value' } }, 'client 0 computed the change (object)')
2026-01-10 00:57:43 +01:00
t.assert(map0.getAttr('y-map').getAttr('y-array').get(0) === -1, 'client 0 computed the change (type)')
2025-12-29 11:37:01 +01:00
t.assert(map0.attrSize === 7, 'client 0 map has correct size')
2017-08-09 02:21:17 +02:00
users[2].connect()
testConnector.flushAllMessages()
2017-08-09 02:21:17 +02:00
2025-12-29 11:37:01 +01:00
t.assert(map1.getAttr('null') === null, 'client 1 received the update (null)')
t.assert(map1.getAttr('number') === 1, 'client 1 received the update (number)')
t.assert(map1.getAttr('string') === 'hello Y', 'client 1 received the update (string)')
t.assert(map1.getAttr('boolean0') === false, 'client 1 computed the change (boolean)')
t.assert(map1.getAttr('boolean1') === true, 'client 1 computed the change (boolean)')
t.compare(map1.getAttr('object'), { key: { key2: 'value' } }, 'client 1 received the update (object)')
2026-01-10 00:57:43 +01:00
t.assert(map1.getAttr('y-map').getAttr('y-array').get(0) === -1, 'client 1 received the update (type)')
2025-12-29 11:37:01 +01:00
t.assert(map1.attrSize === 7, 'client 1 map has correct size')
2017-08-09 02:21:17 +02:00
// compare disconnected user
2025-12-29 11:37:01 +01:00
t.assert(map2.getAttr('null') === null, 'client 2 received the update (null) - was disconnected')
t.assert(map2.getAttr('number') === 1, 'client 2 received the update (number) - was disconnected')
t.assert(map2.getAttr('string') === 'hello Y', 'client 2 received the update (string) - was disconnected')
t.assert(map2.getAttr('boolean0') === false, 'client 2 computed the change (boolean)')
t.assert(map2.getAttr('boolean1') === true, 'client 2 computed the change (boolean)')
t.compare(map2.getAttr('object'), { key: { key2: 'value' } }, 'client 2 received the update (object) - was disconnected')
2026-01-10 00:57:43 +01:00
t.assert(map2.getAttr('y-map').getAttr('y-array').get(0) === -1, 'client 2 received the update (type) - was disconnected')
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
export const testGetAndSetOfMapProperty = tc => {
const { testConnector, users, map0 } = init(tc, { users: 2 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'stuffy')
map0.setAttr('undefined', undefined)
map0.setAttr('null', null)
t.compare(map0.getAttr('stuff'), 'stuffy')
2017-08-09 02:21:17 +02:00
testConnector.flushAllMessages()
2017-08-09 02:21:17 +02:00
2020-01-22 16:42:16 +01:00
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.compare(u.getAttr('stuff'), 'stuffy')
t.assert(u.getAttr('undefined') === undefined, 'undefined')
t.compare(u.getAttr('null'), null, 'null')
2017-08-09 02:21:17 +02:00
}
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
export const testYmapSetsYmap = tc => {
const { users, map0 } = init(tc, { users: 2 })
2025-12-29 11:37:01 +01:00
const map = map0.setAttr('Map', new Y.Type())
t.assert(map0.getAttr('Map') === map)
2026-01-10 00:57:43 +01:00
map.setAttr('one', 1)
t.compare(map.getAttr('one'), 1)
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
export const testYmapSetsYarray = tc => {
const { users, map0 } = init(tc, { users: 2 })
2025-12-29 11:37:01 +01:00
const array = map0.setAttr('Array', new Y.Type())
t.assert(array === map0.getAttr('Array'))
array.insert(0, [1, 2, 3])
2019-04-03 03:08:10 +02:00
// @ts-ignore
2026-01-10 13:28:41 +01:00
t.compare(map0.toJSON().attrs, { Array: { children: [1, 2, 3] } })
compare(users)
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testGetAndSetOfMapPropertySyncs = tc => {
const { testConnector, users, map0 } = init(tc, { users: 2 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'stuffy')
t.compare(map0.getAttr('stuff'), 'stuffy')
testConnector.flushAllMessages()
2020-01-22 16:42:16 +01:00
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.compare(u.getAttr('stuff'), 'stuffy')
2017-08-09 02:21:17 +02:00
}
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testGetAndSetOfMapPropertyWithConflict = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 3 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'c0')
map1.setAttr('stuff', 'c1')
testConnector.flushAllMessages()
2020-01-22 16:42:16 +01:00
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.compare(u.getAttr('stuff'), 'c1')
2017-08-09 02:21:17 +02:00
}
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
2020-06-07 07:44:37 -06:00
/**
* @param {t.TestCase} tc
*/
export const testSizeAndDeleteOfMapProperty = tc => {
const { map0 } = init(tc, { users: 1 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'c0')
map0.setAttr('otherstuff', 'c1')
t.assert(map0.attrSize === 2, `map size is ${map0.attrSize} expected 2`)
map0.deleteAttr('stuff')
t.assert(map0.attrSize === 1, `map size after delete is ${map0.attrSize}, expected 1`)
map0.deleteAttr('otherstuff')
t.assert(map0.attrSize === 0, `map size after delete is ${map0.attrSize}, expected 0`)
2020-06-07 07:44:37 -06:00
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testGetAndSetAndDeleteOfMapProperty = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 3 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'c0')
map1.setAttr('stuff', 'c1')
map1.deleteAttr('stuff')
testConnector.flushAllMessages()
2020-01-22 16:42:16 +01:00
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.assert(u.getAttr('stuff') === undefined)
2017-08-09 02:21:17 +02:00
}
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
export const testSetAndClearOfMapProperties = tc => {
const { testConnector, users, map0 } = init(tc, { users: 1 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'c0')
map0.setAttr('otherstuff', 'c1')
map0.clearAttrs()
testConnector.flushAllMessages()
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.assert(u.getAttr('stuff') === undefined)
t.assert(u.getAttr('otherstuff') === undefined)
t.assert(u.attrSize === 0, `map size after clear is ${u.attrSize}, expected 0`)
}
compare(users)
}
/**
* @param {t.TestCase} tc
*/
export const testSetAndClearOfMapPropertiesWithConflicts = tc => {
const { testConnector, users, map0, map1, map2, map3 } = init(tc, { users: 4 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'c0')
map1.setAttr('stuff', 'c1')
map1.setAttr('stuff', 'c2')
map2.setAttr('stuff', 'c3')
testConnector.flushAllMessages()
2025-12-29 11:37:01 +01:00
map0.setAttr('otherstuff', 'c0')
map1.setAttr('otherstuff', 'c1')
map2.setAttr('otherstuff', 'c2')
map3.setAttr('otherstuff', 'c3')
map3.clearAttrs()
testConnector.flushAllMessages()
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.assert(u.getAttr('stuff') === undefined)
t.assert(u.getAttr('otherstuff') === undefined)
t.assert(u.attrSize === 0, `map size after clear is ${u.attrSize}, expected 0`)
}
compare(users)
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testGetAndSetOfMapPropertyWithThreeConflicts = tc => {
const { testConnector, users, map0, map1, map2 } = init(tc, { users: 3 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'c0')
map1.setAttr('stuff', 'c1')
map1.setAttr('stuff', 'c2')
map2.setAttr('stuff', 'c3')
testConnector.flushAllMessages()
2020-01-22 16:42:16 +01:00
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.compare(u.getAttr('stuff'), 'c3')
2017-08-09 02:21:17 +02:00
}
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testGetAndSetAndDeleteOfMapPropertyWithThreeConflicts = tc => {
const { testConnector, users, map0, map1, map2, map3 } = init(tc, { users: 4 })
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'c0')
map1.setAttr('stuff', 'c1')
map1.setAttr('stuff', 'c2')
map2.setAttr('stuff', 'c3')
testConnector.flushAllMessages()
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 'deleteme')
map1.setAttr('stuff', 'c1')
map2.setAttr('stuff', 'c2')
map3.setAttr('stuff', 'c3')
map3.deleteAttr('stuff')
testConnector.flushAllMessages()
2020-01-22 16:42:16 +01:00
for (const user of users) {
2025-12-29 11:37:01 +01:00
const u = user.get('map')
t.assert(u.getAttr('stuff') === undefined)
2017-08-09 02:21:17 +02:00
}
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
2019-04-03 03:08:10 +02:00
/**
* @param {Object<string,any>} is
* @param {Object<string,any>} should
*/
const compareEvent = (is, should) => {
2020-12-19 16:29:17 +01:00
for (const key in should) {
t.compare(should[key], is[key])
2017-08-09 02:21:17 +02:00
}
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testThrowsAddAndUpdateAndDeleteEvents = tc => {
const { users, map0 } = init(tc, { users: 2 })
2019-04-03 03:08:10 +02:00
/**
* @type {Object<string,any>}
*/
let event = {}
2018-11-25 03:17:00 +01:00
map0.observe(e => {
2017-08-09 02:21:17 +02:00
event = e // just put it on event, should be thrown synchronously anyway
})
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 4)
2019-04-03 03:08:10 +02:00
compareEvent(event, {
2019-03-11 17:52:51 +01:00
target: map0,
keysChanged: new Set(['stuff'])
2017-08-09 02:21:17 +02:00
})
// update, oldValue is in contents
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', new Y.Type())
2019-04-03 03:08:10 +02:00
compareEvent(event, {
2019-03-11 17:52:51 +01:00
target: map0,
keysChanged: new Set(['stuff'])
2017-08-09 02:21:17 +02:00
})
// update, oldValue is in opContents
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 5)
2017-08-09 02:21:17 +02:00
// delete
2025-12-29 11:37:01 +01:00
map0.deleteAttr('stuff')
2019-04-03 03:08:10 +02:00
compareEvent(event, {
2019-03-11 17:52:51 +01:00
keysChanged: new Set(['stuff']),
target: map0
2017-08-09 02:21:17 +02:00
})
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
export const testThrowsDeleteEventsOnClear = tc => {
const { users, map0 } = init(tc, { users: 2 })
/**
* @type {Object<string,any>}
*/
let event = {}
map0.observe(e => {
event = e // just put it on event, should be thrown synchronously anyway
})
// set values
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 4)
map0.setAttr('otherstuff', new Y.Type())
// clear
2025-12-29 11:37:01 +01:00
map0.clearAttrs()
compareEvent(event, {
keysChanged: new Set(['stuff', 'otherstuff']),
target: map0
})
compare(users)
}
2019-09-03 16:33:29 +02:00
/**
* @param {t.TestCase} tc
*/
export const testChangeEvent = tc => {
const { map0, users } = init(tc, { users: 2 })
/**
2025-12-29 11:37:01 +01:00
* @type {delta.Delta<any>?}
2019-09-03 16:33:29 +02:00
*/
let changes = delta.create()
2019-09-03 16:33:29 +02:00
map0.observe(e => {
changes = e.delta
2019-09-03 16:33:29 +02:00
})
2025-12-29 11:37:01 +01:00
map0.setAttr('a', 1)
2025-10-28 17:51:10 +01:00
let keyChange = changes.attrs.a
2026-01-10 00:57:43 +01:00
t.assert(delta.$setAttrOpWith(s.$number).check(keyChange) && keyChange.prevValue === undefined)
2025-12-29 11:37:01 +01:00
map0.setAttr('a', 2)
2025-10-28 17:51:10 +01:00
keyChange = changes.attrs.a
2026-01-10 00:57:43 +01:00
t.assert(delta.$setAttrOpWith(s.$number).check(keyChange) && keyChange.prevValue === 1)
2019-09-03 16:33:29 +02:00
users[0].transact(() => {
2025-12-29 11:37:01 +01:00
map0.setAttr('a', 3)
map0.setAttr('a', 4)
2019-09-03 16:33:29 +02:00
})
2025-10-28 17:51:10 +01:00
keyChange = changes.attrs.a
2026-01-10 00:57:43 +01:00
t.assert(delta.$setAttrOpWith(s.$number).check(keyChange) && keyChange.prevValue === 2)
2019-09-03 16:33:29 +02:00
users[0].transact(() => {
2025-12-29 11:37:01 +01:00
map0.setAttr('b', 1)
map0.setAttr('b', 2)
2019-09-03 16:33:29 +02:00
})
2025-10-28 17:51:10 +01:00
keyChange = changes.attrs.b
2026-01-10 00:57:43 +01:00
t.assert(delta.$setAttrOpWith(s.$number).check(keyChange) && keyChange.prevValue === undefined)
2019-09-03 16:33:29 +02:00
users[0].transact(() => {
2025-12-29 11:37:01 +01:00
map0.setAttr('c', 1)
map0.deleteAttr('c')
2019-09-03 16:33:29 +02:00
})
2025-10-28 17:51:10 +01:00
t.assert(changes !== null && object.isEmpty(changes.attrs))
2019-09-03 16:33:29 +02:00
users[0].transact(() => {
2025-12-29 11:37:01 +01:00
map0.setAttr('d', 1)
map0.setAttr('d', 2)
2019-09-03 16:33:29 +02:00
})
2025-10-28 17:51:10 +01:00
keyChange = changes.attrs.d
2026-01-10 00:57:43 +01:00
t.assert(delta.$setAttrOpWith(s.$number).check(keyChange) && keyChange.prevValue === undefined)
2019-09-03 16:33:29 +02:00
compare(users)
}
/**
2023-01-31 12:16:03 +01:00
* @param {t.TestCase} _tc
*/
2023-01-31 12:16:03 +01:00
export const testYmapEventExceptionsShouldCompleteTransaction = _tc => {
const doc = new Y.Doc()
2025-12-29 11:37:01 +01:00
const map = doc.get('map')
let updateCalled = false
let throwingObserverCalled = false
let throwingDeepObserverCalled = false
doc.on('update', () => {
updateCalled = true
})
const throwingObserver = () => {
throwingObserverCalled = true
throw new Error('Failure')
}
const throwingDeepObserver = () => {
throwingDeepObserverCalled = true
throw new Error('Failure')
}
map.observe(throwingObserver)
map.observeDeep(throwingDeepObserver)
t.fails(() => {
2025-12-29 11:37:01 +01:00
map.setAttr('y', '2')
})
t.assert(updateCalled)
t.assert(throwingObserverCalled)
t.assert(throwingDeepObserverCalled)
// check if it works again
updateCalled = false
throwingObserverCalled = false
throwingDeepObserverCalled = false
t.fails(() => {
2025-12-29 11:37:01 +01:00
map.setAttr('z', '3')
})
t.assert(updateCalled)
t.assert(throwingObserverCalled)
t.assert(throwingDeepObserverCalled)
2025-12-29 11:37:01 +01:00
t.assert(map.getAttr('z') === '3')
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testYmapEventHasCorrectValueWhenSettingAPrimitive = tc => {
const { users, map0 } = init(tc, { users: 3 })
2019-04-03 03:08:10 +02:00
/**
* @type {Object<string,any>}
*/
let event = {}
2018-11-25 03:17:00 +01:00
map0.observe(e => {
2017-08-09 02:21:17 +02:00
event = e
})
2025-12-29 11:37:01 +01:00
map0.setAttr('stuff', 2)
2017-10-26 19:50:43 +02:00
t.compare(event.value, event.target.get(event.name))
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testYmapEventHasCorrectValueWhenSettingAPrimitiveFromOtherUser = tc => {
const { users, map0, map1, testConnector } = init(tc, { users: 3 })
2019-04-03 03:08:10 +02:00
/**
* @type {Object<string,any>}
*/
let event = {}
2018-11-25 03:17:00 +01:00
map0.observe(e => {
2017-08-09 02:21:17 +02:00
event = e
})
2025-12-29 11:37:01 +01:00
map1.setAttr('stuff', 2)
2019-03-11 12:31:37 +01:00
testConnector.flushAllMessages()
2017-10-26 19:50:43 +02:00
t.compare(event.value, event.target.get(event.name))
2019-03-11 12:31:37 +01:00
compare(users)
}
2017-08-09 02:21:17 +02:00
2025-04-28 02:42:06 +02:00
/**
* @param {t.TestCase} _tc
*/
export const testAttributedContent = _tc => {
const ydoc = new Y.Doc({ gc: false })
2025-12-29 11:37:01 +01:00
const ymap = ydoc.get()
2025-04-28 02:42:06 +02:00
let attributionManager = noAttributionsManager
ydoc.on('afterTransaction', tr => {
// attributionManager = new TwosetAttributionManager(createIdMapFromIdSet(tr.insertSet, [new Y.Attribution('insertedAt', 42), new Y.Attribution('insert', 'kevin')]), createIdMapFromIdSet(tr.deleteSet, [new Y.Attribution('delete', 'kevin')]))
attributionManager = new TwosetAttributionManager(createIdMapFromIdSet(tr.insertSet, []), createIdMapFromIdSet(tr.deleteSet, []))
})
t.group('initial value', () => {
2025-12-29 11:37:01 +01:00
ymap.setAttr('test', 42)
const expectedContent = { test: delta.$deltaMapChangeJson.expect({ type: 'insert', value: 42, attribution: { insert: [] } }) }
2026-01-10 17:32:29 +01:00
const attributedContent = ymap.toDelta(attributionManager)
console.log(attributedContent.toJSON())
t.compare(expectedContent, attributedContent.toJSON().attrs)
2025-04-28 02:42:06 +02:00
})
t.group('overwrite value', () => {
2025-12-29 11:37:01 +01:00
ymap.setAttr('test', 'fourtytwo')
2025-10-28 17:51:10 +01:00
const expectedContent = { test: delta.$deltaMapChangeJson.expect({ type: 'insert', value: 'fourtytwo', attribution: { insert: [] } }) }
2026-01-10 17:32:29 +01:00
const attributedContent = ymap.toDelta(attributionManager)
2025-04-28 02:42:06 +02:00
console.log(attributedContent)
t.compare(expectedContent, attributedContent.toJSON().attrs)
2025-04-28 02:42:06 +02:00
})
t.group('delete value', () => {
2025-12-29 11:37:01 +01:00
ymap.deleteAttr('test')
2026-02-25 16:16:46 +01:00
const expectedContent = { test: delta.$deltaMapChangeJson.expect({ type: 'delete', attribution: { delete: [] } }) }
2026-01-10 17:32:29 +01:00
const attributedContent = ymap.toDelta(attributionManager)
console.log(attributedContent.toJSON())
t.compare(expectedContent, attributedContent.toJSON().attrs)
2025-04-28 02:42:06 +02:00
})
}
2019-04-03 03:08:10 +02:00
/**
* @type {Array<function(Doc,prng.PRNG):void>}
2019-04-03 03:08:10 +02:00
*/
2019-03-11 12:31:37 +01:00
const mapTransactions = [
2019-04-03 03:08:10 +02:00
function set (user, gen) {
2020-01-22 16:42:16 +01:00
const key = prng.oneOf(gen, ['one', 'two'])
2020-12-19 16:29:17 +01:00
const value = prng.utf16String(gen)
2025-12-29 11:37:01 +01:00
user.get('map').setAttr(key, value)
2017-08-09 02:21:17 +02:00
},
2019-04-03 03:08:10 +02:00
function setType (user, gen) {
2020-01-22 16:42:16 +01:00
const key = prng.oneOf(gen, ['one', 'two'])
2025-12-29 11:37:01 +01:00
const type = new Y.Type()
user.get('map').setAttr(key, type)
if (prng.bool(gen)) {
2017-08-09 02:21:17 +02:00
type.insert(0, [1, 2, 3, 4])
} else {
2025-12-29 11:37:01 +01:00
type.setAttr('deepkey', 'deepvalue')
2017-08-09 02:21:17 +02:00
}
},
2019-04-03 03:08:10 +02:00
function _delete (user, gen) {
2020-01-22 16:42:16 +01:00
const key = prng.oneOf(gen, ['one', 'two'])
2025-12-29 11:37:01 +01:00
user.get('map').deleteAttr(key)
2017-08-09 02:21:17 +02:00
}
]
/**
* @param {t.TestCase} tc
*/
2019-04-09 00:31:17 +02:00
export const testRepeatGeneratingYmapTests10 = tc => {
applyRandomTests(tc, mapTransactions, 3)
2019-03-11 12:31:37 +01:00
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests40 = tc => {
applyRandomTests(tc, mapTransactions, 40)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests42 = tc => {
applyRandomTests(tc, mapTransactions, 42)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests43 = tc => {
applyRandomTests(tc, mapTransactions, 43)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests44 = tc => {
applyRandomTests(tc, mapTransactions, 44)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests45 = tc => {
applyRandomTests(tc, mapTransactions, 45)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests46 = tc => {
applyRandomTests(tc, mapTransactions, 46)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests300 = tc => {
applyRandomTests(tc, mapTransactions, 300)
}
2017-08-09 02:21:17 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests400 = tc => {
applyRandomTests(tc, mapTransactions, 400)
}
2017-10-26 19:50:43 +02:00
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests500 = tc => {
applyRandomTests(tc, mapTransactions, 500)
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests600 = tc => {
applyRandomTests(tc, mapTransactions, 600)
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests1000 = tc => {
applyRandomTests(tc, mapTransactions, 1000)
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests1800 = tc => {
applyRandomTests(tc, mapTransactions, 1800)
}
2019-04-09 00:48:24 +02:00
/**
* @param {t.TestCase} tc
*/
export const testRepeatGeneratingYmapTests5000 = tc => {
t.skip(!t.production)
applyRandomTests(tc, mapTransactions, 5000)
}
/**
* @param {t.TestCase} tc
*/
2019-03-11 12:31:37 +01:00
export const testRepeatGeneratingYmapTests10000 = tc => {
2019-04-09 00:48:24 +02:00
t.skip(!t.production)
2019-03-11 12:31:37 +01:00
applyRandomTests(tc, mapTransactions, 10000)
}
2019-04-09 00:31:17 +02:00
/**
* @param {t.TestCase} tc
*/
export const testRepeatGeneratingYmapTests100000 = tc => {
t.skip(!t.production)
applyRandomTests(tc, mapTransactions, 100000)
}