mirror of https://codeberg.org/pzp/pzp-db.git
also remove feed-v1 tests
This commit is contained in:
parent
f66807a774
commit
302bfa81f8
|
@ -1,198 +0,0 @@
|
|||
const tape = require('tape')
|
||||
const FeedV1 = require('../../lib/feed-v1')
|
||||
const { generateKeypair } = require('../util')
|
||||
|
||||
let rootMsg = null
|
||||
let rootHash = null
|
||||
tape('FeedV1.createRoot()', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
t.equals(rootMsg.content, null, 'content')
|
||||
t.equals(rootMsg.metadata.hash, null, 'hash')
|
||||
t.equals(rootMsg.metadata.size, 0, 'size')
|
||||
t.equals(rootMsg.metadata.type, 'post', 'type')
|
||||
t.equals(rootMsg.metadata.who, FeedV1.stripAuthor(keys.id), 'who')
|
||||
t.deepEquals(rootMsg.metadata.tangles, {}, 'tangles')
|
||||
|
||||
console.log(rootMsg)
|
||||
rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
t.equals(rootHash, '3F26EgnwbMHm1EEeeVM1Eb', 'root hash')
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('FeedV1.create()', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
const content = { text: 'Hello world!' }
|
||||
|
||||
const tangle1 = new FeedV1.Tangle(rootHash)
|
||||
tangle1.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle1,
|
||||
},
|
||||
})
|
||||
t.deepEquals(
|
||||
Object.keys(msg1.metadata),
|
||||
['hash', 'size', 'tangles', 'type', 'v', 'who'],
|
||||
'metadata fields'
|
||||
)
|
||||
t.equals(
|
||||
msg1.metadata.who,
|
||||
'4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW',
|
||||
'metadata.who'
|
||||
)
|
||||
t.equals(msg1.metadata.type, 'post', 'metadata.type')
|
||||
t.deepEquals(msg1.metadata.hash, '9R7XmBhHF5ooPg34j9TQcz', 'metadata.hash')
|
||||
t.deepEquals(Object.keys(msg1.metadata.tangles), [rootHash], 'tangles')
|
||||
t.equals(msg1.metadata.tangles[rootHash].depth, 1, 'tangle depth')
|
||||
t.deepEquals(msg1.metadata.tangles[rootHash].prev, [rootHash], 'tangle prev')
|
||||
t.deepEquals(msg1.metadata.size, 23, 'metadata.size')
|
||||
t.deepEqual(msg1.content, content, 'content is correct')
|
||||
|
||||
console.log(JSON.stringify(msg1, null, 2))
|
||||
|
||||
const msgHash1 = 'MTYQM89hvHuiVKaw8Ze7kc'
|
||||
|
||||
t.equals(
|
||||
FeedV1.getMsgId(msg1),
|
||||
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/' +
|
||||
msgHash1,
|
||||
'getMsgId'
|
||||
)
|
||||
|
||||
const tangle2 = new FeedV1.Tangle(rootHash)
|
||||
tangle2.add(rootHash, rootMsg)
|
||||
tangle2.add(msgHash1, msg1)
|
||||
|
||||
const content2 = { text: 'Ola mundo!' }
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys,
|
||||
content: content2,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle2,
|
||||
},
|
||||
})
|
||||
t.deepEquals(
|
||||
Object.keys(msg2.metadata),
|
||||
['hash', 'size', 'tangles', 'type', 'v', 'who'],
|
||||
'metadata keys'
|
||||
)
|
||||
t.equals(
|
||||
msg2.metadata.who,
|
||||
'4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW',
|
||||
'metadata.who'
|
||||
)
|
||||
t.equals(msg2.metadata.type, 'post', 'metadata.type')
|
||||
t.deepEquals(Object.keys(msg1.metadata.tangles), [rootHash], 'tangles')
|
||||
t.equals(msg2.metadata.tangles[rootHash].depth, 2, 'tangle depth')
|
||||
t.deepEquals(msg2.metadata.tangles[rootHash].prev, [msgHash1], 'tangle prev')
|
||||
t.deepEquals(msg2.metadata.hash, 'XuZEzH1Dhy1yuRMcviBBcN', 'metadata.hash')
|
||||
t.deepEquals(msg2.metadata.size, 21, 'metadata.size')
|
||||
t.deepEqual(msg2.content, content2, 'content is correct')
|
||||
|
||||
console.log(JSON.stringify(msg2, null, 2))
|
||||
|
||||
t.deepEqual(
|
||||
FeedV1.getMsgId(msg2),
|
||||
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/T7juKvDH2bqEUhJB9Dxctr',
|
||||
'getMsgId'
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('create() handles DAG tips correctly', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '1' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
t.deepEquals(
|
||||
msg1.metadata.tangles[rootHash].prev,
|
||||
[FeedV1.getFeedRootHash(keys.id, 'post')],
|
||||
'msg1.prev is root'
|
||||
)
|
||||
|
||||
tangle.add(msgHash1, msg1)
|
||||
|
||||
const msg2A = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '2A' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
t.deepEquals(
|
||||
msg2A.metadata.tangles[rootHash].prev,
|
||||
[msgHash1],
|
||||
'msg2A.prev is msg1'
|
||||
)
|
||||
|
||||
const msg2B = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '2B' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash2B = FeedV1.getMsgHash(msg2B)
|
||||
t.deepEquals(
|
||||
msg2B.metadata.tangles[rootHash].prev,
|
||||
[msgHash1],
|
||||
'msg2B.prev is msg1'
|
||||
)
|
||||
|
||||
tangle.add(msgHash2B, msg2B)
|
||||
|
||||
const msg3 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '3' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash3 = FeedV1.getMsgHash(msg3)
|
||||
t.deepEquals(
|
||||
msg3.metadata.tangles[rootHash].prev,
|
||||
[rootHash, msgHash2B].sort(),
|
||||
'msg3.prev is [root(lipmaa),msg2B(previous)], sorted'
|
||||
)
|
||||
tangle.add(msgHash3, msg3)
|
||||
|
||||
const msgHash2A = FeedV1.getMsgHash(msg2A)
|
||||
tangle.add(msgHash2A, msg2A)
|
||||
t.pass('msg2A comes into awareness')
|
||||
|
||||
const msg4 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '4' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
t.deepEquals(
|
||||
msg4.metadata.tangles[rootHash].prev,
|
||||
[msgHash3, msgHash2A].sort(),
|
||||
'msg4.prev is [msg3(previous),msg2A(old fork as tip)], sorted'
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
|
@ -1,314 +0,0 @@
|
|||
const tape = require('tape')
|
||||
const base58 = require('bs58')
|
||||
const FeedV1 = require('../../lib/feed-v1')
|
||||
const { generateKeypair } = require('../util')
|
||||
|
||||
tape('invalid msg with non-array prev', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
msg.metadata.tangles[rootHash].prev = null
|
||||
const msgHash = FeedV1.getMsgHash(msg)
|
||||
|
||||
const err = FeedV1.validate(msg, tangle, msgHash, rootHash)
|
||||
t.ok(err, 'invalid 2nd msg throws')
|
||||
t.match(err.message, /prev must be an array/, 'invalid 2nd msg description')
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid msg with bad prev', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
tangle.add(msgHash1, msg1)
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
msg2.metadata.tangles[rootHash].depth = 1
|
||||
msg2.metadata.tangles[rootHash].prev = [1234]
|
||||
const msgHash2 = FeedV1.getMsgHash(msg2)
|
||||
|
||||
const err = FeedV1.validate(msg2, tangle, msgHash2, rootHash)
|
||||
t.ok(err, 'invalid 2nd msg throws')
|
||||
t.match(
|
||||
err.message,
|
||||
/prev must contain strings/,
|
||||
'invalid 2nd msg description'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid msg with URI in prev', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
tangle.add(msgHash1, msg1)
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash2 = FeedV1.getMsgHash(msg2)
|
||||
const randBuf = Buffer.alloc(16).fill(16)
|
||||
const fakeMsgKey1 = `ppppp:message/v1/${base58.encode(randBuf)}`
|
||||
msg2.metadata.tangles[rootHash].depth = 1
|
||||
msg2.metadata.tangles[rootHash].prev = [fakeMsgKey1]
|
||||
|
||||
const err = FeedV1.validate(msg2, tangle, msgHash2, rootHash)
|
||||
t.ok(err, 'invalid 2nd msg throws')
|
||||
t.match(
|
||||
err.message,
|
||||
/prev must not contain URIs/,
|
||||
'invalid 2nd msg description'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid msg with unknown prev', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
tangle.add(msgHash1, msg1)
|
||||
|
||||
const unknownMsg = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Alien' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const unknownMsgHash = FeedV1.getMsgHash(unknownMsg)
|
||||
|
||||
const fakeRootHash = 'ABCDEabcde' + rootHash.substring(10)
|
||||
const tangle2 = new FeedV1.Tangle(fakeRootHash)
|
||||
tangle2.add(fakeRootHash, rootMsg)
|
||||
tangle2.add(unknownMsgHash, unknownMsg)
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle2,
|
||||
},
|
||||
})
|
||||
const msgHash2 = FeedV1.getMsgHash(msg2)
|
||||
|
||||
const err = FeedV1.validate(msg2, tangle, msgHash2, rootHash)
|
||||
t.ok(err, 'invalid 2nd msg throws')
|
||||
t.match(
|
||||
err.message,
|
||||
/all prev are locally unknown/,
|
||||
'invalid 2nd msg description'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid feed msg with a different who', (t) => {
|
||||
const keysA = generateKeypair('alice')
|
||||
const keysB = generateKeypair('bob')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keysA, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
const feedTangle = new FeedV1.Tangle(rootHash)
|
||||
feedTangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg = FeedV1.create({
|
||||
keys: keysB,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: feedTangle,
|
||||
},
|
||||
})
|
||||
const msgHash = FeedV1.getMsgHash(msg)
|
||||
|
||||
const err = FeedV1.validate(msg, feedTangle, msgHash, rootHash)
|
||||
t.match(err.message, /who ".*" does not match feed who/, 'invalid feed msg')
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid feed msg with a different type', (t) => {
|
||||
const keysA = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keysA, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
const feedTangle = new FeedV1.Tangle(rootHash)
|
||||
feedTangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg = FeedV1.create({
|
||||
keys: keysA,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'comment',
|
||||
tangles: {
|
||||
[rootHash]: feedTangle,
|
||||
},
|
||||
})
|
||||
const msgHash = FeedV1.getMsgHash(msg)
|
||||
|
||||
const err = FeedV1.validate(msg, feedTangle, msgHash, rootHash)
|
||||
t.match(
|
||||
err.message,
|
||||
/type "comment" does not match feed type "post"/,
|
||||
'invalid feed msg'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid feed msg with non-alphabetical prev', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '1' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '2' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash2 = FeedV1.getMsgHash(msg2)
|
||||
|
||||
tangle.add(msgHash1, msg1)
|
||||
tangle.add(msgHash2, msg2)
|
||||
|
||||
const msg3 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '3' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash3 = FeedV1.getMsgHash(msg3)
|
||||
|
||||
let prevHashes = msg3.metadata.tangles[rootHash].prev
|
||||
if (prevHashes[0] < prevHashes[1]) {
|
||||
prevHashes = [prevHashes[1], prevHashes[0]]
|
||||
} else {
|
||||
prevHashes = [prevHashes[0], prevHashes[1]]
|
||||
}
|
||||
msg3.metadata.tangles[rootHash].prev = prevHashes
|
||||
|
||||
const err = FeedV1.validate(msg3, tangle, msgHash3, rootHash)
|
||||
t.ok(err, 'invalid 3rd msg throws')
|
||||
t.match(
|
||||
err.message,
|
||||
/prev must be sorted in alphabetical order/,
|
||||
'invalid error message'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid feed msg with duplicate prev', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: '1' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
|
||||
const [prevHash] = msg1.metadata.tangles[rootHash].prev
|
||||
msg1.metadata.tangles[rootHash].prev = [prevHash, prevHash]
|
||||
|
||||
const err = FeedV1.validate(msg1, tangle, msgHash1, rootHash)
|
||||
t.ok(err, 'invalid 1st msg throws')
|
||||
t.match(
|
||||
err.message,
|
||||
/prev must be unique set/,
|
||||
'invalid error message'
|
||||
)
|
||||
t.end()
|
||||
})
|
|
@ -1,89 +0,0 @@
|
|||
const tape = require('tape')
|
||||
const FeedV1 = require('../../lib/feed-v1')
|
||||
const { generateKeypair } = require('../util')
|
||||
|
||||
tape('invalid type not a string', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
t.throws(
|
||||
() => {
|
||||
FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 123,
|
||||
})
|
||||
},
|
||||
/type is not a string/,
|
||||
'invalid type if contains /'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid type with "/" character', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
t.throws(
|
||||
() => {
|
||||
FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'group/init',
|
||||
})
|
||||
},
|
||||
/invalid type/,
|
||||
'invalid type if contains /'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid type with "*" character', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
t.throws(
|
||||
() => {
|
||||
FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'star*',
|
||||
})
|
||||
},
|
||||
/invalid type/,
|
||||
'invalid type if contains *'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid type too short', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
t.throws(
|
||||
() => {
|
||||
FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'xy',
|
||||
})
|
||||
},
|
||||
/shorter than 3/,
|
||||
'invalid type if too short'
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid type too long', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
t.throws(
|
||||
() => {
|
||||
FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'a'.repeat(120),
|
||||
})
|
||||
},
|
||||
/100\+ characters long/,
|
||||
'invalid type if too long'
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
|
@ -1,114 +0,0 @@
|
|||
const tape = require('tape')
|
||||
const FeedV1 = require('../../lib/feed-v1')
|
||||
const { generateKeypair } = require('../util')
|
||||
|
||||
tape('lipmaa prevs', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
const content = { text: 'Hello world!' }
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
tangle.add(msgHash1, msg1)
|
||||
t.equals(msg1.metadata.tangles[rootHash].depth, 1, 'msg1 depth')
|
||||
t.deepEquals(msg1.metadata.tangles[rootHash].prev, [rootHash], 'msg1 prev')
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash2 = FeedV1.getMsgHash(msg2)
|
||||
tangle.add(msgHash2, msg2)
|
||||
t.equals(msg2.metadata.tangles[rootHash].depth, 2, 'msg2 depth')
|
||||
t.deepEquals(msg2.metadata.tangles[rootHash].prev, [msgHash1], 'msg2 prev')
|
||||
|
||||
const msg3 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash3 = FeedV1.getMsgHash(msg3)
|
||||
tangle.add(msgHash3, msg3)
|
||||
t.equals(msg3.metadata.tangles[rootHash].depth, 3, 'msg3 depth')
|
||||
t.deepEquals(
|
||||
msg3.metadata.tangles[rootHash].prev,
|
||||
[rootHash, msgHash2].sort(),
|
||||
'msg3 prev (has lipmaa!)'
|
||||
)
|
||||
|
||||
const msg4 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash4 = FeedV1.getMsgHash(msg4)
|
||||
tangle.add(msgHash4, msg4)
|
||||
t.equals(msg4.metadata.tangles[rootHash].depth, 4, 'msg4 depth')
|
||||
t.deepEquals(msg4.metadata.tangles[rootHash].prev, [msgHash3], 'msg4 prev')
|
||||
|
||||
const msg5 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash5 = FeedV1.getMsgHash(msg5)
|
||||
tangle.add(msgHash5, msg5)
|
||||
t.equals(msg5.metadata.tangles[rootHash].depth, 5, 'msg5 depth')
|
||||
t.deepEquals(msg5.metadata.tangles[rootHash].prev, [msgHash4], 'msg5 prev')
|
||||
|
||||
const msg6 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash6 = FeedV1.getMsgHash(msg6)
|
||||
tangle.add(msgHash6, msg6)
|
||||
t.equals(msg6.metadata.tangles[rootHash].depth, 6, 'msg6 depth')
|
||||
t.deepEquals(msg6.metadata.tangles[rootHash].prev, [msgHash5], 'msg6 prev')
|
||||
|
||||
const msg7 = FeedV1.create({
|
||||
keys,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash7 = FeedV1.getMsgHash(msg7)
|
||||
tangle.add(msgHash7, msg7)
|
||||
t.equals(msg7.metadata.tangles[rootHash].depth, 7, 'msg7 depth')
|
||||
t.deepEquals(
|
||||
msg7.metadata.tangles[rootHash].prev,
|
||||
[msgHash3, msgHash6].sort(),
|
||||
'msg7 prev (has lipmaa!)'
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
|
@ -1,162 +0,0 @@
|
|||
const tape = require('tape')
|
||||
const FeedV1 = require('../../lib/feed-v1')
|
||||
const { generateKeypair } = require('../util')
|
||||
|
||||
tape('simple multi-author tangle', (t) => {
|
||||
const keysA = generateKeypair('alice')
|
||||
const keysB = generateKeypair('bob')
|
||||
|
||||
const rootMsgA = FeedV1.createRoot(keysA, 'post')
|
||||
const rootHashA = FeedV1.getMsgHash(rootMsgA)
|
||||
const tangleA = new FeedV1.Tangle(rootHashA)
|
||||
tangleA.add(rootHashA, rootMsgA)
|
||||
|
||||
const rootMsgB = FeedV1.createRoot(keysB, 'post')
|
||||
const rootHashB = FeedV1.getMsgHash(rootMsgB)
|
||||
const tangleB = new FeedV1.Tangle(rootHashB)
|
||||
tangleB.add(rootHashB, rootMsgB)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys: keysA,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHashA]: tangleA,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
t.deepEquals(
|
||||
Object.keys(msg1.metadata.tangles),
|
||||
[rootHashA],
|
||||
'msg1 has only feed tangle'
|
||||
)
|
||||
|
||||
const tangleX = new FeedV1.Tangle(msgHash1)
|
||||
tangleX.add(msgHash1, msg1)
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys: keysB,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHashB]: tangleB,
|
||||
[msgHash1]: tangleX,
|
||||
},
|
||||
})
|
||||
|
||||
t.deepEquals(
|
||||
Object.keys(msg2.metadata.tangles),
|
||||
[rootHashB, msgHash1].sort(),
|
||||
'msg2 has feed tangle and misc tangle'
|
||||
)
|
||||
t.equal(msg2.metadata.tangles[rootHashB].depth, 1, 'msg2 feed tangle depth')
|
||||
t.deepEquals(
|
||||
msg2.metadata.tangles[rootHashB].prev,
|
||||
[rootHashB],
|
||||
'msg2 feed tangle prev'
|
||||
)
|
||||
|
||||
t.equal(msg2.metadata.tangles[msgHash1].depth, 1, 'msg2 has tangle depth 1')
|
||||
t.deepEquals(
|
||||
msg2.metadata.tangles[msgHash1].prev,
|
||||
[msgHash1],
|
||||
'msg2 has tangle prev'
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('lipmaa in multi-author tangle', (t) => {
|
||||
const keysA = generateKeypair('alice')
|
||||
const keysB = generateKeypair('bob')
|
||||
|
||||
const content = { text: 'Hello world!' }
|
||||
|
||||
const rootMsgA = FeedV1.createRoot(keysA, 'post')
|
||||
const rootHashA = FeedV1.getMsgHash(rootMsgA)
|
||||
const tangleA = new FeedV1.Tangle(rootHashA)
|
||||
tangleA.add(rootHashA, rootMsgA)
|
||||
|
||||
const rootMsgB = FeedV1.createRoot(keysB, 'post')
|
||||
const rootHashB = FeedV1.getMsgHash(rootMsgB)
|
||||
const tangleB = new FeedV1.Tangle(rootHashB)
|
||||
tangleB.add(rootHashB, rootMsgB)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys: keysA,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHashA]: tangleA,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
tangleA.add(msgHash1, msg1)
|
||||
const tangleThread = new FeedV1.Tangle(msgHash1)
|
||||
tangleThread.add(msgHash1, msg1)
|
||||
|
||||
t.deepEquals(
|
||||
Object.keys(msg1.metadata.tangles),
|
||||
[rootHashA],
|
||||
'A:msg1 has only feed tangle'
|
||||
)
|
||||
|
||||
const msg2 = FeedV1.create({
|
||||
keys: keysB,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHashB]: tangleB,
|
||||
[msgHash1]: tangleThread,
|
||||
},
|
||||
})
|
||||
const msgHash2 = FeedV1.getMsgHash(msg2)
|
||||
tangleB.add(msgHash2, msg2)
|
||||
tangleThread.add(msgHash2, msg2)
|
||||
|
||||
t.deepEquals(
|
||||
msg2.metadata.tangles[msgHash1].prev,
|
||||
[msgHash1],
|
||||
'B:msg2 points to A:msg1'
|
||||
)
|
||||
|
||||
const msg3 = FeedV1.create({
|
||||
keys: keysB,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHashB]: tangleB,
|
||||
[msgHash1]: tangleThread,
|
||||
},
|
||||
})
|
||||
const msgHash3 = FeedV1.getMsgHash(msg3)
|
||||
tangleB.add(msgHash3, msg3)
|
||||
tangleThread.add(msgHash3, msg3)
|
||||
|
||||
t.deepEquals(
|
||||
msg3.metadata.tangles[msgHash1].prev,
|
||||
[msgHash2],
|
||||
'B:msg3 points to B:msg2'
|
||||
)
|
||||
|
||||
const msg4 = FeedV1.create({
|
||||
keys: keysA,
|
||||
content,
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHashA]: tangleA,
|
||||
[msgHash1]: tangleThread,
|
||||
},
|
||||
})
|
||||
const msgHash4 = FeedV1.getMsgHash(msg4)
|
||||
tangleB.add(msgHash4, msg4)
|
||||
tangleThread.add(msgHash4, msg4)
|
||||
|
||||
t.deepEquals(
|
||||
msg4.metadata.tangles[msgHash1].prev,
|
||||
[msgHash1, msgHash3].sort(),
|
||||
'A:msg4 points to A:msg1,B:msg3'
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
|
@ -1,111 +0,0 @@
|
|||
const tape = require('tape')
|
||||
const base58 = require('bs58')
|
||||
const FeedV1 = require('../../lib/feed-v1')
|
||||
const { generateKeypair } = require('../util')
|
||||
|
||||
tape('validate root msg', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
|
||||
const err = FeedV1.validate(rootMsg, tangle, rootHash, rootHash)
|
||||
if (err) console.log(err)
|
||||
t.error(err, 'valid root msg')
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('validate 2nd msg with existing root', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
tangle.add(msgHash1, msg1)
|
||||
|
||||
const err = FeedV1.validate(msg1, tangle, msgHash1, rootHash)
|
||||
if (err) console.log(err)
|
||||
t.error(err, 'valid 2nd msg')
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('validate 2nd forked msg', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1A = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
existing: new Map(),
|
||||
})
|
||||
const msgHash1A = FeedV1.getMsgHash(msg1A)
|
||||
|
||||
const msg1B = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1B = FeedV1.getMsgHash(msg1B)
|
||||
|
||||
tangle.add(msgHash1A, msg1A)
|
||||
tangle.add(msgHash1B, msg1B)
|
||||
const err = FeedV1.validate(msg1B, tangle, msgHash1B, rootHash)
|
||||
if (err) console.log(err)
|
||||
t.error(err, 'valid 2nd forked msg')
|
||||
t.end()
|
||||
})
|
||||
|
||||
tape('invalid msg with unknown previous', (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'post')
|
||||
const rootHash = FeedV1.getMsgHash(rootMsg)
|
||||
const tangle = new FeedV1.Tangle(rootHash)
|
||||
tangle.add(rootHash, rootMsg)
|
||||
|
||||
const msg1 = FeedV1.create({
|
||||
keys,
|
||||
content: { text: 'Hello world!' },
|
||||
type: 'post',
|
||||
tangles: {
|
||||
[rootHash]: tangle,
|
||||
},
|
||||
})
|
||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||
|
||||
const fakeMsgHash = base58.encode(Buffer.alloc(16).fill(42))
|
||||
|
||||
msg1.metadata.tangles[rootHash].prev = [fakeMsgHash]
|
||||
|
||||
const err = FeedV1.validate(msg1, tangle, msgHash1, rootHash)
|
||||
t.ok(err, 'invalid 2nd msg throws')
|
||||
t.match(
|
||||
err.message,
|
||||
/all prev are locally unknown/,
|
||||
'invalid 2nd msg description'
|
||||
)
|
||||
t.end()
|
||||
})
|
Loading…
Reference in New Issue