From 93f00dbd04267f472fbf2f3ae63495092d3a921e Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 5 Mar 2024 17:07:33 +0200 Subject: [PATCH] Test that dataless msgs can be replaced by dataful --- lib/algorithm.js | 2 +- package.json | 10 ++-- test/dict-sync.test.js | 24 ++++----- test/feed-sync.test.js | 119 +++++++++++++++++++++++++++++++++++++++++ test/set-sync.test.js | 56 +++++++++---------- 5 files changed, 164 insertions(+), 47 deletions(-) diff --git a/lib/algorithm.js b/lib/algorithm.js index bd3c569..01fc722 100644 --- a/lib/algorithm.js +++ b/lib/algorithm.js @@ -86,7 +86,7 @@ class Algorithm { const [minRemoteHave, maxRemoteHave] = remoteHaveRange if (maxRemoteHave < minLocalHave) return EMPTY_RANGE const maxWant = maxRemoteHave - const size = Math.max(maxWant - maxLocalHave, count) + const size = count > maxWant - maxLocalHave ? count : maxWant - maxLocalHave const minWant = Math.max( maxWant - size + 1, maxLocalHave - size + 1, diff --git a/package.json b/package.json index b19c9ba..99d7a9e 100644 --- a/package.json +++ b/package.json @@ -38,11 +38,11 @@ "bs58": "^5.0.0", "c8": "7", "ppppp-caps": "github:staltz/ppppp-caps#93fa810b9a40b78aef4872d4c2a8412cccb52929", - "ppppp-db": "github:staltz/ppppp-db#667b33779d98aff12a9b0cd2d7c80469a95cd04e", - "ppppp-dict": "github:staltz/ppppp-dict#6f0ff4e3383a8c18b766949f6db9b51460ecb640", - "ppppp-goals": "github:staltz/ppppp-goals#f862c2de624649906a4375711f3813db3b94a2ca", - "ppppp-keypair": "github:staltz/ppppp-keypair#61ef4420578f450dc2cc7b1efc1c5a691a871c74", - "ppppp-set": "github:staltz/ppppp-set#8983ba29f03db95a76b4bd9a55aa4392b350fdbb", + "ppppp-db": "github:staltz/ppppp-db#cf1532965ea1d16929ed2291a9b737a4ce74caac", + "ppppp-dict": "github:staltz/ppppp-dict#c40d51be6cb96982b4fe691a292b3c12b6f49a36", + "ppppp-goals": "github:staltz/ppppp-goals#46a8d8889c668cf291607963fd7301f21aa634b5", + "ppppp-keypair": "github:staltz/ppppp-keypair#c33980c580e33f9a35cb0c672b916ec9fe8b4c6d", + "ppppp-set": "github:staltz/ppppp-set#07c3e295b2d09d2d6c3ac6b5b93ad2ea80698452", "prettier": "^2.6.2", "pretty-quick": "^3.1.3", "rimraf": "^4.4.0", diff --git a/test/dict-sync.test.js b/test/dict-sync.test.js index f2f4cff..d66077f 100644 --- a/test/dict-sync.test.js +++ b/test/dict-sync.test.js @@ -181,13 +181,11 @@ test('sync goal=dict with ghostSpan=2', async (t) => { } // 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.deepEqual( + alice.dict.read(aliceID, 'profile'), + { age: 25, name: 'ALICE' }, + 'alice has age+name dict' + ) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec1.id, rec2.id]) // Trigger sync @@ -200,13 +198,11 @@ test('sync goal=dict with ghostSpan=2', async (t) => { assert('sync!') // Assert situation at Alice before sync: she got the branched off msg - { - 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', 'w'], 'alice has age+name+gender dict') - } + assert.deepEqual( + alice.dict.read(aliceID, 'profile'), + { age: 25, name: 'ALICE', gender: 'w' }, + 'alice has age+name+gender dict' + ) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec2.id]) await p(remoteAlice.close)(true) diff --git a/test/feed-sync.test.js b/test/feed-sync.test.js index 66bae2a..1026f0c 100644 --- a/test/feed-sync.test.js +++ b/test/feed-sync.test.js @@ -392,3 +392,122 @@ test('sync a feed with goal=newest but too far behind', async (t) => { await p(alice.close)(true) await p(bob.close)(true) }) + +// Bob replicates a small "newest" part of Carol's feed, then +// Alice replicates what Bob has, even though she wants more. +// Finally, Alice replicates from Carol the whole feed. +test('sync small newest slice of a feed, then the whole feed', async (t) => { + const alice = createPeer({ name: 'alice' }) + const bob = createPeer({ name: 'bob' }) + const carol = createPeer({ name: 'carol' }) + + await alice.db.loaded() + await bob.db.loaded() + await carol.db.loaded() + + const carolID = await p(carol.db.account.create)({ + subdomain: 'account', + _nonce: 'carol', + }) + const carolIDMsg = carol.db.get(carolID) + + // Alice and Bob know Carol + await p(alice.db.add)(carolIDMsg, carolID) + await p(bob.db.add)(carolIDMsg, carolID) + + const carolPosts = [] + for (let i = 1; i <= 9; i++) { + const rec = await p(carol.db.feed.publish)({ + account: carolID, + domain: 'post', + data: { text: 'm' + i }, + }) + carolPosts.push(rec.msg) + } + + const carolPostsMootID = carol.db.feed.getID(carolID, 'post') + const carolPostsMoot = carol.db.get(carolPostsMootID) + + { + const arr = [...bob.db.msgs()] + .filter((msg) => msg.metadata.account === carolID && msg.data) + .map((msg) => msg.data.text) + assert.deepEqual(arr, [], 'bob has nothing from carol') + } + + { + const arr = [...alice.db.msgs()] + .filter((msg) => msg.metadata.account === carolID && msg.data) + .map((msg) => msg.data.text) + assert.deepEqual(arr, [], 'alice has nothing from carol') + } + + alice.goals.set(carolPostsMootID, 'all') + bob.goals.set(carolPostsMootID, 'newest-4') + carol.goals.set(carolPostsMootID, 'all') + + const bobDialingCarol = await p(bob.connect)(carol.getAddress()) + assert('bob connected to carol') + + bob.sync.start() + await p(setTimeout)(1000) + assert('sync!') + + { + const arr = [...bob.db.msgs()] + .filter((msg) => msg.metadata.account === carolID && msg.data) + .map((msg) => msg.data.text) + assert.deepEqual( + arr, + ['m6', 'm7', 'm8', 'm9'], + 'bob has msgs 6..9 from carol' + ) + } + + await p(bobDialingCarol.close)(true) + + const aliceDialingBob = await p(alice.connect)(bob.getAddress()) + assert('alice connected to bob') + + alice.sync.start() + await p(setTimeout)(1000) + assert('sync!') + + { + const arr = [...alice.db.msgs()] + .filter((msg) => msg.metadata.account === carolID && msg.data) + .map((msg) => msg.data.text) + assert.deepEqual( + arr, + ['m6', 'm7', 'm8', 'm9'], + 'alice has msgs 6..9 from carol' + ) + } + + await p(aliceDialingBob.close)(true) + + const aliceDialingCarol = await p(alice.connect)(carol.getAddress()) + assert('alice connected to alice') + + alice.sync.start() + await p(setTimeout)(2000) + assert('sync!') + + { + const arr = [...alice.db.msgs()] + .filter((msg) => msg.metadata.account === carolID && msg.data) + .map((msg) => msg.data.text) + .sort() + assert.deepEqual( + arr, + ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9'], + 'alice has msgs 1..9 from carol' + ) + } + + await p(aliceDialingCarol.close)(true) + + await p(alice.close)(true) + await p(bob.close)(true) + await p(carol.close)(true) +}) diff --git a/test/set-sync.test.js b/test/set-sync.test.js index 39ad839..2ef4878 100644 --- a/test/set-sync.test.js +++ b/test/set-sync.test.js @@ -38,6 +38,11 @@ test('sync goal=set from scratch', async (t) => { const aliceAccountRoot = alice.db.get(aliceID) // Bob knows Alice + const bobID = await p(bob.db.account.create)({ + subdomain: 'account', + _nonce: 'bob', + }) + await p(bob.set.load)(bobID) await p(bob.db.add)(aliceAccountRoot, aliceID) // Alice constructs a set @@ -46,16 +51,14 @@ test('sync goal=set from scratch', async (t) => { 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.deepEqual( + alice.set.values('names', aliceID), + ['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') - } + assert.deepEqual(bob.set.values('names', aliceID), [], 'bob has empty set') // Trigger sync alice.goals.set(mootID, 'set') @@ -63,14 +66,15 @@ test('sync goal=set from scratch', async (t) => { const remoteAlice = await p(bob.connect)(alice.getAddress()) assert('bob connected to alice') bob.sync.start() - await p(setTimeout)(1000) + await p(setTimeout)(2000) 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') - } + assert.deepEqual( + bob.set.values('names', aliceID), + ['Alice', 'Bob'], + 'bob has Alice+Bob set' + ) await p(remoteAlice.close)(true) await p(alice.close)(true) @@ -83,7 +87,7 @@ test('sync goal=set from scratch', async (t) => { // o // // where "o" is a set update and "?" is a ghost -test('sync goal=set with ghostSpan=2', async (t) => { +test('sync goal=set with ghostSpan=5', async (t) => { const SPAN = 5 const alice = createPeer({ name: 'alice', @@ -184,10 +188,11 @@ test('sync goal=set with ghostSpan=2', async (t) => { } // Assert situation at Alice before sync - { - const arr = getItems('follows', [...alice.db.msgs()]) - assert.deepEqual(arr, ['Alice', 'Bob'], 'alice has Alice+Bob set') - } + assert.deepEqual( + alice.set.values('follows', aliceID), + ['Alice', 'Bob'], + 'alice has Alice+Bob set' + ) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec1.id, rec2.id]) // Trigger sync @@ -196,18 +201,15 @@ test('sync goal=set with ghostSpan=2', async (t) => { const remoteAlice = await p(bob.connect)(alice.getAddress()) assert('bob connected to alice') bob.sync.start() - await p(setTimeout)(1000) + await p(setTimeout)(2000) assert('sync!') // Assert situation at Alice after sync: she got the branched off msg - { - const arr = getItems('follows', [...alice.db.msgs()]) - assert.deepEqual( - arr, - ['Alice', 'Bob', 'Carol'], - 'alice has Alice+Bob+Carol set' - ) - } + assert.deepEqual( + alice.set.values('follows', aliceID), + ['Carol', 'Alice', 'Bob'], + 'alice has Alice+Bob+Carol set' + ) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec2.id]) await p(remoteAlice.close)(true)