<commit_before>{assert} = require 'chai'
sinon = require 'sinon'
proxyquire = require('proxyquire').noCallThru()

fsStub = require 'fs'
protagonistStub = require 'protagonist'
loggerStub = require '../../src/logger'

blueprintAstToRuntime = require '../../src/blueprint-ast-to-runtime'
blueprintAstToRuntimeStub = sinon.spy blueprintAstToRuntime

Dredd = proxyquire '../../src/dredd', {
  'protagonist': protagonistStub
  './blueprint-ast-to-runtime': blueprintAstToRuntimeStub
  'fs': fsStub
  './logger': loggerStub
}

describe 'Dredd class', () ->

  configuration = {}
  dredd = {}

  beforeEach () ->
    sinon.spy protagonistStub, 'parse'
    sinon.spy fsStub, 'readFile'

  afterEach () ->
    protagonistStub.parse.restore()
    fsStub.readFile.restore()

  describe 'with legacy configuration', () ->
    before () ->
      configuration =
        server: 'http://localhost:3000/'
        blueprintPath: './test/fixtures/apiary.apib'

    it 'should not explode and run executeTransaction', (done) ->
      fn = () ->
        dredd = new Dredd(configuration)
        sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
          callback()
        dredd.run (error) ->
          assert.ok dredd.runner.executeTransaction.called
          dredd.runner.executeTransaction.restore()
          done()

      assert.doesNotThrow fn

  describe 'with valid configuration', () ->
    before () ->
      configuration =
        server: 'http://localhost:3000/'
        options:
          silent: true
          method: 'get'
          header: 'Accept:application/json'
          user: 'bob:test'
          sorted: true
          path: ['./test/fixtures/apiary.apib']

    it 'should copy configuration on creation', () ->
      dredd = new Dredd(configuration)
      assert.ok(dredd.configuration.options.silent)
      assert.notOk(dredd.configuration.options['dry-run'])

    it 'should load the file on given path', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.ok fsStub.readFile.calledWith configuration.options.path[0]
        dredd.runner.executeTransaction.restore()
        done()

    it 'should parse blueprint to ast', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.ok protagonistStub.parse.called
        dredd.runner.executeTransaction.restore()
        done()

    it 'should not pass any error to the callback function', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.isNull(error)
        dredd.runner.executeTransaction.restore()
        done()

    it 'should pass the reporter as second argument', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error, reporter) ->
        assert.isDefined reporter
        dredd.runner.executeTransaction.restore()
        done()

    it 'should convert ast to runtime', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.ok blueprintAstToRuntimeStub.called
        dredd.runner.executeTransaction.restore()
        done()

    describe 'when paths specified with glob paterns', () ->
      before () ->
        configuration =
          server: 'http://localhost:3000/'
          options:
            silent: true
            path: ['./test/fixtures/multifile/*.apib', './test/fixtures/multifile/*.apib' ,'./test/fixtures/multifile/*.balony']
        dredd = new Dredd(configuration)

      beforeEach () ->
        sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
          callback()

      afterEach () ->
        dredd.runner.executeTransaction.restore()

      it 'should expand all glob patterns and resolved paths should be unique', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.equal dredd.configuration.files.length, 3
          assert.include dredd.configuration.files, './test/fixtures/multifile/message.apib'
          done()

      it 'should remove globs from config', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.notInclude dredd.configuration.files, './test/fixtures/multifile/*.apib'
          assert.notInclude dredd.configuration.files, './test/fixtures/multifile/*.balony'
          done()

      it 'should load file contents on paths to config', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.isObject dredd.configuration.data
          assert.property dredd.configuration.data, './test/fixtures/multifile/greeting.apib'
          assert.isObject dredd.configuration.data['./test/fixtures/multifile/greeting.apib']
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'filename'
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'raw'
          done()

      it 'should parse loaded files', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.isObject dredd.configuration.data['./test/fixtures/multifile/greeting.apib']
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'parsed'
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'filename'
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'raw'
          done()


    describe 'when glob pattern does not match any files', () ->
      before () ->
        configuration =
          server: 'http://localhost:3000/'
          options:
            silent: true
            path: ['./test/fixtures/multifile/*.balony']
        dredd = new Dredd(configuration)

      beforeEach () ->
        sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
          callback()

      afterEach () ->
        dredd.runner.executeTransaction.restore()

      it 'should return error', (done) ->
        dredd.run (error) ->
          assert.ok error
          done()

  describe 'when Blueprint parsing error', () ->

    before () ->
      configuration =
        url: 'http://localhost:3000/'
        options:
          silent: true
          path: ['./test/fixtures/error-blueprint.apib']
      dredd = new Dredd(configuration)

    beforeEach () ->
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()

    afterEach () ->
      dredd.runner.executeTransaction.restore()

    it 'should exit with an error', (done) ->
      dredd.run (error) ->
        assert.ok error
        done()

    it 'should NOT execute any transaction', (done) ->
      dredd.run () ->
        assert.notOk dredd.runner.executeTransaction.called<commit_msg>Use config "options.directInput" as a {key (filename) : raw (API blueprint code)} source<commit_after>{assert} = require 'chai'
sinon = require 'sinon'
proxyquire = require('proxyquire').noCallThru()

fsStub = require 'fs'
protagonistStub = require 'protagonist'
loggerStub = require '../../src/logger'

blueprintAstToRuntime = require '../../src/blueprint-ast-to-runtime'
blueprintAstToRuntimeStub = sinon.spy blueprintAstToRuntime

Dredd = proxyquire '../../src/dredd', {
  'protagonist': protagonistStub
  './blueprint-ast-to-runtime': blueprintAstToRuntimeStub
  'fs': fsStub
  './logger': loggerStub
}

describe 'Dredd class', () ->

  configuration = {}
  dredd = {}

  beforeEach () ->
    sinon.spy protagonistStub, 'parse'
    sinon.spy fsStub, 'readFile'

  afterEach () ->
    protagonistStub.parse.restore()
    fsStub.readFile.restore()

  describe 'with legacy configuration', () ->
    before () ->
      configuration =
        server: 'http://localhost:3000/'
        blueprintPath: './test/fixtures/apiary.apib'

    it 'should not explode and run executeTransaction', (done) ->
      fn = () ->
        dredd = new Dredd(configuration)
        sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
          callback()
        dredd.run (error) ->
          assert.ok dredd.runner.executeTransaction.called
          dredd.runner.executeTransaction.restore()
          done()

      assert.doesNotThrow fn

  describe 'with valid configuration', () ->
    before () ->
      configuration =
        server: 'http://localhost:3000/'
        options:
          silent: true
          method: 'get'
          header: 'Accept:application/json'
          user: 'bob:test'
          sorted: true
          path: ['./test/fixtures/apiary.apib']

    it 'should copy configuration on creation', () ->
      dredd = new Dredd(configuration)
      assert.ok(dredd.configuration.options.silent)
      assert.notOk(dredd.configuration.options['dry-run'])

    it 'should load the file on given path', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.ok fsStub.readFile.calledWith configuration.options.path[0]
        dredd.runner.executeTransaction.restore()
        done()

    it 'should parse blueprint to ast', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.ok protagonistStub.parse.called
        dredd.runner.executeTransaction.restore()
        done()

    it 'should not pass any error to the callback function', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.isNull(error)
        dredd.runner.executeTransaction.restore()
        done()

    it 'should pass the reporter as second argument', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error, reporter) ->
        assert.isDefined reporter
        dredd.runner.executeTransaction.restore()
        done()

    it 'should convert ast to runtime', (done) ->
      dredd = new Dredd(configuration)
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()
      dredd.run (error) ->
        assert.ok blueprintAstToRuntimeStub.called
        dredd.runner.executeTransaction.restore()
        done()

    describe 'when paths specified with glob paterns', () ->
      before () ->
        configuration =
          server: 'http://localhost:3000/'
          options:
            silent: true
            path: ['./test/fixtures/multifile/*.apib', './test/fixtures/multifile/*.apib' ,'./test/fixtures/multifile/*.balony']
        dredd = new Dredd(configuration)

      beforeEach () ->
        sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
          callback()

      afterEach () ->
        dredd.runner.executeTransaction.restore()

      it 'should expand all glob patterns and resolved paths should be unique', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.equal dredd.configuration.files.length, 3
          assert.include dredd.configuration.files, './test/fixtures/multifile/message.apib'
          done()

      it 'should remove globs from config', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.notInclude dredd.configuration.files, './test/fixtures/multifile/*.apib'
          assert.notInclude dredd.configuration.files, './test/fixtures/multifile/*.balony'
          done()

      it 'should load file contents on paths to config', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.isObject dredd.configuration.data
          assert.property dredd.configuration.data, './test/fixtures/multifile/greeting.apib'
          assert.isObject dredd.configuration.data['./test/fixtures/multifile/greeting.apib']
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'filename'
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'raw'
          done()

      it 'should parse loaded files', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.isObject dredd.configuration.data['./test/fixtures/multifile/greeting.apib']
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'parsed'
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'filename'
          assert.property dredd.configuration.data['./test/fixtures/multifile/greeting.apib'], 'raw'
          done()


    describe 'when glob pattern does not match any files', () ->
      before () ->
        configuration =
          server: 'http://localhost:3000/'
          options:
            silent: true
            path: ['./test/fixtures/multifile/*.balony']
        dredd = new Dredd(configuration)

      beforeEach () ->
        sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
          callback()

      afterEach () ->
        dredd.runner.executeTransaction.restore()

      it 'should return error', (done) ->
        dredd.run (error) ->
          assert.ok error
          done()

    describe 'when configuration contains directInput object with "filename" as key, and an API Blueprint string as value', () ->
      beforeEach ->
        configuration =
          server: 'http://localhost:3000/'
          directInput: {
            'direct': """
              # API name

              GET /url
              + Response 200 (application/json)

                      {"a":"b"}'
              """
          }
          options:
            silent: true
        dredd = new Dredd(configuration)
        sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
          callback()

      afterEach () ->
        dredd.runner.executeTransaction.restore()

      it 'should not expand any glob patterns', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.lengthOf dredd.configuration.files, 0
          done()

      it 'should pass data contents to config', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.isObject dredd.configuration.data
          assert.deepPropertyVal dredd, 'configuration.data.direct.filename', 'direct'
          assert.deepProperty    dredd, 'configuration.data.direct.raw'
          done()

      it 'should parse passed data contents', (done) ->
        dredd.run (error) ->
          return done error if error
          assert.deepProperty dredd, 'configuration.data.direct.parsed'
          done()

      describe 'and I also set configuration.options.path to an existing file', ->
        localdredd = null
        beforeEach ->
          configuration.options ?= {}
          configuration.options.path = ['./test/fixtures/apiary.apib']
          localdredd = new Dredd(configuration)
          sinon.stub localdredd.runner, 'executeTransaction', (transaction, callback) ->
            callback()

        afterEach ->
          localdredd.runner.executeTransaction.restore()

        it 'should fill configuration data with directInput and one file at that path', (done) ->
          localdredd.run (error) ->
            return done error if error
            assert.lengthOf    localdredd.configuration.files, 1
            assert.isObject    localdredd.configuration.data
            assert.lengthOf    Object.keys(localdredd.configuration.data), 2
            assert.property    localdredd.configuration.data, './test/fixtures/apiary.apib'
            assert.propertyVal localdredd.configuration.data['./test/fixtures/apiary.apib'], 'filename', './test/fixtures/apiary.apib'
            assert.property    localdredd.configuration.data['./test/fixtures/apiary.apib'], 'raw'
            assert.property    localdredd.configuration.data['./test/fixtures/apiary.apib'], 'parsed'
            assert.deepPropertyVal localdredd, 'configuration.data.direct.filename', 'direct'
            assert.deepProperty    localdredd, 'configuration.data.direct.raw'
            assert.deepProperty    localdredd, 'configuration.data.direct.parsed'
            done()



  describe 'when Blueprint parsing error', () ->

    before () ->
      configuration =
        url: 'http://localhost:3000/'
        options:
          silent: true
          path: ['./test/fixtures/error-blueprint.apib']
      dredd = new Dredd(configuration)

    beforeEach () ->
      sinon.stub dredd.runner, 'executeTransaction', (transaction, callback) ->
        callback()

    afterEach () ->
      dredd.runner.executeTransaction.restore()

    it 'should exit with an error', (done) ->
      dredd.run (error) ->
        assert.ok error
        done()

    it 'should NOT execute any transaction', (done) ->
      dredd.run () ->
        assert.notOk dredd.runner.executeTransaction.called
{'fraction_non_alphanumeric': 0.12394862468742895, 'fraction_numerical': 0.003068879290747897, 'mean_word_length': 2.373011309181522, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2732926', 'n_tokens_mistral': 5073, 'n_tokens_neox': 5035, 'n_words': 1113}