'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _helpers = require('../../formatter/helpers');

var _command_types = require('./command_types');

var _command_types2 = _interopRequireDefault(_command_types);

var _events = require('events');

var _events2 = _interopRequireDefault(_events);

var _readline = require('readline');

var _readline2 = _interopRequireDefault(_readline);

var _serializeError = require('serialize-error');

var _serializeError2 = _interopRequireDefault(_serializeError);

var _stack_trace_filter = require('../stack_trace_filter');

var _stack_trace_filter2 = _interopRequireDefault(_stack_trace_filter);

var _support_code_library_builder = require('../../support_code_library_builder');

var _support_code_library_builder2 = _interopRequireDefault(_support_code_library_builder);

var _test_case_runner = require('../test_case_runner');

var _test_case_runner2 = _interopRequireDefault(_test_case_runner);

var _user_code_runner = require('../../user_code_runner');

var _user_code_runner2 = _interopRequireDefault(_user_code_runner);

var _verror = require('verror');

var _verror2 = _interopRequireDefault(_verror);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var EVENTS = ['test-case-prepared', 'test-case-started', 'test-step-started', 'test-step-attachment', 'test-step-finished', 'test-case-finished'];

function replacerSerializeErrors(key, value) {
  if (_lodash2.default.isError(value)) {
    return (0, _serializeError2.default)(value);
  }
  return value;
}

var Slave = function () {
  function Slave(_ref) {
    var _this = this;

    var cwd = _ref.cwd,
        stdin = _ref.stdin,
        stdout = _ref.stdout;
    (0, _classCallCheck3.default)(this, Slave);

    this.initialized = false;
    this.stdin = stdin;
    this.stdout = stdout;
    this.cwd = cwd;
    this.eventBroadcaster = new _events2.default();
    this.stackTraceFilter = new _stack_trace_filter2.default();
    EVENTS.forEach(function (name) {
      _this.eventBroadcaster.on(name, function (data) {
        return _this.stdout.write(JSON.stringify({ command: _command_types2.default.EVENT, name: name, data: data }, replacerSerializeErrors) + '\n');
      });
    });
  }

  (0, _createClass3.default)(Slave, [{
    key: 'initialize',
    value: function () {
      var _ref3 = (0, _bluebird.coroutine)(function* (_ref2) {
        var filterStacktraces = _ref2.filterStacktraces,
            supportCodeRequiredModules = _ref2.supportCodeRequiredModules,
            supportCodePaths = _ref2.supportCodePaths,
            worldParameters = _ref2.worldParameters;

        supportCodeRequiredModules.map(function (module) {
          return require(module);
        });
        _support_code_library_builder2.default.reset(this.cwd);
        supportCodePaths.forEach(function (codePath) {
          return require(codePath);
        });
        this.supportCodeLibrary = _support_code_library_builder2.default.finalize();
        this.worldParameters = worldParameters;
        this.filterStacktraces = filterStacktraces;
        if (this.filterStacktraces) {
          this.stackTraceFilter.filter();
        }
        yield this.runTestRunHooks('beforeTestRunHookDefinitions', 'a BeforeAll');
        this.stdout.write(JSON.stringify({ command: _command_types2.default.READY }) + '\n');
      });

      function initialize(_x) {
        return _ref3.apply(this, arguments);
      }

      return initialize;
    }()
  }, {
    key: 'finalize',
    value: function () {
      var _ref4 = (0, _bluebird.coroutine)(function* () {
        yield this.runTestRunHooks('afterTestRunHookDefinitions', 'an AfterAll');
        if (this.filterStacktraces) {
          this.stackTraceFilter.unfilter();
        }
        process.exit();
      });

      function finalize() {
        return _ref4.apply(this, arguments);
      }

      return finalize;
    }()
  }, {
    key: 'parseMasterLine',
    value: function parseMasterLine(line) {
      var input = JSON.parse(line);
      if (input.command === 'initialize') {
        this.initialize(input);
      } else if (input.command === 'finalize') {
        this.finalize();
      } else if (input.command === 'run') {
        this.runTestCase(input);
      }
    }
  }, {
    key: 'run',
    value: function () {
      var _ref5 = (0, _bluebird.coroutine)(function* () {
        var _this2 = this;

        this.rl = _readline2.default.createInterface({ input: this.stdin });
        this.rl.on('line', function (line) {
          _this2.parseMasterLine(line);
        });
      });

      function run() {
        return _ref5.apply(this, arguments);
      }

      return run;
    }()
  }, {
    key: 'runTestCase',
    value: function () {
      var _ref7 = (0, _bluebird.coroutine)(function* (_ref6) {
        var testCase = _ref6.testCase,
            skip = _ref6.skip;

        var testCaseRunner = new _test_case_runner2.default({
          eventBroadcaster: this.eventBroadcaster,
          skip: skip,
          supportCodeLibrary: this.supportCodeLibrary,
          testCase: testCase,
          worldParameters: this.worldParameters
        });
        yield testCaseRunner.run();
        this.stdout.write(JSON.stringify({ command: _command_types2.default.READY }) + '\n');
      });

      function runTestCase(_x2) {
        return _ref7.apply(this, arguments);
      }

      return runTestCase;
    }()
  }, {
    key: 'runTestRunHooks',
    value: function () {
      var _ref8 = (0, _bluebird.coroutine)(function* (key, name) {
        var _this3 = this;

        yield _bluebird2.default.each(this.supportCodeLibrary[key], function () {
          var _ref9 = (0, _bluebird.coroutine)(function* (hookDefinition) {
            var _ref10 = yield _user_code_runner2.default.run({
              argsArray: [],
              fn: hookDefinition.code,
              thisArg: null,
              timeoutInMilliseconds: hookDefinition.options.timeout || _this3.supportCodeLibrary.defaultTimeout
            }),
                error = _ref10.error;

            if (error) {
              var location = (0, _helpers.formatLocation)(hookDefinition);
              throw new _verror2.default(error, name + ' hook errored, process exiting: ' + location);
            }
          });

          return function (_x5) {
            return _ref9.apply(this, arguments);
          };
        }());
      });

      function runTestRunHooks(_x3, _x4) {
        return _ref8.apply(this, arguments);
      }

      return runTestRunHooks;
    }()
  }]);
  return Slave;
}();

exports.default = Slave;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 10196 characters]