Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for branchStream root opt #122

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
"husky": "^4.3.0",
"prettier": "^2.1.2",
"pretty-quick": "^3.1.0",
"pull-many": "^1.0.9",
"rimraf": "^3.0.2",
"secret-stack": "^6.4.0",
"ssb-bendy-butt": "^1.0.1",
"ssb-box2": "^3.0.0",
"ssb-caps": "^1.1.0",
"ssb-classic": "^1.1.0",
"ssb-db2": "^6.3.0",
"ssb-ebt": "^9.1.2",
"tap-arc": "^0.3.5",
"tape": "^5.6.1"
},
Expand Down
88 changes: 87 additions & 1 deletion test/api/branch-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const test = require('tape')
const pull = require('pull-stream')
const pullMany = require('pull-many')
const ssbKeys = require('ssb-keys')
const p = require('util').promisify
const Testbot = require('../testbot')
Expand Down Expand Up @@ -238,7 +239,7 @@ test('branchStream can reprocess encrypted announces', async (t) => {

let expectedLive = ['root/v1/2/group']
pull(
sbot.metafeeds.branchStream({old: false, live: true}),
sbot.metafeeds.branchStream({ old: false, live: true }),
pull.drain((branch) => {
const path = branch.map((f) => f.purpose).join('/')
t.equals(path, expectedLive.shift(), 'branchStream can decrypt announce')
Expand Down Expand Up @@ -289,3 +290,88 @@ test('branchStream can reprocess encrypted announces', async (t) => {

await p(sbot.close)(true)
})

// replicate from person2 to person1 and vice versa
async function replicate(person1, person2) {
pull(
pullMany([
person1.metafeeds.branchStream({ old: true, live: true }),
person2.metafeeds.branchStream({ old: true, live: true }),
]),
pull.flatten(),
pull.map((feedDetails) => feedDetails.id),
pull.unique(),
pull.asyncMap((feedId, cb) => {
// hack to make it look like we request feeds in the right order
// instead of just one big pile, ssb-meta-feeds operates under
// the assumption that we get messages in proper order
console.log('setting timeout for', feedId)
setTimeout(() => cb(null, feedId), 200)
}, 1),
(drain = pull.drain((feedId) => {
person1.ebt.request(feedId, true)
person2.ebt.request(feedId, true)
}))
)

await p(setTimeout)(3000)
}

test('branchStream with root opt', async (t) => {
const alice = Testbot()
const bob = Testbot()

const aliceRoot = await p(alice.metafeeds.findOrCreate)()
const bobRoot = await p(bob.metafeeds.findOrCreate)()

await p(alice.metafeeds.findOrCreate)({ purpose: 'potatoes' })
await p(alice.metafeeds.findOrCreate)({ purpose: 'onions' })
await p(bob.metafeeds.findOrCreate)({ purpose: 'dogs' })
await p(bob.metafeeds.findOrCreate)({ purpose: 'ducks' })

await replicate(alice, bob)

const aliceBranches = await pull(
alice.metafeeds.branchStream({
root: aliceRoot.id,
old: true,
live: false,
}),
pull.collectAsPromise()
)

const alicePurposes = aliceBranches
.filter((branch) => branch.length === 4)
.map((branch) => branch[3].purpose)

t.equals(alicePurposes.length, 3, 'correct amount of feeds for alice')
t.deepEquals(
alicePurposes.sort(),
['main', 'onions', 'potatoes'],
'alice has all her feeds'
)

const bobBranches = await pull(
// still from alice's perspective
alice.metafeeds.branchStream({
root: bobRoot.id,
old: true,
live: false,
}),
pull.collectAsPromise()
)

const bobPurposes = bobBranches
.filter((branch) => branch.length === 4)
.map((branch) => branch[3].purpose)

t.equals(bobPurposes.length, 3, 'correct amount of feeds for bob')
t.deepEquals(
bobPurposes.sort(),
['dogs', 'ducks', 'main'],
'alice has all of bobs feeds'
)

await p(alice.close)(true)
await p(bob.close)(true)
})
2 changes: 2 additions & 0 deletions test/testbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module.exports = function createSbot(opts = {}) {
.use(require('ssb-bendy-butt'))
.use(require('ssb-classic'))
.use(require('ssb-box2'))
.use(require('ssb-db2/compat/ebt'))
.use(require('ssb-ebt'))
.use(require('../'))

return stack({
Expand Down