"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const path_1 = require("path");
const pkg_up_1 = __importDefault(require("pkg-up"));
const projectHelper = __importStar(require("../shared.specFiles/projectHelper"));
const bump_1 = require("./bump");
const assertCommitMessage = (workDir, prefix, ticketId, version) => {
    const message = bump_1.getCommitMessage(prefix, ticketId).replace("{{currentTag}}", version);
    chai_1.assert.equal(projectHelper.getLatestCommitMessage(workDir), message);
};
describe("bump", () => {
    const packageJsonRoot = pkg_up_1.default.sync();
    if (!packageJsonRoot) {
        throw new Error(`Did not find package.json in root, something went wrong`);
    }
    const rootPath = path_1.dirname(packageJsonRoot);
    const baseProjectPath = projectHelper.mkTempDir();
    const fixtureSourcePath = path_1.join(rootPath, "resources", "fixtures", "test-repo");
    describe("on release branch", function releaseTest() {
        this.timeout(5000);
        const branchName = "release/test1";
        const workDir = projectHelper.setupProject(branchName, baseProjectPath, fixtureSourcePath);
        it("should start with a clean project on release/test1 branch", () => {
            projectHelper.changePackageKey(workDir, "version", "1.2.0");
            chai_1.assert.equal(projectHelper.getBranch(workDir), branchName);
        });
        it("'bump' must create a prerelease version if on a 'release/*' branch", () => {
            chai_1.assert.equal(projectHelper.getBranch(workDir), branchName);
            projectHelper.createRandomFile(workDir);
            projectHelper.commit(workDir, "fix: patchlevel 1");
            const result = projectHelper.runCiToolkit("", rootPath, "bump", workDir);
            console.log(result.stdout);
            chai_1.assert.equal(result.stderr, "");
            chai_1.assert.equal(result.code, 0);
            chai_1.assert.equal(projectHelper.getPackageJsonVersion(workDir), "1.2.1-beta.0");
            chai_1.assert.equal(projectHelper.getLatestTag(workDir), "v1.2.1-beta.0");
        });
        it("'bump' must include the last ticketId in bump-message, (ticketId in the middle)", () => {
            projectHelper.changePackageKey(workDir, "version", "1.3.0");
            projectHelper.createRandomFile(workDir);
            projectHelper.commit(workDir, "feat(what): SHTL-12345 minor 1");
            projectHelper.runCiToolkit("", rootPath, "bump", workDir);
            assertCommitMessage(workDir, "pre-release", "SHTL-12345", "1.4.0-beta.0");
        });
        it("'bump' must include the last ticketId in bump-message, (ticketId at the end)", () => {
            projectHelper.changePackageKey(workDir, "version", "1.4.0");
            projectHelper.createRandomFile(workDir);
            projectHelper.commit(workDir, "feat: minor 1 [SHTL-12345]");
            projectHelper.runCiToolkit("", rootPath, "bump", workDir);
            assertCommitMessage(workDir, "pre-release", "SHTL-12345", "1.5.0-beta.0");
        });
        it("honors the configurable ticket-prefix and acts accordingly", () => {
            projectHelper.changePackageKey(workDir, "version", "1.5.0");
            projectHelper.createRandomFile(workDir);
            projectHelper.commit(workDir, "feat: minor 1 [EXAMPLE-12345]");
            projectHelper.runCiToolkit("", rootPath, "bump", workDir, "v", "EXAMPLE");
            assertCommitMessage(workDir, "pre-release", "EXAMPLE-12345", "1.6.0-beta.0");
        });
    });
    describe("on master branch", function masterTest() {
        this.timeout(5000);
        const branchName = "master";
        const pwd = projectHelper.setupProject(branchName, baseProjectPath, fixtureSourcePath);
        it("should set up a clean project on master branch", () => {
            projectHelper.changePackageKey(pwd, "version", "1.4.5-beta.0");
            chai_1.assert.equal(projectHelper.getBranch(pwd), branchName);
        });
        it("'bump' must increase the version to a new stable release", () => {
            projectHelper.createRandomFile(pwd);
            projectHelper.commit(pwd, "feat: minor 1");
            projectHelper.runCiToolkit("", rootPath, "bump", pwd);
            chai_1.assert.equal(projectHelper.getPackageJsonVersion(pwd), "1.5.0");
            chai_1.assert.equal(projectHelper.getLatestTag(pwd), "v1.5.0");
        });
        it("'bump' must include the last ticketId in bump-message, (ticketId in the middle)", () => {
            projectHelper.changePackageKey(pwd, "version", "1.5.0");
            projectHelper.createRandomFile(pwd);
            projectHelper.commit(pwd, "feat: SHTL-12345 minor 1");
            projectHelper.runCiToolkit("", rootPath, "bump", pwd);
            assertCommitMessage(pwd, "release", "SHTL-12345", "1.6.0");
        });
        it("'bump' must include the last ticketId in bump-message, (ticketId at the end)", () => {
            projectHelper.changePackageKey(pwd, "version", "1.6.0");
            projectHelper.createRandomFile(pwd);
            projectHelper.commit(pwd, "feat: minor 1 [SHTL-12345]");
            projectHelper.runCiToolkit("", rootPath, "bump", pwd);
            assertCommitMessage(pwd, "release", "SHTL-12345", "1.7.0");
        });
    });
});
//# sourceMappingURL=bump.spec.js.map