 
 describe "Command line interface", () ->
 
-  describe "When blueprint file not found", ->
-    before (done) ->
-      cmd = "./bin/dredd ./test/fixtures/nonexistent_path.md http://localhost:#{PORT}"
-
-      execCommand cmd, done
-
-    it 'should exit with status 1', () ->
-      assert.equal exitStatus, 1
-
-    it 'should print error message to stderr', () ->
-      assert.include stderr, 'not found'
-
-
-  describe "When blueprint file should be loaded from 'http(s)://...' url", ->
-    server = null
-    loadedFromServer = null
-    connectedToServer = null
-    notFound = null
-    fileFound = null
-
-    errorCmd = "./bin/dredd http://localhost:#{PORT+1}/connection-error.apib http://localhost:#{PORT+1}"
-    wrongCmd = "./bin/dredd http://localhost:#{PORT}/not-found.apib http://localhost:#{PORT}"
-    goodCmd = "./bin/dredd http://localhost:#{PORT}/file.apib http://localhost:#{PORT}"
-
-    afterEach ->
-      connectedToServer = null
-
-    before (done) ->
-      app = express()
-
-      app.use (req, res, next) ->
-        connectedToServer = true
-        next()
-
-      app.get '/', (req, res) ->
-        res.sendStatus 404
-
-      app.get '/file.apib', (req, res) ->
-        fileFound = true
-        res.type('text')
-        stream = fs.createReadStream './test/fixtures/single-get.apib'
-        stream.pipe res
-
-      app.get '/machines', (req, res) ->
-        res.setHeader 'Content-Type', 'application/json'
-        machine =
-          type: 'bulldozer'
-          name: 'willy'
-        response = [machine]
-        res.status(200).send response
-
-      app.get '/not-found.apib', (req, res) ->
-        notFound = true
-        res.status(404).end()
-
-      server = app.listen PORT, ->
-        done()
-
-    after (done) ->
-      server.close ->
-        app = null
-        server = null
-        done()
-
-    describe 'and I try to load a file from bad hostname at all', ->
-      before (done) ->
-        execCommand errorCmd, ->
-          done()
-
-      after ->
-        connectedToServer = null
-
-      it 'should not send a GET to the server', ->
-        assert.isNull connectedToServer
-
-      it 'should exit with status 1', ->
-        assert.equal exitStatus, 1
-
-      it 'should print error message to stderr', ->
-        assert.include stderr, 'Error when loading file from URL'
-        assert.include stderr, 'Is the provided URL correct?'
-        assert.include stderr, 'connection-error.apib'
-
-    describe 'and I try to load a file that does not exist from an existing server', ->
-      before (done) ->
-        execCommand wrongCmd, ->
-          done()
-
-      after ->
-        connectedToServer = null
-
-      it 'should connect to the right server', ->
-        assert.isTrue connectedToServer
-
-      it 'should send a GET to server at wrong URL', ->
-        assert.isTrue notFound
-
-      it 'should exit with status 1', ->
-        assert.equal exitStatus, 1
-
-      it 'should print error message to stderr', ->
-        assert.include stderr, 'Unable to load file from URL'
-        assert.include stderr, 'responded with status code 404'
-        assert.include stderr, 'not-found.apib'
-
-    describe 'and I try to load a file that actually is there', ->
-      before (done) ->
-        execCommand goodCmd, ->
-          done()
-
-      it 'should send a GET to the right server', ->
-        assert.isTrue connectedToServer
-
-      it 'should send a GET to server at good URL', ->
-        assert.isTrue fileFound
-
-      it 'should exit with status 0', ->
-        assert.equal exitStatus, 0
-
-
   describe "Arguments with existing blueprint and responding server", () ->
     describe "when executing the command and the server is responding as specified in the blueprint", () ->
 


        it 'should execute some transaction', () ->
          assert.isTrue apiHit

    describe "when using reporter -r apiary", () ->
      server = null
      server2 = null
      receivedRequest = null

      before (done) ->
        cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT} --reporter apiary"

        apiary = express()
        app = express()

        apiary.use bodyParser.json(size:'5mb')

        apiary.post '/apis/*', (req, res) ->
          if req.body and req.url.indexOf('/tests/steps') > -1
            receivedRequest ?= clone(req.body)
          res.type('json')
          res.status(201).send
            _id: '1234_id'
            testRunId: '6789_testRunId'
            reportUrl: 'http://url.me/test/run/1234_id'

        apiary.all '*', (req, res) ->
          res.type 'json'
          res.send {}

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

        server = app.listen PORT, () ->
          server2 = apiary.listen (PORT+1), ->
            env = clone process.env
            env['APIARY_API_URL'] = "http://127.0.0.1:#{PORT+1}"
            execCommand cmd, {env}, (error, stdout, stderr, exitStatus) ->

              server2.close ->
                server.close ->

        server.on 'close', done

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

      it 'should print using the new reporter', ()->
        assert.include stdout, 'http://url.me/test/run/1234_id'

      it 'should print warning about missing APIARY_API_KEY', ()->
        assert.include stdout, 'Apiary reporter environment variable APIARY_API_KEY'

      it 'should send results from gavel', ()->
        assert.isObject receivedRequest
        assert.deepProperty receivedRequest, 'resultData.request'
        assert.deepProperty receivedRequest, 'resultData.realResponse'
        assert.deepProperty receivedRequest, 'resultData.expectedResponse'
        assert.deepProperty receivedRequest, 'resultData.result.body.validator'
        assert.deepProperty receivedRequest, 'resultData.result.headers.validator'
        assert.deepProperty receivedRequest, 'resultData.result.statusCode.validator'


    describe "when using reporter -r apiary with hooks.log used in hookfile", () ->
      server = null
      server2 = null
      receivedRequest = null
      receivedStep = null

      before (done) ->
        cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT} --reporter apiary --hookfiles=./test/fixtures/hooks_log.coffee"
        stderr = stdout = ''

        apiary = express()
        app = express()

        apiary.use bodyParser.json(size:'5mb')

        apiary.patch '/apis/*', (req, res) ->
          if req.body and req.url.indexOf('/tests/run') > -1
            receivedRequest ?= clone(req.body)
          res.type('json').status(200).send
            _id: 'xyz_ABC_id'
            testRunId: '7890_testRunId'
            reportUrl: 'http://url.me/test/run/7890_id'

        apiary.post '/apis/*', (req, res) ->
          if req.body and req.url.indexOf('/tests/steps') > -1
            receivedStep ?= clone(req.body)
          res.type('json').status(201).send
            _id: 'xyz_ABC_id'
            testRunId: '7890_testRunId'
            reportUrl: 'http://url.me/test/run/7890_id'

        apiary.all '*', (req, res) ->
          res.type 'json'
          res.send {}

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

        server = app.listen PORT, () ->
          server2 = apiary.listen (PORT+1), ->
            env = clone process.env
            env['APIARY_API_URL'] = "http://127.0.0.1:#{PORT+1}"
            execCommand cmd, {env}, () ->
              server2.close ->
                server.close ->

        server.once 'close', done

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

      it 'should print log to console too (thanks to logger)', ()->
        # because --level=info is lower than --level=hook
        assert.include (stdout + stderr), 'using hooks.log to debug'

      it 'should use toString when using log in hooks too', ->
        assert.include (stdout + stderr), 'Error object!'

      it 'should send result stats in PATCH request to Apiary with logs', ()->
        assert.isObject receivedRequest
        assert.deepPropertyVal receivedRequest, 'status', 'passed'
        assert.deepProperty receivedRequest, 'endedAt'
        assert.deepProperty receivedRequest, 'logs'
        assert.isArray receivedRequest.logs
        assert.lengthOf receivedRequest.logs, 3
        assert.property receivedRequest.logs[0], 'timestamp'
        assert.include receivedRequest.logs[0].content, 'Error object!'
        assert.property receivedRequest.logs[1], 'timestamp'
        assert.deepPropertyVal receivedRequest.logs[1], 'content', 'true'
        assert.property receivedRequest.logs[2], 'timestamp'
        assert.deepPropertyVal receivedRequest.logs[2], 'content', 'using hooks.log to debug'
        assert.deepProperty receivedRequest, 'result.tests'
        assert.deepProperty receivedRequest, 'result.failures'
        assert.deepProperty receivedRequest, 'result.errors'
        assert.deepProperty receivedRequest, 'result.passes'
        assert.deepProperty receivedRequest, 'result.start'
        assert.deepProperty receivedRequest, 'result.end'

      it 'should send testStep with startedAt larger than before hook log timestamp', () ->
        assert.isObject receivedStep
        assert.isNumber receivedStep.startedAt
        assert.operator receivedStep.startedAt, '>=', receivedRequest.logs[0].timestamp
        assert.operator receivedStep.startedAt, '>=', receivedRequest.logs[1].timestamp

      it 'should send testStep with startedAt smaller than after hook log timestamp', () ->
        assert.isObject receivedStep
        assert.isNumber receivedStep.startedAt
        assert.operator receivedStep.startedAt, '<=', receivedRequest.logs[2].timestamp


    describe "when being in sandbox and while using reporter -r apiary with hooks.log used in hookfile", () ->
      server = null
      server2 = null
      receivedRequest = null
      receivedStep = null

      before (done) ->
        cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT} --reporter apiary --level=info --sandbox --hookfiles=./test/fixtures/sandboxed_hooks_log.js"
        stderr = stdout = ''

        apiary = express()
        app = express()

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

        apiary.use bodyParser.json(size:'5mb')

        apiary.patch '/apis/*', (req, res) ->
          if req.body and req.url.indexOf('/tests/run') > -1
            receivedRequest ?= clone(req.body)
          res.type('json').status(200).send
            _id: 'ABC_123_id'
            testRunId: '5555_my_testRunId'
            reportUrl: 'http://url.different.tld/test/run/abcd_id'

        apiary.post '/apis/*', (req, res) ->
          if req.body and req.url.indexOf('/tests/steps') > -1
            receivedStep ?= clone(req.body)
          res.type('json').status(201).send
            _id: 'ABC_123_id'
            testRunId: '5555_my_testRunId'
            reportUrl: 'http://url.different.tld/test/run/abcd_id'

        apiary.all '*', (req, res) ->
          res.type 'json'
          res.send {}

        server = app.listen PORT, () ->
          server2 = apiary.listen (PORT+1), ->
            env = clone process.env
            env['APIARY_API_URL'] = "http://127.0.0.1:#{PORT+1}"
            execCommand cmd, {env}, () ->
              server2.close ->
                server.close ->

        server.once 'close', done

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

      it 'should not contain the logs from hooks in console output', ->
        # because we are running in sandboxed mode with higher --level
        assert.notInclude (stdout + stderr), 'using sandboxed hooks.log'
        assert.notInclude (stdout + stderr), 'shall not print'

      it 'should send result stats in PATCH request to Apiary with logs', ()->
        assert.isObject receivedRequest
        assert.deepPropertyVal receivedRequest, 'status', 'passed'
        assert.deepProperty receivedRequest, 'endedAt'
        assert.deepProperty receivedRequest, 'logs'
        assert.isArray receivedRequest.logs
        assert.lengthOf receivedRequest.logs, 2
        assert.property receivedRequest.logs[0], 'timestamp'
        assert.deepPropertyVal receivedRequest.logs[0], 'content', 'shall not print, but be present in logs'
        assert.property receivedRequest.logs[1], 'timestamp'
        assert.deepPropertyVal receivedRequest.logs[1], 'content', 'using sandboxed hooks.log'

      it 'should send testStep with startedAt larger than before hook log timestamp', () ->
        assert.isObject receivedStep
        assert.isNumber receivedStep.startedAt
        assert.operator receivedStep.startedAt, '>=', receivedRequest.logs[0].timestamp

      it 'should send testStep with startedAt smaller than after hook log timestamp', () ->
        assert.isObject receivedStep
        assert.isNumber receivedStep.startedAt
        assert.operator receivedStep.startedAt, '<=', receivedRequest.logs[1].timestamp


    describe "when using additional reporters with -r", () ->
      before (done) ->
        cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT} -r nyan"

        app = express()

        app.get '/machines', (req, res) ->
          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.close()

        server.on 'close', done

      it 'should print using the new reporter', ()->
        # nyan cat ears should exist in stdout
        assert.ok stdout.indexOf('/\\_/\\') > -1


    describe 'when using an output path with -o', () ->
      before (done) ->
        cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT} -r junit -o test_file_output.xml"

        app = express()

        app.get '/machines', (req, res) ->
          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.close()

        server.on 'close', done

      after () ->
        fs.unlinkSync process.cwd() + "/test_file_output.xml"

      it 'should write to the specified file', () ->
        assert.ok fs.existsSync process.cwd() + "/test_file_output.xml"


    describe "when adding additional headers with -h", () ->


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

  describe "when called with arguments", () ->

