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('../'))
test('sync a feed with goal=all', async (t) => {
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')
test('sync a feed with goal=all', async (t) => {
rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR)
const alice = createPeer({
keys: generateKeypair('alice'),
keys: aliceKeys,
path: ALICE_DIR,
})
const bob = createPeer({
keys: generateKeypair('bob'),
keys: bobKeys,
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) => {
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR)
const alice = createPeer({
keys: generateKeypair('alice'),
keys: aliceKeys,
path: ALICE_DIR,
})
const bob = createPeer({
keys: generateKeypair('bob'),
keys: bobKeys,
path: BOB_DIR,
})
@ -172,20 +171,17 @@ test('sync a feed with goal=newest', async (t) => {
await p(bob.close)(true)
})
test('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')
test('sync a feed with goal=newest but too far behind', async (t) => {
rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR)
const alice = createPeer({
keys: generateKeypair('alice'),
keys: aliceKeys,
path: ALICE_DIR,
})
const bob = createPeer({
keys: generateKeypair('bob'),
keys: bobKeys,
path: BOB_DIR,
})

View File

@ -1,11 +1,9 @@
const test = require('tape')
const ssbKeys = require('ssb-keys')
const path = require('path')
const os = require('os')
const rimraf = require('rimraf')
const SecretStack = require('secret-stack')
const caps = require('ssb-caps')
const FeedV1 = require('ppppp-db/lib/feed-v1')
const p = require('util').promisify
const { generateKeypair } = require('./util')
@ -14,6 +12,11 @@ const createSSB = SecretStack({ appKey: caps.shs })
.use(require('ssb-box'))
.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) {
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) => {
const ALICE_DIR = path.join(os.tmpdir(), 'dagsync-alice')
const BOB_DIR = path.join(os.tmpdir(), 'dagsync-bob')
rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR)
const aliceKeys = generateKeypair('alice')
const alice = createSSB({
keys: aliceKeys,
path: ALICE_DIR,
})
const bobKeys = generateKeypair('bob')
const bob = createSSB({
keys: bobKeys,
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)(startA.msg, rootHashA)
await p(setTimeout)(10)
const replyB1 = await p(bob.db.create)({
type: 'post',
content: { text: 'B1' },
@ -103,8 +99,6 @@ test('sync a thread where both peers have portions', async (t) => {
keys: bobKeys,
})
await p(setTimeout)(10)
const replyB2 = await p(bob.db.create)({
type: 'post',
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)(replyB2.msg, rootHashB)
await p(setTimeout)(10)
const replyC1 = await p(alice.db.create)({
type: 'post',
content: { text: 'C1' },
tangles: [startA.hash],
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)({
type: 'post',
@ -177,20 +165,15 @@ test('sync a thread where both peers have portions', async (t) => {
await p(bob.close)(true)
})
test('sync a thread where one 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')
test('sync a thread where initiator does not have the root', async (t) => {
rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR)
const aliceKeys = generateKeypair('alice')
const alice = createSSB({
keys: aliceKeys,
path: ALICE_DIR,
})
const bobKeys = generateKeypair('bob')
const bob = createSSB({
keys: bobKeys,
path: BOB_DIR,
@ -205,8 +188,6 @@ test('sync a thread where one peer does not have the root', async (t) => {
keys: aliceKeys,
})
await p(setTimeout)(10)
const replyA1 = await p(alice.db.create)({
type: 'post',
content: { text: 'A1' },
@ -214,8 +195,6 @@ test('sync a thread where one peer does not have the root', async (t) => {
keys: aliceKeys,
})
await p(setTimeout)(10)
const replyA2 = await p(alice.db.create)({
type: 'post',
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')
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())
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)
})
// FIXME:
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')
test('sync a thread where receiver does not have the root', async (t) => {
rimraf.sync(ALICE_DIR)
rimraf.sync(BOB_DIR)
const alice = createSSB({
keys: ssbKeys.generate('ed25519', 'alice'),
keys: aliceKeys,
path: ALICE_DIR,
})
const bob = createSSB({
keys: ssbKeys.generate('ed25519', 'bob'),
keys: bobKeys,
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()
const rootA = await p(alice.db.create)({
feedFormat: 'classic',
content: { type: 'post', text: 'A' },
keys: alice.config.keys,
type: 'post',
content: { text: 'A' },
keys: aliceKeys,
})
await p(setTimeout)(10)
const replyA1 = await p(alice.db.create)({
feedFormat: 'classic',
content: { type: 'post', text: 'A1', root: rootA.key, branch: rootA.key },
keys: alice.config.keys,
type: 'post',
content: { text: 'A1' },
tangles: [rootA.hash],
keys: aliceKeys,
})
await p(setTimeout)(10)
const replyA2 = await p(alice.db.create)({
feedFormat: 'classic',
content: { type: 'post', text: 'A2', root: rootA.key, branch: replyA1.key },
keys: alice.config.keys,
type: 'post',
content: { text: 'A2' },
tangles: [rootA.hash],
keys: aliceKeys,
})
t.deepEquals(
alice.db.filterAsArray((msg) => true).map((msg) => msg.value.content.text),
getTexts(alice.db.msgs()),
['A', 'A1', 'A2'],
'alice has the full thread'
)
t.deepEquals(
bob.db.filterAsArray((msg) => true).map((msg) => msg.value.content.text),
[],
'bob has nothing'
)
t.deepEquals(getTexts(bob.db.msgs()), [], 'bob has nothing')
bob.tangleSync.setGoal(rootA.hash, 'all')
alice.tangleSync.setGoal(rootA.hash, 'all')
const remoteBob = await p(alice.connect)(bob.getAddress())
t.pass('alice connected to bob')
alice.threadSync.request(rootA.key)
alice.tangleSync.initiate()
await p(setTimeout)(1000)
t.pass('threadSync!')
t.pass('tangleSync!')
t.deepEquals(
bob.db.filterAsArray((msg) => true).map((msg) => msg.value.content.text),
getTexts(bob.db.msgs()),
['A', 'A1', 'A2'],
'bob has the full thread'
)