"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var old_version_deprecier_1 = require("./old-version-deprecier");
var strong_mock_1 = require("strong-mock");
var rule_interface_1 = require("./interfaces/rule.interface");
var deprecier_state_1 = require("./deprecier-state");
var semver_1 = require("semver");
describe("OldVersionDeprecier", function () {
    function setup() {
        var packageInfoRetriever = strong_mock_1.mock();
        var ruleApplier = strong_mock_1.mock();
        var deprecier = strong_mock_1.mock();
        var authentifier = strong_mock_1.mock();
        var configurationLoader = strong_mock_1.mock();
        var listActiveVersions = strong_mock_1.mock();
        var deprecierState = new deprecier_state_1.DeprecierState();
        var npm = strong_mock_1.mock();
        var oldVersionDeprecier = new old_version_deprecier_1.OldVersionDeprecier(strong_mock_1.instance(packageInfoRetriever), strong_mock_1.instance(ruleApplier), strong_mock_1.instance(deprecier), strong_mock_1.instance(authentifier), strong_mock_1.instance(configurationLoader), strong_mock_1.instance(listActiveVersions), strong_mock_1.instance(npm), deprecierState);
        return {
            oldVersionDeprecier: oldVersionDeprecier,
            packageInfoRetriever: packageInfoRetriever,
            ruleApplier: ruleApplier,
            deprecier: deprecier,
            authentifier: authentifier,
            configurationLoader: configurationLoader,
            listActiveVersions: listActiveVersions,
            npm: npm,
            deprecierState: deprecierState,
        };
    }
    describe("verifyConditions", function () {
        it("should generate rules", function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, configurationLoader, oldVersionDeprecier, deprecierState, rule, rules, config;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = setup(), configurationLoader = _a.configurationLoader, oldVersionDeprecier = _a.oldVersionDeprecier, deprecierState = _a.deprecierState;
                        rule = strong_mock_1.mock();
                        rules = [strong_mock_1.instance(rule)];
                        config = {
                            debug: true,
                        };
                        strong_mock_1.when(configurationLoader.generateRules(config)).thenReturn(rules);
                        return [4 /*yield*/, oldVersionDeprecier.verifyConditions(config)];
                    case 1:
                        _b.sent();
                        strong_mock_1.verify(configurationLoader);
                        expect(deprecierState.rules).toBe(rules);
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe("prepare", function () {
        it("should retrieve the config", function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, deprecierState, npm, oldVersionDeprecier, logger, context, npmConfig;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = setup(), deprecierState = _a.deprecierState, npm = _a.npm, oldVersionDeprecier = _a.oldVersionDeprecier;
                        logger = strong_mock_1.mock();
                        context = {
                            logger: strong_mock_1.instance(logger),
                            cwd: "here",
                            env: {},
                        };
                        npmConfig = {
                            registry: "registry",
                        };
                        strong_mock_1.when(npm.getConfig(context)).thenResolve(npmConfig);
                        return [4 /*yield*/, oldVersionDeprecier.prepare({}, context)];
                    case 1:
                        _b.sent();
                        expect(deprecierState.npmConfig).toBe(npmConfig);
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe("publish", function () {
        it("should not do anything if there is no package info", function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, oldVersionDeprecier, listActiveVersions, packageInfoRetriever, deprecierState, logger, context;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = setup(), oldVersionDeprecier = _a.oldVersionDeprecier, listActiveVersions = _a.listActiveVersions, packageInfoRetriever = _a.packageInfoRetriever, deprecierState = _a.deprecierState;
                        logger = strong_mock_1.mock();
                        context = {
                            logger: strong_mock_1.instance(logger),
                            env: {},
                        };
                        deprecierState.npmConfig = {
                            registry: "registry",
                        };
                        strong_mock_1.when(packageInfoRetriever.getInfo(deprecierState.npmConfig, context)).thenResolve(undefined);
                        strong_mock_1.when(logger.log("This project does not seem to be a npm package")).thenReturn();
                        return [4 /*yield*/, oldVersionDeprecier.publish({}, context)];
                    case 1:
                        _b.sent();
                        strong_mock_1.verify(logger);
                        strong_mock_1.verify(listActiveVersions);
                        return [2 /*return*/];
                }
            });
        }); });
        it("should apply the rules on active versions and deprecate the selected versions", function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, deprecierState, oldVersionDeprecier, listActiveVersions, ruleApplier, authentifier, deprecier, packageInfoRetriever, packageInfo, rule, logger, context, actions;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = setup(), deprecierState = _a.deprecierState, oldVersionDeprecier = _a.oldVersionDeprecier, listActiveVersions = _a.listActiveVersions, ruleApplier = _a.ruleApplier, authentifier = _a.authentifier, deprecier = _a.deprecier, packageInfoRetriever = _a.packageInfoRetriever;
                        packageInfo = {
                            name: "foo",
                            versions: {
                                "1.0.0": {
                                    name: "foo",
                                    version: "1.0.0",
                                },
                                "2.0.0": {
                                    name: "foo",
                                    version: "2.0.0",
                                },
                            },
                        };
                        rule = strong_mock_1.mock();
                        deprecierState.rules = [strong_mock_1.instance(rule)];
                        deprecierState.npmConfig = {
                            registry: "registry",
                        };
                        logger = strong_mock_1.mock();
                        context = {
                            logger: strong_mock_1.instance(logger),
                            env: {},
                        };
                        strong_mock_1.when(packageInfoRetriever.getInfo(deprecierState.npmConfig, context)).thenResolve(packageInfo);
                        strong_mock_1.when(listActiveVersions(packageInfo)).thenReturn(["1.0.0", "2.0.0"]);
                        strong_mock_1.when(logger.log("Active versions: 1.0.0, 2.0.0")).thenReturn();
                        actions = [
                            {
                                version: new semver_1.SemVer("1.0.0"),
                                action: rule_interface_1.Action.deprecate,
                                reason: "because",
                            },
                            {
                                version: new semver_1.SemVer("2.0.0"),
                                action: rule_interface_1.Action.support,
                            },
                        ];
                        strong_mock_1.when(ruleApplier.applyRules([new semver_1.SemVer("1.0.0"), new semver_1.SemVer("2.0.0")], deprecierState.rules)).thenReturn(actions);
                        strong_mock_1.when(logger.log("Versions to deprecate", "1.0.0")).thenReturn();
                        strong_mock_1.when(authentifier.authenticate(deprecierState.npmConfig, context)).thenResolve();
                        strong_mock_1.when(authentifier.checkAuthentication(context)).thenResolve();
                        strong_mock_1.when(deprecier.deprecate(packageInfo, actions[0], context)).thenResolve();
                        return [4 /*yield*/, oldVersionDeprecier.publish({}, context)];
                    case 1:
                        _b.sent();
                        strong_mock_1.verifyAll();
                        return [2 /*return*/];
                }
            });
        }); });
        it("should log that nothing will be deprecated if this is the case", function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, deprecierState, oldVersionDeprecier, listActiveVersions, ruleApplier, packageInfoRetriever, packageInfo, logger, context, rule, actions;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = setup(), deprecierState = _a.deprecierState, oldVersionDeprecier = _a.oldVersionDeprecier, listActiveVersions = _a.listActiveVersions, ruleApplier = _a.ruleApplier, packageInfoRetriever = _a.packageInfoRetriever;
                        packageInfo = {
                            name: "foo",
                            versions: {
                                "1.0.0": {
                                    name: "foo",
                                    version: "1.0.0",
                                },
                                "2.0.0": {
                                    name: "foo",
                                    version: "2.0.0",
                                },
                            },
                        };
                        deprecierState.npmConfig = {
                            registry: "registry",
                        };
                        logger = strong_mock_1.mock();
                        context = {
                            logger: strong_mock_1.instance(logger),
                            env: {},
                        };
                        strong_mock_1.when(packageInfoRetriever.getInfo(deprecierState.npmConfig, context)).thenResolve(packageInfo);
                        rule = strong_mock_1.mock();
                        deprecierState.rules = [strong_mock_1.instance(rule)];
                        strong_mock_1.when(listActiveVersions(packageInfo)).thenReturn(["1.0.0", "2.0.0"]);
                        strong_mock_1.when(logger.log("Active versions: 1.0.0, 2.0.0")).thenReturn();
                        actions = [
                            {
                                version: new semver_1.SemVer("1.0.0"),
                                action: rule_interface_1.Action.support,
                            },
                            {
                                version: new semver_1.SemVer("2.0.0"),
                                action: rule_interface_1.Action.support,
                            },
                        ];
                        strong_mock_1.when(ruleApplier.applyRules([new semver_1.SemVer("1.0.0"), new semver_1.SemVer("2.0.0")], deprecierState.rules)).thenReturn(actions);
                        strong_mock_1.when(logger.log("No version to deprecate")).thenReturn();
                        return [4 /*yield*/, oldVersionDeprecier.publish({}, context)];
                    case 1:
                        _b.sent();
                        strong_mock_1.verifyAll();
                        return [2 /*return*/];
                }
            });
        }); });
    });
    it("should throw an error if the npm config could not be retrieved", function () { return __awaiter(void 0, void 0, void 0, function () {
        var oldVersionDeprecier, logger, context;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    oldVersionDeprecier = setup().oldVersionDeprecier;
                    logger = strong_mock_1.mock();
                    context = {
                        logger: strong_mock_1.instance(logger),
                        cwd: "here",
                        env: {},
                    };
                    return [4 /*yield*/, expect(oldVersionDeprecier.publish({}, context)).rejects.toThrow("Unable to deprecate version as the configuration of NPM could not be retrieved")];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); });
});
