From cdbc2de15ee282110fff94022f9b25081dfdf65a Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 16 Jan 2024 16:17:15 +0200 Subject: [PATCH] add tests for empty dict and set --- test/dict-sync.test.js | 71 ++++++++++++++++++++++++++++++++++++++++++ test/set-sync.test.js | 70 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 137 insertions(+), 4 deletions(-) diff --git a/test/dict-sync.test.js b/test/dict-sync.test.js index 50377a2..f2f4cff 100644 --- a/test/dict-sync.test.js +++ b/test/dict-sync.test.js @@ -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 // \ diff --git a/test/set-sync.test.js b/test/set-sync.test.js index bf06fef..39ad839 100644 --- a/test/set-sync.test.js +++ b/test/set-sync.test.js @@ -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'],