refactor/prettify tests

This commit is contained in:
Andre Staltz 2023-11-10 11:06:19 +02:00
parent 9356b9b3d9
commit f40ea71ff9
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
14 changed files with 1533 additions and 1519 deletions

View File

@ -12,6 +12,7 @@ const DIR = path.join(os.tmpdir(), 'ppppp-db-account-add')
rimraf.sync(DIR) rimraf.sync(DIR)
test('account.add()', async (t) => { test('account.add()', async (t) => {
await t.test('Basic usage', async (t) => {
const keypair1 = Keypair.generate('ed25519', 'alice') const keypair1 = Keypair.generate('ed25519', 'alice')
const keypair2 = Keypair.generate('ed25519', 'bob') const keypair2 = Keypair.generate('ed25519', 'bob')
@ -68,9 +69,9 @@ test('account.add()', async (t) => {
assert.equal(peer.db.account.has({ account, keypair: keypair2 }), true) assert.equal(peer.db.account.has({ account, keypair: keypair2 }), true)
await p(peer.close)() await p(peer.close)()
}) })
test('keypair with no "add" powers cannot account.add()', async (t) => { await t.test('keypair with no "add" powers cannot add', async (t) => {
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair1 = Keypair.generate('ed25519', 'alice') const keypair1 = Keypair.generate('ed25519', 'alice')
const keypair2 = Keypair.generate('ed25519', 'bob') const keypair2 = Keypair.generate('ed25519', 'bob')
@ -148,9 +149,9 @@ test('keypair with no "add" powers cannot account.add()', async (t) => {
) )
await p(peer1again.close)() await p(peer1again.close)()
}) })
test('publish with a key in the account', async (t) => { await t.test('publish with a key in the account', async (t) => {
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair1 = Keypair.generate('ed25519', 'alice') const keypair1 = Keypair.generate('ed25519', 'alice')
@ -225,4 +226,5 @@ test('publish with a key in the account', async (t) => {
// t.pass('carol added all msgs successfully') // t.pass('carol added all msgs successfully')
await p(carol.close)() await p(carol.close)()
})
}) })

View File

@ -11,7 +11,8 @@ const Keypair = require('ppppp-keypair')
const DIR = path.join(os.tmpdir(), 'ppppp-db-account-create') const DIR = path.join(os.tmpdir(), 'ppppp-db-account-create')
rimraf.sync(DIR) rimraf.sync(DIR)
test('account.create() with just "domain"', async (t) => { test('account.create() ', async (t) => {
await t.test('create with just "domain"', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse }) const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib')) .use(require('../lib'))
@ -51,9 +52,9 @@ test('account.create() with just "domain"', async (t) => {
assert.equal(msg.pubkey, keypair.public, 'msg.pubkey') assert.equal(msg.pubkey, keypair.public, 'msg.pubkey')
await p(peer.close)() await p(peer.close)()
}) })
test('account.create() with "keypair" and "domain"', async (t) => { await t.test('create with "keypair" and "domain"', async (t) => {
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
@ -80,9 +81,9 @@ test('account.create() with "keypair" and "domain"', async (t) => {
assert.equal(msg.pubkey, keypair.public, 'msg.pubkey') assert.equal(msg.pubkey, keypair.public, 'msg.pubkey')
await p(peer.close)() await p(peer.close)()
}) })
test('account.find() can find', async (t) => { await t.test('account.find() can find', async (t) => {
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person' const domain = 'person'
@ -100,9 +101,9 @@ test('account.find() can find', async (t) => {
assert.equal(found, account, 'found') assert.equal(found, account, 'found')
await p(peer.close)() await p(peer.close)()
}) })
test('account.findOrCreate() can find', async (t) => { await t.test('account.findOrCreate() can find', async (t) => {
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person' const domain = 'person'
@ -120,9 +121,9 @@ test('account.findOrCreate() can find', async (t) => {
assert.equal(found, account, 'found') assert.equal(found, account, 'found')
await p(peer.close)() await p(peer.close)()
}) })
test('account.findOrCreate() can create', async (t) => { await t.test('account.findOrCreate() can create', async (t) => {
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const domain = 'person' const domain = 'person'
@ -155,4 +156,5 @@ test('account.findOrCreate() can create', async (t) => {
assert.equal(msg.pubkey, keypair.public, 'msg.pubkey') assert.equal(msg.pubkey, keypair.public, 'msg.pubkey')
await p(peer.close)() await p(peer.close)()
})
}) })

View File

@ -6,14 +6,13 @@ const p = require('node:util').promisify
const rimraf = require('rimraf') const rimraf = require('rimraf')
const SecretStack = require('secret-stack') const SecretStack = require('secret-stack')
const Log = require('../lib/log') const Log = require('../lib/log')
const push = require('push-stream')
const caps = require('ppppp-caps') const caps = require('ppppp-caps')
const Keypair = require('ppppp-keypair') const Keypair = require('ppppp-keypair')
const DIR = path.join(os.tmpdir(), 'ppppp-db-del') const DIR = path.join(os.tmpdir(), 'ppppp-db-del')
rimraf.sync(DIR) rimraf.sync(DIR)
test('del', async (t) => { test('del()', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse }) const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib')) .use(require('../lib'))

View File

@ -13,7 +13,7 @@ const Keypair = require('ppppp-keypair')
const DIR = path.join(os.tmpdir(), 'ppppp-db-erase') const DIR = path.join(os.tmpdir(), 'ppppp-db-erase')
rimraf.sync(DIR) rimraf.sync(DIR)
test('erase', async (t) => { test('erase()', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse }) const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib')) .use(require('../lib'))

View File

@ -12,31 +12,23 @@ const MsgV3 = require('../lib/msg-v3')
const DIR = path.join(os.tmpdir(), 'ppppp-db-feed-publish') const DIR = path.join(os.tmpdir(), 'ppppp-db-feed-publish')
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice') test('feed.getID()', async (t) => {
let peer const keypair = Keypair.generate('ed25519', 'alice')
let id const peer = SecretStack({ appKey: caps.shse })
let moot
let mootID
test('setup', async (t) => {
peer = SecretStack({ appKey: caps.shse })
.use(require('../lib')) .use(require('../lib'))
.use(require('ssb-box')) .use(require('ssb-box'))
.call(null, { keypair, path: DIR }) .call(null, { keypair, path: DIR })
await peer.db.loaded() await peer.db.loaded()
id = (await p(peer.db.account.create)({domain: 'person'})) const id = await p(peer.db.account.create)({ domain: 'person' })
moot = MsgV3.createMoot(id, 'post', keypair) const moot = MsgV3.createMoot(id, 'post', keypair)
mootID = MsgV3.getMsgID(moot) const mootID = MsgV3.getMsgID(moot)
await p(peer.db.add)(moot, mootID) await p(peer.db.add)(moot, mootID)
})
test('feed.getID()', async (t) => {
const feedID = peer.db.feed.getID(id, 'post') const feedID = peer.db.feed.getID(id, 'post')
assert.equal(feedID, mootID, 'feed.getID() returns moot ID') assert.equal(feedID, mootID, 'feed.getID() returns moot ID')
})
test('teardown', (t) => { await p(peer.close)(true)
peer.close(t.end)
}) })

View File

@ -12,13 +12,16 @@ const MsgV3 = require('../lib/msg-v3')
const DIR = path.join(os.tmpdir(), 'ppppp-db-feed-publish') const DIR = path.join(os.tmpdir(), 'ppppp-db-feed-publish')
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice') test('feed.publish()', async (t) => {
const bobKeypair = Keypair.generate('ed25519', 'bob') const keypair = Keypair.generate('ed25519', 'alice')
let peer const bobKeypair = Keypair.generate('ed25519', 'bob')
let id let peer
let moot let id
let mootID let moot
test('setup', async (t) => { let mootID
// Setup
{
peer = SecretStack({ appKey: caps.shse }) peer = SecretStack({ appKey: caps.shse })
.use(require('../lib')) .use(require('../lib'))
.use(require('ssb-box')) .use(require('ssb-box'))
@ -26,15 +29,16 @@ test('setup', async (t) => {
await peer.db.loaded() await peer.db.loaded()
id = (await p(peer.db.account.create)({domain: 'person'})) id = await p(peer.db.account.create)({ domain: 'person' })
moot = MsgV3.createMoot(id, 'post', keypair) moot = MsgV3.createMoot(id, 'post', keypair)
mootID = MsgV3.getMsgID(moot) mootID = MsgV3.getMsgID(moot)
}) }
let msgID1 let msgID1
let rec1 let rec1
let msgID2 let msgID2
test('feed.publish()', async (t) => {
await t.test('can add new msgs to the db', async (t) => {
rec1 = await p(peer.db.feed.publish)({ rec1 = await p(peer.db.feed.publish)({
account: id, account: id,
domain: 'post', domain: 'post',
@ -71,9 +75,9 @@ test('feed.publish()', async (t) => {
'msg2 tangle prev correct' 'msg2 tangle prev correct'
) )
msgID2 = MsgV3.getMsgID(rec2.msg) msgID2 = MsgV3.getMsgID(rec2.msg)
}) })
test('add() forked then feed.publish() merged', async (t) => { await t.test('merges tangle after a forked add()', async (t) => {
const tangle = new MsgV3.Tangle(mootID) const tangle = new MsgV3.Tangle(mootID)
tangle.add(mootID, moot) tangle.add(mootID, moot)
tangle.add(rec1.id, rec1.msg) tangle.add(rec1.id, rec1.msg)
@ -115,9 +119,9 @@ test('add() forked then feed.publish() merged', async (t) => {
rec4.msg.metadata.tangles[mootID].prev.includes(msgID3), rec4.msg.metadata.tangles[mootID].prev.includes(msgID3),
'msg4 prev has msg3' 'msg4 prev has msg3'
) )
}) })
test('feed.publish() encrypted with box', async (t) => { await t.test('publish encrypted with box', async (t) => {
const recEncrypted = await p(peer.db.feed.publish)({ const recEncrypted = await p(peer.db.feed.publish)({
account: id, account: id,
domain: 'post', domain: 'post',
@ -129,9 +133,9 @@ test('feed.publish() encrypted with box', async (t) => {
const msgDecrypted = peer.db.get(recEncrypted.id) const msgDecrypted = peer.db.get(recEncrypted.id)
assert.equal(msgDecrypted.data.text, 'I am chewing food') assert.equal(msgDecrypted.data.text, 'I am chewing food')
}) })
test('feed.publish() with tangles', async (t) => { await t.test('publish with tangles', async (t) => {
const recA = await p(peer.db.feed.publish)({ const recA = await p(peer.db.feed.publish)({
account: id, account: id,
domain: 'comment', domain: 'comment',
@ -152,8 +156,7 @@ test('feed.publish() with tangles', async (t) => {
[recA.id], [recA.id],
'tangle prev' 'tangle prev'
) )
}) })
test('teardown', (t) => { await p(peer.close)(true)
peer.close(t.end)
}) })

View File

@ -12,34 +12,27 @@ const MsgV3 = require('../lib/msg-v3')
const DIR = path.join(os.tmpdir(), 'ppppp-db-get') const DIR = path.join(os.tmpdir(), 'ppppp-db-get')
rimraf.sync(DIR) rimraf.sync(DIR)
const keypair = Keypair.generate('ed25519', 'alice') test('get()', async (t) => {
let peer const keypair = Keypair.generate('ed25519', 'alice')
let id const peer = SecretStack({ appKey: caps.shse })
let msgID1
test('setup', async (t) => {
peer = SecretStack({ appKey: caps.shse })
.use(require('../lib')) .use(require('../lib'))
.use(require('ssb-box')) .use(require('ssb-box'))
.call(null, { keypair, path: DIR }) .call(null, { keypair, path: DIR })
await peer.db.loaded() await peer.db.loaded()
id = (await p(peer.db.account.create)({domain: 'person'})) const id = await p(peer.db.account.create)({ domain: 'person' })
const rec1 = await p(peer.db.feed.publish)({ const rec1 = await p(peer.db.feed.publish)({
account: id, account: id,
domain: 'post', domain: 'post',
data: { text: 'I am 1st post' }, data: { text: 'I am 1st post' },
}) })
msgID1 = MsgV3.getMsgID(rec1.msg) const msgID1 = MsgV3.getMsgID(rec1.msg)
})
test('get() supports msg IDs', async (t) => {
const msg = peer.db.get(msgID1) const msg = peer.db.get(msgID1)
assert.ok(msg, 'msg exists') assert.ok(msg, 'msg exists')
assert.equal(msg.data.text, 'I am 1st post') assert.equal(msg.data.text, 'I am 1st post')
})
test('teardown', (t) => { await p(peer.close)(true)
peer.close(t.end)
}) })

View File

@ -11,10 +11,13 @@ const Keypair = require('ppppp-keypair')
const DIR = path.join(os.tmpdir(), 'ppppp-db-tangle') const DIR = path.join(os.tmpdir(), 'ppppp-db-tangle')
rimraf.sync(DIR) rimraf.sync(DIR)
let peer test('getTangle()', async (t) => {
let rootPost, reply1Lo, reply1Hi, reply2, reply3Lo, reply3Hi let peer
let tangle let rootPost, reply1Lo, reply1Hi, reply2, reply3Lo, reply3Hi
test('setup', async (t) => { let tangle
// Setup
{
const keypairA = Keypair.generate('ed25519', 'alice') const keypairA = Keypair.generate('ed25519', 'alice')
const keypairB = Keypair.generate('ed25519', 'bob') const keypairB = Keypair.generate('ed25519', 'bob')
const keypairC = Keypair.generate('ed25519', 'carol') const keypairC = Keypair.generate('ed25519', 'carol')
@ -92,9 +95,9 @@ test('setup', async (t) => {
reply3Hi = reply3B.localeCompare(reply3C) < 0 ? reply3C : reply3B reply3Hi = reply3B.localeCompare(reply3C) < 0 ? reply3C : reply3B
tangle = peer.db.getTangle(rootPost) tangle = peer.db.getTangle(rootPost)
}) }
test('Tangle.has', (t) => { await t.test('Tangle.has', (t) => {
assert.equal(tangle.has(rootPost), true, 'has rootPost') assert.equal(tangle.has(rootPost), true, 'has rootPost')
assert.equal(tangle.has(reply1Lo), true, 'has reply1Lo') assert.equal(tangle.has(reply1Lo), true, 'has reply1Lo')
assert.equal(tangle.has(reply1Hi), true, 'has reply1Hi') assert.equal(tangle.has(reply1Hi), true, 'has reply1Hi')
@ -102,22 +105,22 @@ test('Tangle.has', (t) => {
assert.equal(tangle.has(reply3Lo), true, 'has reply3Lo') assert.equal(tangle.has(reply3Lo), true, 'has reply3Lo')
assert.equal(tangle.has(reply3Hi), true, 'has reply3Hi') assert.equal(tangle.has(reply3Hi), true, 'has reply3Hi')
assert.equal(tangle.has('nonsense'), false, 'does not have nonsense') assert.equal(tangle.has('nonsense'), false, 'does not have nonsense')
}) })
test('Tangle.getDepth', (t) => { await t.test('Tangle.getDepth', (t) => {
assert.equal(tangle.getDepth(rootPost), 0, 'depth of rootPost is 0') assert.equal(tangle.getDepth(rootPost), 0, 'depth of rootPost is 0')
assert.equal(tangle.getDepth(reply1Lo), 1, 'depth of reply1Lo is 1') assert.equal(tangle.getDepth(reply1Lo), 1, 'depth of reply1Lo is 1')
assert.equal(tangle.getDepth(reply1Hi), 1, 'depth of reply1Hi is 1') assert.equal(tangle.getDepth(reply1Hi), 1, 'depth of reply1Hi is 1')
assert.equal(tangle.getDepth(reply2), 2, 'depth of reply2A is 2') assert.equal(tangle.getDepth(reply2), 2, 'depth of reply2A is 2')
assert.equal(tangle.getDepth(reply3Lo), 3, 'depth of reply3Lo is 3') assert.equal(tangle.getDepth(reply3Lo), 3, 'depth of reply3Lo is 3')
assert.equal(tangle.getDepth(reply3Hi), 3, 'depth of reply3Hi is 3') assert.equal(tangle.getDepth(reply3Hi), 3, 'depth of reply3Hi is 3')
}) })
test('Tangle.maxDepth', (t) => { await t.test('Tangle.maxDepth', (t) => {
assert.equal(tangle.maxDepth, 3, 'max depth is 3') assert.equal(tangle.maxDepth, 3, 'max depth is 3')
}) })
test('Tangle.topoSort', (t) => { await t.test('Tangle.topoSort', (t) => {
const sorted = tangle.topoSort() const sorted = tangle.topoSort()
assert.deepEqual(sorted, [ assert.deepEqual(sorted, [
@ -128,9 +131,9 @@ test('Tangle.topoSort', (t) => {
reply3Lo, reply3Lo,
reply3Hi, reply3Hi,
]) ])
}) })
test('Tangle.precedes', (t) => { await t.test('Tangle.precedes', (t) => {
assert.equal( assert.equal(
tangle.precedes(rootPost, reply1Lo), tangle.precedes(rootPost, reply1Lo),
true, true,
@ -171,17 +174,17 @@ test('Tangle.precedes', (t) => {
false, false,
'reply3Lo doesnt precede reply1Hi' 'reply3Lo doesnt precede reply1Hi'
) )
}) })
test('Tangle.tips', (t) => { await t.test('Tangle.tips', (t) => {
const tips = tangle.tips const tips = tangle.tips
assert.equal(tips.size, 2, 'there are 2 tips') assert.equal(tips.size, 2, 'there are 2 tips')
assert.equal(tips.has(reply3Lo), true, 'tips contains reply3Lo') assert.equal(tips.has(reply3Lo), true, 'tips contains reply3Lo')
assert.equal(tips.has(reply3Hi), true, 'tips contains reply3Hi') assert.equal(tips.has(reply3Hi), true, 'tips contains reply3Hi')
}) })
test('Tangle.getLipmaaSet', (t) => { await t.test('Tangle.getLipmaaSet', (t) => {
assert.equal(tangle.getLipmaaSet(0).size, 0, 'lipmaa 0 (empty)') assert.equal(tangle.getLipmaaSet(0).size, 0, 'lipmaa 0 (empty)')
assert.equal(tangle.getLipmaaSet(1).size, 1, 'lipmaa 1 (-1)') assert.equal(tangle.getLipmaaSet(1).size, 1, 'lipmaa 1 (-1)')
@ -199,16 +202,16 @@ test('Tangle.getLipmaaSet', (t) => {
assert.equal(tangle.getLipmaaSet(4).has(reply3Hi), true, 'lipmaa 4 (-1)') assert.equal(tangle.getLipmaaSet(4).has(reply3Hi), true, 'lipmaa 4 (-1)')
assert.equal(tangle.getLipmaaSet(5).size, 0, 'lipmaa 5 (empty)') assert.equal(tangle.getLipmaaSet(5).size, 0, 'lipmaa 5 (empty)')
}) })
test('Tangle.getDeletablesAndErasables basic', (t) => { await t.test('Tangle.getDeletablesAndErasables basic', (t) => {
const { deletables, erasables } = tangle.getDeletablesAndErasables(reply2) const { deletables, erasables } = tangle.getDeletablesAndErasables(reply2)
assert.deepEqual([...deletables], [reply1Hi], 'deletables') assert.deepEqual([...deletables], [reply1Hi], 'deletables')
assert.deepEqual([...erasables], [reply1Lo, rootPost], 'erasables') assert.deepEqual([...erasables], [reply1Lo, rootPost], 'erasables')
}) })
test('Tangle.getDeletablesAndErasables with many inputs', (t) => { await t.test('Tangle.getDeletablesAndErasables with many inputs', (t) => {
const { deletables, erasables } = tangle.getDeletablesAndErasables( const { deletables, erasables } = tangle.getDeletablesAndErasables(
reply3Lo, reply3Lo,
reply2 reply2
@ -216,26 +219,37 @@ test('Tangle.getDeletablesAndErasables with many inputs', (t) => {
assert.deepEqual([...deletables], [reply1Hi], 'deletables') assert.deepEqual([...deletables], [reply1Hi], 'deletables')
assert.deepEqual([...erasables], [reply1Lo, rootPost], 'erasables') assert.deepEqual([...erasables], [reply1Lo, rootPost], 'erasables')
}) })
test('Tangle.getDeletablesAndErasables with many inputs again', (t) => { await t.test(
'Tangle.getDeletablesAndErasables with many inputs again',
(t) => {
const { deletables, erasables } = tangle.getDeletablesAndErasables( const { deletables, erasables } = tangle.getDeletablesAndErasables(
reply3Lo, reply3Lo,
reply3Hi reply3Hi
) )
assert.deepEqual([...deletables], [reply1Lo, reply1Hi, reply2], 'deletables') assert.deepEqual(
[...deletables],
[reply1Lo, reply1Hi, reply2],
'deletables'
)
assert.deepEqual([...erasables], [rootPost], 'erasables') assert.deepEqual([...erasables], [rootPost], 'erasables')
}) }
)
test('Tangle.getDeletablesAndErasables with lipmaa', (t) => { await t.test('Tangle.getDeletablesAndErasables with lipmaa', (t) => {
const { deletables, erasables } = tangle.getDeletablesAndErasables(reply3Lo) const { deletables, erasables } = tangle.getDeletablesAndErasables(reply3Lo)
assert.deepEqual([...deletables], [reply1Lo, reply1Hi, reply2], 'deletables') assert.deepEqual(
[...deletables],
[reply1Lo, reply1Hi, reply2],
'deletables'
)
assert.deepEqual([...erasables], [rootPost], 'erasables') assert.deepEqual([...erasables], [rootPost], 'erasables')
}) })
test('Tangle.getMinimumAmong', (t) => { await t.test('Tangle.getMinimumAmong', (t) => {
const actual1 = tangle.getMinimumAmong([reply1Lo, reply1Hi]) const actual1 = tangle.getMinimumAmong([reply1Lo, reply1Hi])
const expected1 = [reply1Lo, reply1Hi] const expected1 = [reply1Lo, reply1Hi]
assert.deepEqual(actual1, expected1) assert.deepEqual(actual1, expected1)
@ -251,10 +265,13 @@ test('Tangle.getMinimumAmong', (t) => {
const actual4 = tangle.getMinimumAmong([reply1Hi, reply3Lo]) const actual4 = tangle.getMinimumAmong([reply1Hi, reply3Lo])
const expected4 = [reply1Hi] const expected4 = [reply1Hi]
assert.deepEqual(actual4, expected4) assert.deepEqual(actual4, expected4)
}) })
test('Tangle.topoSort after some have been deleted and erased', async (t) => { await t.test(
const { deletables, erasables } = tangle.getDeletablesAndErasables(reply3Lo) 'Tangle.topoSort after some have been deleted and erased',
async (t) => {
const { deletables, erasables } =
tangle.getDeletablesAndErasables(reply3Lo)
for (const msgID of deletables) { for (const msgID of deletables) {
await p(peer.db.del)(msgID) await p(peer.db.del)(msgID)
} }
@ -266,8 +283,8 @@ test('Tangle.topoSort after some have been deleted and erased', async (t) => {
const sorted = tangle2.topoSort() const sorted = tangle2.topoSort()
assert.deepEqual(sorted, [rootPost, reply3Lo, reply3Hi]) assert.deepEqual(sorted, [rootPost, reply3Lo, reply3Hi])
}) }
)
test('teardown', async (t) => {
await p(peer.close)(true) await p(peer.close)(true)
}) })

View File

@ -4,7 +4,8 @@ const fs = require('node:fs')
const p = require('node:util').promisify const p = require('node:util').promisify
const Log = require('../../lib/log') const Log = require('../../lib/log')
test('Log handles basic binary records', async function (t) { test('Log basics', async function (t) {
await t.test('Log handles basic binary records', async function (t) {
const file = '/tmp/ppppp-db-log-test-basic-binary.log' const file = '/tmp/ppppp-db-log-test-basic-binary.log'
try { try {
fs.unlinkSync(file) fs.unlinkSync(file)
@ -27,12 +28,12 @@ test('Log handles basic binary records', async function (t) {
assert.equal(b2.toString(), msg2.toString()) assert.equal(b2.toString(), msg2.toString())
await p(log.close)() await p(log.close)()
}) })
const json1 = { text: 'testing' } const json1 = { text: 'testing' }
const json2 = { test: 'testing2' } const json2 = { test: 'testing2' }
test('Log handles basic json records', async function (t) { await t.test('Log handles basic json records', async function (t) {
const file = '/tmp/ppppp-db-log-test-basic-json.log' const file = '/tmp/ppppp-db-log-test-basic-json.log'
try { try {
fs.unlinkSync(file) fs.unlinkSync(file)
@ -55,9 +56,9 @@ test('Log handles basic json records', async function (t) {
assert.deepEqual(rec2, json2) assert.deepEqual(rec2, json2)
await p(log.close)() await p(log.close)()
}) })
test('Log handles basic json record re-reading', async function (t) { await t.test('Log handles basic json record re-reading', async function (t) {
const file = '/tmp/ppppp-db-log-test-basic-json.log' const file = '/tmp/ppppp-db-log-test-basic-json.log'
const log = Log(file, { const log = Log(file, {
blockSize: 2 * 1024, blockSize: 2 * 1024,
@ -74,4 +75,5 @@ test('Log handles basic json record re-reading', async function (t) {
assert.deepEqual(rec2, json2) assert.deepEqual(rec2, json2)
await p(log.close)() await p(log.close)()
})
}) })

View File

@ -14,7 +14,8 @@ const msg3 = Buffer.from(
'hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db' 'hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db hello offsetty db'
) )
test('Log performing simple delete', async (t) => { test('Log deletes', async (t) => {
await t.test('Simple delete', async (t) => {
const file = '/tmp/ppppp-db-log-test-del.log' const file = '/tmp/ppppp-db-log-test-del.log'
try { try {
fs.unlinkSync(file) fs.unlinkSync(file)
@ -45,9 +46,9 @@ test('Log performing simple delete', async (t) => {
}) })
await p(log.close)() await p(log.close)()
}) })
test('Log deleted records are not invalid upon re-opening', async (t) => { await t.test('Deleted records are not invalid upon re-opening', async (t) => {
const file = '/tmp/ppppp-db-log-test-del-invalid.log' const file = '/tmp/ppppp-db-log-test-del-invalid.log'
try { try {
fs.unlinkSync(file) fs.unlinkSync(file)
@ -107,9 +108,9 @@ test('Log deleted records are not invalid upon re-opening', async (t) => {
}) })
await p(log2.close)() await p(log2.close)()
}) })
test('Log deletes are handled by scan()', async (t) => { await t.test('Deletes are noticed by scan()', async (t) => {
const file = '/tmp/offset-test_' + Date.now() + '.log' const file = '/tmp/offset-test_' + Date.now() + '.log'
const log = Log(file, { blockSize: 64 * 1024 }) const log = Log(file, { blockSize: 64 * 1024 })
@ -138,9 +139,9 @@ test('Log deletes are handled by scan()', async (t) => {
assert.deepEqual(arr, [null, buf2]) assert.deepEqual(arr, [null, buf2])
await p(log.close)() await p(log.close)()
}) })
test('Log can handle many deleted records', { timeout: 60e3 }, async (t) => { await t.test('Many deleted records', { timeout: 60e3 }, async (t) => {
const file = '/tmp/aaol-test-delete-many' + Date.now() + '.log' const file = '/tmp/aaol-test-delete-many' + Date.now() + '.log'
const log = Log(file, { blockSize: 64 * 1024 }) const log = Log(file, { blockSize: 64 * 1024 })
@ -186,4 +187,5 @@ test('Log can handle many deleted records', { timeout: 60e3 }, async (t) => {
}) })
await p(log.close)() await p(log.close)()
})
}) })

View File

@ -9,7 +9,8 @@ var file = '/tmp/ds-test_restart.log'
var msg1 = { text: 'hello world hello world' } var msg1 = { text: 'hello world hello world' }
var msg2 = { text: 'hello world hello world 2' } var msg2 = { text: 'hello world hello world 2' }
test('Log (fix buggy write) simple', async (t) => { test('Log fix buggy write', async (t) => {
await t.test('Simple', async (t) => {
try { try {
fs.unlinkSync(file) fs.unlinkSync(file)
} catch (_) {} } catch (_) {}
@ -39,9 +40,9 @@ test('Log (fix buggy write) simple', async (t) => {
assert.deepEqual(arr, [msg1, msg2]) assert.deepEqual(arr, [msg1, msg2])
await p(log.close)() await p(log.close)()
}) })
test('Log (fix buggy write) reread', async (t) => { await t.test('Re-read', async (t) => {
const log = Log(file, { const log = Log(file, {
block: 16 * 1024, block: 16 * 1024,
codec: require('flumecodec/json'), codec: require('flumecodec/json'),
@ -63,4 +64,5 @@ test('Log (fix buggy write) reread', async (t) => {
assert.deepEqual(arr, [msg1, msg2]) assert.deepEqual(arr, [msg1, msg2])
await p(log.close)() await p(log.close)()
})
}) })

View File

@ -3,7 +3,8 @@ const assert = require('node:assert')
const Keypair = require('ppppp-keypair') const Keypair = require('ppppp-keypair')
const MsgV3 = require('../../lib/msg-v3') const MsgV3 = require('../../lib/msg-v3')
test('MsgV3 invalid domain not a string', (t) => { test('MsgV3 domain validation', async (t) => {
await t.test('Not a string', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
assert.throws( assert.throws(
@ -17,9 +18,9 @@ test('MsgV3 invalid domain not a string', (t) => {
/invalid domain/, /invalid domain/,
'not a string' 'not a string'
) )
}) })
test('MsgV3 invalid domain with "/" character', (t) => { await t.test('"/" character', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
assert.throws( assert.throws(
@ -33,9 +34,9 @@ test('MsgV3 invalid domain with "/" character', (t) => {
/invalid domain/, /invalid domain/,
'invalid domain if contains /' 'invalid domain if contains /'
) )
}) })
test('MsgV3 invalid domain with "*" character', (t) => { await t.test('"*" character', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
assert.throws( assert.throws(
@ -49,9 +50,9 @@ test('MsgV3 invalid domain with "*" character', (t) => {
/invalid domain/, /invalid domain/,
'invalid domain if contains *' 'invalid domain if contains *'
) )
}) })
test('MsgV3 invalid domain too short', (t) => { await t.test('Too short', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
assert.throws( assert.throws(
@ -65,9 +66,9 @@ test('MsgV3 invalid domain too short', (t) => {
/shorter than 3/, /shorter than 3/,
'invalid domain if too short' 'invalid domain if too short'
) )
}) })
test('MsgV3 invalid domain too long', (t) => { await t.test('too long', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
assert.throws( assert.throws(
@ -81,4 +82,5 @@ test('MsgV3 invalid domain too long', (t) => {
/100\+ characters long/, /100\+ characters long/,
'invalid domain if too long' 'invalid domain if too long'
) )
})
}) })

View File

@ -10,7 +10,8 @@ const account = MsgV3.getMsgID(
) )
const pubkeys = new Set([keypair.public]) const pubkeys = new Set([keypair.public])
test('MsgV3 invalid msg with non-array prev', (t) => { test('MsgV3 tangles prev validation', async (t) => {
await t.test('Non-array is a bad prev', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const moot = MsgV3.createMoot(account, 'post', keypair) const moot = MsgV3.createMoot(account, 'post', keypair)
@ -39,9 +40,9 @@ test('MsgV3 invalid msg with non-array prev', (t) => {
/prev ".*" should have been an array/, /prev ".*" should have been an array/,
'invalid 2nd msg description' 'invalid 2nd msg description'
) )
}) })
test('MsgV3 invalid msg with bad prev', (t) => { await t.test('Number not allowed in prev', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const moot = MsgV3.createMoot(account, 'post', keypair) const moot = MsgV3.createMoot(account, 'post', keypair)
@ -84,9 +85,9 @@ test('MsgV3 invalid msg with bad prev', (t) => {
/prev item ".*" should have been a string/, /prev item ".*" should have been a string/,
'invalid 2nd msg description' 'invalid 2nd msg description'
) )
}) })
test('MsgV3 invalid msg with URI in prev', (t) => { await t.test('URI not allowed in prev', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const moot = MsgV3.createMoot(account, 'post', keypair) const moot = MsgV3.createMoot(account, 'post', keypair)
@ -127,9 +128,9 @@ test('MsgV3 invalid msg with URI in prev', (t) => {
const err = MsgV3.validate(msg2, tangle, pubkeys, msgID2, mootID) const err = MsgV3.validate(msg2, tangle, pubkeys, msgID2, mootID)
assert.ok(err, 'invalid 2nd msg throws') assert.ok(err, 'invalid 2nd msg throws')
assert.match(err, /prev item ".*" is a URI/, 'invalid 2nd msg description') assert.match(err, /prev item ".*" is a URI/, 'invalid 2nd msg description')
}) })
test('MsgV3 invalid msg with unknown prev', (t) => { await t.test('Locally unknown prev msgID', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const moot = MsgV3.createMoot(account, 'post', keypair) const moot = MsgV3.createMoot(account, 'post', keypair)
@ -187,9 +188,9 @@ test('MsgV3 invalid msg with unknown prev', (t) => {
/all prev are locally unknown/, /all prev are locally unknown/,
'invalid 2nd msg description' 'invalid 2nd msg description'
) )
}) })
test('MsgV3 invalid feed msg with a different pubkey', (t) => { await t.test('Feed msg with the wrong pubkey', (t) => {
const keypairA = Keypair.generate('ed25519', 'alice') const keypairA = Keypair.generate('ed25519', 'alice')
const keypairB = Keypair.generate('ed25519', 'bob') const keypairB = Keypair.generate('ed25519', 'bob')
@ -221,9 +222,9 @@ test('MsgV3 invalid feed msg with a different pubkey', (t) => {
/pubkey ".*" should have been one of ".*" from the account ".*"/, /pubkey ".*" should have been one of ".*" from the account ".*"/,
'invalid msg' 'invalid msg'
) )
}) })
test('MsgV3 invalid feed msg with a different domain', (t) => { await t.test('Feed msg with the wrong domain', (t) => {
const keypairA = Keypair.generate('ed25519', 'alice') const keypairA = Keypair.generate('ed25519', 'alice')
const moot = MsgV3.createMoot(account, 'post', keypair) const moot = MsgV3.createMoot(account, 'post', keypair)
@ -250,9 +251,9 @@ test('MsgV3 invalid feed msg with a different domain', (t) => {
/domain "comment" should have been feed domain "post"/, /domain "comment" should have been feed domain "post"/,
'invalid feed msg' 'invalid feed msg'
) )
}) })
test('MsgV3 invalid feed msg with non-alphabetical prev', (t) => { await t.test('Feed msg with non-alphabetically sorted prev', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const moot = MsgV3.createMoot(account, 'post', keypair) const moot = MsgV3.createMoot(account, 'post', keypair)
@ -315,9 +316,9 @@ test('MsgV3 invalid feed msg with non-alphabetical prev', (t) => {
/prev ".*" should have been alphabetically sorted/, /prev ".*" should have been alphabetically sorted/,
'invalid error message' 'invalid error message'
) )
}) })
test('MsgV3 invalid feed msg with duplicate prev', (t) => { await t.test('Feed msg with duplicate prev', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const moot = MsgV3.createMoot(account, 'post', keypair) const moot = MsgV3.createMoot(account, 'post', keypair)
@ -344,4 +345,5 @@ test('MsgV3 invalid feed msg with duplicate prev', (t) => {
const err = MsgV3.validate(msg1, tangle, pubkeys, msgID1, mootID) const err = MsgV3.validate(msg1, tangle, pubkeys, msgID1, mootID)
assert.ok(err, 'invalid 1st msg throws') assert.ok(err, 'invalid 1st msg throws')
assert.match(err, /prev ".*" contains duplicates/, 'invalid error message') assert.match(err, /prev ".*" contains duplicates/, 'invalid error message')
})
}) })

View File

@ -3,7 +3,8 @@ const assert = require('node:assert')
const Keypair = require('ppppp-keypair') const Keypair = require('ppppp-keypair')
const MsgV3 = require('../../lib/msg-v3') const MsgV3 = require('../../lib/msg-v3')
test('MsgV3 validate root msg', (t) => { test('MsgV3 validation', async (t) => {
await t.test('Correct root msg', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const account = MsgV3.getMsgID( const account = MsgV3.getMsgID(
MsgV3.createAccount(keypair, 'person', 'alice') MsgV3.createAccount(keypair, 'person', 'alice')
@ -17,9 +18,9 @@ test('MsgV3 validate root msg', (t) => {
const err = MsgV3.validate(moot, tangle, pubkeys, mootID, mootID) const err = MsgV3.validate(moot, tangle, pubkeys, mootID, mootID)
assert.ifError(err, 'valid root msg') assert.ifError(err, 'valid root msg')
}) })
test('MsgV3 validate account tangle', (t) => { await t.test('Correct account tangle', (t) => {
const pubkeys = new Set() const pubkeys = new Set()
const keypair1 = Keypair.generate('ed25519', 'alice') const keypair1 = Keypair.generate('ed25519', 'alice')
pubkeys.add(keypair1.public) pubkeys.add(keypair1.public)
@ -56,17 +57,11 @@ test('MsgV3 validate account tangle', (t) => {
}) })
const accountMsg1ID = MsgV3.getMsgID(accountMsg1) const accountMsg1ID = MsgV3.getMsgID(accountMsg1)
err = MsgV3.validate( err = MsgV3.validate(accountMsg1, tangle, pubkeys, accountMsg1ID, account)
accountMsg1,
tangle,
pubkeys,
accountMsg1ID,
account
)
assert.ifError(err, 'valid account msg') assert.ifError(err, 'valid account msg')
}) })
test('MsgV3 validate 2nd msg with existing root', (t) => { await t.test('2nd msg correct with existing root', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const account = MsgV3.getMsgID( const account = MsgV3.getMsgID(
MsgV3.createAccount(keypair, 'person', 'alice') MsgV3.createAccount(keypair, 'person', 'alice')
@ -93,9 +88,9 @@ test('MsgV3 validate 2nd msg with existing root', (t) => {
const err = MsgV3.validate(msg1, tangle, pubkeys, msgID1, mootID) const err = MsgV3.validate(msg1, tangle, pubkeys, msgID1, mootID)
assert.ifError(err, 'valid 2nd msg') assert.ifError(err, 'valid 2nd msg')
}) })
test('MsgV3 validate 2nd forked msg', (t) => { await t.test('2nd forked msg correct', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const account = MsgV3.getMsgID( const account = MsgV3.getMsgID(
MsgV3.createAccount(keypair, 'person', 'alice') MsgV3.createAccount(keypair, 'person', 'alice')
@ -135,9 +130,9 @@ test('MsgV3 validate 2nd forked msg', (t) => {
tangle.add(msgID1B, msg1B) tangle.add(msgID1B, msg1B)
const err = MsgV3.validate(msg1B, tangle, pubkeys, msgID1B, mootID) const err = MsgV3.validate(msg1B, tangle, pubkeys, msgID1B, mootID)
assert.ifError(err, 'valid 2nd forked msg') assert.ifError(err, 'valid 2nd forked msg')
}) })
test('MsgV3 validate erased msg', (t) => { await t.test('Correct erased msg', (t) => {
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const account = MsgV3.getMsgID( const account = MsgV3.getMsgID(
MsgV3.createAccount(keypair, 'person', 'alice') MsgV3.createAccount(keypair, 'person', 'alice')
@ -164,4 +159,5 @@ test('MsgV3 validate erased msg', (t) => {
const err = MsgV3.validate(msg1, tangle, pubkeys, msgID1, mootID) const err = MsgV3.validate(msg1, tangle, pubkeys, msgID1, mootID)
assert.ifError(err, 'valid erased msg') assert.ifError(err, 'valid erased msg')
})
}) })