const t = require('../')
const Stdin = require('../lib/stdin.js')
const MP = require('minipass')

process.env.TAP_BAIL = ''
process.env.TAP_BUFFER = ''

t.test('uses stdin if no stream provided', t => {
  const s = new Stdin()
  t.equal(s.stream, process.stdin)
  t.equal(s.name, '/dev/stdin')
  t.end()
})

t.test('basic test', t => {
  const stream = new MP()
  const s = new Stdin({
    tapStream: stream,
    name: 'foo',
    buffered: true
  })
  t.equal(s.stream, stream)
  t.equal(s.name, 'foo')

  s.main(_ => {
    t.match(s.results, {
      ok: true,
      count: 1,
      pass: 1,
      fail: 0,
      bailout: false,
      plan: {
        start: 1,
        end: 1,
        skipAll: false
      },
      failures: []
    })
    t.end()
  })

  s.stream.end(`TAP version 13
1..1
ok 1 - this is fine
`)
})

t.test('failure test', t => {
  const stream = new MP()
  const s = new Stdin({
    tapStream: stream
  })

  s.main(_ => {
    t.match(s.results, {
      ok: false,
      count: 2,
      pass: 1,
      fail: 1,
      bailout: false,
      todo: 0,
      skip: 0,
      plan: {
        start: 1,
        end: 2,
        skipAll: false
      },
      failures: [{
        ok: false,
        id: 2,
        name: 'oops'
      }]
    })
    t.end()
  })

  s.stream.write(`TAP version 13
ok 1 - this is fine
`)

  s.threw(new Error('oops'))
})

@@ -1346,12 +1346,279 @@
 'oops'))%0A%7D)%0A
+%0At.test('doting parent', t =%3E %7B%0A  const EE = require('events').EventEmitter%0A  const parent = new EE()%0A  const tapStream = new MP()%0A  parent.on('stdin', child =%3E %7B%0A    t.equal(child, s)%0A    t.end()%0A  %7D)%0A  const s = new Stdin(%7BtapStream, parent%7D)%0A  s.main(() =%3E %7B%7D)%0A%7D)%0A

test parent emitting event in stdin.js