<commit_before>var Surrogate,
  _ = require("lodash"),
  schemas = require("./schemas"),
  defineSurrogateRoutes = require("./define-surrogate-routes"),
  Errors = require("./errors"),
  Hapi = require("hapi");

/**
 * Surrogate
 */

Surrogate = function (options) {
  options = options || {};
  this.port = options.port || 1337;
  this.ip = options.ip || "0.0.0.0";
  this.verbose = options.verbose || false;
  this._state = options.initialState || {};
  this._routes = [];
  this.hapiRoutes = [];
  this._server = null;
};

Surrogate.prototype._assertNewRoute = function (routeCfg) {
  _.each(this._routes, function (route) {
    if (route.name === routeCfg.name) {
      throw new Errors.ConfigurationError(
        "Route with name '" + routeCfg.name + "' is already defined."
      );
    }
  });
};

Surrogate.prototype.jsonRoute = function (routeCfg) {
  schemas.validateConfig(schemas.route, routeCfg);
  this._assertNewRoute(routeCfg);
  _.each(routeCfg.handlers, _.partial(schemas.validateConfig, schemas.handler));
  this._routes.push(_.extend({}, routeCfg, { type: "application/json" }));
};

Surrogate.prototype.htmlRoute = function (routeCfg) {
  schemas.validateConfig(schemas.route, routeCfg);
  this._assertNewRoute(routeCfg);
  _.each(routeCfg.handlers, _.partial(schemas.validateConfig, schemas.handler));
  this._routes.push(_.extend({}, routeCfg, { type: "text/html" }));
};

Surrogate.prototype.hapiRoute = function (routeCfg) {
  this.hapiRoutes.push(routeCfg);
};

Surrogate.prototype.start = function (cb) {
  var self = this;
  console.log("starting server on " + this.ip + ":" + this.port + "...");

  this.server = new Hapi.Server(this.ip, this.port, {
    router: { stripTrailingSlash: true }
  });

  defineSurrogateRoutes.apply(this);

  this.reset(false, function () {
    self.server.start(cb);
  });
};

Surrogate.prototype.stop = function (cb) {
  console.log("stopping server...");
  this.server.stop(cb);
};

Surrogate.prototype.reset = function (logReset, cb) {
  if (logReset) { console.log("resetting to initial state..."); }
  this.routes = _.cloneDeep(this._routes);
  this.delay = 0;
  this.state = _.cloneDeep(this._state);
  if (_.isFunction(cb)) { cb(); }
};


module.exports = Surrogate;
<commit_msg>auto-set handler and option selections on reset<commit_after>var Surrogate,
  _ = require("lodash"),
  schemas = require("./schemas"),
  defineSurrogateRoutes = require("./define-surrogate-routes"),
  Errors = require("./errors"),
  Hapi = require("hapi");

/**
 * Surrogate
 */

Surrogate = function (options) {
  options = options || {};
  this.port = options.port || 1337;
  this.ip = options.ip || "0.0.0.0";
  this.verbose = options.verbose || false;
  this._state = options.initialState || {};
  this._routes = [];
  this.hapiRoutes = [];
  this._server = null;
};

Surrogate.prototype._assertNewRoute = function (routeCfg) {
  _.each(this._routes, function (route) {
    if (route.name === routeCfg.name) {
      throw new Errors.ConfigurationError(
        "Route with name '" + routeCfg.name + "' is already defined."
      );
    }
  });
};

Surrogate.prototype.jsonRoute = function (routeCfg) {
  schemas.validateConfig(schemas.route, routeCfg);
  this._assertNewRoute(routeCfg);
  _.each(routeCfg.handlers, _.partial(schemas.validateConfig, schemas.handler));
  this._routes.push(_.extend({}, routeCfg, { type: "application/json" }));
};

Surrogate.prototype.htmlRoute = function (routeCfg) {
  schemas.validateConfig(schemas.route, routeCfg);
  this._assertNewRoute(routeCfg);
  _.each(routeCfg.handlers, _.partial(schemas.validateConfig, schemas.handler));
  this._routes.push(_.extend({}, routeCfg, { type: "text/html" }));
};

Surrogate.prototype.hapiRoute = function (routeCfg) {
  this.hapiRoutes.push(routeCfg);
};

Surrogate.prototype.start = function (cb) {
  var self = this;
  console.log("starting server on " + this.ip + ":" + this.port + "...");

  this.server = new Hapi.Server(this.ip, this.port, {
    router: { stripTrailingSlash: true }
  });

  defineSurrogateRoutes.apply(this);

  this.reset(false, function () {
    self.server.start(cb);
  });
};

Surrogate.prototype.stop = function (cb) {
  console.log("stopping server...");
  this.server.stop(cb);
};

Surrogate.prototype.reset = function (logReset, cb) {
  if (logReset) { console.log("resetting to initial state..."); }
  this.routes = _.cloneDeep(this._routes);
  _.each(this.routes, function (route) {
    route.selectedHandler = route.selectedHandler || _.keys(route.handlers)[0];
    _.each(route.handlers, function (handler) {
      if (handler.optionsType === "selectOne") {
        handler.selectedOption = handler.selectedOption || _.keys(handler.options)[0];
      }
    });
  });
  this.delay = 0;
  this.state = _.cloneDeep(this._state);
  if (_.isFunction(cb)) { cb(); }
};


module.exports = Surrogate;

{'fraction_non_alphanumeric': 0.17281153450051492, 'fraction_numerical': 0.004119464469618949, 'mean_word_length': 3.9399796541200405, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '169861', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1586, 'n_words': 281}