"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('Test videos history', function () {
    let server = null;
    let video1Id;
    let video1UUID;
    let video2UUID;
    let video3UUID;
    let video3WatchedDate;
    let userAccessToken;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            server = yield (0, server_commands_1.createSingleServer)(1);
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            const fixture = 'video_short1.webm';
            {
                const { id, uuid } = yield server.videos.upload({ attributes: { name: 'video 1', fixture } });
                video1UUID = uuid;
                video1Id = id;
            }
            {
                const { uuid } = yield server.videos.upload({ attributes: { name: 'video 2', fixture } });
                video2UUID = uuid;
            }
            {
                const { uuid } = yield server.videos.upload({ attributes: { name: 'video 3', fixture } });
                video3UUID = uuid;
            }
            userAccessToken = yield server.users.generateUserAndToken('user_1');
        });
    });
    it('Should get videos, without watching history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { data } = yield server.videos.listWithToken();
            for (const video of data) {
                const videoDetails = yield server.videos.getWithToken({ id: video.id });
                (0, chai_1.expect)(video.userHistory).to.be.undefined;
                (0, chai_1.expect)(videoDetails.userHistory).to.be.undefined;
            }
        });
    });
    it('Should watch the first and second video', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 });
            yield server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 });
        });
    });
    it('Should return the correct history when listing, searching and getting videos', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const videosOfVideos = [];
            {
                const { data } = yield server.videos.listWithToken();
                videosOfVideos.push(data);
            }
            {
                const body = yield server.search.searchVideos({ token: server.accessToken, search: 'video' });
                videosOfVideos.push(body.data);
            }
            for (const videos of videosOfVideos) {
                const video1 = videos.find(v => v.uuid === video1UUID);
                const video2 = videos.find(v => v.uuid === video2UUID);
                const video3 = videos.find(v => v.uuid === video3UUID);
                (0, chai_1.expect)(video1.userHistory).to.not.be.undefined;
                (0, chai_1.expect)(video1.userHistory.currentTime).to.equal(3);
                (0, chai_1.expect)(video2.userHistory).to.not.be.undefined;
                (0, chai_1.expect)(video2.userHistory.currentTime).to.equal(8);
                (0, chai_1.expect)(video3.userHistory).to.be.undefined;
            }
            {
                const videoDetails = yield server.videos.getWithToken({ id: video1UUID });
                (0, chai_1.expect)(videoDetails.userHistory).to.not.be.undefined;
                (0, chai_1.expect)(videoDetails.userHistory.currentTime).to.equal(3);
            }
            {
                const videoDetails = yield server.videos.getWithToken({ id: video2UUID });
                (0, chai_1.expect)(videoDetails.userHistory).to.not.be.undefined;
                (0, chai_1.expect)(videoDetails.userHistory.currentTime).to.equal(8);
            }
            {
                const videoDetails = yield server.videos.getWithToken({ id: video3UUID });
                (0, chai_1.expect)(videoDetails.userHistory).to.be.undefined;
            }
        });
    });
    it('Should have these videos when listing my history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            video3WatchedDate = new Date();
            yield server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 });
            const body = yield server.history.list();
            (0, chai_1.expect)(body.total).to.equal(3);
            const videos = body.data;
            (0, chai_1.expect)(videos[0].name).to.equal('video 3');
            (0, chai_1.expect)(videos[1].name).to.equal('video 1');
            (0, chai_1.expect)(videos[2].name).to.equal('video 2');
        });
    });
    it('Should not have videos history on another user', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const body = yield server.history.list({ token: userAccessToken });
            (0, chai_1.expect)(body.total).to.equal(0);
            (0, chai_1.expect)(body.data).to.have.lengthOf(0);
        });
    });
    it('Should be able to search through videos in my history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const body = yield server.history.list({ search: '2' });
            (0, chai_1.expect)(body.total).to.equal(1);
            const videos = body.data;
            (0, chai_1.expect)(videos[0].name).to.equal('video 2');
        });
    });
    it('Should clear my history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() });
        });
    });
    it('Should have my history cleared', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const body = yield server.history.list();
            (0, chai_1.expect)(body.total).to.equal(1);
            const videos = body.data;
            (0, chai_1.expect)(videos[0].name).to.equal('video 3');
        });
    });
    it('Should disable videos history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.users.updateMe({
                videosHistoryEnabled: false
            });
            yield server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 });
            const { data } = yield server.history.list();
            (0, chai_1.expect)(data[0].name).to.not.equal('video 2');
        });
    });
    it('Should re-enable videos history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.users.updateMe({
                videosHistoryEnabled: true
            });
            yield server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 });
            const { data } = yield server.history.list();
            (0, chai_1.expect)(data[0].name).to.equal('video 2');
        });
    });
    it('Should not clean old history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(50000);
            yield (0, server_commands_1.killallServers)([server]);
            yield server.run({ history: { videos: { max_age: '10 days' } } });
            yield (0, core_utils_1.wait)(6000);
            const body = yield server.history.list();
            (0, chai_1.expect)(body.total).to.equal(2);
        });
    });
    it('Should clean old history', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(50000);
            yield (0, server_commands_1.killallServers)([server]);
            yield server.run({ history: { videos: { max_age: '5 seconds' } } });
            yield (0, core_utils_1.wait)(6000);
            const body = yield server.history.list();
            (0, chai_1.expect)(body.total).to.equal(0);
        });
    });
    it('Should delete a specific history element', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            {
                yield server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 });
                yield server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 });
            }
            {
                const body = yield server.history.list();
                (0, chai_1.expect)(body.total).to.equal(2);
            }
            {
                yield server.history.removeElement({ videoId: video1Id });
                const body = yield server.history.list();
                (0, chai_1.expect)(body.total).to.equal(1);
                (0, chai_1.expect)(body.data[0].uuid).to.equal(video2UUID);
            }
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=videos-history.js.map