"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");
function createLive(server, permanent) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const attributes = {
            channelId: server.store.channel.id,
            privacy: 1,
            name: 'my super live',
            saveReplay: true,
            permanentLive: permanent
        };
        const { uuid } = yield server.live.create({ fields: attributes });
        return uuid;
    });
}
function checkFilesExist(servers, videoUUID, numberOfFiles) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const server of servers) {
            const video = yield server.videos.get({ id: videoUUID });
            (0, chai_1.expect)(video.files).to.have.lengthOf(0);
            (0, chai_1.expect)(video.streamingPlaylists).to.have.lengthOf(1);
            const files = video.streamingPlaylists[0].files;
            (0, chai_1.expect)(files).to.have.lengthOf(numberOfFiles);
            for (const file of files) {
                (0, shared_1.expectStartWith)(file.fileUrl, server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl());
                yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, expectedStatus: 200 });
            }
        }
    });
}
function checkFilesCleanup(server, videoUUID, resolutions) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const resolutionFiles = resolutions.map((_value, i) => `${i}.m3u8`);
        for (const playlistName of ['master.m3u8'].concat(resolutionFiles)) {
            yield server.live.getPlaylistFile({
                videoUUID,
                playlistName,
                expectedStatus: 404,
                objectStorage: true
            });
        }
        yield server.live.getSegmentFile({
            videoUUID,
            playlistNumber: 0,
            segment: 0,
            objectStorage: true,
            expectedStatus: 404
        });
    });
}
describe('Object storage for lives', function () {
    if ((0, core_utils_1.areMockObjectStorageTestsDisabled)())
        return;
    let servers;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            yield server_commands_1.ObjectStorageCommand.prepareDefaultMockBuckets();
            servers = yield (0, server_commands_1.createMultipleServers)(2, server_commands_1.ObjectStorageCommand.getDefaultMockConfig());
            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.enableTranscoding();
        });
    });
    describe('Without live transcoding', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let videoUUID;
            before(function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].config.enableLive({ transcoding: false });
                    videoUUID = yield createLive(servers[0], false);
                });
            });
            it('Should create a live and publish it on object storage', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(220000);
                    const ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID });
                    yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, videoUUID);
                    yield (0, shared_1.testVideoResolutions)({
                        originServer: servers[0],
                        servers,
                        liveVideoId: videoUUID,
                        resolutions: [720],
                        transcoded: false,
                        objectStorage: true
                    });
                    yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                });
            });
            it('Should have saved the replay on object storage', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(220000);
                    yield (0, server_commands_1.waitUntilLiveReplacedByReplayOnAllServers)(servers, videoUUID);
                    yield (0, server_commands_1.waitJobs)(servers);
                    yield checkFilesExist(servers, videoUUID, 1);
                });
            });
            it('Should have cleaned up live files from object storage', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield checkFilesCleanup(servers[0], videoUUID, [720]);
                });
            });
        });
    });
    describe('With live transcoding', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const resolutions = [720, 480, 360, 240, 144];
            before(function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].config.enableLive({ transcoding: true });
                });
            });
            describe('Normal replay', function () {
                let videoUUIDNonPermanent;
                before(function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        videoUUIDNonPermanent = yield createLive(servers[0], false);
                    });
                });
                it('Should create a live and publish it on object storage', function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        this.timeout(240000);
                        const ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUIDNonPermanent });
                        yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, videoUUIDNonPermanent);
                        yield (0, shared_1.testVideoResolutions)({
                            originServer: servers[0],
                            servers,
                            liveVideoId: videoUUIDNonPermanent,
                            resolutions,
                            transcoded: true,
                            objectStorage: true
                        });
                        yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                    });
                });
                it('Should have saved the replay on object storage', function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        this.timeout(220000);
                        yield (0, server_commands_1.waitUntilLiveReplacedByReplayOnAllServers)(servers, videoUUIDNonPermanent);
                        yield (0, server_commands_1.waitJobs)(servers);
                        yield checkFilesExist(servers, videoUUIDNonPermanent, 5);
                    });
                });
                it('Should have cleaned up live files from object storage', function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        yield checkFilesCleanup(servers[0], videoUUIDNonPermanent, resolutions);
                    });
                });
            });
            describe('Permanent replay', function () {
                let videoUUIDPermanent;
                before(function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        videoUUIDPermanent = yield createLive(servers[0], true);
                    });
                });
                it('Should create a live and publish it on object storage', function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        this.timeout(240000);
                        const ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUIDPermanent });
                        yield (0, server_commands_1.waitUntilLivePublishedOnAllServers)(servers, videoUUIDPermanent);
                        yield (0, shared_1.testVideoResolutions)({
                            originServer: servers[0],
                            servers,
                            liveVideoId: videoUUIDPermanent,
                            resolutions,
                            transcoded: true,
                            objectStorage: true
                        });
                        yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                    });
                });
                it('Should have saved the replay on object storage', function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        this.timeout(220000);
                        yield (0, server_commands_1.waitUntilLiveWaitingOnAllServers)(servers, videoUUIDPermanent);
                        yield (0, server_commands_1.waitJobs)(servers);
                        const videoLiveDetails = yield servers[0].videos.get({ id: videoUUIDPermanent });
                        const replay = yield (0, server_commands_1.findExternalSavedVideo)(servers[0], videoLiveDetails);
                        yield checkFilesExist(servers, replay.uuid, 5);
                    });
                });
                it('Should have cleaned up live files from object storage', function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        yield checkFilesCleanup(servers[0], videoUUIDPermanent, resolutions);
                    });
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.killallServers)(servers);
        });
    });
});
//# sourceMappingURL=live.js.map