Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 54dce0b

Browse files
committed
test: add tests for get-peer-info
1 parent bf35765 commit 54dce0b

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/get-peer-info.spec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
9+
const PeerBook = require('peer-book')
10+
const PeerInfo = require('peer-info')
11+
const PeerId = require('peer-id')
12+
const MultiAddr = require('multiaddr')
13+
const TestPeerInfos = require('./test-data/ids.json').infos
14+
15+
const getPeerInfo = require('../src/get-peer-info')
16+
17+
describe('Get peer info', () => {
18+
let peerBook
19+
let peerInfoA
20+
let multiaddrA
21+
let peerIdA
22+
23+
before((done) => {
24+
peerBook = new PeerBook()
25+
PeerId.createFromJSON(TestPeerInfos[0].id, (err, id) => {
26+
peerIdA = id
27+
peerInfoA = new PeerInfo(peerIdA)
28+
multiaddrA = MultiAddr('/ipfs/QmdWYwTywvXBeLKWthrVNjkq9SafEDn1PbAZdz4xZW7Jd9')
29+
peerInfoA.multiaddrs.add(multiaddrA)
30+
peerBook.put(peerInfoA)
31+
done(err)
32+
})
33+
})
34+
35+
it('should be able get peer info from multiaddr', () => {
36+
let _peerInfo = getPeerInfo(multiaddrA, peerBook)
37+
expect(peerBook.has(_peerInfo)).to.equal(true)
38+
expect(peerInfoA).to.deep.equal(_peerInfo)
39+
})
40+
41+
it('should return a new PeerInfo with a multiAddr not in the PeerBook', () => {
42+
let wrongMultiAddr = MultiAddr('/ipfs/QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi')
43+
let _peerInfo = getPeerInfo(wrongMultiAddr, peerBook)
44+
expect(PeerInfo.isPeerInfo(_peerInfo)).to.equal(true)
45+
expect(peerBook.has(_peerInfo)).to.equal(false)
46+
})
47+
48+
it('should be able get peer info from peer id', () => {
49+
let _peerInfo = getPeerInfo(multiaddrA, peerBook)
50+
expect(peerBook.has(_peerInfo)).to.equal(true)
51+
expect(peerInfoA).to.deep.equal(_peerInfo)
52+
})
53+
54+
it('should not be able to get the peer info for a wrong peer id', (done) => {
55+
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
56+
let func = () => {
57+
getPeerInfo(id, peerBook)
58+
}
59+
60+
expect(func).to.throw('Couldnt get PeerInfo')
61+
62+
done(err)
63+
})
64+
})
65+
66+
it('an invalid peer type should throw an error', () => {
67+
let func = () => {
68+
getPeerInfo('/ip4/127.0.0.1/tcp/1234', peerBook)
69+
}
70+
71+
expect(func).to.throw('peer type not recognized')
72+
})
73+
})

0 commit comments

Comments
 (0)