<commit_before>var
  rewire = require('rewire'),
  should = require('should'),
  sinon = require('sinon'),
  InternalEngine = rewire('../../../lib/services/internalEngine'),
  KuzzleMock = require('../../mocks/kuzzle.mock'),
  NotFoundError = require.main.require('kuzzle-common-objects').Errors.notFoundError;

describe('InternalEngine', () => {
  var
    kuzzle,
    reset;

  beforeEach(() => {
    reset = InternalEngine.__set__({
      Elasticsearch: {
        Client: function () {
          this.indices = {
            create: sinon.stub().resolves(),
            delete: sinon.stub().resolves(),
            exists: sinon.stub().resolves(),
            getMapping: sinon.stub().resolves(),
            putMapping: sinon.stub().resolves(),
            refresh: sinon.stub().resolves()
          };

          this.create = sinon.stub().resolves();
          this.delete = sinon.stub().resolves();
          this.exists = sinon.stub().resolves();
          this.get = sinon.stub().resolves();
          this.index = sinon.stub().resolves();
          this.mget = sinon.stub().resolves();
          this.search = sinon.stub().resolves();
          this.update = sinon.stub().resolves();
        }
      }
    });
    kuzzle = new KuzzleMock();
    kuzzle.internalEngine = new InternalEngine(kuzzle);

    return kuzzle.internalEngine.init();
  });

  afterEach(() => {
    reset();
  });

  describe('#init', () => {
    it('should act as a singleton', () => {
      return should(kuzzle.internalEngine.init()).be.fulfilledWith(kuzzle.internalEngine);
    });
  });

  describe('#search', () => {
    it('should harmonize search results', () => {
      var
        collection = 'collection',
        query = { 'some': 'filters' };

      kuzzle.internalEngine.client.search.resolves({hits: { hits: ['foo', 'bar'], total: 123}});

      return kuzzle.internalEngine.search(collection, query)
        .then(result => {
          should(kuzzle.internalEngine.client.search)
            .be.calledOnce()
            .be.calledWithMatch({
              index: kuzzle.internalEngine.index,
              type: collection,
              body: {
                query: query,
                from: 0,
                size: 20
              }
            });

          should(result).be.an.Object().and.not.be.empty();
          should(result.total).be.eql(123);
          should(result.hits).be.an.Array().and.match(['foo', 'bar']);
        });
    });

    it('should perform a search on an empty filter if the filters argument is missing', () => {
      var
        collection = 'collection';

      kuzzle.internalEngine.client.search.resolves({hits: {hits: ['foo', 'bar']}, total: 123});

      return kuzzle.internalEngine.search(collection)
        .then(result => {
          should(kuzzle.internalEngine.client.search)
            .be.calledOnce()
            .be.calledWithMatch({
              index: kuzzle.internalEngine.index,
              type: collection,
              body: {
                from: 0,
                size: 20
              }
            });

          should(result).be.an.Object().and.not.be.empty();
          should(result.total).be.eql(123);
          should(result.hits).be.an.Array().and.match(['foo', 'bar']);
        });<commit_msg>Add test case for coverage ...<commit_after>var
  rewire = require('rewire'),
  should = require('should'),
  sinon = require('sinon'),
  InternalEngine = rewire('../../../lib/services/internalEngine'),
  KuzzleMock = require('../../mocks/kuzzle.mock'),
  NotFoundError = require.main.require('kuzzle-common-objects').Errors.notFoundError;

describe('InternalEngine', () => {
  var
    kuzzle,
    reset;

  beforeEach(() => {
    reset = InternalEngine.__set__({
      Elasticsearch: {
        Client: function () {
          this.indices = {
            create: sinon.stub().resolves(),
            delete: sinon.stub().resolves(),
            exists: sinon.stub().resolves(),
            getMapping: sinon.stub().resolves(),
            putMapping: sinon.stub().resolves(),
            refresh: sinon.stub().resolves()
          };

          this.create = sinon.stub().resolves();
          this.delete = sinon.stub().resolves();
          this.exists = sinon.stub().resolves();
          this.get = sinon.stub().resolves();
          this.index = sinon.stub().resolves();
          this.mget = sinon.stub().resolves();
          this.search = sinon.stub().resolves();
          this.update = sinon.stub().resolves();
        }
      }
    });
    kuzzle = new KuzzleMock();
    kuzzle.internalEngine = new InternalEngine(kuzzle);

    return kuzzle.internalEngine.init();
  });

  afterEach(() => {
    reset();
  });

  describe('#init', () => {
    it('should act as a singleton', () => {
      return should(kuzzle.internalEngine.init()).be.fulfilledWith(kuzzle.internalEngine);
    });
  });

  describe('#search', () => {
    it('should harmonize search results', () => {
      var
        collection = 'collection',
        query = { 'some': 'filters' };

      kuzzle.internalEngine.client.search.resolves({hits: { hits: ['foo', 'bar'], total: 123}});

      return kuzzle.internalEngine.search(collection, query)
        .then(result => {
          should(kuzzle.internalEngine.client.search)
            .be.calledOnce()
            .be.calledWithMatch({
              index: kuzzle.internalEngine.index,
              type: collection,
              body: {
                query: query,
                from: 0,
                size: 20
              }
            });

          should(result).be.an.Object().and.not.be.empty();
          should(result.total).be.eql(123);
          should(result.hits).be.an.Array().and.match(['foo', 'bar']);
        });
    });

    it('should harmonize search results', () => {
      var
        collection = 'collection',
        query = { query: {'some': 'filters'} };

      kuzzle.internalEngine.client.search.resolves({hits: { hits: ['foo', 'bar'], total: 123}});

      return kuzzle.internalEngine.search(collection, query)
        .then(result => {
          should(kuzzle.internalEngine.client.search)
            .be.calledOnce()
            .be.calledWithMatch({
              index: kuzzle.internalEngine.index,
              type: collection,
              body: {
                query: query,
                from: 0,
                size: 20
              }
            });

          should(result).be.an.Object().and.not.be.empty();
          should(result.total).be.eql(123);
          should(result.hits).be.an.Array().and.match(['foo', 'bar']);
        });
    });

    it('should perform a search on an empty filter if the filters argument is missing', () => {
      var
        collection = 'collection';

      kuzzle.internalEngine.client.search.resolves({hits: {hits: ['foo', 'bar']}, total: 123});

      return kuzzle.internalEngine.search(collection)
        .then(result => {
          should(kuzzle.internalEngine.client.search)
            .be.calledOnce()
            .be.calledWithMatch({
              index: kuzzle.internalEngine.index,
              type: collection,
              body: {
                from: 0,
                size: 20
              }
            });

          should(result).be.an.Object().and.not.be.empty();
          should(result.total).be.eql(123);
          should(result.hits).be.an.Array().and.match(['foo', 'bar']);
        });
{'fraction_non_alphanumeric': 0.1654094827586207, 'fraction_numerical': 0.006061422413793103, 'mean_word_length': 1.9452598175327251, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '3737234', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2109, 'n_words': 346}