"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 videos files', function () {
    let servers;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(150000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            yield servers[0].config.enableTranscoding(true, true);
        });
    });
    describe('When deleting all files', function () {
        let validId1;
        let validId2;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(360000);
                {
                    const { uuid } = yield servers[0].videos.quickUpload({ name: 'video 1' });
                    validId1 = uuid;
                }
                {
                    const { uuid } = yield servers[0].videos.quickUpload({ name: 'video 2' });
                    validId2 = uuid;
                }
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should delete webtorrent files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: validId1 });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: validId1 });
                    (0, chai_1.expect)(video.files).to.have.lengthOf(0);
                    (0, chai_1.expect)(video.streamingPlaylists).to.have.lengthOf(1);
                }
            });
        });
        it('Should delete HLS files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[0].videos.removeHLSPlaylist({ videoId: validId2 });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: validId2 });
                    (0, chai_1.expect)(video.files).to.have.length.above(0);
                    (0, chai_1.expect)(video.streamingPlaylists).to.have.lengthOf(0);
                }
            });
        });
    });
    describe('When deleting a specific file', function () {
        let webtorrentId;
        let hlsId;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                {
                    const { uuid } = yield servers[0].videos.quickUpload({ name: 'webtorrent' });
                    webtorrentId = uuid;
                }
                {
                    const { uuid } = yield servers[0].videos.quickUpload({ name: 'hls' });
                    hlsId = uuid;
                }
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Shoulde delete a webtorrent file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                const video = yield servers[0].videos.get({ id: webtorrentId });
                const files = video.files;
                yield servers[0].videos.removeWebTorrentFile({ videoId: webtorrentId, fileId: files[0].id });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: webtorrentId });
                    (0, chai_1.expect)(video.files).to.have.lengthOf(files.length - 1);
                    (0, chai_1.expect)(video.files.find(f => f.id === files[0].id)).to.not.exist;
                }
            });
        });
        it('Should delete all webtorrent files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                const video = yield servers[0].videos.get({ id: webtorrentId });
                const files = video.files;
                for (const file of files) {
                    yield servers[0].videos.removeWebTorrentFile({ videoId: webtorrentId, fileId: file.id });
                }
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: webtorrentId });
                    (0, chai_1.expect)(video.files).to.have.lengthOf(0);
                }
            });
        });
        it('Should delete a hls file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                const video = yield servers[0].videos.get({ id: hlsId });
                const files = video.streamingPlaylists[0].files;
                const toDelete = files[0];
                yield servers[0].videos.removeHLSFile({ videoId: hlsId, fileId: toDelete.id });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: hlsId });
                    (0, chai_1.expect)(video.streamingPlaylists[0].files).to.have.lengthOf(files.length - 1);
                    (0, chai_1.expect)(video.streamingPlaylists[0].files.find(f => f.id === toDelete.id)).to.not.exist;
                    const { text } = yield (0, server_commands_1.makeRawRequest)({ url: video.streamingPlaylists[0].playlistUrl, expectedStatus: 200 });
                    (0, chai_1.expect)(text.includes(`-${toDelete.resolution.id}.m3u8`)).to.be.false;
                    (0, chai_1.expect)(text.includes(`-${video.streamingPlaylists[0].files[0].resolution.id}.m3u8`)).to.be.true;
                }
            });
        });
        it('Should delete all hls files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                const video = yield servers[0].videos.get({ id: hlsId });
                const files = video.streamingPlaylists[0].files;
                for (const file of files) {
                    yield servers[0].videos.removeHLSFile({ videoId: hlsId, fileId: file.id });
                }
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: hlsId });
                    (0, chai_1.expect)(video.streamingPlaylists).to.have.lengthOf(0);
                }
            });
        });
        it('Should not delete last file of a video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                const webtorrentOnly = yield servers[0].videos.get({ id: hlsId });
                const hlsOnly = yield servers[0].videos.get({ id: webtorrentId });
                for (let i = 0; i < 4; i++) {
                    yield servers[0].videos.removeWebTorrentFile({ videoId: webtorrentOnly.id, fileId: webtorrentOnly.files[i].id });
                    yield servers[0].videos.removeHLSFile({ videoId: hlsOnly.id, fileId: hlsOnly.streamingPlaylists[0].files[i].id });
                }
                const expectedStatus = 400;
                yield servers[0].videos.removeWebTorrentFile({ videoId: webtorrentOnly.id, fileId: webtorrentOnly.files[4].id, expectedStatus });
                yield servers[0].videos.removeHLSFile({ videoId: hlsOnly.id, fileId: hlsOnly.streamingPlaylists[0].files[4].id, expectedStatus });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=video-files.js.map