From 81c53c326425fad5af4e1b6830b75f3f4065c30c Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Wed, 5 Apr 2023 18:02:34 +0200 Subject: [PATCH] Add test for branchStream root opt --- package.json | 2 + test/api/branch-stream.test.js | 88 +++++++++++++++++++++++++++++++++- test/testbot.js | 2 + 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0107a11..72493ac 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "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", @@ -45,6 +46,7 @@ "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" }, diff --git a/test/api/branch-stream.test.js b/test/api/branch-stream.test.js index 7ef8f91..1c209a8 100644 --- a/test/api/branch-stream.test.js +++ b/test/api/branch-stream.test.js @@ -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') @@ -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') @@ -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) +}) diff --git a/test/testbot.js b/test/testbot.js index aa3159c..ff9d296 100644 --- a/test/testbot.js +++ b/test/testbot.js @@ -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({