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()
    })
  }
})

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.stderr.match(function (output) {
    var readyPeer = output.indexOf('Waiting for incoming connections') > -1
    if (!readyPeer) return false

    t.ok(output.indexOf('Phase one success!') > -1, 'doctor connects to public peer')

    var key = help.matchLink(output)
    startPhysiciansAssistant(key)
    return true
  }, 'doctor started')

  function startPhysiciansAssistant (link) {
    var assist = spawn(t, dat + ' doctor ' + link, {end: false})
    assist.stderr.match(function (output) {
      var readyPeer = output.indexOf('Waiting for incoming connections') > -1
      if (!readyPeer) return false

      t.same(help.matchLink(output), link, 'key of peer matches')
      t.ok(readyPeer, 'starts looking for peers')
      t.skip(output.indexOf('Remote peer echoed expected data back') > -1, 'echo data back')
      st.kill()
      return true
    })
    assist.end(function () {
      t.end()
    })
  }
})

check public peer in doctor