"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test video studio API validator', function () {
    let server;
    let command;
    let userAccessToken;
    let videoUUID;
    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]);
            userAccessToken = yield server.users.generateUserAndToken('user1');
            yield server.config.enableMinimumTranscoding();
            const { uuid } = yield server.videos.quickUpload({ name: 'video' });
            videoUUID = uuid;
            command = server.videoStudio;
            yield (0, server_commands_1.waitJobs)([server]);
        });
    });
    describe('Task creation', function () {
        describe('Config settings', function () {
            it('Should fail if studio is disabled', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield server.config.updateExistingSubConfig({
                        newConfig: {
                            videoStudio: {
                                enabled: false
                            }
                        }
                    });
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 400
                    });
                });
            });
            it('Should fail to enable studio if transcoding is disabled', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield server.config.updateExistingSubConfig({
                        newConfig: {
                            videoStudio: {
                                enabled: true
                            },
                            transcoding: {
                                enabled: false
                            }
                        },
                        expectedStatus: 400
                    });
                });
            });
            it('Should succeed to enable video studio', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield server.config.updateExistingSubConfig({
                        newConfig: {
                            videoStudio: {
                                enabled: true
                            },
                            transcoding: {
                                enabled: true
                            }
                        }
                    });
                });
            });
        });
        describe('Common tasks', function () {
            it('Should fail without token', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        token: null,
                        videoId: videoUUID,
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 401
                    });
                });
            });
            it('Should fail with another user token', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        token: userAccessToken,
                        videoId: videoUUID,
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 403
                    });
                });
            });
            it('Should fail with an invalid video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        videoId: 'tintin',
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 400
                    });
                });
            });
            it('Should fail with an unknown video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        videoId: 42,
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 404
                    });
                });
            });
            it('Should fail with an already in transcoding state video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(30000);
                    const { uuid } = yield server.videos.quickUpload({ name: 'transcoded video' });
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield server.jobs.pauseJobQueue();
                    yield server.videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' });
                    yield command.createEditionTasks({
                        videoId: uuid,
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 409
                    });
                    yield server.jobs.resumeJobQueue();
                });
            });
            it('Should fail with a bad complex task', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: [
                            {
                                name: 'cut',
                                options: {
                                    start: 1,
                                    end: 2
                                }
                            },
                            {
                                name: 'hadock',
                                options: {
                                    start: 1,
                                    end: 2
                                }
                            }
                        ],
                        expectedStatus: 400
                    });
                });
            });
            it('Should fail without task', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: [],
                        expectedStatus: 400
                    });
                });
            });
            it('Should fail with too many tasks', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const tasks = [];
                    for (let i = 0; i < 110; i++) {
                        tasks.push({
                            name: 'cut',
                            options: {
                                start: 1
                            }
                        });
                    }
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks,
                        expectedStatus: 400
                    });
                });
            });
            it('Should succeed with correct parameters', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield server.jobs.pauseJobQueue();
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 204
                    });
                });
            });
            it('Should fail with a video that is already waiting for edition', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: server_commands_1.VideoStudioCommand.getComplexTask(),
                        expectedStatus: 409
                    });
                    yield server.jobs.resumeJobQueue();
                    yield (0, server_commands_1.waitJobs)([server]);
                });
            });
        });
        describe('Cut task', function () {
            function cut(start, end, expectedStatus = 400) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: [
                            {
                                name: 'cut',
                                options: {
                                    start,
                                    end
                                }
                            }
                        ],
                        expectedStatus
                    });
                });
            }
            it('Should fail with bad start/end', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const invalid = [
                        'tintin',
                        -1,
                        undefined
                    ];
                    for (const value of invalid) {
                        yield cut(value, undefined);
                        yield cut(undefined, value);
                    }
                });
            });
            it('Should fail with the same start/end', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield cut(2, 2);
                });
            });
            it('Should fail with inconsistents start/end', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield cut(2, 1);
                });
            });
            it('Should fail without start and end', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield cut(undefined, undefined);
                });
            });
            it('Should succeed with the correct params', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    yield cut(0, 2, 204);
                    yield (0, server_commands_1.waitJobs)([server]);
                });
            });
        });
        describe('Watermark task', function () {
            function addWatermark(file, expectedStatus = 400) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: [
                            {
                                name: 'add-watermark',
                                options: {
                                    file
                                }
                            }
                        ],
                        expectedStatus
                    });
                });
            }
            it('Should fail without waterkmark', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield addWatermark(undefined);
                });
            });
            it('Should fail with an invalid watermark', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield addWatermark('video_short.mp4');
                });
            });
            it('Should succeed with the correct params', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    yield addWatermark('thumbnail.jpg', 204);
                    yield (0, server_commands_1.waitJobs)([server]);
                });
            });
        });
        describe('Intro/Outro task', function () {
            function addIntroOutro(type, file, expectedStatus = 400) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.createEditionTasks({
                        videoId: videoUUID,
                        tasks: [
                            {
                                name: type,
                                options: {
                                    file
                                }
                            }
                        ],
                        expectedStatus
                    });
                });
            }
            it('Should fail without file', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield addIntroOutro('add-intro', undefined);
                    yield addIntroOutro('add-outro', undefined);
                });
            });
            it('Should fail with an invalid file', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield addIntroOutro('add-intro', 'thumbnail.jpg');
                    yield addIntroOutro('add-outro', 'thumbnail.jpg');
                });
            });
            it('Should fail with a file that does not contain video stream', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield addIntroOutro('add-intro', 'sample.ogg');
                    yield addIntroOutro('add-outro', 'sample.ogg');
                });
            });
            it('Should succeed with the correct params', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    yield addIntroOutro('add-intro', 'video_very_short_240p.mp4', 204);
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield addIntroOutro('add-outro', 'video_very_short_240p.mp4', 204);
                    yield (0, server_commands_1.waitJobs)([server]);
                });
            });
            it('Should check total quota when creating the task', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    const user = yield server.users.create({ username: 'user_quota_1' });
                    const token = yield server.login.getAccessToken('user_quota_1');
                    const { uuid } = yield server.videos.quickUpload({ token, name: 'video_quota_1', fixture: 'video_short.mp4' });
                    const addIntroOutroByUser = (type, expectedStatus) => {
                        return command.createEditionTasks({
                            token,
                            videoId: uuid,
                            tasks: [
                                {
                                    name: type,
                                    options: {
                                        file: 'video_short.mp4'
                                    }
                                }
                            ],
                            expectedStatus
                        });
                    };
                    yield (0, server_commands_1.waitJobs)([server]);
                    const { videoQuotaUsed } = yield server.users.getMyQuotaUsed({ token });
                    yield server.users.update({ userId: user.id, videoQuota: Math.round(videoQuotaUsed * 2.5) });
                    yield addIntroOutroByUser('add-intro', 204);
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield addIntroOutroByUser('add-intro', 413);
                    yield addIntroOutroByUser('add-outro', 413);
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=video-studio.js.map