"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const chai = (0, tslib_1.__importStar)(require("chai"));
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
const expect = chai.expect;
describe('Save replay setting', function () {
    let servers = [];
    let liveVideoUUID;
    let ffmpegCommand;
    function createLiveWrapper(saveReplay) {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            if (liveVideoUUID) {
                try {
                    yield servers[0].videos.remove({ id: liveVideoUUID });
                    yield (0, server_commands_1.waitJobs)(servers);
                }
                catch (_a) { }
            }
            const attributes = {
                channelId: servers[0].store.channel.id,
                privacy: 1,
                name: 'my super live',
                saveReplay
            };
            const { uuid } = yield servers[0].live.create({ fields: attributes });
            return uuid;
        });
    }
    function checkVideosExist(videoId, existsInList, expectedStatus) {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            for (const server of servers) {
                const length = existsInList ? 1 : 0;
                const { data, total } = yield server.videos.list();
                expect(data).to.have.lengthOf(length);
                expect(total).to.equal(length);
                if (expectedStatus) {
                    yield server.videos.get({ id: videoId, expectedStatus });
                }
            }
        });
    }
    function checkVideoState(videoId, state) {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            for (const server of servers) {
                const video = yield server.videos.get({ id: videoId });
                expect(video.state.id).to.equal(state);
            }
        });
    }
    before(function () {
        return (0, 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: false,
                            resolutions: server_commands_1.ConfigCommand.getCustomConfigResolutions(true)
                        }
                    }
                }
            });
        });
    });
    describe('With save replay disabled', function () {
        before(function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(10000);
            });
        });
        it('Should correctly create and federate the "waiting for stream" live', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(20000);
                liveVideoUUID = yield createLiveWrapper(false);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, false, 200);
                yield checkVideoState(liveVideoUUID, 4);
            });
        });
        it('Should correctly have updated the live and federated it when streaming in the live', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(30000);
                ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID });
                yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, liveVideoUUID);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, true, 200);
                yield checkVideoState(liveVideoUUID, 1);
            });
        });
        it('Should correctly delete the video files after the stream ended', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(40000);
                yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                for (const server of servers) {
                    yield server.live.waitUntilEnded({ videoId: liveVideoUUID });
                }
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, false, 200);
                yield checkVideoState(liveVideoUUID, 5);
                yield (0, shared_1.checkLiveCleanupAfterSave)(servers[0], liveVideoUUID, []);
            });
        });
        it('Should correctly terminate the stream on blacklist and delete the live', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(40000);
                liveVideoUUID = yield createLiveWrapper(false);
                ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID });
                yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, liveVideoUUID);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, true, 200);
                yield Promise.all([
                    servers[0].blacklist.add({ videoId: liveVideoUUID, reason: 'bad live', unfederate: true }),
                    (0, server_commands_1.testFfmpegStreamError)(ffmpegCommand, true)
                ]);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, false);
                yield servers[0].videos.get({ id: liveVideoUUID, expectedStatus: 401 });
                yield servers[1].videos.get({ id: liveVideoUUID, expectedStatus: 404 });
                yield (0, core_utils_1.wait)(5000);
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkLiveCleanupAfterSave)(servers[0], liveVideoUUID, []);
            });
        });
        it('Should correctly terminate the stream on delete and delete the video', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(40000);
                liveVideoUUID = yield createLiveWrapper(false);
                ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID });
                yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, liveVideoUUID);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, true, 200);
                yield Promise.all([
                    (0, server_commands_1.testFfmpegStreamError)(ffmpegCommand, true),
                    servers[0].videos.remove({ id: liveVideoUUID })
                ]);
                yield (0, core_utils_1.wait)(5000);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, false, 404);
                yield (0, shared_1.checkLiveCleanupAfterSave)(servers[0], liveVideoUUID, []);
            });
        });
    });
    describe('With save replay enabled', function () {
        it('Should correctly create and federate the "waiting for stream" live', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(20000);
                liveVideoUUID = yield createLiveWrapper(true);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, false, 200);
                yield checkVideoState(liveVideoUUID, 4);
            });
        });
        it('Should correctly have updated the live and federated it when streaming in the live', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(20000);
                ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID });
                yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, liveVideoUUID);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, true, 200);
                yield checkVideoState(liveVideoUUID, 1);
            });
        });
        it('Should correctly have saved the live and federated it after the streaming', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                yield (0, server_commands_1.waitUntilLiveSavedOnAllServers)(servers, liveVideoUUID);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, true, 200);
                yield checkVideoState(liveVideoUUID, 1);
            });
        });
        it('Should update the saved live and correctly federate the updated attributes', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[0].videos.update({ id: liveVideoUUID, attributes: { name: 'video updated' } });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: liveVideoUUID });
                    expect(video.name).to.equal('video updated');
                    expect(video.isLive).to.be.false;
                }
            });
        });
        it('Should have cleaned up the live files', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                yield (0, shared_1.checkLiveCleanupAfterSave)(servers[0], liveVideoUUID, [720]);
            });
        });
        it('Should correctly terminate the stream on blacklist and blacklist the saved replay video', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(40000);
                liveVideoUUID = yield createLiveWrapper(true);
                ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID });
                yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, liveVideoUUID);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, true, 200);
                yield Promise.all([
                    servers[0].blacklist.add({ videoId: liveVideoUUID, reason: 'bad live', unfederate: true }),
                    (0, server_commands_1.testFfmpegStreamError)(ffmpegCommand, true)
                ]);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, false);
                yield servers[0].videos.get({ id: liveVideoUUID, expectedStatus: 401 });
                yield servers[1].videos.get({ id: liveVideoUUID, expectedStatus: 404 });
                yield (0, core_utils_1.wait)(5000);
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.checkLiveCleanupAfterSave)(servers[0], liveVideoUUID, [720]);
            });
        });
        it('Should correctly terminate the stream on delete and delete the video', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                this.timeout(40000);
                liveVideoUUID = yield createLiveWrapper(true);
                ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID });
                yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, liveVideoUUID);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, true, 200);
                yield Promise.all([
                    servers[0].videos.remove({ id: liveVideoUUID }),
                    (0, server_commands_1.testFfmpegStreamError)(ffmpegCommand, true)
                ]);
                yield (0, core_utils_1.wait)(5000);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkVideosExist(liveVideoUUID, false, 404);
                yield (0, shared_1.checkLiveCleanupAfterSave)(servers[0], liveVideoUUID, []);
            });
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=live-save-replay.js.map