'use strict'
var test = require('tap').test
var pino = require('../')
var sink = require('./helper').sink

var os = require('os')
var pid = process.pid
var hostname = os.hostname()
var level = 50
var name = 'error'

test('err is serialized with additional properties set on the Error object', function (t) {
  t.plan(2)
  var err = Object.assign(new Error('myerror'), {foo: 'bar'})
  var instance = pino(sink(function (chunk, enc, cb) {
    t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
    delete chunk.time
    t.deepEqual(chunk, {
      pid: pid,
      hostname: hostname,
      level: level,
      type: 'Error',
      msg: err.message,
      stack: err.stack,
      foo: err.foo,
      v: 1
    })
    cb()
  }))

  instance.level = name
  instance[name](err)
})

test('type should be retained, even if type is a property', function (t) {
  t.plan(2)
  var err = Object.assign(new Error('myerror'), {type: 'bar'})
  var instance = pino(sink(function (chunk, enc, cb) {
    t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
    delete chunk.time
    t.deepEqual(chunk, {
      pid: pid,
      hostname: hostname,
      level: level,
      type: 'bar',
      msg: err.message,
      stack: err.stack,
      v: 1
    })
    cb()
  }))

  instance.level = name
  instance[name](err)
})

test('type, message and stack should be first level properties', function (t) {
  t.plan(2)
  var err = Object.assign(new Error('foo'), { foo: 'bar' })
  var instance = pino(sink(function (chunk, enc, cb) {
    t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
    delete chunk.time
    t.deepEqual(chunk, {
      pid: pid,
      hostname: hostname,
      level: level,
      type: 'Error',
      msg: err.message,
      stack: err.stack,
      foo: err.foo,
      v: 1
    })
    cb()
  }))

  instance.level = name
  instance[name](err)
})

test('err serializer', function (t) {
  t.plan(2)
  var err = Object.assign(new Error('myerror'), {foo: 'bar'})
  var instance = pino({
    serializers: {
      err: pino.stdSerializers.err
    }
  }, sink(function (chunk, enc, cb) {
    t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
    delete chunk.time
    t.deepEqual(chunk, {
      pid: pid,
      hostname: hostname,
      level: level,
      err: {
        type: 'Error',
        message: err.message,
        stack: err.stack,
        foo: err.foo
      },
      v: 1
    })
    cb()
  }))

  instance.level = name
  instance[name]({ err })
})

@@ -2534,12 +2534,623 @@
 %7B err %7D)%0A%7D)%0A
+%0Atest('an error with statusCode property is not confused for a http response', function (t) %7B%0A  t.plan(2)%0A  var err = Object.assign(new Error('StatusCodeErr'), %7B statusCode: 500 %7D)%0A  var instance = pino(sink(function (chunk, enc, cb) %7B%0A    t.ok(new Date(chunk.time) %3C= new Date(), 'time is greater than Date.now()')%0A    delete chunk.time%0A    t.deepEqual(chunk, %7B%0A      pid: pid,%0A      hostname: hostname,%0A      level: level,%0A      type: 'Error',%0A      msg: err.message,%0A      stack: err.stack,%0A      statusCode: err.statusCode,%0A      v: 1%0A    %7D)%0A    cb()%0A  %7D))%0A%0A  instance.level = name%0A  instance%5Bname%5D(err)%0A%7D)%0A

Add unit test: an error with `statusCode` is not a http response