101 lines
16 KiB
JavaScript
101 lines
16 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.Quarantine = exports.getQuarantineOptions = exports.validateQuarantineOptions = void 0;
|
||
|
const base_1 = __importDefault(require("./base"));
|
||
|
const quarantine_option_names_1 = __importDefault(require("../../configuration/quarantine-option-names"));
|
||
|
const types_1 = require("../../errors/types");
|
||
|
const runtime_1 = require("../../errors/runtime");
|
||
|
const DEFAULT_ATTEMPT_LIMIT = 5;
|
||
|
const DEFAULT_THRESHOLD = 3;
|
||
|
const MIN_ATTEMPT_LIMIT = 2;
|
||
|
const MIN_SUCCESS_THRESHOLD = 1;
|
||
|
function _isQuarantineOption(option) {
|
||
|
return Object.values(quarantine_option_names_1.default).includes(option);
|
||
|
}
|
||
|
function validateQuarantineOptions(options) {
|
||
|
for (const key in options) {
|
||
|
if (!_isQuarantineOption(key))
|
||
|
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.invalidQuarantineOption, key);
|
||
|
}
|
||
|
const attemptLimit = typeof options.attemptLimit === 'number' ? options.attemptLimit : DEFAULT_ATTEMPT_LIMIT;
|
||
|
const successThreshold = typeof options.successThreshold === 'number' ? options.successThreshold : DEFAULT_THRESHOLD;
|
||
|
if (attemptLimit < MIN_ATTEMPT_LIMIT)
|
||
|
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.invalidAttemptLimitValue, quarantine_option_names_1.default.attemptLimit, MIN_ATTEMPT_LIMIT);
|
||
|
if (successThreshold < MIN_SUCCESS_THRESHOLD)
|
||
|
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.invalidSuccessThresholdValue, quarantine_option_names_1.default.successThreshold, MIN_SUCCESS_THRESHOLD);
|
||
|
if (successThreshold >= attemptLimit)
|
||
|
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.invalidQuarantineParametersRatio, attemptLimit, successThreshold);
|
||
|
}
|
||
|
exports.validateQuarantineOptions = validateQuarantineOptions;
|
||
|
async function getQuarantineOptions(optionName, options) {
|
||
|
if (typeof options === 'boolean')
|
||
|
return true;
|
||
|
const parsedOptions = await (0, base_1.default)(options, {
|
||
|
skipOptionValueTypeConversion: true,
|
||
|
async onOptionParsed(key, value) {
|
||
|
if (!key || !value)
|
||
|
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.optionValueIsNotValidKeyValue, optionName);
|
||
|
return Number(value);
|
||
|
},
|
||
|
});
|
||
|
validateQuarantineOptions(parsedOptions);
|
||
|
return parsedOptions;
|
||
|
}
|
||
|
exports.getQuarantineOptions = getQuarantineOptions;
|
||
|
class Quarantine {
|
||
|
constructor() {
|
||
|
this.attempts = [];
|
||
|
this.attemptLimit = DEFAULT_ATTEMPT_LIMIT;
|
||
|
this.successThreshold = DEFAULT_THRESHOLD;
|
||
|
this.failureThreshold = DEFAULT_THRESHOLD;
|
||
|
}
|
||
|
getFailedAttempts() {
|
||
|
return this.attempts.filter(({ errors }) => !!errors.length);
|
||
|
}
|
||
|
getPassedAttempts() {
|
||
|
return this.attempts.filter(({ errors }) => errors.length === 0);
|
||
|
}
|
||
|
setCustomParameters(attemptLimit, successThreshold) {
|
||
|
const needToUpdateTestRunThreshold = typeof attemptLimit === 'number';
|
||
|
const needToUpdatePassedQuarantineThreshold = typeof successThreshold === 'number';
|
||
|
const needToRecalculateFailedThreshold = needToUpdateTestRunThreshold || needToUpdatePassedQuarantineThreshold;
|
||
|
if (needToUpdateTestRunThreshold)
|
||
|
this.attemptLimit = attemptLimit;
|
||
|
if (needToUpdatePassedQuarantineThreshold)
|
||
|
this.successThreshold = successThreshold;
|
||
|
if (needToRecalculateFailedThreshold)
|
||
|
this._setFailedThreshold();
|
||
|
}
|
||
|
getNextAttemptNumber() {
|
||
|
return this.attempts.length + 1;
|
||
|
}
|
||
|
isThresholdReached(extraErrors) {
|
||
|
const { passedTimes, failedTimes } = this._getAttemptsResult(extraErrors);
|
||
|
const successThresholdReached = passedTimes >= this.successThreshold;
|
||
|
const failureThresholdReached = failedTimes >= this.failureThreshold;
|
||
|
return successThresholdReached || failureThresholdReached;
|
||
|
}
|
||
|
isFirstAttemptSuccessful(extraErrors) {
|
||
|
const { failedTimes, passedTimes } = this._getAttemptsResult(extraErrors);
|
||
|
return failedTimes === 0 && passedTimes > 0;
|
||
|
}
|
||
|
_getAttemptsResult(extraErrors) {
|
||
|
let failedTimes = this.getFailedAttempts().length;
|
||
|
let passedTimes = this.getPassedAttempts().length;
|
||
|
if (extraErrors) {
|
||
|
if (extraErrors.length)
|
||
|
failedTimes += extraErrors.length;
|
||
|
else
|
||
|
passedTimes += 1;
|
||
|
}
|
||
|
return { failedTimes, passedTimes };
|
||
|
}
|
||
|
_setFailedThreshold() {
|
||
|
this.failureThreshold = this.attemptLimit - this.successThreshold + 1;
|
||
|
}
|
||
|
}
|
||
|
exports.Quarantine = Quarantine;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicXVhcmFudGluZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy91dGlscy9nZXQtb3B0aW9ucy9xdWFyYW50aW5lLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLGtEQUFvQztBQUNwQywwR0FBa0Y7QUFDbEYsOENBQW9EO0FBQ3BELGtEQUFvRDtBQUlwRCxNQUFNLHFCQUFxQixHQUFHLENBQUMsQ0FBQztBQUNoQyxNQUFNLGlCQUFpQixHQUFPLENBQUMsQ0FBQztBQUNoQyxNQUFNLGlCQUFpQixHQUFPLENBQUMsQ0FBQztBQUNoQyxNQUFNLHFCQUFxQixHQUFHLENBQUMsQ0FBQztBQUVoQyxTQUFTLG1CQUFtQixDQUFFLE1BQWM7SUFDeEMsT0FBTyxNQUFNLENBQUM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