/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const parallel = require('async/parallel')

const MulticastDNS = require('./../src')

describe('MulticastDNS', () => {
  let pA
  let pB
  let pC

  before((done) => {
    parallel([
      (cb) => {
        PeerInfo.create((err, peer) => {
          if (err) { cb(err) }
          pA = peer
          pA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/20001'))
          cb()
        })
      },
      (cb) => {
        PeerInfo.create((err, peer) => {
          if (err) { cb(err) }
          pB = peer
          pB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/20002'))
          cb()
        })
      },
      (cb) => {
        PeerInfo.create((err, peer) => {
          if (err) { cb(err) }
          pC = peer
          pC.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/20003'))
          pC.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/30003/ws'))
          cb()
        })
      }
    ], done)
  })

  it('find another peer', (done) => {
    const options = {
      port: 50001   // port must be the same
    }
    const mdnsA = new MulticastDNS(pA, options)
    const mdnsB = new MulticastDNS(pB, options)

    mdnsA.once('peer', (peerInfo) => {
      expect(pB.id.toB58String()).to.eql(peerInfo.id.toB58String())
      done()
    })

    mdnsB.once('peer', (peerInfo) => {})
  })

  it('only announce TCP multiaddrs', (done) => {
    const options = {
      port: 50003   // port must be the same
    }
    const mdnsA = new MulticastDNS(pA, options)
    const mdnsC = new MulticastDNS(pC, options)

    mdnsA.once('peer', (peerInfo) => {
      expect(pC.id.toB58String()).to.eql(peerInfo.id.toB58String())
      expect(peerInfo.multiaddrs.length).to.equal(1)
      done()
    })

    mdnsC.once('peer', (peerInfo) => {})
  })
})

@@ -290,16 +290,25 @@
   let pC
+%0A  let pD
 %0A%0A  befo
@@ -1020,24 +1020,236 @@
 %0A        %7D)%0A
+      %7D,%0A      (cb) =%3E %7B%0A        PeerInfo.create((err, peer) =%3E %7B%0A          if (err) %7B cb(err) %7D%0A          pD = peer%0A          pD.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/30003/ws'))%0A          cb()%0A        %7D)%0A
       %7D%0A    
@@ -1760,32 +1760,33 @@
  the same%0A    %7D%0A
+%0A
     const mdnsA 
@@ -1856,32 +1856,80 @@
 DNS(pC, options)
+%0A    const mdnsD = new MulticastDNS(pD, options)
 %0A%0A    mdnsA.once
@@ -2087,32 +2087,32 @@
  done()%0A    %7D)%0A%0A
-
     mdnsC.once('
@@ -2132,20 +2132,61 @@
 nfo) =%3E %7B%7D)%0A
+    mdnsD.once('peer', (peerInfo) =%3E %7B%7D)%0A
   %7D)%0A%7D)%0A

add peer that has no TCP