
var test    = require('tap').test
  , ldj     = require('./')
  , through = require('through2')
  , net     = require('net')
  , counter = 9999

function nextPort() {
  return counter++
}

function ps() {
  return through(function(chunk, enc, cb) {
    this.push(chunk)
    cb()
  })
}

test('ldj sends objects through the line delimited json stream', function(t) {
  t.plan(1)

  var binary    = ps()
    , instance  = ldj(binary)
    , msg       = { hello: 'world' }

  instance.on('data', function(chunk) {
    t.deepEqual(chunk, msg)
  })

  instance.end(msg)
})

test('ldj works with net streams', function(t) {
  t.plan(2)

  function handler(client) {
    var wrappedClient = ldj(client)
    wrappedClient.on('data', function(data) {
      t.deepEqual(data, { request: 'message' })
      wrappedClient.end({ response: '42' })
    })
  }

  var server  = net.createServer(handler)
    , port    = nextPort()

  server.listen(port)
  server.on('listening', function() {
    var client = ldj(net.connect(port))

    client.write({ request: 'message' })

    client.on('data', function(data) {
      t.deepEqual(data, { response: '42' })
      server.close()
    })
  })
})

@@ -1167,12 +1167,264 @@
  %7D)%0A  %7D)%0A%7D)%0A
+%0Atest('ldj accepts plain strings / invalid json', function(t) %7B%0A  t.plan(1)%0A%0A  var binary    = ps()%0A    , instance  = ldj(binary)%0A    , msg       = 'test'%0A%0A  instance.on('data', function(chunk) %7B%0A    t.deepEqual(chunk, msg)%0A  %7D)%0A%0A  instance.end(msg)%0A%7D)

test for pr #1