 ABAO_BIN = './bin/abao'
 MOCHA_BIN = './node_modules/mocha/bin/mocha'
 
+mochaJsonReportKeys = [
+  'stats',
+  'tests',
+  'pending',
+  'failures',
+  'passes'
+]
+
 stderr = ''
 stdout = ''
 report = ''
 exitStatus = null
 
 receivedRequest = {}
+
+#
+# To dump individual raw test results:
+#
+# describe('show me the results', () ->
+#   runTestAsync = (done) ->
+#     cmd = "#{ABAO_BIN}"
+#     execCommand cmd, done
+#   before (done) ->
+#     debugExecCommand = true
+#     runTestAsync done
+#   after () ->
+#     debugExecCommand = false
+#
+debugExecCommand = false
+
 
 execCommand = (cmd, callback) ->
   stderr = ''


          server.on 'close', done

        it 'should have an additional header in the request', () ->
          assert.equal receivedRequest.headers.accept, 'application/json'

        it 'exit status should be 0', () ->
          assert.equal exitStatus, 0

        it 'should print count of tests run', () ->
          assert.include stdout, '1 passing'


      describe 'when invoked with "--hookfiles" option', () ->

        receivedRequest = {}

        before (done) ->
          ramlFile = "#{RAML_DIR}/single-get.raml"
          cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --hookfiles=#{HOOK_DIR}/*_hooks.*"

          app = express()

          app.get '/machines', (req, res) ->
            receivedRequest = req
            res.setHeader 'Content-Type', 'application/json'
            machine =
              type: 'bulldozer'
              name: 'willy'
            response = [machine]
            res.status(200).send response

          server = app.listen PORT, () ->
            execCommand cmd, () ->


          server.on 'close', done

        before (done) ->
          runHooksOnlyTestAsync done

        it 'should not run test without hooks', () ->
          expect(stdout).to.have.string('1 pending')

        it 'should exit normally', () ->
          expect(exitStatus).to.equal(0)


      describe 'when invoked with "--timeout" option', () ->

        timeout = undefined
        elapsed = -1
        finished = undefined

        runTimeoutTestAsync = (done) ->
          ramlFile = "#{RAML_DIR}/single-get.raml"
          cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --timeout #{timeout}"

          beginTime = undefined
          finished = false

          app = express()

          app.use (req, res, next) ->
            beginTime = new Date()
            res.on 'finish', () ->
              finished = true
            next()

          app.use (req, res, next) ->
            delay = timeout * 2
            setTimeout next, delay

          app.get '/machines', (req, res) ->
            machine =
              type: 'bulldozer'
              name: 'willy'
            res.status(200).json([machine])

          server = app.listen PORT, () ->
            execCommand cmd, () ->
              endTime = new Date()
              if finished
                elapsed = endTime - beginTime
                console.log "elapsed = #{elapsed} msecs (req/res)"
              server.close()

          server.on 'close', done


        context 'given insufficient time to complete', () ->

          before (done) ->
            timeout = 20
            console.log "timeout = #{timeout} msecs"
            runTimeoutTestAsync done

          after () ->
            finished = undefined

          it 'should not finish before timeout occurs', () ->
            expect(finished).to.be.false

          # Errors thrown by Mocha show up in stdout; those by Abao in stderr.
          it 'Mocha should throw an error', () ->
            detail = "Error: Timeout of #{timeout}ms exceeded."
            expect(stdout).to.have.string(detail)

          it 'should run test but not complete', () ->
            expect(stdout).to.have.string('1 failing')

          it 'should exit due to error', () ->
            expect(exitStatus).to.equal(1)


      describe 'when invoked with "--schema" option', () ->

        runSchemaTestAsync = (done) ->
          pattern = "#{SCHEMA_DIR}/*.json"
          ramlFile = "#{RAML_DIR}/with-json-refs.raml"
          cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER} --schemas=#{pattern}"

          app = express()

          app.get '/machines', (req, res) ->
            machine =
              type: 'bulldozer'
              name: 'willy'
            res.status(200).json([machine])

          server = app.listen PORT, () ->
            execCommand cmd, () ->
