<commit_before>require('../lib');

var mockery = require('./mock_services.js');

// ---------------------------------------------------------------------------------------------------
describe('service.js', function() {
  this.timeout(20000);

  var item, returnField, returnValue = 'foo-bar', mockServer;

  var realGetMaxAttempts;
  var realSetMaxAttempts;
  var realGetTimeout;
  var realSetTimeout;


  // ---------------------------------------------------------------------------------------------------
  before(function(done) {
    var type = '';

    // Wait for the config file and initial moduleshave finished loading before starting up the server
    var delayStartup = setInterval(function() {
      if (typeof Item !== 'undefined') {
        clearInterval(delayStartup);

        // save these so we can put them back after mocking them
        realGetMaxAttempts = Service.prototype.getMaxAttempts;
        realSetMaxAttempts = Service.prototype.setMaxAttempts;
        realGetTimeout = Service.prototype.getTimeout;
        realGetTranslator = Service.prototype.getTranslator;
        realSetTranslator = Service.prototype.setTranslator;
        realGetTarget = Service.prototype.getTarget;
        realSetTarget = Service.prototype.setTarget;
        realSetReferrerBlock = Service.prototype.setReferrerBlock;
        realSetItemTypes = Service.prototype.setItemTypes;
        realRunTest = Service.prototype.runTest;

        // ---------------------------------------------------------------------------------------------------
        // Mock some methods onto the Service object so we can manipulate and view all attributes
        // ---------------------------------------------------------------------------------------------------
        Service.prototype.getMaxAttempts = function() {
          return this._maxAttempts;
        };
        Service.prototype.setMaxAttempts = function(value) {
          this._maxAttempts = value;
        };
        Service.prototype.getTimeout = function() {
          return this._timeout;
        };
        Service.prototype.setTimeout = function(value) {
          this._timeout = value;
        };
        Service.prototype.getTranslator = function() {
          return this._translator;
        };
        Service.prototype.setTranslator = function(value) {
          this._translator = value;
        };
        Service.prototype.getTarget = function() {
          return this._target;
        };
        Service.prototype.setTarget = function(value) {
          this._target = value;
        };
        Service.prototype.setReferrerBlock = function(values) {
          this._referrerBlock = values;
        };
        Service.prototype.setItemTypes = function(values) {
          this._itemTypesReturned = values;
        };

        Service.prototype.runTest = function(target, item, callback) {
          this.setTarget(target === '' ? '' : ("http://localhost:9000/" + target));

          this.on('success', function(items) {
            //console.log('... success');

            callback({'success': true,
              'count': _.size(items),
              'isArray': items instanceof Array,
              'isItem': _.first(items) instanceof Item,
              'attributeCount': (typeof _.first(items) !== 'undefined') ? _.size(_.first(items).getAttributes()) : 0});
          });

          this.on('error', function(error) {
            //console.log('... failure: ' + error);

            callback({'success': false,
              'isArray': false,
              'isItem': true,
              'attributeCount': 0,
              'level': (error instanceof Item) ? error.getAttribute('level') : 'unknown',
              'message': (error instanceof Item) ? error.getAttribute('message') : 'Got an error!'});
          });

          this.call(item, {});
        };

        _.forEach(CONFIGS.data.objects, function(config, name) {
          if (typeof config.root !== 'undefined') {
            type = name;
            returnField = config.attributes[0];
          }
        });

        item = new Item(type, true, {});

        // Spin up some stub http servers for testing
        mockServer = mockery.spinUpServer(returnField, returnValue);

        done();
      }
    });
  });

  // ---------------------------------------------------------------------------------------------------
  after(function(done) {
    mockServer.close();

    // Remove monkey patches and return Service to its original state
    Service.prototype.getMaxAttempts = realGetMaxAttempts;
    Service.prototype.setMaxAttempts = realSetMaxAttempts;
    Service.prototype.getTimeout = realGetTimeout;
    Service.prototype.setTimeout = realSetTimeout;
    Service.prototype.getTranslator = realGetTranslator;
    Service.prototype.setTranslator = realSetTranslator;
    Service.prototype.getTarget = realGetTarget;
    Service.prototype.setTarget = realSetTarget;
    Service.prototype.setReferrerBlock = realSetReferrerBlock;
    Service.prototype.setItemTypes = realSetItemTypes;
    Service.prototype.runTest = realRunTest;

    console.log('shutdown mock server.');
    done();
  });


  // ---------------------------------------------------------------------------------------------------
  it('should throw an error if no name is supplied!', function() {
    assert.throws(function() {
      new Service();
    });
    assert.throws(function() {
      new Service(undefined, log);
    });
    assert.throws(function() {
      new Service('', log);
    });
  });

  // ---------------------------------------------------------------------------------------------------
  it('undefined service should return as empty service and disabled!', function() {
    var svc = new Service('tester', log);

    console.log('SERVICE: verifying that defaults are properly set');

    assert(!svc.isEnabled());<commit_msg>backed out error on service test<commit_after>require('../lib');

var mockery = require('./mock_services.js');

// ---------------------------------------------------------------------------------------------------
describe('service.js', function() {
  this.timeout(20000);

  var item, returnField, returnValue = 'foo-bar', mockServer;

  // ---------------------------------------------------------------------------------------------------
  before(function(done) {
    var type = '';

    // Wait for the config file and initial moduleshave finished loading before starting up the server
    var delayStartup = setInterval(function() {
      if (typeof Item !== 'undefined') {
        clearInterval(delayStartup);

        // ---------------------------------------------------------------------------------------------------
        // Mock some methods onto the Service object so we can manipulate and view all attributes
        // ---------------------------------------------------------------------------------------------------
        Service.prototype.getMaxAttempts = function() {
          return this._maxAttempts;
        };
        Service.prototype.setMaxAttempts = function(value) {
          this._maxAttempts = value;
        };
        Service.prototype.getTimeout = function() {
          return this._timeout;
        };
        Service.prototype.setTimeout = function(value) {
          this._timeout = value;
        };
        Service.prototype.getTranslator = function() {
          return this._translator;
        };
        Service.prototype.setTranslator = function(value) {
          this._translator = value;
        };
        Service.prototype.getTarget = function() {
          return this._target;
        };
        Service.prototype.setTarget = function(value) {
          this._target = value;
        };
        Service.prototype.setReferrerBlock = function(values) {
          this._referrerBlock = values;
        };
        Service.prototype.setItemTypes = function(values) {
          this._itemTypesReturned = values;
        };

        Service.prototype.runTest = function(target, item, callback) {
          this.setTarget(target === '' ? '' : ("http://localhost:9000/" + target));

          this.on('success', function(items) {
            //console.log('... success');

            callback({'success': true,
              'count': _.size(items),
              'isArray': items instanceof Array,
              'isItem': _.first(items) instanceof Item,
              'attributeCount': (typeof _.first(items) !== 'undefined') ? _.size(_.first(items).getAttributes()) : 0});
          });

          this.on('error', function(error) {
            //console.log('... failure: ' + error);

            callback({'success': false,
              'isArray': false,
              'isItem': true,
              'attributeCount': 0,
              'level': (error instanceof Item) ? error.getAttribute('level') : 'unknown',
              'message': (error instanceof Item) ? error.getAttribute('message') : 'Got an error!'});
          });

          this.call(item, {});
        };

        _.forEach(CONFIGS.data.objects, function(config, name) {
          if (typeof config.root !== 'undefined') {
            type = name;
            returnField = config.attributes[0];
          }
        });

        item = new Item(type, true, {});

        // Spin up some stub http servers for testing
        mockServer = mockery.spinUpServer(returnField, returnValue);

        done();
      }
    });
  });

  // ---------------------------------------------------------------------------------------------------
  after(function(done) {
    mockServer.close();

    // Remove monkey patches and return Service to its original state
    Service.prototype.getMaxAttempts = undefined;
    Service.prototype.setMaxAttempts = undefined;
    Service.prototype.getTimeout = undefined;
    Service.prototype.setTimeout = undefined;
    Service.prototype.getTranslator = undefined;
    Service.prototype.setTranslator = undefined;
    Service.prototype.getTarget = undefined;
    Service.prototype.setTarget = undefined;
    Service.prototype.setReferrerBlock = undefined;
    Service.prototype.setItemTypes = undefined;
    Service.prototype.runTest = undefined;

    console.log('shutdown mock server.');
    done();
  });


  // ---------------------------------------------------------------------------------------------------
  it('should throw an error if no name is supplied!', function() {
    assert.throws(function() {
      new Service();
    });
    assert.throws(function() {
      new Service(undefined, log);
    });
    assert.throws(function() {
      new Service('', log);
    });
  });

  // ---------------------------------------------------------------------------------------------------
  it('undefined service should return as empty service and disabled!', function() {
    var svc = new Service('tester', log);

    console.log('SERVICE: verifying that defaults are properly set');

    assert(!svc.isEnabled());
{'fraction_non_alphanumeric': 0.23603620074961149, 'fraction_numerical': 0.0021939848249382943, 'mean_word_length': 1.5856771448830065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5791405', 'n_tokens_mistral': 2589, 'n_tokens_neox': 2505, 'n_words': 563}