"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Fast restream in live', function () {
    let server;
    function createLiveWrapper(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const attributes = {
                channelId: server.store.channel.id,
                privacy: 1,
                name: 'my super live',
                saveReplay: options.replay,
                permanentLive: options.permanent
            };
            const { uuid } = yield server.live.create({ fields: attributes });
            return uuid;
        });
    }
    function fastRestreamWrapper({ replay }) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const liveVideoUUID = yield createLiveWrapper({ permanent: true, replay });
            yield (0, server_commands_1.waitJobs)([server]);
            const rtmpOptions = {
                videoId: liveVideoUUID,
                copyCodecs: true,
                fixtureName: 'video_short.mp4'
            };
            let ffmpegCommand = yield server.live.sendRTMPStreamInVideo(rtmpOptions);
            yield server.live.waitUntilPublished({ videoId: liveVideoUUID });
            const video = yield server.videos.get({ id: liveVideoUUID });
            const session1PlaylistId = video.streamingPlaylists[0].id;
            yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
            yield server.live.waitUntilWaiting({ videoId: liveVideoUUID });
            ffmpegCommand = yield server.live.sendRTMPStreamInVideo(rtmpOptions);
            let hasNewPlaylist = false;
            do {
                const video = yield server.videos.get({ id: liveVideoUUID });
                hasNewPlaylist = video.streamingPlaylists.length === 1 && video.streamingPlaylists[0].id !== session1PlaylistId;
                yield (0, core_utils_1.wait)(100);
            } while (!hasNewPlaylist);
            yield server.live.waitUntilSegmentGeneration({
                server,
                videoUUID: liveVideoUUID,
                segment: 1,
                playlistNumber: 0,
                objectStorage: false
            });
            return { ffmpegCommand, liveVideoUUID };
        });
    }
    function ensureLastLiveWorks(liveId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (let i = 0; i < 100; i++) {
                const video = yield server.videos.get({ id: liveId });
                (0, chai_1.expect)(video.streamingPlaylists).to.have.lengthOf(1);
                yield server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 });
                yield (0, server_commands_1.makeRawRequest)({ url: video.streamingPlaylists[0].playlistUrl, expectedStatus: 200 });
                yield (0, server_commands_1.makeRawRequest)({ url: video.streamingPlaylists[0].segmentsSha256Url, expectedStatus: 200 });
                yield (0, core_utils_1.wait)(100);
            }
        });
    }
    function runTest(replay) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { ffmpegCommand, liveVideoUUID } = yield fastRestreamWrapper({ replay });
            yield ensureLastLiveWorks(liveVideoUUID);
            yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
            yield server.live.waitUntilWaiting({ videoId: liveVideoUUID });
            yield (0, server_commands_1.waitJobs)([server]);
            const { total, data: sessions } = yield server.live.listSessions({ videoId: liveVideoUUID });
            (0, chai_1.expect)(total).to.equal(2);
            (0, chai_1.expect)(sessions).to.have.lengthOf(2);
            for (const session of sessions) {
                (0, chai_1.expect)(session.error).to.be.null;
                if (replay) {
                    (0, chai_1.expect)(session.replayVideo).to.exist;
                    yield server.videos.get({ id: session.replayVideo.uuid });
                }
                else {
                    (0, chai_1.expect)(session.replayVideo).to.not.exist;
                }
            }
        });
    }
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' };
            server = yield (0, server_commands_1.createSingleServer)(1, {}, { env });
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            yield (0, server_commands_1.setDefaultVideoChannel)([server]);
            yield server.config.enableMinimumTranscoding(false, true);
            yield server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' });
        });
    });
    it('Should correctly fast reastream in a permanent live with and without save replay', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(480000);
            yield Promise.all([
                runTest(true),
                runTest(false)
            ]);
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=live-fast-restream.js.map