var path = require('path')
var fs = require('fs')
var get = require('simple-get')
var test = require('tape')
var WebTorrent = require('../')

var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')
var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent'))

test('start http server programmatically', function (t) {
  t.plan(4)

  var client = new WebTorrent({ tracker: false, dht: false })

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

  var torrent = client.add(leavesTorrent, { dht: false, tracker: false }, function (torrent) {
    t.pass('got "torrent" event')

    var server = torrent.createServer()
    server.listen(0, function () {
      t.pass('server is listening')
      var port = server.address().port
      get.concat('http://localhost:' + port + '/0', function (err, data) {
        if (err) throw err
        // Verify data for first (and only file)
        t.deepEqual(data, fs.readFileSync(leavesPath))

        server.close()
        client.destroy()
      })
    })
  })
  torrent.on('ready', function () {
    torrent.load(fs.createReadStream(leavesPath), function (err) {
      if (err) throw err
      t.pass('loaded seed content into torrent')
    })
  })
})

@@ -1,31 +1,4 @@
-var path = require('path')%0A
 var 
@@ -48,16 +48,43 @@
 e-get')%0A
+var path = require('path')%0A
 var test
@@ -329,25 +329,31 @@
 st('
-start http s
+torrent.createS
 erver
+():
  pro
@@ -365,12 +365,20 @@
 atic
-ally
+ http server
 ', f
@@ -404,9 +404,9 @@
 lan(
-4
+9
 )%0A%0A 
@@ -466,17 +466,16 @@
 alse %7D)%0A
-%0A
   client
@@ -580,22 +580,8 @@
 )%0A%0A 
- var torrent =
  cli
@@ -606,40 +606,8 @@
 ent,
- %7B dht: false, tracker: false %7D,
  fun
@@ -658,17 +658,16 @@
 event')%0A
-%0A
     var 
@@ -698,16 +698,17 @@
 erver()%0A
+%0A
     serv
@@ -738,44 +738,8 @@
 ) %7B%0A
-      t.pass('server is listening')%0A
     
@@ -783,51 +783,363 @@
     
-get.concat('http://localhost:' + por
+t.pass('server is listening on ' + port)%0A%0A      // Seeding after server is created should work%0A      torrent.load(fs.createReadStream(leavesPath), function (err) %7B%0A        t.error(err, 'loaded seed content into torrent')%0A      %7D)%0A%0A      var host = 'http://localhost:' + port%0A%0A      // Index page should list files in the torrent%0A      get.concat(hos
 t + '/
-0
 ', f
@@ -1168,34 +1168,134 @@
         
-if (err) throw err
+t.error(err)%0A        data = data.toString()%0A        t.ok(data.indexOf('Leaves of Grass by Walt Whitman.epub') !== -1)%0A
 %0A       
@@ -1305,20 +1305,28 @@
  Verify 
-data
+file content
  for fir
@@ -1341,15 +1341,95 @@
 only
+)
  file
-)
 %0A
+        get.concat(host + '/0', function (err, data) %7B%0A          t.error(err)%0A  
     
@@ -1484,24 +1484,26 @@
 ))%0A%0A        
+  
 server.close
@@ -1507,231 +1507,136 @@
 ose(
-)%0A        client.destroy()%0A      %7D)%0A    %7D)%0A  %7D)%0A  torrent.on('ready', function () %7B%0A    torrent.load(fs.createReadStream(leavesPath), function (err) %7B%0A      if (err) throw err%0A      t.pass('loaded seed content into torrent'
+function () %7B t.pass('server closed') %7D)%0A          client.destroy(function () %7B t.pass('client destroyed') %7D)%0A        %7D)%0A      %7D
 )%0A  

improve torrent.createServer() test