"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 studio', function () {
    let servers = [];
    let videoUUID;
    function checkDuration(server, duration) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const video = yield server.videos.get({ id: videoUUID });
            (0, chai_1.expect)(video.duration).to.be.approximately(duration, 1);
            for (const file of video.files) {
                const metadata = yield server.videos.getFileMetadata({ url: file.metadataUrl });
                for (const stream of metadata.streams) {
                    (0, chai_1.expect)(Math.round(stream.duration)).to.be.approximately(duration, 1);
                }
            }
        });
    }
    function renewVideo(fixture = 'video_short.webm') {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const video = yield servers[0].videos.quickUpload({ name: 'video', fixture });
            videoUUID = video.uuid;
            yield (0, server_commands_1.waitJobs)(servers);
        });
    }
    function createTasks(tasks) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield servers[0].videoStudio.createEditionTasks({ videoId: videoUUID, tasks });
            yield (0, server_commands_1.waitJobs)(servers);
        });
    }
    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 (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            yield servers[0].config.enableMinimumTranscoding();
            yield servers[0].config.enableStudio();
        });
    });
    describe('Cutting', function () {
        it('Should cut the beginning of the video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo();
                yield (0, server_commands_1.waitJobs)(servers);
                const beforeTasks = new Date();
                yield createTasks([
                    {
                        name: 'cut',
                        options: {
                            start: 2
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 3);
                    const video = yield server.videos.get({ id: videoUUID });
                    (0, chai_1.expect)(new Date(video.publishedAt)).to.be.below(beforeTasks);
                }
            });
        });
        it('Should cut the end of the video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo();
                yield createTasks([
                    {
                        name: 'cut',
                        options: {
                            end: 2
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 2);
                }
            });
        });
        it('Should cut start/end of the video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo('video_short1.webm');
                yield createTasks([
                    {
                        name: 'cut',
                        options: {
                            start: 2,
                            end: 6
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 4);
                }
            });
        });
    });
    describe('Intro/Outro', function () {
        it('Should add an intro', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo();
                yield createTasks([
                    {
                        name: 'add-intro',
                        options: {
                            file: 'video_short.webm'
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 10);
                }
            });
        });
        it('Should add an outro', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo();
                yield createTasks([
                    {
                        name: 'add-outro',
                        options: {
                            file: 'video_very_short_240p.mp4'
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 7);
                }
            });
        });
        it('Should add an intro/outro', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo();
                yield createTasks([
                    {
                        name: 'add-intro',
                        options: {
                            file: 'video_very_short_240p.mp4'
                        }
                    },
                    {
                        name: 'add-outro',
                        options: {
                            file: 'video_short2.webm'
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 12);
                }
            });
        });
        it('Should add an intro to a video without audio', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo('video_short_no_audio.mp4');
                yield createTasks([
                    {
                        name: 'add-intro',
                        options: {
                            file: 'video_very_short_240p.mp4'
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 7);
                }
            });
        });
        it('Should add an outro without audio to a video with audio', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo();
                yield createTasks([
                    {
                        name: 'add-outro',
                        options: {
                            file: 'video_short_no_audio.mp4'
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 10);
                }
            });
        });
        it('Should add an outro without audio to a video with audio', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo('video_short_no_audio.mp4');
                yield createTasks([
                    {
                        name: 'add-outro',
                        options: {
                            file: 'video_short_no_audio.mp4'
                        }
                    }
                ]);
                for (const server of servers) {
                    yield checkDuration(server, 10);
                }
            });
        });
    });
    describe('Watermark', function () {
        it('Should add a watermark to the video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield renewVideo();
                const video = yield servers[0].videos.get({ id: videoUUID });
                const oldFileUrls = (0, core_utils_1.getAllFiles)(video).map(f => f.fileUrl);
                yield createTasks([
                    {
                        name: 'add-watermark',
                        options: {
                            file: 'thumbnail.png'
                        }
                    }
                ]);
                for (const server of servers) {
                    const video = yield server.videos.get({ id: videoUUID });
                    const fileUrls = (0, core_utils_1.getAllFiles)(video).map(f => f.fileUrl);
                    for (const oldUrl of oldFileUrls) {
                        (0, chai_1.expect)(fileUrls).to.not.include(oldUrl);
                    }
                }
            });
        });
    });
    describe('Complex tasks', function () {
        it('Should run a complex task', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield renewVideo();
                yield createTasks(server_commands_1.VideoStudioCommand.getComplexTask());
                for (const server of servers) {
                    yield checkDuration(server, 9);
                }
            });
        });
    });
    describe('HLS only video edition', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].config.updateExistingSubConfig({
                    newConfig: {
                        transcoding: {
                            webtorrent: {
                                enabled: false
                            }
                        }
                    }
                });
            });
        });
        it('Should run a complex task on HLS only video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield renewVideo();
                yield createTasks(server_commands_1.VideoStudioCommand.getComplexTask());
                for (const server of servers) {
                    const video = yield server.videos.get({ id: videoUUID });
                    (0, chai_1.expect)(video.files).to.have.lengthOf(0);
                    yield checkDuration(server, 9);
                }
            });
        });
    });
    describe('Object storage video edition', function () {
        if ((0, core_utils_1.areMockObjectStorageTestsDisabled)())
            return;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server_commands_1.ObjectStorageCommand.prepareDefaultMockBuckets();
                yield servers[0].kill();
                yield servers[0].run(server_commands_1.ObjectStorageCommand.getDefaultMockConfig());
                yield servers[0].config.enableMinimumTranscoding();
            });
        });
        it('Should run a complex task on a video in object storage', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield renewVideo();
                const video = yield servers[0].videos.get({ id: videoUUID });
                const oldFileUrls = (0, core_utils_1.getAllFiles)(video).map(f => f.fileUrl);
                yield createTasks(server_commands_1.VideoStudioCommand.getComplexTask());
                for (const server of servers) {
                    const video = yield server.videos.get({ id: videoUUID });
                    const files = (0, core_utils_1.getAllFiles)(video);
                    for (const f of files) {
                        (0, chai_1.expect)(oldFileUrls).to.not.include(f.fileUrl);
                    }
                    for (const webtorrentFile of video.files) {
                        (0, shared_1.expectStartWith)(webtorrentFile.fileUrl, server_commands_1.ObjectStorageCommand.getMockWebTorrentBaseUrl());
                    }
                    for (const hlsFile of video.streamingPlaylists[0].files) {
                        (0, shared_1.expectStartWith)(hlsFile.fileUrl, server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl());
                    }
                    yield checkDuration(server, 9);
                }
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=video-studio.js.map