From 7a84f8b51e56b2702dee3614d1e15a92610010f8 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 26 May 2023 14:46:08 +0300 Subject: [PATCH] tests for identity tangle sync --- test/identity-sync.test.js | 86 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 test/identity-sync.test.js diff --git a/test/identity-sync.test.js b/test/identity-sync.test.js new file mode 100644 index 0000000..ac30649 --- /dev/null +++ b/test/identity-sync.test.js @@ -0,0 +1,86 @@ +const test = require('tape') +const path = require('path') +const os = require('os') +const rimraf = require('rimraf') +const SecretStack = require('secret-stack') +const caps = require('ssb-caps') +const p = require('util').promisify +const { generateKeypair } = require('./util') + +const createSSB = SecretStack({ appKey: caps.shs }) + .use(require('ppppp-db')) + .use(require('ssb-box')) + .use(require('../lib')) + +const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice') +const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob') +const aliceKeys = generateKeypair('alice') +const bobKeys = generateKeypair('bob') + +function getIdentity(iter) { + return [...iter] + .filter((msg) => msg.metadata.group === null && msg.data) + .map((msg) => msg.data.add) +} + +test('sync an identity tangle', async (t) => { + rimraf.sync(ALICE_DIR) + rimraf.sync(BOB_DIR) + + const alice = createSSB({ + keys: aliceKeys, + path: ALICE_DIR, + }) + + const bob = createSSB({ + keys: bobKeys, + path: BOB_DIR, + }) + + await alice.db.loaded() + await bob.db.loaded() + + // Alice's identity tangle + await alice.db.loaded() + const aliceGroupRec0 = await p(alice.db.group.create)({ _nonce: 'alice' }) + const aliceId = aliceGroupRec0.hash + await p(alice.db.add)(aliceGroupRec0.msg, aliceId) + + const aliceKeys1 = generateKeypair('alice1') + await p(alice.db.group.add)({ group: aliceId, keys: aliceKeys1 }) + + const aliceKeys2 = generateKeypair('alice2') + await p(alice.db.group.add)({ group: aliceId, keys: aliceKeys2 }) + + t.deepEquals( + getIdentity(alice.db.msgs()), + [aliceKeys.id, aliceKeys1.id, aliceKeys2.id], + "alice has her identity tangle" + ) + + t.deepEquals( + getIdentity(bob.db.msgs()), + [], + "bob doesn't have alice's identity tangle" + ) + + bob.tangleSync.setGoal(aliceId, 'all') + alice.tangleSync.setGoal(aliceId, 'all') + + const remoteAlice = await p(bob.connect)(alice.getAddress()) + t.pass('bob connected to alice') + + bob.tangleSync.initiate() + await p(setTimeout)(1000) + t.pass('tangleSync!') + + t.deepEquals( + getIdentity(bob.db.msgs()), + [aliceKeys.id, aliceKeys1.id, aliceKeys2.id], + "bob has alice's identity tangle" + ) + + await p(remoteAlice.close)(true) + await p(alice.close)(true) + await p(bob.close)(true) +})