"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const path = require("path");
const sinon = require("sinon");
const errors_1 = require("../src/server/errors");
const project_1 = require("../src/server/project");
const publisher_1 = require("../src/server/publish/publisher");
const _setup_1 = require("./_setup");
describe('Publisher', () => {
    let requester;
    let publisher;
    // tslint:disable-next-line
    const projectPath = path.join(__dirname, 'fixture/custom-readme');
    const mockConfig = {
        name: 'interactive-launchpad',
        version: '0.1.0',
        description: 'My awesome bundle',
        homepage: 'https://mixer.com',
        controls: {},
        scenes: {},
    };
    beforeEach(() => {
        requester = new _setup_1.MockRequester();
        publisher = new publisher_1.Publisher(requester);
    });
    it('refuses to publish private packages', () => __awaiter(this, void 0, void 0, function* () {
        const project = new project_1.Project('', '');
        sinon.stub(project, 'packageConfig').resolves(Object.assign({}, mockConfig, { private: true }));
        yield chai_1.expect(publisher.publish(project)).to.eventually.be.rejectedWith(errors_1.PublishPrivateError);
    }));
    it('works otherwise', () => __awaiter(this, void 0, void 0, function* () {
        const project = new project_1.Project('', projectPath);
        sinon.stub(project, 'packageConfig').resolves(mockConfig);
        requester.json.resolves({ status: 200 });
        yield publisher.publish(project);
        chai_1.expect(requester.json).to.have.been.calledWith('post', '/interactive/bundles/interactive-launchpad/versions/0.1.0/publish', {
            readme: '<h1 id="heyo-">Heyo!</h1>\n',
            homepage: 'https://mixer.com',
            tags: [],
            description: 'My awesome bundle',
        });
    }));
    it('formats errors nicely', () => __awaiter(this, void 0, void 0, function* () {
        const project = new project_1.Project('', projectPath);
        sinon.stub(project, 'packageConfig').resolves(mockConfig);
        requester.json.resolves({ status: 400, text: () => __awaiter(this, void 0, void 0, function* () { return 'You messed up!'; }) });
        yield chai_1.expect(publisher.publish(project)).to.eventually.be.rejectedWith(errors_1.UnexpectedHttpError, /Unexpected status code 400/);
    }));
    it('unpublishes packages', () => __awaiter(this, void 0, void 0, function* () {
        requester.run.resolves({ status: 200 });
        yield publisher.unpublish('interactive-launchpad', '0.1.0');
        chai_1.expect(requester.run).to.have.been.calledWith('/interactive/bundles/interactive-launchpad/versions/0.1.0', {
            method: 'DELETE',
        });
    }));
});
