var Client = require('../')
var magnet = require('magnet-uri')
var Server = require('../').Server
var test = require('tape')

var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub'
var parsedTorrent = magnet(uri)
var peerId = new Buffer('01234567890123456789')

test('magnet + udp: client.start/update/stop()', function (t) {
  t.plan(10)

  var server = new Server({ http: false, ws: false })

  server.on('error', function (err) {
    t.fail(err.message)
  })

  server.on('warning', function (err) {
    t.fail(err.message)
  })

  server.listen(0, function () {
    var port = server.udp.address().port
    var announceUrl = 'udp://127.0.0.1:' + port

    // remove all tracker servers except a single UDP one
    parsedTorrent.announce = [ announceUrl ]

    var client = new Client(peerId, 6881, parsedTorrent)

    client.on('error', function (err) {
      t.error(err)
    })

    client.once('update', function (data) {
      t.equal(data.announce, announceUrl)
      t.equal(typeof data.complete, 'number')
      t.equal(typeof data.incomplete, 'number')
    })

    client.start()

    client.once('peer', function () {
      t.pass('there is at least one peer')

      client.update()

      client.once('update', function (data) {
        // receive one final update after calling stop
        t.equal(data.announce, announceUrl)
        t.equal(typeof data.complete, 'number')
        t.equal(typeof data.incomplete, 'number')

        client.stop()

        client.once('update', function (data) {
          // received an update!
          t.equal(data.announce, announceUrl)
          t.equal(typeof data.complete, 'number')
          t.equal(typeof data.incomplete, 'number')

          server.close()
          client.destroy()
        })
      })
    })
  })
})

@@ -25,22 +25,22 @@
 /')%0Avar 
-magnet
+common
  = requi
@@ -47,31 +47,29 @@
 re('
-magnet-uri
+./common
 ')%0Avar 
-Server
+magnet
  = r
@@ -80,20 +80,20 @@
 re('
-../').Server
+magnet-uri')
 %0Avar
@@ -315,609 +315,409 @@
 ')%0A%0A
-test('magnet + udp: client.start/update/stop()', function (t) %7B%0A  t.plan(10)%0A%0A  var server = new Server(%7B http: false, ws: false %7D)%0A%0A  server.on('error', function (err) %7B%0A    t.fail(err.message)%0A  %7D)%0A%0A  server.on('warning', function (err) %7B%0A    t.fail(err.message)%0A  %7D)%0A%0A  server.listen(0, function () %7B%0A    var port = server.udp.address().port%0A    var announceUrl = 'udp://127.0.0.1:' + port%0A%0A    // remove all tracker servers except a single UDP one%0A    parsedTorrent.announce = %5B announceUrl %5D%0A%0A    var client = new Client(peerId, 6881, parsedTorrent)%0A%0A    client.on('error', function (err) %7B%0A     
+function testMagnet (t, serverType) %7B%0A  t.plan(9)%0A%0A  common.createServer(t, serverType, function (server, announceUrl) %7B%0A    parsedTorrent.announce = %5B announceUrl %5D%0A%0A    var client = new Client(peerId, 6881, parsedTorrent, %7B wrtc: %7B%7D %7D)%0A%0A    if (serverType === 'ws') common.mockWebsocketTracker(client)%0A    client.on('error', function (err) %7B t.error(err) %7D)%0A    client.on('warning', function (err) %7B
  t.e
@@ -725,20 +725,16 @@
 ror(err)
-%0A   
  %7D)%0A%0A   
@@ -913,117 +913,8 @@
 er')
-%0A    %7D)%0A%0A    client.start()%0A%0A    client.once('peer', function () %7B%0A      t.pass('there is at least one peer')
 %0A%0A  
@@ -984,63 +984,8 @@
 ) %7B%0A
-        // receive one final update after calling stop%0A
     
@@ -1198,41 +1198,8 @@
 ) %7B%0A
-          // received an update!%0A
     
@@ -1422,16 +1422,305 @@
 %0A    %7D)%0A
-  %7D
+%0A    client.start()%0A  %7D)%0A%7D%0A%0Atest('http: magnet: client.start/update/stop()', function (t) %7B%0A  testMagnet(t, 'http')%0A%7D)%0A%0Atest('udp: magnet: client.start/update/stop()', function (t) %7B%0A  testMagnet(t, 'udp')%0A%7D)%0A%0Atest('ws: magnet: client.start/update/stop()', function (t) %7B%0A  testMagnet(t, 'ws'
 )%0A%7D)%0A

test http/ws trackers in magnet test