"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");
const shared_1 = require("../../shared");
describe('Test live constraints', function () {
    let servers = [];
    let userId;
    let userAccessToken;
    let userChannelId;
    function createLiveWrapper(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { replay, permanent } = options;
            const liveAttributes = {
                name: 'user live',
                channelId: userChannelId,
                privacy: 1,
                saveReplay: replay,
                permanentLive: permanent
            };
            const { uuid } = yield servers[0].live.create({ token: userAccessToken, fields: liveAttributes });
            return uuid;
        });
    }
    function checkSaveReplay(videoId, resolutions = [720]) {
        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.isLive).to.be.false;
                (0, chai_1.expect)(video.duration).to.be.greaterThan(0);
            }
            yield (0, shared_1.checkLiveCleanup)({ server: servers[0], permanent: false, videoUUID: videoId, savedResolutions: resolutions });
        });
    }
    function updateQuota(options) {
        return servers[0].users.update({
            userId,
            videoQuota: options.total,
            videoQuotaDaily: options.daily
        });
    }
    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 servers[0].config.updateCustomSubConfig({
                newConfig: {
                    live: {
                        enabled: true,
                        allowReplay: true,
                        transcoding: {
                            enabled: false
                        }
                    }
                }
            });
            {
                const res = yield servers[0].users.generate('user1');
                userId = res.userId;
                userChannelId = res.userChannelId;
                userAccessToken = res.token;
                yield updateQuota({ total: 1, daily: -1 });
            }
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        });
    });
    it('Should not have size limit if save replay is disabled', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            const userVideoLiveoId = yield createLiveWrapper({ replay: false, permanent: false });
            yield servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false });
        });
    });
    it('Should have size limit depending on user global quota if save replay is enabled on non permanent live', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield (0, core_utils_1.wait)(5000);
            const userVideoLiveoId = yield createLiveWrapper({ replay: true, permanent: false });
            yield servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true });
            yield (0, server_commands_1.waitUntilLiveReplacedByReplayOnAllServers)(servers, userVideoLiveoId);
            yield (0, server_commands_1.waitJobs)(servers);
            yield checkSaveReplay(userVideoLiveoId);
            const session = yield servers[0].live.getReplaySession({ videoId: userVideoLiveoId });
            (0, chai_1.expect)(session.error).to.equal(3);
        });
    });
    it('Should have size limit depending on user global quota if save replay is enabled on a permanent live', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield (0, core_utils_1.wait)(5000);
            const userVideoLiveoId = yield createLiveWrapper({ replay: true, permanent: true });
            yield servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true });
            yield (0, server_commands_1.waitJobs)(servers);
            yield (0, server_commands_1.waitUntilLiveWaitingOnAllServers)(servers, userVideoLiveoId);
            const session = yield servers[0].live.findLatestSession({ videoId: userVideoLiveoId });
            (0, chai_1.expect)(session.error).to.equal(3);
        });
    });
    it('Should have size limit depending on user daily quota if save replay is enabled', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield (0, core_utils_1.wait)(5000);
            yield updateQuota({ total: -1, daily: 1 });
            const userVideoLiveoId = yield createLiveWrapper({ replay: true, permanent: false });
            yield servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true });
            yield (0, server_commands_1.waitUntilLiveReplacedByReplayOnAllServers)(servers, userVideoLiveoId);
            yield (0, server_commands_1.waitJobs)(servers);
            yield checkSaveReplay(userVideoLiveoId);
            const session = yield servers[0].live.getReplaySession({ videoId: userVideoLiveoId });
            (0, chai_1.expect)(session.error).to.equal(3);
        });
    });
    it('Should succeed without quota limit', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield (0, core_utils_1.wait)(5000);
            yield updateQuota({ total: 10 * 1000 * 1000, daily: -1 });
            const userVideoLiveoId = yield createLiveWrapper({ replay: true, permanent: false });
            yield servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false });
        });
    });
    it('Should have the same quota in admin and as a user', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            const userVideoLiveoId = yield createLiveWrapper({ replay: true, permanent: false });
            const ffmpegCommand = yield servers[0].live.sendRTMPStreamInVideo({ token: userAccessToken, videoId: userVideoLiveoId });
            yield servers[0].live.waitUntilPublished({ videoId: userVideoLiveoId });
            yield (0, core_utils_1.wait)(3000);
            const quotaUser = yield servers[0].users.getMyQuotaUsed({ token: userAccessToken });
            const { data } = yield servers[0].users.list();
            const quotaAdmin = data.find(u => u.username === 'user1');
            (0, chai_1.expect)(quotaUser.videoQuotaUsed).to.equal(quotaAdmin.videoQuotaUsed);
            (0, chai_1.expect)(quotaUser.videoQuotaUsedDaily).to.equal(quotaAdmin.videoQuotaUsedDaily);
            (0, chai_1.expect)(quotaUser.videoQuotaUsed).to.be.above(10);
            (0, chai_1.expect)(quotaUser.videoQuotaUsedDaily).to.be.above(10);
            yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
        });
    });
    it('Should have max duration limit', 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(true)
                        }
                    }
                }
            });
            const userVideoLiveoId = yield createLiveWrapper({ replay: true, permanent: false });
            yield servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true });
            yield (0, server_commands_1.waitUntilLiveReplacedByReplayOnAllServers)(servers, userVideoLiveoId);
            yield (0, server_commands_1.waitJobs)(servers);
            yield checkSaveReplay(userVideoLiveoId, [720, 480, 360, 240, 144]);
            const session = yield servers[0].live.getReplaySession({ videoId: userVideoLiveoId });
            (0, chai_1.expect)(session.error).to.equal(2);
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=live-constraints.js.map