"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var semver_1 = require("semver");
var configuration_loader_1 = __importDefault(require("./configuration-loader"));
var plugin_config_interface_1 = require("./interfaces/plugin-config.interface");
describe("ConfigurationLoader", function () {
    function setup() {
        var injections = {
            deprecateAll: jest.fn().mockReturnValue("deprecate"),
            supportLatest: jest.fn().mockReturnValue("latest"),
            supportPreReleaseIfNotReleased: jest.fn().mockReturnValue("prerelease"),
        };
        var loader = new configuration_loader_1.default(injections);
        return { injections: injections, loader: loader };
    }
    describe("when no configuration is provided", function () {
        it("should return the default configuration", function () {
            var _a = setup(), loader = _a.loader, injections = _a.injections;
            var config = loader.generateRules({});
            expect(config).toHaveLength(3);
            var run1Result = config[0](new semver_1.SemVer("1.0.0"), []);
            expect(run1Result).toBe("latest");
            expect(injections.supportLatest).toHaveBeenCalledWith(undefined, new semver_1.SemVer("1.0.0"), []);
            var run2Result = config[1](new semver_1.SemVer("1.0.0"), []);
            expect(run2Result).toBe("prerelease");
            expect(injections.supportPreReleaseIfNotReleased).toHaveBeenCalledWith(undefined, new semver_1.SemVer("1.0.0"), []);
            var run3Result = config[2](new semver_1.SemVer("1.0.0"), []);
            expect(run3Result).toBe("deprecate");
            expect(injections.deprecateAll).toHaveBeenCalledWith(undefined, new semver_1.SemVer("1.0.0"), []);
        });
    });
    describe("when the configuration contains a function", function () {
        it("should return the function itself", function () {
            var loader = setup().loader;
            var rule = jest.fn();
            var config = loader.generateRules({ rules: [rule] });
            expect(config).toHaveLength(1);
            expect(config[0]).toBe(rule);
        });
    });
    describe("when the configuration contains strings", function () {
        it("should return the deprecateAll rule", function () {
            var loader = setup().loader;
            var config = loader.generateRules({
                rules: [plugin_config_interface_1.SupportedDefaultRule.deprecateAll],
            });
            expect(config).toHaveLength(1);
            var runResult = config[0](new semver_1.SemVer("1.0.0"), []);
            expect(runResult).toBe("deprecate");
        });
        it("should return the supportLatest rule", function () {
            var loader = setup().loader;
            var config = loader.generateRules({
                rules: [plugin_config_interface_1.SupportedDefaultRule.supportLatest],
            });
            expect(config).toHaveLength(1);
            var runResult = config[0](new semver_1.SemVer("1.0.0"), []);
            expect(runResult).toBe("latest");
        });
        it("should return the supportPreReleaseIfNotReleased rule", function () {
            var loader = setup().loader;
            var config = loader.generateRules({
                rules: [plugin_config_interface_1.SupportedDefaultRule.supportPreReleaseIfNotReleased],
            });
            expect(config).toHaveLength(1);
            var runResult = config[0](new semver_1.SemVer("1.0.0"), []);
            expect(runResult).toBe("prerelease");
        });
        it("should throw an error if the string is not supported", function () {
            var loader = setup().loader;
            expect(function () {
                loader.generateRules({
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    rules: ["garbage"],
                });
            }).toThrowError("Unsupported rule garbage");
        });
    });
    describe("when the configuration contains configuration objects", function () {
        it("should pass options to the supportLatest rule", function () {
            var _a = setup(), loader = _a.loader, injections = _a.injections;
            var config = loader.generateRules({
                rules: [
                    {
                        rule: plugin_config_interface_1.SupportedDefaultRule.supportLatest,
                        options: { numberOfMajorReleases: 2 },
                    },
                ],
            });
            expect(config).toHaveLength(1);
            config[0](new semver_1.SemVer("1.0.0"), []);
            expect(injections.supportLatest).toHaveBeenCalledWith({ numberOfMajorReleases: 2 }, new semver_1.SemVer("1.0.0"), []);
        });
        it("should pass options to the supportPreReleaseIfNotReleased rule", function () {
            var _a = setup(), loader = _a.loader, injections = _a.injections;
            var config = loader.generateRules({
                rules: [
                    {
                        rule: plugin_config_interface_1.SupportedDefaultRule.supportPreReleaseIfNotReleased,
                        options: { numberOfPreReleases: 2 },
                    },
                ],
            });
            expect(config).toHaveLength(1);
            config[0](new semver_1.SemVer("1.0.0"), []);
            expect(injections.supportPreReleaseIfNotReleased).toHaveBeenCalledWith({ numberOfPreReleases: 2 }, new semver_1.SemVer("1.0.0"), []);
        });
        it("should throw an error if the rule is not supported", function () {
            var loader = setup().loader;
            expect(function () {
                loader.generateRules({
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    rules: [{ rule: "garbage" }],
                });
            }).toThrowError("Unsupported rule garbage");
        });
    });
});
