2020-04-09 16:07:18 +02:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.use(require('dirty-chai'))
|
|
|
|
const { expect } = chai
|
|
|
|
|
|
|
|
const pDefer = require('p-defer')
|
|
|
|
|
2020-04-16 15:20:42 +02:00
|
|
|
const PeerStore = require('../../src/peer-store')
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
const peerUtils = require('../utils/creators/peer')
|
|
|
|
const {
|
|
|
|
ERR_INVALID_PARAMETERS
|
|
|
|
} = require('../../src/errors')
|
|
|
|
|
|
|
|
const arraysAreEqual = (a, b) => a.length === b.length && a.sort().every((item, index) => b[index] === item)
|
|
|
|
|
|
|
|
describe('protoBook', () => {
|
|
|
|
let peerId
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
[peerId] = await peerUtils.createPeerId()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('protoBook.set', () => {
|
2020-04-16 15:20:42 +02:00
|
|
|
let peerStore, pb
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore = new PeerStore()
|
|
|
|
pb = peerStore.protoBook
|
2020-04-09 16:07:18 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.removeAllListeners()
|
2020-04-09 16:07:18 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
|
|
|
expect(() => {
|
|
|
|
pb.set('invalid peerId')
|
|
|
|
}).to.throw(ERR_INVALID_PARAMETERS)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throwns invalid parameters error if no protocols provided', () => {
|
|
|
|
expect(() => {
|
|
|
|
pb.set(peerId)
|
|
|
|
}).to.throw(ERR_INVALID_PARAMETERS)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('replaces the stored content by default and emit change event', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
const supportedProtocols = ['protocol1', 'protocol2']
|
|
|
|
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.once('change:protocols', ({ peerId, protocols }) => {
|
2020-04-09 16:07:18 +02:00
|
|
|
expect(peerId).to.exist()
|
|
|
|
expect(protocols).to.have.deep.members(supportedProtocols)
|
|
|
|
defer.resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
pb.set(peerId, supportedProtocols)
|
|
|
|
const protocols = pb.get(peerId)
|
|
|
|
expect(protocols).to.have.deep.members(supportedProtocols)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits on set if not storing the exact same content', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
const supportedProtocolsA = ['protocol1', 'protocol2']
|
|
|
|
const supportedProtocolsB = ['protocol2']
|
|
|
|
|
|
|
|
let changeCounter = 0
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.on('change:protocols', () => {
|
2020-04-09 16:07:18 +02:00
|
|
|
changeCounter++
|
|
|
|
if (changeCounter > 1) {
|
|
|
|
defer.resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// set 1
|
|
|
|
pb.set(peerId, supportedProtocolsA)
|
|
|
|
|
|
|
|
// set 2 (same content)
|
|
|
|
pb.set(peerId, supportedProtocolsB)
|
|
|
|
const protocols = pb.get(peerId)
|
|
|
|
expect(protocols).to.have.deep.members(supportedProtocolsB)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not emit on set if it is storing the exact same content', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
const supportedProtocols = ['protocol1', 'protocol2']
|
|
|
|
|
|
|
|
let changeCounter = 0
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.on('change:protocols', () => {
|
2020-04-09 16:07:18 +02:00
|
|
|
changeCounter++
|
|
|
|
if (changeCounter > 1) {
|
|
|
|
defer.reject()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// set 1
|
|
|
|
pb.set(peerId, supportedProtocols)
|
|
|
|
|
|
|
|
// set 2 (same content)
|
|
|
|
pb.set(peerId, supportedProtocols)
|
|
|
|
|
|
|
|
// Wait 50ms for incorrect second event
|
|
|
|
setTimeout(() => {
|
|
|
|
defer.resolve()
|
|
|
|
}, 50)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('protoBook.add', () => {
|
2020-04-16 15:20:42 +02:00
|
|
|
let peerStore, pb
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore = new PeerStore()
|
|
|
|
pb = peerStore.protoBook
|
2020-04-09 16:07:18 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.removeAllListeners()
|
2020-04-09 16:07:18 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
|
|
|
expect(() => {
|
|
|
|
pb.add('invalid peerId')
|
|
|
|
}).to.throw(ERR_INVALID_PARAMETERS)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throwns invalid parameters error if no protocols provided', () => {
|
|
|
|
expect(() => {
|
|
|
|
pb.add(peerId)
|
|
|
|
}).to.throw(ERR_INVALID_PARAMETERS)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds the new content and emits change event', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
const supportedProtocolsA = ['protocol1', 'protocol2']
|
|
|
|
const supportedProtocolsB = ['protocol3']
|
|
|
|
const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB)
|
|
|
|
|
|
|
|
let changeTrigger = 2
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.on('change:protocols', ({ protocols }) => {
|
2020-04-09 16:07:18 +02:00
|
|
|
changeTrigger--
|
|
|
|
if (changeTrigger === 0 && arraysAreEqual(protocols, finalProtocols)) {
|
|
|
|
defer.resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Replace
|
|
|
|
pb.set(peerId, supportedProtocolsA)
|
|
|
|
let protocols = pb.get(peerId)
|
|
|
|
expect(protocols).to.have.deep.members(supportedProtocolsA)
|
|
|
|
|
|
|
|
// Add
|
|
|
|
pb.add(peerId, supportedProtocolsB)
|
|
|
|
protocols = pb.get(peerId)
|
|
|
|
expect(protocols).to.have.deep.members(finalProtocols)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits on add if the content to add not exists', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
const supportedProtocolsA = ['protocol1']
|
|
|
|
const supportedProtocolsB = ['protocol2']
|
|
|
|
const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB)
|
|
|
|
|
|
|
|
let changeCounter = 0
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.on('change:protocols', () => {
|
2020-04-09 16:07:18 +02:00
|
|
|
changeCounter++
|
|
|
|
if (changeCounter > 1) {
|
|
|
|
defer.resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// set 1
|
|
|
|
pb.set(peerId, supportedProtocolsA)
|
|
|
|
|
|
|
|
// set 2 (content already existing)
|
|
|
|
pb.add(peerId, supportedProtocolsB)
|
|
|
|
const protocols = pb.get(peerId)
|
|
|
|
expect(protocols).to.have.deep.members(finalProtocols)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not emit on add if the content to add already exists', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
const supportedProtocolsA = ['protocol1', 'protocol2']
|
|
|
|
const supportedProtocolsB = ['protocol2']
|
|
|
|
|
|
|
|
let changeCounter = 0
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.on('change:protocols', () => {
|
2020-04-09 16:07:18 +02:00
|
|
|
changeCounter++
|
|
|
|
if (changeCounter > 1) {
|
|
|
|
defer.reject()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// set 1
|
|
|
|
pb.set(peerId, supportedProtocolsA)
|
|
|
|
|
|
|
|
// set 2 (content already existing)
|
|
|
|
pb.add(peerId, supportedProtocolsB)
|
|
|
|
|
|
|
|
// Wait 50ms for incorrect second event
|
|
|
|
setTimeout(() => {
|
|
|
|
defer.resolve()
|
|
|
|
}, 50)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('protoBook.get', () => {
|
2020-04-16 15:20:42 +02:00
|
|
|
let peerStore, pb
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore = new PeerStore()
|
|
|
|
pb = peerStore.protoBook
|
2020-04-09 16:07:18 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
|
|
|
expect(() => {
|
|
|
|
pb.get('invalid peerId')
|
|
|
|
}).to.throw(ERR_INVALID_PARAMETERS)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns undefined if no protocols are known for the provided peer', () => {
|
|
|
|
const protocols = pb.get(peerId)
|
|
|
|
|
|
|
|
expect(protocols).to.not.exist()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns the protocols stored', () => {
|
|
|
|
const supportedProtocols = ['protocol1', 'protocol2']
|
|
|
|
|
|
|
|
pb.set(peerId, supportedProtocols)
|
|
|
|
|
|
|
|
const protocols = pb.get(peerId)
|
|
|
|
expect(protocols).to.have.deep.members(supportedProtocols)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('protoBook.delete', () => {
|
2020-04-16 15:20:42 +02:00
|
|
|
let peerStore, pb
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore = new PeerStore()
|
|
|
|
pb = peerStore.protoBook
|
2020-04-09 16:07:18 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
|
|
|
expect(() => {
|
|
|
|
pb.delete('invalid peerId')
|
|
|
|
}).to.throw(ERR_INVALID_PARAMETERS)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns false if no records exist for the peer and no event is emitted', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.on('change:protocols', () => {
|
2020-04-09 16:07:18 +02:00
|
|
|
defer.reject()
|
|
|
|
})
|
|
|
|
|
|
|
|
const deleted = pb.delete(peerId)
|
|
|
|
|
|
|
|
expect(deleted).to.equal(false)
|
|
|
|
|
|
|
|
// Wait 50ms for incorrect invalid event
|
|
|
|
setTimeout(() => {
|
|
|
|
defer.resolve()
|
|
|
|
}, 50)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns true if the record exists and an event is emitted', () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
const supportedProtocols = ['protocol1', 'protocol2']
|
|
|
|
pb.set(peerId, supportedProtocols)
|
|
|
|
|
|
|
|
// Listen after set
|
2020-04-16 15:20:42 +02:00
|
|
|
peerStore.on('change:protocols', ({ protocols }) => {
|
2020-04-09 16:07:18 +02:00
|
|
|
expect(protocols.length).to.eql(0)
|
|
|
|
defer.resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
const deleted = pb.delete(peerId)
|
|
|
|
|
|
|
|
expect(deleted).to.equal(true)
|
|
|
|
|
|
|
|
return defer.promise
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|