"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

function _command() {
  const data = _interopRequireDefault(require("../../command"));

  _command = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../api/consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _chalkBox() {
  const data = require("../../chalk-box");

  _chalkBox = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../../../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

const validForkLevels = _ramda().default.values(_constants().TESTS_FORK_LEVEL);

let verboseReport = false;

class Test extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'test [id]');
    (0, _defineProperty2().default)(this, "description", `test any set of components with a configured tester as defined in bit.json (by default applies only on modified components)\n  https://${_constants().BASE_DOCS_DOMAIN}/docs/testing-components.html)`);
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['a', 'all', 'test all components in your workspace, including unmodified components'], ['v', 'verbose', 'showing npm verbose output for inspection and prints stack trace'], ['j', 'json', 'return results in json format'], ['', 'fork-level <forkLevel>', 'NONE / ONE / COMPONENT how many child process create for test running']]);
    (0, _defineProperty2().default)(this, "loader", true);
    (0, _defineProperty2().default)(this, "migration", true);
  }

  action([id], {
    all,
    verbose,
    forkLevel
  }) {
    return (0, _bluebird().coroutine)(function* () {
      if (id && all) {
        throw new (_generalError().default)('use "--all" to test all components or use a component ID to test a specific component. run tests to all new and modified components by removing all flags and parameters');
      }

      if (forkLevel && !validForkLevels.includes(forkLevel)) {
        return Promise.reject(new (_generalError().default)(`fork level must be one of: ${validForkLevels.join()}`));
      }

      verboseReport = verbose || false;
      const testRes = yield (0, _consumer().test)(id, forkLevel, all, verbose);
      const pass = testRes.results.every(comp => comp.pass);
      const res = {
        data: testRes,
        __code: pass ? 0 : 1
      };
      return res;
    })();
  }

  report(results, args, flags) {
    if (flags.json) return JSON.stringify(results, null, 2);
    const specsResultsWithComponentId = results.results;

    if (specsResultsWithComponentId && Array.isArray(specsResultsWithComponentId)) {
      return (0, _chalkBox().paintAllSpecsResults)(results, verboseReport) + (0, _chalkBox().paintSummarySpecsResults)(specsResultsWithComponentId);
    }

    return "couldn't get test results...";
  }

}

exports.default = Test;