"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test user videos', function () {
    let server;
    let videoId;
    let videoId2;
    let token;
    let anotherUserToken;
    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]);
            yield (0, server_commands_1.setDefaultChannelAvatar)([server]);
            yield (0, server_commands_1.setDefaultAccountAvatar)([server]);
            yield server.videos.quickUpload({ name: 'root video' });
            yield server.videos.quickUpload({ name: 'root video 2' });
            token = yield server.users.generateUserAndToken('user');
            anotherUserToken = yield server.users.generateUserAndToken('user2');
        });
    });
    describe('List my videos', function () {
        it('Should list my videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { data, total } = yield server.videos.listMyVideos();
                (0, chai_1.expect)(total).to.equal(2);
                (0, chai_1.expect)(data).to.have.lengthOf(2);
            });
        });
    });
    describe('Upload', function () {
        it('Should upload the video with the correct token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.videos.upload({ token });
                const { data } = yield server.videos.list();
                const video = data[0];
                (0, chai_1.expect)(video.account.name).to.equal('user');
                videoId = video.id;
            });
        });
        it('Should upload the video again with the correct token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { id } = yield server.videos.upload({ token });
                videoId2 = id;
            });
        });
    });
    describe('Ratings', function () {
        it('Should retrieve a video rating', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.videos.rate({ id: videoId, token, rating: 'like' });
                const rating = yield server.users.getMyRating({ token, videoId });
                (0, chai_1.expect)(rating.videoId).to.equal(videoId);
                (0, chai_1.expect)(rating.rating).to.equal('like');
            });
        });
        it('Should retrieve ratings list', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.videos.rate({ id: videoId, token, rating: 'like' });
                const body = yield server.accounts.listRatings({ accountName: 'user', token });
                (0, chai_1.expect)(body.total).to.equal(1);
                (0, chai_1.expect)(body.data[0].video.id).to.equal(videoId);
                (0, chai_1.expect)(body.data[0].rating).to.equal('like');
            });
        });
        it('Should retrieve ratings list by rating type', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                {
                    const body = yield server.accounts.listRatings({ accountName: 'user', token, rating: 'like' });
                    (0, chai_1.expect)(body.data.length).to.equal(1);
                }
                {
                    const body = yield server.accounts.listRatings({ accountName: 'user', token, rating: 'dislike' });
                    (0, chai_1.expect)(body.data.length).to.equal(0);
                }
            });
        });
    });
    describe('Remove video', function () {
        it('Should not be able to remove the video with an incorrect token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: 401 });
            });
        });
        it('Should not be able to remove the video with the token of another account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.videos.remove({ token: anotherUserToken, id: videoId, expectedStatus: 403 });
            });
        });
        it('Should be able to remove the video with the correct token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.videos.remove({ token, id: videoId });
                yield server.videos.remove({ token, id: videoId2 });
            });
        });
    });
    describe('My videos & quotas', function () {
        it('Should be able to upload a video with a user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(10000);
                const attributes = {
                    name: 'super user video',
                    fixture: 'video_short.webm'
                };
                yield server.videos.upload({ token, attributes });
                yield server.channels.create({ token, attributes: { name: 'other_channel' } });
            });
        });
        it('Should have video quota updated', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const quota = yield server.users.getMyQuotaUsed({ token });
                (0, chai_1.expect)(quota.videoQuotaUsed).to.equal(218910);
                (0, chai_1.expect)(quota.videoQuotaUsedDaily).to.equal(218910);
                const { data } = yield server.users.list();
                const tmpUser = data.find(u => u.username === 'user');
                (0, chai_1.expect)(tmpUser.videoQuotaUsed).to.equal(218910);
                (0, chai_1.expect)(tmpUser.videoQuotaUsedDaily).to.equal(218910);
            });
        });
        it('Should be able to list my videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { total, data } = yield server.videos.listMyVideos({ token });
                (0, chai_1.expect)(total).to.equal(1);
                (0, chai_1.expect)(data).to.have.lengthOf(1);
                const video = data[0];
                (0, chai_1.expect)(video.name).to.equal('super user video');
                (0, chai_1.expect)(video.thumbnailPath).to.not.be.null;
                (0, chai_1.expect)(video.previewPath).to.not.be.null;
            });
        });
        it('Should be able to filter by channel in my videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const myInfo = yield server.users.getMyInfo({ token });
                const mainChannel = myInfo.videoChannels.find(c => c.name !== 'other_channel');
                const otherChannel = myInfo.videoChannels.find(c => c.name === 'other_channel');
                {
                    const { total, data } = yield server.videos.listMyVideos({ token, channelId: mainChannel.id });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                    const video = data[0];
                    (0, chai_1.expect)(video.name).to.equal('super user video');
                    (0, chai_1.expect)(video.thumbnailPath).to.not.be.null;
                    (0, chai_1.expect)(video.previewPath).to.not.be.null;
                }
                {
                    const { total, data } = yield server.videos.listMyVideos({ token, channelId: otherChannel.id });
                    (0, chai_1.expect)(total).to.equal(0);
                    (0, chai_1.expect)(data).to.have.lengthOf(0);
                }
            });
        });
        it('Should be able to search in my videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                {
                    const { total, data } = yield server.videos.listMyVideos({ token, sort: '-createdAt', search: 'user video' });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                }
                {
                    const { total, data } = yield server.videos.listMyVideos({ token, sort: '-createdAt', search: 'toto' });
                    (0, chai_1.expect)(total).to.equal(0);
                    (0, chai_1.expect)(data).to.have.lengthOf(0);
                }
            });
        });
        it('Should disable webtorrent, enable HLS, and update my quota', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(160000);
                {
                    const config = yield server.config.getCustomConfig();
                    config.transcoding.webtorrent.enabled = false;
                    config.transcoding.hls.enabled = true;
                    config.transcoding.enabled = true;
                    yield server.config.updateCustomSubConfig({ newConfig: config });
                }
                {
                    const attributes = {
                        name: 'super user video 2',
                        fixture: 'video_short.webm'
                    };
                    yield server.videos.upload({ token, attributes });
                    yield (0, server_commands_1.waitJobs)([server]);
                }
                {
                    const data = yield server.users.getMyQuotaUsed({ token });
                    (0, chai_1.expect)(data.videoQuotaUsed).to.be.greaterThan(220000);
                    (0, chai_1.expect)(data.videoQuotaUsedDaily).to.be.greaterThan(220000);
                }
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=user-videos.js.map