improve tests

This commit is contained in:
Andre Staltz 2023-04-21 13:07:15 +03:00
parent af33dc691f
commit 2f4c07d505
2 changed files with 42 additions and 73 deletions

View File

@ -14,20 +14,22 @@ const createPeer = SecretStack({ appKey: caps.shs })
.use(require('ssb-box')) .use(require('ssb-box'))
.use(require('../')) .use(require('../'))
test('sync a feed with goal=all', async (t) => { const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice') const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob') const aliceKeys = generateKeypair('alice')
const bobKeys = generateKeypair('bob')
test('sync a feed with goal=all', async (t) => {
rimraf.sync(ALICE_DIR) rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR) rimraf.sync(BOB_DIR)
const alice = createPeer({ const alice = createPeer({
keys: generateKeypair('alice'), keys: aliceKeys,
path: ALICE_DIR, path: ALICE_DIR,
}) })
const bob = createPeer({ const bob = createPeer({
keys: generateKeypair('bob'), keys: bobKeys,
path: BOB_DIR, path: BOB_DIR,
}) })
@ -94,19 +96,16 @@ test('sync a feed with goal=all', async (t) => {
}) })
test('sync a feed with goal=newest', async (t) => { test('sync a feed with goal=newest', async (t) => {
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
rimraf.sync(ALICE_DIR) rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR) rimraf.sync(BOB_DIR)
const alice = createPeer({ const alice = createPeer({
keys: generateKeypair('alice'), keys: aliceKeys,
path: ALICE_DIR, path: ALICE_DIR,
}) })
const bob = createPeer({ const bob = createPeer({
keys: generateKeypair('bob'), keys: bobKeys,
path: BOB_DIR, path: BOB_DIR,
}) })
@ -172,20 +171,17 @@ test('sync a feed with goal=newest', async (t) => {
await p(bob.close)(true) await p(bob.close)(true)
}) })
test('with goal=newest but too far behind', async (t) => { test('sync a feed with goal=newest but too far behind', async (t) => {
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
rimraf.sync(ALICE_DIR) rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR) rimraf.sync(BOB_DIR)
const alice = createPeer({ const alice = createPeer({
keys: generateKeypair('alice'), keys: aliceKeys,
path: ALICE_DIR, path: ALICE_DIR,
}) })
const bob = createPeer({ const bob = createPeer({
keys: generateKeypair('bob'), keys: bobKeys,
path: BOB_DIR, path: BOB_DIR,
}) })

View File

@ -1,11 +1,9 @@
const test = require('tape') const test = require('tape')
const ssbKeys = require('ssb-keys')
const path = require('path') const path = require('path')
const os = require('os') const os = require('os')
const rimraf = require('rimraf') const rimraf = require('rimraf')
const SecretStack = require('secret-stack') const SecretStack = require('secret-stack')
const caps = require('ssb-caps') const caps = require('ssb-caps')
const FeedV1 = require('ppppp-db/lib/feed-v1')
const p = require('util').promisify const p = require('util').promisify
const { generateKeypair } = require('./util') const { generateKeypair } = require('./util')
@ -14,6 +12,11 @@ const createSSB = SecretStack({ appKey: caps.shs })
.use(require('ssb-box')) .use(require('ssb-box'))
.use(require('../')) .use(require('../'))
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 getTexts(iter) { function getTexts(iter) {
return [...iter].filter((msg) => msg.content).map((msg) => msg.content.text) return [...iter].filter((msg) => msg.content).map((msg) => msg.content.text)
} }
@ -56,19 +59,14 @@ graph TB;
``` ```
*/ */
test('sync a thread where both peers have portions', async (t) => { test('sync a thread where both peers have portions', async (t) => {
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
rimraf.sync(ALICE_DIR) rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR) rimraf.sync(BOB_DIR)
const aliceKeys = generateKeypair('alice')
const alice = createSSB({ const alice = createSSB({
keys: aliceKeys, keys: aliceKeys,
path: ALICE_DIR, path: ALICE_DIR,
}) })
const bobKeys = generateKeypair('bob')
const bob = createSSB({ const bob = createSSB({
keys: bobKeys, keys: bobKeys,
path: BOB_DIR, path: BOB_DIR,
@ -94,8 +92,6 @@ test('sync a thread where both peers have portions', async (t) => {
await p(bob.db.add)(rootMsgA, rootHashA) await p(bob.db.add)(rootMsgA, rootHashA)
await p(bob.db.add)(startA.msg, rootHashA) await p(bob.db.add)(startA.msg, rootHashA)
await p(setTimeout)(10)
const replyB1 = await p(bob.db.create)({ const replyB1 = await p(bob.db.create)({
type: 'post', type: 'post',
content: { text: 'B1' }, content: { text: 'B1' },
@ -103,8 +99,6 @@ test('sync a thread where both peers have portions', async (t) => {
keys: bobKeys, keys: bobKeys,
}) })
await p(setTimeout)(10)
const replyB2 = await p(bob.db.create)({ const replyB2 = await p(bob.db.create)({
type: 'post', type: 'post',
content: { text: 'B2' }, content: { text: 'B2' },
@ -118,18 +112,12 @@ test('sync a thread where both peers have portions', async (t) => {
await p(alice.db.add)(replyB1.msg, rootHashB) await p(alice.db.add)(replyB1.msg, rootHashB)
await p(alice.db.add)(replyB2.msg, rootHashB) await p(alice.db.add)(replyB2.msg, rootHashB)
await p(setTimeout)(10)
const replyC1 = await p(alice.db.create)({ const replyC1 = await p(alice.db.create)({
type: 'post', type: 'post',
content: { text: 'C1' }, content: { text: 'C1' },
tangles: [startA.hash], tangles: [startA.hash],
keys: carolKeys, keys: carolKeys,
}) })
// const rootHashC = alice.db.getFeedRoot(carolKeys.id, 'post')
// const rootMsgC = alice.db.get(rootHashC)
await p(setTimeout)(10)
const replyD1 = await p(bob.db.create)({ const replyD1 = await p(bob.db.create)({
type: 'post', type: 'post',
@ -177,20 +165,15 @@ test('sync a thread where both peers have portions', async (t) => {
await p(bob.close)(true) await p(bob.close)(true)
}) })
test('sync a thread where one peer does not have the root', async (t) => { test('sync a thread where initiator does not have the root', async (t) => {
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
rimraf.sync(ALICE_DIR) rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR) rimraf.sync(BOB_DIR)
const aliceKeys = generateKeypair('alice')
const alice = createSSB({ const alice = createSSB({
keys: aliceKeys, keys: aliceKeys,
path: ALICE_DIR, path: ALICE_DIR,
}) })
const bobKeys = generateKeypair('bob')
const bob = createSSB({ const bob = createSSB({
keys: bobKeys, keys: bobKeys,
path: BOB_DIR, path: BOB_DIR,
@ -205,8 +188,6 @@ test('sync a thread where one peer does not have the root', async (t) => {
keys: aliceKeys, keys: aliceKeys,
}) })
await p(setTimeout)(10)
const replyA1 = await p(alice.db.create)({ const replyA1 = await p(alice.db.create)({
type: 'post', type: 'post',
content: { text: 'A1' }, content: { text: 'A1' },
@ -214,8 +195,6 @@ test('sync a thread where one peer does not have the root', async (t) => {
keys: aliceKeys, keys: aliceKeys,
}) })
await p(setTimeout)(10)
const replyA2 = await p(alice.db.create)({ const replyA2 = await p(alice.db.create)({
type: 'post', type: 'post',
content: { text: 'A2' }, content: { text: 'A2' },
@ -232,7 +211,8 @@ test('sync a thread where one peer does not have the root', async (t) => {
t.deepEquals(getTexts(bob.db.msgs()), [], 'bob has nothing') t.deepEquals(getTexts(bob.db.msgs()), [], 'bob has nothing')
bob.tangleSync.setGoal(rootA.hash, 'all') bob.tangleSync.setGoal(rootA.hash, 'all')
alice.tangleSync.setGoal(rootA.hash, 'all') // ON PURPOSE: alice does not set the goal
// alice.tangleSync.setGoal(rootA.hash, 'all')
const remoteAlice = await p(bob.connect)(alice.getAddress()) const remoteAlice = await p(bob.connect)(alice.getAddress())
t.pass('bob connected to alice') t.pass('bob connected to alice')
@ -252,21 +232,17 @@ test('sync a thread where one peer does not have the root', async (t) => {
await p(bob.close)(true) await p(bob.close)(true)
}) })
// FIXME: test('sync a thread where receiver does not have the root', async (t) => {
test.skip('sync a thread where second peer does not have the root', async (t) => {
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
rimraf.sync(ALICE_DIR) rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR) rimraf.sync(BOB_DIR)
const alice = createSSB({ const alice = createSSB({
keys: ssbKeys.generate('ed25519', 'alice'), keys: aliceKeys,
path: ALICE_DIR, path: ALICE_DIR,
}) })
const bob = createSSB({ const bob = createSSB({
keys: ssbKeys.generate('ed25519', 'bob'), keys: bobKeys,
path: BOB_DIR, path: BOB_DIR,
}) })
@ -274,48 +250,45 @@ test.skip('sync a thread where second peer does not have the root', async (t) =>
await bob.db.loaded() await bob.db.loaded()
const rootA = await p(alice.db.create)({ const rootA = await p(alice.db.create)({
feedFormat: 'classic', type: 'post',
content: { type: 'post', text: 'A' }, content: { text: 'A' },
keys: alice.config.keys, keys: aliceKeys,
}) })
await p(setTimeout)(10)
const replyA1 = await p(alice.db.create)({ const replyA1 = await p(alice.db.create)({
feedFormat: 'classic', type: 'post',
content: { type: 'post', text: 'A1', root: rootA.key, branch: rootA.key }, content: { text: 'A1' },
keys: alice.config.keys, tangles: [rootA.hash],
keys: aliceKeys,
}) })
await p(setTimeout)(10)
const replyA2 = await p(alice.db.create)({ const replyA2 = await p(alice.db.create)({
feedFormat: 'classic', type: 'post',
content: { type: 'post', text: 'A2', root: rootA.key, branch: replyA1.key }, content: { text: 'A2' },
keys: alice.config.keys, tangles: [rootA.hash],
keys: aliceKeys,
}) })
t.deepEquals( t.deepEquals(
alice.db.filterAsArray((msg) => true).map((msg) => msg.value.content.text), getTexts(alice.db.msgs()),
['A', 'A1', 'A2'], ['A', 'A1', 'A2'],
'alice has the full thread' 'alice has the full thread'
) )
t.deepEquals( t.deepEquals(getTexts(bob.db.msgs()), [], 'bob has nothing')
bob.db.filterAsArray((msg) => true).map((msg) => msg.value.content.text),
[], bob.tangleSync.setGoal(rootA.hash, 'all')
'bob has nothing' alice.tangleSync.setGoal(rootA.hash, 'all')
)
const remoteBob = await p(alice.connect)(bob.getAddress()) const remoteBob = await p(alice.connect)(bob.getAddress())
t.pass('alice connected to bob') t.pass('alice connected to bob')
alice.threadSync.request(rootA.key) alice.tangleSync.initiate()
await p(setTimeout)(1000) await p(setTimeout)(1000)
t.pass('threadSync!') t.pass('tangleSync!')
t.deepEquals( t.deepEquals(
bob.db.filterAsArray((msg) => true).map((msg) => msg.value.content.text), getTexts(bob.db.msgs()),
['A', 'A1', 'A2'], ['A', 'A1', 'A2'],
'bob has the full thread' 'bob has the full thread'
) )