var WS = require('../')
var tape = require('tape')
var pull = require('pull-stream')
var JSONDL = require('pull-json-doubleline')

tape('simple echo server', function (t) {

  var server = WS.createServer(function (stream) {
    pull(stream, stream)
  }).listen(5678, function () {

    pull(
      pull.values([1,2,3]),
      //need a delay, because otherwise ws hangs up wrong.
      //otherwise use pull-goodbye.
      function (read) {
        return function (err, cb) {
          setTimeout(function () {
            read(null, cb)
          }, 10)
        }
      },
      JSONDL.stringify(),
      WS.connect('ws://localhost:5678'),
      JSONDL.parse(),
      pull.collect(function (err, ary) {
        if(err) throw err
        t.deepEqual(ary, [1,2,3])
        server.close(function () {
          t.end()
        })
      })
    )
  })

})