"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const auto_1 = tslib_1.__importDefault(require("../auto"));
const semver_1 = tslib_1.__importDefault(require("../semver"));
const logger_1 = require("../utils/logger");
const make_commit_from_msg_1 = tslib_1.__importDefault(require("./make-commit-from-msg"));
jest.mock('env-ci', () => () => ({
    pr: 123,
    build: 1,
    isCi: true,
    isPr: true,
    branch: 'ci-test'
}));
const defaults = {
    owner: 'foo',
    repo: 'bar',
    token: 'XXXX'
};
describe('canary in ci', () => {
    test('calls the canary hook with the canary version', async () => {
        const auto = new auto_1.default(Object.assign(Object.assign({}, defaults), { plugins: [] }));
        auto.logger = logger_1.dummyLog();
        await auto.loadConfig();
        auto.release.getCommitsInRelease = () => Promise.resolve([make_commit_from_msg_1.default('Test Commit')]);
        const canary = jest.fn();
        auto.hooks.canary.tap('test', canary);
        const addToPrBody = jest.fn();
        auto.git.addToPrBody = addToPrBody;
        jest.spyOn(auto.release, 'getCommits').mockImplementation();
        await auto.canary();
        expect(canary).toHaveBeenCalledWith(semver_1.default.patch, '.123.1');
    });
    test('comments on PR in CI', async () => {
        const auto = new auto_1.default(Object.assign(Object.assign({}, defaults), { plugins: [] }));
        auto.logger = logger_1.dummyLog();
        await auto.loadConfig();
        auto.git.getLatestRelease = () => Promise.resolve('1.2.3');
        auto.release.getCommitsInRelease = () => Promise.resolve([make_commit_from_msg_1.default('Test Commit')]);
        const addToPrBody = jest.fn();
        auto.git.addToPrBody = addToPrBody;
        jest.spyOn(auto.release, 'getCommits').mockImplementation();
        auto.hooks.canary.tap('test', () => '1.2.4-canary.123.1');
        const version = await auto.canary({ pr: 123, build: 1 });
        expect(addToPrBody).toHaveBeenCalled();
        expect(version.newVersion).toBe('1.2.4-canary.123.1');
    });
    test('should fail when canaries not implemented', async () => {
        const auto = new auto_1.default(Object.assign(Object.assign({}, defaults), { plugins: [] }));
        // @ts-ignore
        jest.spyOn(process, 'exit').mockImplementationOnce(() => { });
        auto.logger = logger_1.dummyLog();
        await auto.loadConfig();
        auto.git.getLatestRelease = () => Promise.resolve('1.2.3');
        auto.release.getCommitsInRelease = () => Promise.resolve([make_commit_from_msg_1.default('Test Commit')]);
        const addToPrBody = jest.fn();
        auto.git.addToPrBody = addToPrBody;
        jest.spyOn(auto.release, 'getCommits').mockImplementation();
        await auto.canary({ pr: 123, build: 1, message: 'false' });
        expect(process.exit).toHaveBeenCalled();
    });
    test('should not comment when passed "false"', async () => {
        const auto = new auto_1.default(Object.assign(Object.assign({}, defaults), { plugins: [] }));
        auto.logger = logger_1.dummyLog();
        await auto.loadConfig();
        auto.git.getLatestRelease = () => Promise.resolve('1.2.3');
        auto.release.getCommitsInRelease = () => Promise.resolve([make_commit_from_msg_1.default('Test Commit')]);
        const addToPrBody = jest.fn();
        auto.git.addToPrBody = addToPrBody;
        jest.spyOn(auto.release, 'getCommits').mockImplementation();
        auto.hooks.canary.tap('test', (bump, post) => `1.2.4-canary${post}`);
        await auto.canary({ pr: 123, build: 1, message: 'false' });
        expect(addToPrBody).not.toHaveBeenCalled();
    });
    test('can override pr and build', async () => {
        const auto = new auto_1.default(Object.assign(Object.assign({}, defaults), { plugins: [] }));
        auto.logger = logger_1.dummyLog();
        await auto.loadConfig();
        auto.release.getCommitsInRelease = () => Promise.resolve([make_commit_from_msg_1.default('Test Commit')]);
        const addToPrBody = jest.fn();
        auto.git.addToPrBody = addToPrBody;
        jest.spyOn(auto.release, 'getCommits').mockImplementation();
        auto.hooks.canary.tap('test', (bump, post) => `1.2.4-canary${post}`);
        const version = await auto.canary({ pr: 456, build: 5 });
        expect(version.newVersion).toBe('1.2.4-canary.456.5');
    });
});
describe('shipit in ci', () => {
    test('should publish canary in PR', async () => {
        const auto = new auto_1.default(Object.assign(Object.assign({}, defaults), { plugins: [] }));
        auto.logger = logger_1.dummyLog();
        await auto.loadConfig();
        auto.git.getLatestRelease = () => Promise.resolve('1.2.3');
        jest.spyOn(auto.git, 'addToPrBody').mockImplementation();
        auto.release.getCommitsInRelease = () => Promise.resolve([]);
        auto.release.getCommits = () => Promise.resolve([]);
        const canary = jest.fn();
        auto.hooks.canary.tap('test', canary);
        await auto.shipit();
        expect(canary).toHaveBeenCalledWith(semver_1.default.patch, '.123.1');
    });
});
//# sourceMappingURL=auto-canary-in-pr-ci.test.js.map