Test that dataless msgs can be replaced by dataful

This commit is contained in:
Andre Staltz 2024-03-05 17:07:33 +02:00
parent 47eb2dd27f
commit 93f00dbd04
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
5 changed files with 164 additions and 47 deletions

View File

@ -86,7 +86,7 @@ class Algorithm {
const [minRemoteHave, maxRemoteHave] = remoteHaveRange const [minRemoteHave, maxRemoteHave] = remoteHaveRange
if (maxRemoteHave < minLocalHave) return EMPTY_RANGE if (maxRemoteHave < minLocalHave) return EMPTY_RANGE
const maxWant = maxRemoteHave const maxWant = maxRemoteHave
const size = Math.max(maxWant - maxLocalHave, count) const size = count > maxWant - maxLocalHave ? count : maxWant - maxLocalHave
const minWant = Math.max( const minWant = Math.max(
maxWant - size + 1, maxWant - size + 1,
maxLocalHave - size + 1, maxLocalHave - size + 1,

View File

@ -38,11 +38,11 @@
"bs58": "^5.0.0", "bs58": "^5.0.0",
"c8": "7", "c8": "7",
"ppppp-caps": "github:staltz/ppppp-caps#93fa810b9a40b78aef4872d4c2a8412cccb52929", "ppppp-caps": "github:staltz/ppppp-caps#93fa810b9a40b78aef4872d4c2a8412cccb52929",
"ppppp-db": "github:staltz/ppppp-db#667b33779d98aff12a9b0cd2d7c80469a95cd04e", "ppppp-db": "github:staltz/ppppp-db#cf1532965ea1d16929ed2291a9b737a4ce74caac",
"ppppp-dict": "github:staltz/ppppp-dict#6f0ff4e3383a8c18b766949f6db9b51460ecb640", "ppppp-dict": "github:staltz/ppppp-dict#c40d51be6cb96982b4fe691a292b3c12b6f49a36",
"ppppp-goals": "github:staltz/ppppp-goals#f862c2de624649906a4375711f3813db3b94a2ca", "ppppp-goals": "github:staltz/ppppp-goals#46a8d8889c668cf291607963fd7301f21aa634b5",
"ppppp-keypair": "github:staltz/ppppp-keypair#61ef4420578f450dc2cc7b1efc1c5a691a871c74", "ppppp-keypair": "github:staltz/ppppp-keypair#c33980c580e33f9a35cb0c672b916ec9fe8b4c6d",
"ppppp-set": "github:staltz/ppppp-set#8983ba29f03db95a76b4bd9a55aa4392b350fdbb", "ppppp-set": "github:staltz/ppppp-set#07c3e295b2d09d2d6c3ac6b5b93ad2ea80698452",
"prettier": "^2.6.2", "prettier": "^2.6.2",
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"rimraf": "^4.4.0", "rimraf": "^4.4.0",

View File

@ -181,13 +181,11 @@ test('sync goal=dict with ghostSpan=2', async (t) => {
} }
// Assert situation at Alice before sync // Assert situation at Alice before sync
{ assert.deepEqual(
const arr = [...alice.db.msgs()] alice.dict.read(aliceID, 'profile'),
.map((msg) => msg.data?.update) { age: 25, name: 'ALICE' },
.filter((x) => !!x) 'alice has age+name dict'
.map((x) => x.age ?? x.name ?? x.gender) )
assert.deepEqual(arr, [25, 'ALICE'], 'alice has age+name dict')
}
assert.deepEqual(alice.db.ghosts.get(moot.id), [rec1.id, rec2.id]) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec1.id, rec2.id])
// Trigger sync // Trigger sync
@ -200,13 +198,11 @@ test('sync goal=dict with ghostSpan=2', async (t) => {
assert('sync!') assert('sync!')
// Assert situation at Alice before sync: she got the branched off msg // Assert situation at Alice before sync: she got the branched off msg
{ assert.deepEqual(
const arr = [...alice.db.msgs()] alice.dict.read(aliceID, 'profile'),
.map((msg) => msg.data?.update) { age: 25, name: 'ALICE', gender: 'w' },
.filter((x) => !!x) 'alice has age+name+gender dict'
.map((x) => x.age ?? x.name ?? x.gender) )
assert.deepEqual(arr, [25, 'ALICE', 'w'], 'alice has age+name+gender dict')
}
assert.deepEqual(alice.db.ghosts.get(moot.id), [rec2.id]) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec2.id])
await p(remoteAlice.close)(true) await p(remoteAlice.close)(true)

View File

@ -392,3 +392,122 @@ test('sync a feed with goal=newest but too far behind', async (t) => {
await p(alice.close)(true) await p(alice.close)(true)
await p(bob.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)
})

View File

@ -38,6 +38,11 @@ test('sync goal=set from scratch', async (t) => {
const aliceAccountRoot = alice.db.get(aliceID) const aliceAccountRoot = alice.db.get(aliceID)
// Bob knows Alice // 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) await p(bob.db.add)(aliceAccountRoot, aliceID)
// Alice constructs a set // Alice constructs a set
@ -46,16 +51,14 @@ test('sync goal=set from scratch', async (t) => {
const mootID = alice.set.getFeedID('names') const mootID = alice.set.getFeedID('names')
// Assert situation at Alice before sync // Assert situation at Alice before sync
{ assert.deepEqual(
const arr = getItems('names', [...alice.db.msgs()]) alice.set.values('names', aliceID),
assert.deepEqual(arr, ['Alice', 'Bob'], 'alice has Alice+Bob set') ['Alice', 'Bob'],
} 'alice has Alice+Bob set'
)
// Assert situation at Bob before sync // Assert situation at Bob before sync
{ assert.deepEqual(bob.set.values('names', aliceID), [], 'bob has empty set')
const arr = getItems('names', [...bob.db.msgs()])
assert.deepEqual(arr, [], 'alice has empty set')
}
// Trigger sync // Trigger sync
alice.goals.set(mootID, 'set') 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()) const remoteAlice = await p(bob.connect)(alice.getAddress())
assert('bob connected to alice') assert('bob connected to alice')
bob.sync.start() bob.sync.start()
await p(setTimeout)(1000) await p(setTimeout)(2000)
assert('sync!') assert('sync!')
// Assert situation at Bob after sync // Assert situation at Bob after sync
{ assert.deepEqual(
const arr = getItems('names', [...bob.db.msgs()]) bob.set.values('names', aliceID),
assert.deepEqual(arr, ['Alice', 'Bob'], 'alice has Alice+Bob set') ['Alice', 'Bob'],
} 'bob has Alice+Bob set'
)
await p(remoteAlice.close)(true) await p(remoteAlice.close)(true)
await p(alice.close)(true) await p(alice.close)(true)
@ -83,7 +87,7 @@ test('sync goal=set from scratch', async (t) => {
// o // o
// //
// where "o" is a set update and "?" is a ghost // 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 SPAN = 5
const alice = createPeer({ const alice = createPeer({
name: 'alice', name: 'alice',
@ -184,10 +188,11 @@ test('sync goal=set with ghostSpan=2', async (t) => {
} }
// Assert situation at Alice before sync // Assert situation at Alice before sync
{ assert.deepEqual(
const arr = getItems('follows', [...alice.db.msgs()]) alice.set.values('follows', aliceID),
assert.deepEqual(arr, ['Alice', 'Bob'], 'alice has Alice+Bob set') ['Alice', 'Bob'],
} 'alice has Alice+Bob set'
)
assert.deepEqual(alice.db.ghosts.get(moot.id), [rec1.id, rec2.id]) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec1.id, rec2.id])
// Trigger sync // Trigger sync
@ -196,18 +201,15 @@ test('sync goal=set with ghostSpan=2', async (t) => {
const remoteAlice = await p(bob.connect)(alice.getAddress()) const remoteAlice = await p(bob.connect)(alice.getAddress())
assert('bob connected to alice') assert('bob connected to alice')
bob.sync.start() bob.sync.start()
await p(setTimeout)(1000) await p(setTimeout)(2000)
assert('sync!') assert('sync!')
// Assert situation at Alice after sync: she got the branched off msg // Assert situation at Alice after sync: she got the branched off msg
{
const arr = getItems('follows', [...alice.db.msgs()])
assert.deepEqual( assert.deepEqual(
arr, alice.set.values('follows', aliceID),
['Alice', 'Bob', 'Carol'], ['Carol', 'Alice', 'Bob'],
'alice has Alice+Bob+Carol set' 'alice has Alice+Bob+Carol set'
) )
}
assert.deepEqual(alice.db.ghosts.get(moot.id), [rec2.id]) assert.deepEqual(alice.db.ghosts.get(moot.id), [rec2.id])
await p(remoteAlice.close)(true) await p(remoteAlice.close)(true)