"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('Test video views/viewers counters', function () {
    let servers;
    function checkCounter(field, id, expected) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (const server of servers) {
                const video = yield server.videos.get({ id });
                const messageSuffix = video.isLive
                    ? 'live video'
                    : 'vod video';
                (0, chai_1.expect)(video[field]).to.equal(expected, `${field} not valid on server ${server.serverNumber} for ${messageSuffix} ${video.uuid}`);
            }
        });
    }
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, shared_1.prepareViewsServers)();
        });
    });
    describe('Test views counter on VOD', function () {
        let videoUUID;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video' });
                videoUUID = uuid;
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should not view a video if watch time is below the threshold', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [1, 2] });
                yield (0, shared_1.processViewsBuffer)(servers);
                yield checkCounter('views', videoUUID, 0);
            });
        });
        it('Should view a video if watch time is above the threshold', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [1, 4] });
                yield (0, shared_1.processViewsBuffer)(servers);
                yield checkCounter('views', videoUUID, 1);
            });
        });
        it('Should not view again this video with the same IP', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].views.simulateViewer({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1', currentTimes: [1, 4] });
                yield servers[0].views.simulateViewer({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1', currentTimes: [1, 4] });
                yield (0, shared_1.processViewsBuffer)(servers);
                yield checkCounter('views', videoUUID, 2);
            });
        });
        it('Should view the video from server 2 and send the event', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[1].views.simulateViewer({ id: videoUUID, currentTimes: [1, 4] });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.processViewsBuffer)(servers);
                yield checkCounter('views', videoUUID, 3);
            });
        });
    });
    describe('Test views and viewers counters on live and VOD', function () {
        let liveVideoId;
        let vodVideoId;
        let command;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                ({ vodVideoId, liveVideoId, ffmpegCommand: command } = yield (0, shared_1.prepareViewsVideos)({ servers, live: true, vod: true }));
            });
        });
        it('Should display no views and viewers', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield checkCounter('views', liveVideoId, 0);
                yield checkCounter('viewers', liveVideoId, 0);
                yield checkCounter('views', vodVideoId, 0);
                yield checkCounter('viewers', vodVideoId, 0);
            });
        });
        it('Should view twice and display 1 view/viewer', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [0, 35] });
                yield servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [0, 35] });
                yield servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 5] });
                yield servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 5] });
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkCounter('viewers', liveVideoId, 1);
                yield checkCounter('viewers', vodVideoId, 1);
                yield (0, shared_1.processViewsBuffer)(servers);
                yield checkCounter('views', liveVideoId, 1);
                yield checkCounter('views', vodVideoId, 1);
            });
        });
        it('Should wait and display 0 viewers but still have 1 view', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield (0, core_utils_1.wait)(12000);
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkCounter('views', liveVideoId, 1);
                yield checkCounter('viewers', liveVideoId, 0);
                yield checkCounter('views', vodVideoId, 1);
                yield checkCounter('viewers', vodVideoId, 0);
            });
        });
        it('Should view on a remote and on local and display 2 viewers and 3 views', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 5] });
                yield servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 5] });
                yield servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 5] });
                yield servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [0, 35] });
                yield servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [0, 35] });
                yield servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [0, 35] });
                yield (0, server_commands_1.waitJobs)(servers);
                yield checkCounter('viewers', liveVideoId, 2);
                yield checkCounter('viewers', vodVideoId, 2);
                yield (0, shared_1.processViewsBuffer)(servers);
                yield checkCounter('views', liveVideoId, 3);
                yield checkCounter('views', vodVideoId, 3);
            });
        });
        after(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.stopFfmpeg)(command);
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=video-views-counter.js.map