"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Permanent live', function () {
    let servers = [];
    let videoUUID;
    function createLiveWrapper(permanentLive) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const attributes = {
                channelId: servers[0].store.channel.id,
                privacy: 1,
                name: 'my super live',
                saveReplay: false,
                permanentLive
            };
            const { uuid } = yield servers[0].live.create({ fields: attributes });
            return uuid;
        });
    }
    function checkVideoState(videoId, state) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (const server of servers) {
                const video = yield server.videos.get({ id: videoId });
                (0, chai_1.expect)(video.state.id).to.equal(state);
            }
        });
    }
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.setDefaultVideoChannel)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            yield servers[0].config.updateCustomSubConfig({
                newConfig: {
                    live: {
                        enabled: true,
                        allowReplay: true,
                        maxDuration: -1,
                        transcoding: {
                            enabled: true,
                            resolutions: server_commands_1.ConfigCommand.getCustomConfigResolutions(true)
                        }
                    }
                }
            });
        });
    });
    it('Should create a non permanent live and update it to be a permanent live', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(20000);
            const videoUUID = yield createLiveWrapper(false);
            {
                const live = yield servers[0].live.get({ videoId: videoUUID });
                (0, chai_1.expect)(live.permanentLive).to.be.false;
            }
            yield servers[0].live.update({ videoId: videoUUID, fields: { permanentLive: true } });
            {
                const live = yield servers[0].live.get({ videoId: videoUUID });
                (0, chai_1.expect)(live.permanentLive).to.be.true;
            }
        });
    });
    it('Should create a permanent live', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(20000);
            videoUUID = yield createLiveWrapper(true);
            const live = yield servers[0].live.get({ videoId: videoUUID });
            (0, chai_1.expect)(live.permanentLive).to.be.true;
            yield (0, server_commands_1.waitJobs)(servers);
        });
    });
    it('Should stream into this permanent live', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(240000);
            const beforePublication = new Date();
            const ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID });
            for (const server of servers) {
                yield server.live.waitUntilPublished({ videoId: videoUUID });
            }
            yield checkVideoState(videoUUID, 1);
            for (const server of servers) {
                const video = yield server.videos.get({ id: videoUUID });
                (0, chai_1.expect)(new Date(video.publishedAt)).greaterThan(beforePublication);
            }
            yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
            yield servers[0].live.waitUntilWaiting({ videoId: videoUUID });
            yield (0, server_commands_1.waitJobs)(servers);
        });
    });
    it('Should have cleaned up this live', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(40000);
            yield (0, core_utils_1.wait)(5000);
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const videoDetails = yield server.videos.get({ id: videoUUID });
                (0, chai_1.expect)(videoDetails.streamingPlaylists).to.have.lengthOf(0);
            }
            yield (0, shared_1.checkLiveCleanup)({ server: servers[0], permanent: true, videoUUID });
        });
    });
    it('Should have set this live to waiting for live state', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(20000);
            yield checkVideoState(videoUUID, 4);
        });
    });
    it('Should be able to stream again in the permanent live', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].config.updateCustomSubConfig({
                newConfig: {
                    live: {
                        enabled: true,
                        allowReplay: true,
                        maxDuration: -1,
                        transcoding: {
                            enabled: true,
                            resolutions: server_commands_1.ConfigCommand.getCustomConfigResolutions(false)
                        }
                    }
                }
            });
            const ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID });
            for (const server of servers) {
                yield server.live.waitUntilPublished({ videoId: videoUUID });
            }
            yield checkVideoState(videoUUID, 1);
            const count = yield servers[0].live.countPlaylists({ videoUUID });
            (0, chai_1.expect)(count).to.equal(2);
            yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
        });
    });
    it('Should have appropriate sessions', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].live.waitUntilWaiting({ videoId: videoUUID });
            const { data, total } = yield servers[0].live.listSessions({ videoId: videoUUID });
            (0, chai_1.expect)(total).to.equal(2);
            (0, chai_1.expect)(data).to.have.lengthOf(2);
            for (const session of data) {
                (0, chai_1.expect)(session.startDate).to.exist;
                (0, chai_1.expect)(session.endDate).to.exist;
                (0, chai_1.expect)(session.error).to.not.exist;
            }
        });
    });
    it('Should remove the live and have cleaned up the directory', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].videos.remove({ id: videoUUID });
            yield (0, server_commands_1.waitJobs)(servers);
            yield (0, shared_1.checkLiveCleanup)({ server: servers[0], permanent: true, videoUUID });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=live-permanent.js.map