var path = require('path')
var test = require('tape')
var spawn = require('./helpers/spawn.js')
var help = require('./helpers')

var dat = path.resolve(path.join(__dirname, '..', 'bin', 'cli.js'))

test('misc - doctor option works ', function (t) {
  var st = spawn(t, dat + ' doctor', {end: false})
  st.stdout.match(function (output) {
    var key = help.matchLink(output)
    if (!key) return false
    startPhysiciansAssistant(key)
    return true
  }, 'doctor started')

  function startPhysiciansAssistant (link) {
    var assist = spawn(t, dat + ' doctor ' + link, {end: false})
    assist.stdout.match(function (output) {
      if (output.indexOf('Public IP:') > -1) {
        st.kill()
        return true
      }
    }, 'download one started')
    assist.end(function () {
      t.end()
    })
  }
})

@@ -297,27 +297,27 @@
 e%7D)%0A  st.std
-out
+err
 .match(funct
@@ -327,24 +327,221 @@
  (output) %7B%0A
+    var readyPeer = output.indexOf('Waiting for incoming connections') %3E -1%0A    if (!readyPeer) return false%0A%0A    t.ok(output.indexOf('Phase one success!') %3E -1, 'doctor connects to public peer')%0A%0A
     var key 
@@ -569,35 +569,8 @@
 ut)%0A
-    if (!key) return false%0A
     
@@ -763,19 +763,19 @@
 sist.std
-out
+err
 .match(f
@@ -803,124 +803,364 @@
     
-if (output.indexOf('Public IP:
+var readyPeer = output.indexOf('Waiting for incoming connections
 ') %3E -1
-) %7B
 %0A      
-  st.kill()%0A        return true%0A      %7D%0A    %7D, 'download one started'
+if (!readyPeer) return false%0A%0A      t.same(help.matchLink(output), link, 'key of peer matches')%0A      t.ok(readyPeer, 'starts looking for peers')%0A      t.skip(output.indexOf('Remote peer echoed expected data back') %3E -1, 'echo data back')%0A      st.kill()%0A      return true%0A    %7D
 )%0A  

check public peer in doctor