<commit_before>/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const CyclonPeer = require('../src')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const parallel = require('run-parallel')
const series = require('run-series')
const waterfall = require('run-waterfall')
const debug = require('debug')('gossip:cyclon:test')

const alice = {
  id: '<KEY>',
  privKey: '<KEY>',
  pubKey: '<KEY>
}

describe('cyclon-peer', function () {
  this.timeout(20000)
  const AliceId = PeerId.createFromPrivKey(alice.privKey)
  const Bob = new CyclonPeer({peers: [new PeerInfo(PeerId.createFromB58String(AliceId.toB58String()))]})

  it('create with bootstrap peers', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    expect(Alice).to.exist
    expect(Bob).to.exist
    expect(Bob.partialView).to.have.lengthOf(1)
    expect(Object.keys(Bob.partialView.peers)[0]).to.eql(Bob.partialView.peerToId(Alice.peer))
    expect(Bob.partialView.get(Alice.peer)).to.exist
    done()
  })

  it('add peers to neighbors set', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    Alice.addPeers([Bob.peer])
    expect(Alice.partialView).to.have.lengthOf(1)
    expect(Alice.partialView.get(Bob.peer)).to.exist
    expect(Alice.partialView.peers[Alice.partialView.peerToId(Bob.peer)]).to.exist
    done()
  })

  it('adding a peer emits an `add` event', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    Alice.partialView.on('add', (peer) => {
      expect(peer).to.exist
      expect(Alice.partialView.peerToId(peer)).to.eql(Alice.partialView.peerToId(Bob.peer))
      done()
    })
    Alice.addPeers([Bob.peer])
  })

  it('removing a peer emits a remove event', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    Alice.addPeers([Bob.peer])
    Alice.partialView.on('remove', (peer) => {
      expect(peer).to.exist
      expect(peer).to.eql(Alice.partialView.peerToId(Bob.peer))
      expect(Alice.partialView).to.have.lengthOf(0)
      done()
    })
    Alice.partialView.remove(Bob.peer)
  })

  describe('listen', () => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})

    it('starts listening on a tcp port', (done) => {
      Alice.listen((err) => {
        expect(err).to.not.exist
        Alice.close(() => {
          done()
        })
      })
    })
  })

  describe('shuffle', () => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    const Charles = new CyclonPeer()
    const Eve = new CyclonPeer()
    debug('Alice:', Alice.peer.id.toB58String().substr(2, 6))
    debug('Bob:', Bob.peer.id.toB58String().substr(2, 6))<commit_msg>adding extra test for maxPeers<commit_after>/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const CyclonPeer = require('../src')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const parallel = require('run-parallel')
const series = require('run-series')
const waterfall = require('run-waterfall')
const debug = require('debug')('gossip:cyclon:test')

const alice = {
  id: '<KEY>',
  privKey: '<KEY>',
  pubKey: '<KEY>
}

describe('cyclon-peer', function () {
  this.timeout(20000)
  const AliceId = PeerId.createFromPrivKey(alice.privKey)
  const Bob = new CyclonPeer({peers: [new PeerInfo(PeerId.createFromB58String(AliceId.toB58String()))]})
  const Charles = new CyclonPeer()
  const Eve = new CyclonPeer()

  it('create with bootstrap peers', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    expect(Alice).to.exist
    expect(Bob).to.exist
    expect(Bob.partialView).to.have.lengthOf(1)
    expect(Object.keys(Bob.partialView.peers)[0]).to.eql(Bob.partialView.peerToId(Alice.peer))
    expect(Bob.partialView.get(Alice.peer)).to.exist
    done()
  })

  it('partialView is within the maxPeers limit', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId), maxPeers: 1})
    Alice.addPeers([Bob.peer])
    Alice.addPeers([Charles.peer])
    expect(Alice.partialView.length).to.eql(1)
    Alice.addPeers([Charles.peer], [Bob.peer])
    expect(Alice.partialView.length).to.eql(1)
    done()
  })

  it('add peers to neighbors set', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    Alice.addPeers([Bob.peer])
    expect(Alice.partialView).to.have.lengthOf(1)
    expect(Alice.partialView.get(Bob.peer)).to.exist
    expect(Alice.partialView.peers[Alice.partialView.peerToId(Bob.peer)]).to.exist
    done()
  })

  it('adding a peer emits an `add` event', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    Alice.partialView.on('add', (peer) => {
      expect(peer).to.exist
      expect(Alice.partialView.peerToId(peer)).to.eql(Alice.partialView.peerToId(Bob.peer))
      done()
    })
    Alice.addPeers([Bob.peer])
  })

  it('removing a peer emits a remove event', (done) => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    Alice.addPeers([Bob.peer])
    Alice.partialView.on('remove', (peer) => {
      expect(peer).to.exist
      expect(peer).to.eql(Alice.partialView.peerToId(Bob.peer))
      expect(Alice.partialView).to.have.lengthOf(0)
      done()
    })
    Alice.partialView.remove(Bob.peer)
  })

  describe('listen', () => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})

    it('starts listening on a tcp port', (done) => {
      Alice.listen((err) => {
        expect(err).to.not.exist
        Alice.close(() => {
          done()
        })
      })
    })
  })

  describe('shuffle', () => {
    const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
    debug('Alice:', Alice.peer.id.toB58String().substr(2, 6))
    debug('Bob:', Bob.peer.id.toB58String().substr(2, 6))
{'fraction_non_alphanumeric': 0.17339749097783125, 'fraction_numerical': 0.0077332875064444065, 'mean_word_length': 3.758789860997547, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '191994', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2034, 'n_words': 353}