add tests for empty dict and set

This commit is contained in:
Andre Staltz 2024-01-16 16:17:15 +02:00
parent 6e54ca2724
commit cdbc2de15e
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 137 additions and 4 deletions

View File

@ -7,6 +7,77 @@ const { createPeer } = require('./util')
const aliceKeypair = Keypair.generate('ed25519', 'alice')
test('sync goal=dict from scratch', async (t) => {
const SPAN = 5
const alice = createPeer({
name: 'alice',
global: {
keypair: aliceKeypair,
},
dict: { ghostSpan: SPAN },
})
const bob = createPeer({ name: 'bob' })
await alice.db.loaded()
await bob.db.loaded()
// Alice sets up an account and a dict
const aliceID = await p(alice.db.account.create)({
subdomain: 'account',
_nonce: 'alice',
})
await p(alice.dict.load)(aliceID)
const aliceAccountRoot = alice.db.get(aliceID)
// Bob knows Alice
await p(bob.db.add)(aliceAccountRoot, aliceID)
// Alice constructs a dict
await p(alice.dict.update)('profile', { age: 25 })
await p(alice.dict.update)('profile', { name: 'ALICE' })
const mootID = alice.dict.getFeedID('profile')
// Assert situation at Alice before sync
{
const arr = [...alice.db.msgs()]
.map((msg) => msg.data?.update)
.filter((x) => !!x)
.map((x) => x.age ?? x.name ?? x.gender)
assert.deepEqual(arr, [25, 'ALICE'], 'alice has age+name dict')
}
// Assert situation at Bob before sync
{
const arr = [...bob.db.msgs()]
.map((msg) => msg.data?.update)
.filter((x) => !!x)
.map((x) => x.age ?? x.name ?? x.gender)
assert.deepEqual(arr, [], 'alice has empty dict')
}
// Trigger sync
alice.goals.set(mootID, 'dict')
bob.goals.set(mootID, 'dict')
const remoteAlice = await p(bob.connect)(alice.getAddress())
assert('bob connected to alice')
bob.sync.start()
await p(setTimeout)(1000)
assert('sync!')
// Assert situation at Bob after sync
{
const arr = [...bob.db.msgs()]
.map((msg) => msg.data?.update)
.filter((x) => !!x)
.map((x) => x.age ?? x.name ?? x.gender)
assert.deepEqual(arr, [25, 'ALICE'], 'alice has age+name dict')
}
await p(remoteAlice.close)(true)
await p(alice.close)(true)
await p(bob.close)(true)
})
//
// R-?-?-o-o
// \

View File

@ -7,14 +7,76 @@ const { createPeer } = require('./util')
const aliceKeypair = Keypair.generate('ed25519', 'alice')
function getItems(arr) {
function getItems(subdomain, arr) {
return arr
.filter((msg) => msg.metadata.domain === 'set_v1__follows')
.filter((msg) => msg.metadata.domain === `set_v1__${subdomain}`)
.map((msg) => msg.data)
.filter((data) => !!data)
.map((data) => data.add?.[0] ?? '-' + data.del?.[0])
}
test('sync goal=set from scratch', async (t) => {
const SPAN = 5
const alice = createPeer({
name: 'alice',
global: {
keypair: aliceKeypair,
},
set: { ghostSpan: SPAN },
})
const bob = createPeer({ name: 'bob' })
await alice.db.loaded()
await bob.db.loaded()
// Alice sets up an account and a set
const aliceID = await p(alice.db.account.create)({
subdomain: 'account',
_nonce: 'alice',
})
await p(alice.set.load)(aliceID)
const aliceAccountRoot = alice.db.get(aliceID)
// Bob knows Alice
await p(bob.db.add)(aliceAccountRoot, aliceID)
// Alice constructs a set
await p(alice.set.add)('names', 'Alice')
await p(alice.set.add)('names', 'Bob')
const mootID = alice.set.getFeedID('names')
// Assert situation at Alice before sync
{
const arr = getItems('names', [...alice.db.msgs()])
assert.deepEqual(arr, ['Alice', 'Bob'], 'alice has Alice+Bob set')
}
// Assert situation at Bob before sync
{
const arr = getItems('names', [...bob.db.msgs()])
assert.deepEqual(arr, [], 'alice has empty set')
}
// Trigger sync
alice.goals.set(mootID, 'set')
bob.goals.set(mootID, 'set')
const remoteAlice = await p(bob.connect)(alice.getAddress())
assert('bob connected to alice')
bob.sync.start()
await p(setTimeout)(1000)
assert('sync!')
// Assert situation at Bob after sync
{
const arr = getItems('names', [...bob.db.msgs()])
assert.deepEqual(arr, ['Alice', 'Bob'], 'alice has Alice+Bob set')
}
await p(remoteAlice.close)(true)
await p(alice.close)(true)
await p(bob.close)(true)
})
//
// R-?-?-?-?-o-o
// \
@ -123,7 +185,7 @@ test('sync goal=set with ghostSpan=2', async (t) => {
// Assert situation at Alice before sync
{
const arr = getItems([...alice.db.msgs()])
const arr = getItems('follows', [...alice.db.msgs()])
assert.deepEqual(arr, ['Alice', 'Bob'], 'alice has Alice+Bob set')
}
assert.deepEqual(alice.db.ghosts.get(moot.id), [rec1.id, rec2.id])
@ -139,7 +201,7 @@ test('sync goal=set with ghostSpan=2', async (t) => {
// Assert situation at Alice after sync: she got the branched off msg
{
const arr = getItems([...alice.db.msgs()])
const arr = getItems('follows', [...alice.db.msgs()])
assert.deepEqual(
arr,
['Alice', 'Bob', 'Carol'],