'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var time_time = require('../time/time.js');
var wecomRobot_message = require('../wecom-robot/message.js');
var e2eTest_config = require('./config.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);

var TRIGGER_MAP = {
  MANUAL: '手动',
  TIME_TRIGGER: '定时',
  WEB_HOOK: 'WebHook',
  SERVICE: '内部服务',
  PIPELINE: '子流水线',
  REMOTE: '远程'
};
function getStartTypeDesc(data) {
  var bkStartType = data.bkStartType;
  var content = '';
  if (bkStartType && TRIGGER_MAP[bkStartType]) {
    content = TRIGGER_MAP[bkStartType];
  } else if (bkStartType) {
    content = bkStartType;
  }
  if (content) {
    return [{
      content: content,
      label: '触发方式'
    }];
  }
  return [];
}
function getFailedTestUnits(_ref) {
  var passes = _ref.passes,
    tests = _ref.tests,
    testList = _ref.testList;
  if (passes == tests) return {};
  if (!(testList === null || testList === void 0 ? void 0 : testList.length)) return {};
  var failedTests = testList.filter(function (item) {
    return !!item.fail;
  });
  var failTestTitles = failedTests.map(function (item) {
    return item.title;
  });
  if (!(failTestTitles === null || failTestTitles === void 0 ? void 0 : failTestTitles.length)) return {};
  return {
    errorTitle: "\u5931\u8D25\uFF1A".concat(failTestTitles.join('、')),
    errorDetail: getFailedDetail(failedTests)
  };
}
function getFailedDetail(failedTests) {
  return failedTests.map(function (item) {
    var errMsg = (item.err.estack || '').replace(/[\n]+/g, '; ');
    return "".concat(item.title, ": ").concat(errMsg);
  }).filter(function (item) {
    return item;
  });
}
function getE2ETestRobotMessage(data) {
  var notificationList = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var start = data.start,
    duration = data.duration,
    passes = data.passes,
    tests = data.tests,
    _data$projectLink = data.projectLink,
    projectLink = _data$projectLink === void 0 ? '' : _data$projectLink,
    _data$checkUrl = data.checkUrl,
    checkUrl = _data$checkUrl === void 0 ? '' : _data$checkUrl,
    _data$name = data.name,
    name = _data$name === void 0 ? '' : _data$name,
    _data$comment = data.comment,
    comment = _data$comment === void 0 ? '' : _data$comment,
    _data$fileList = data.fileList,
    fileList = _data$fileList === void 0 ? [] : _data$fileList;
  var hasFailed = passes !== tests;
  var time = time_time.timeStampFormat(new Date(start).getTime(), 'MM-dd hh:mm:ss');
  var title = ">".concat(hasFailed ? e2eTest_config.EMOJI_MAP.FAIL : e2eTest_config.EMOJI_MAP.PASS, "\u3010\u81EA\u52A8\u5316\u6D4B\u8BD5\u3011");
  var errorDetailList = [];
  var messageList = [["".concat(title).concat(wecomRobot_message.parseRobotMessage({
    content: "".concat(name, "-").concat(comment),
    link: projectLink
  })), {
    content: "".concat(passes, "/").concat(tests),
    label: '成功/全部'
  }, {
    content: "".concat(Math.ceil(duration / 1000), "s"),
    label: '耗时'
  }].concat(_toConsumableArray__default["default"](getStartTypeDesc(data)), [{
    content: time,
    link: checkUrl
  }])];
  if (hasFailed) {
    var _messageList$;
    (_messageList$ = messageList[0]).push.apply(_messageList$, _toConsumableArray__default["default"](notificationList.map(function (item) {
      return "<@".concat(item, ">");
    })));
  }
  var fileMessageList = (fileList || []).filter(function (fileInfo) {
    var tests = fileInfo.tests,
      passes = fileInfo.passes;
    return tests !== passes;
  }).map(function (fileInfo) {
    var _a;
    var file = fileInfo.file,
      tests = fileInfo.tests,
      passes = fileInfo.passes,
      link = fileInfo.link,
      testList = fileInfo.testList;
    var fileName = wecomRobot_message.parseRobotMessage({
      content: file,
      link: link
    });
    var failedInfo = getFailedTestUnits({
      testList: testList,
      passes: passes,
      tests: tests
    });
    if ((_a = failedInfo.errorDetail) === null || _a === void 0 ? void 0 : _a.length) {
      errorDetailList.push(['\n']);
      errorDetailList.push(failedInfo.errorDetail);
    }
    return ["- ".concat(fileName), "".concat(passes, "/").concat(tests), (failedInfo === null || failedInfo === void 0 ? void 0 : failedInfo.errorTitle) || ''];
  });
  messageList.push.apply(messageList, _toConsumableArray__default["default"](fileMessageList));
  messageList.push.apply(messageList, ['\n', '\n']);
  messageList.push.apply(messageList, errorDetailList);
  return {
    message: wecomRobot_message.genRobotMessage(messageList),
    start: start,
    hasFailed: hasFailed
  };
}

exports.TRIGGER_MAP = TRIGGER_MAP;
exports.getE2ETestRobotMessage = getE2ETestRobotMessage;
