|
| 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