From a3f37381cd3fb2d6217aafce05118aa074229d1d Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 26 May 2023 14:37:15 +0300 Subject: [PATCH] test group.create() and group.add() --- test/group-add.test.js | 41 +++++++++++++++++++++++++++++ test/group-create.test.js | 54 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 test/group-add.test.js create mode 100644 test/group-create.test.js diff --git a/test/group-add.test.js b/test/group-add.test.js new file mode 100644 index 0000000..35603c4 --- /dev/null +++ b/test/group-add.test.js @@ -0,0 +1,41 @@ +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 DIR = path.join(os.tmpdir(), 'ppppp-db-group-add') +rimraf.sync(DIR) + +test('group.add()', async (t) => { + const keys1 = generateKeypair('alice') + const keys2 = generateKeypair('bob') + + const peer = SecretStack({ appKey: caps.shs }) + .use(require('../lib')) + .use(require('ssb-box')) + .call(null, { keys: keys1, path: DIR }) + + await peer.db.loaded() + const groupRec0 = await p(peer.db.group.create)({ keys: keys1 }) + const group = groupRec0.hash + + const groupRec1 = await p(peer.db.group.add)({ group, keys: keys2 }) + t.ok(groupRec1, 'groupRec1 exists') + const { hash, msg } = groupRec1 + t.ok(hash, 'hash exists') + t.equals(msg.data.add, keys2.id, 'msg.data.add NEW KEY') + t.equals(msg.metadata.group, null, 'msg.metadata.group') + t.equals(msg.metadata.groupTips, null, 'msg.metadata.groupTips') + t.deepEquals( + msg.metadata.tangles, + { [group]: { depth: 1, prev: [group] } }, + 'msg.metadata.tangles' + ) + t.equals(msg.pubkey, keys1.id, 'msg.pubkey OLD KEY') + + await p(peer.close)() +}) diff --git a/test/group-create.test.js b/test/group-create.test.js new file mode 100644 index 0000000..73a079f --- /dev/null +++ b/test/group-create.test.js @@ -0,0 +1,54 @@ +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 DIR = path.join(os.tmpdir(), 'ppppp-db-group-create') +rimraf.sync(DIR) + +test('group.create() without args', async (t) => { + const keys = generateKeypair('alice') + const peer = SecretStack({ appKey: caps.shs }) + .use(require('../lib')) + .use(require('ssb-box')) + .call(null, { keys, path: DIR }) + + await peer.db.loaded() + const groupRec0 = await p(peer.db.group.create)({}) + t.ok(groupRec0, 'groupRec0 exists') + const { hash, msg } = groupRec0 + t.ok(hash, 'hash exists') + t.equals(msg.data.add, keys.id, 'msg.data.add') + t.equals(msg.metadata.group, null, 'msg.metadata.group') + t.equals(msg.metadata.groupTips, null, 'msg.metadata.groupTips') + t.deepEquals(Object.keys(msg.metadata.tangles), [], 'msg.metadata.tangles') + t.equals(msg.pubkey, keys.id, 'msg.pubkey') + + await p(peer.close)() +}) + +test('group.create() with "keys" arg', async (t) => { + const keys = generateKeypair('alice') + + const peer = SecretStack({ appKey: caps.shs }) + .use(require('../lib')) + .use(require('ssb-box')) + .call(null, { keys, path: DIR }) + + await peer.db.loaded() + const groupRec0 = await p(peer.db.group.create)({ keys }) + t.ok(groupRec0, 'groupRec0 exists') + const { hash, msg } = groupRec0 + t.ok(hash, 'hash exists') + t.equals(msg.data.add, keys.id, 'msg.data.add') + t.equals(msg.metadata.group, null, 'msg.metadata.group') + t.equals(msg.metadata.groupTips, null, 'msg.metadata.groupTips') + t.deepEquals(Object.keys(msg.metadata.tangles), [], 'msg.metadata.tangles') + t.equals(msg.pubkey, keys.id, 'msg.pubkey') + + await p(peer.close)() +})