"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const chai = (0, tslib_1.__importStar)(require("chai"));
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
const expect = chai.expect;
function checkFilesInObjectStorage(video) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        for (const file of video.files) {
            (0, shared_1.expectStartWith)(file.fileUrl, server_commands_1.ObjectStorageCommand.getWebTorrentBaseUrl());
            yield (0, server_commands_1.makeRawRequest)(file.fileUrl, 200);
        }
        if (video.streamingPlaylists.length === 0)
            return;
        const hlsPlaylist = video.streamingPlaylists[0];
        for (const file of hlsPlaylist.files) {
            (0, shared_1.expectStartWith)(file.fileUrl, server_commands_1.ObjectStorageCommand.getPlaylistBaseUrl());
            yield (0, server_commands_1.makeRawRequest)(file.fileUrl, 200);
        }
        (0, shared_1.expectStartWith)(hlsPlaylist.playlistUrl, server_commands_1.ObjectStorageCommand.getPlaylistBaseUrl());
        yield (0, server_commands_1.makeRawRequest)(hlsPlaylist.playlistUrl, 200);
        (0, shared_1.expectStartWith)(hlsPlaylist.segmentsSha256Url, server_commands_1.ObjectStorageCommand.getPlaylistBaseUrl());
        yield (0, server_commands_1.makeRawRequest)(hlsPlaylist.segmentsSha256Url, 200);
    });
}
function runTests(objectStorage) {
    let servers = [];
    let videoUUID;
    let publishedAt;
    before(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(120000);
            const config = objectStorage
                ? server_commands_1.ObjectStorageCommand.getDefaultConfig()
                : {};
            servers = yield (0, server_commands_1.createMultipleServers)(2, config);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield servers[0].config.disableTranscoding();
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            if (objectStorage)
                yield server_commands_1.ObjectStorageCommand.prepareDefaultBuckets();
            const { shortUUID } = yield servers[0].videos.quickUpload({ name: 'video' });
            videoUUID = shortUUID;
            yield (0, server_commands_1.waitJobs)(servers);
            const video = yield servers[0].videos.get({ id: videoUUID });
            publishedAt = video.publishedAt;
            yield servers[0].config.enableTranscoding();
        });
    });
    it('Should generate HLS', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].videos.runTranscoding({
                videoId: videoUUID,
                transcodingType: 'hls'
            });
            yield (0, server_commands_1.waitJobs)(servers);
            yield (0, server_commands_1.expectNoFailedTranscodingJob)(servers[0]);
            for (const server of servers) {
                const videoDetails = yield server.videos.get({ id: videoUUID });
                expect(videoDetails.files).to.have.lengthOf(1);
                expect(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
        });
    });
    it('Should generate WebTorrent', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].videos.runTranscoding({
                videoId: videoUUID,
                transcodingType: 'webtorrent'
            });
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const videoDetails = yield server.videos.get({ id: videoUUID });
                expect(videoDetails.files).to.have.lengthOf(5);
                expect(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
        });
    });
    it('Should generate WebTorrent from HLS only video', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].videos.removeWebTorrentFiles({ videoId: videoUUID });
            yield (0, server_commands_1.waitJobs)(servers);
            yield servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' });
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const videoDetails = yield server.videos.get({ id: videoUUID });
                expect(videoDetails.files).to.have.lengthOf(5);
                expect(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
        });
    });
    it('Should only generate WebTorrent', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].videos.removeHLSFiles({ videoId: videoUUID });
            yield (0, server_commands_1.waitJobs)(servers);
            yield servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' });
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const videoDetails = yield server.videos.get({ id: videoUUID });
                expect(videoDetails.files).to.have.lengthOf(5);
                expect(videoDetails.streamingPlaylists).to.have.lengthOf(0);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
        });
    });
    it('Should correctly update HLS playlist on resolution change', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(120000);
            yield servers[0].config.updateExistingSubConfig({
                newConfig: {
                    transcoding: {
                        enabled: true,
                        resolutions: server_commands_1.ConfigCommand.getCustomConfigResolutions(false),
                        webtorrent: {
                            enabled: true
                        },
                        hls: {
                            enabled: true
                        }
                    }
                }
            });
            const { uuid } = yield servers[0].videos.quickUpload({ name: 'quick' });
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const videoDetails = yield server.videos.get({ id: uuid });
                expect(videoDetails.files).to.have.lengthOf(1);
                expect(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
            yield servers[0].config.updateExistingSubConfig({
                newConfig: {
                    transcoding: {
                        enabled: true,
                        resolutions: server_commands_1.ConfigCommand.getCustomConfigResolutions(true),
                        webtorrent: {
                            enabled: true
                        },
                        hls: {
                            enabled: true
                        }
                    }
                }
            });
            yield servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' });
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const videoDetails = yield server.videos.get({ id: uuid });
                expect(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5);
                if (objectStorage) {
                    yield checkFilesInObjectStorage(videoDetails);
                    const hlsPlaylist = videoDetails.streamingPlaylists[0];
                    const resolutions = hlsPlaylist.files.map(f => f.resolution.id);
                    yield (0, shared_1.checkResolutionsInMasterPlaylist)({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions });
                    const shaBody = yield servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url });
                    expect(Object.keys(shaBody)).to.have.lengthOf(5);
                }
            }
        });
    });
    it('Should not have updated published at attributes', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const video = yield servers[0].videos.get({ id: videoUUID });
            expect(video.publishedAt).to.equal(publishedAt);
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
}
describe('Test create transcoding jobs from API', function () {
    describe('On filesystem', function () {
        runTests(false);
    });
    describe('On object storage', function () {
        if ((0, core_utils_1.areObjectStorageTestsDisabled)())
            return;
        runTests(true);
    });
});
//# sourceMappingURL=video-create-transcoding.js.map