"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");
function checkFilesInObjectStorage(video) {
    return 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.getMockWebTorrentBaseUrl());
            yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, expectedStatus: 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.getMockPlaylistBaseUrl());
            yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, expectedStatus: 200 });
        }
        (0, shared_1.expectStartWith)(hlsPlaylist.playlistUrl, server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl());
        yield (0, server_commands_1.makeRawRequest)({ url: hlsPlaylist.playlistUrl, expectedStatus: 200 });
        (0, shared_1.expectStartWith)(hlsPlaylist.segmentsSha256Url, server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl());
        yield (0, server_commands_1.makeRawRequest)({ url: hlsPlaylist.segmentsSha256Url, expectedStatus: 200 });
    });
}
function runTests(objectStorage) {
    let servers = [];
    let videoUUID;
    let publishedAt;
    let shouldBeDeleted;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            const config = objectStorage
                ? server_commands_1.ObjectStorageCommand.getDefaultMockConfig()
                : {};
            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.prepareDefaultMockBuckets();
            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 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 });
                (0, chai_1.expect)(videoDetails.files).to.have.lengthOf(1);
                (0, chai_1.expect)(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                (0, chai_1.expect)(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
        });
    });
    it('Should generate WebTorrent', function () {
        return 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 });
                (0, chai_1.expect)(videoDetails.files).to.have.lengthOf(5);
                (0, chai_1.expect)(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                (0, chai_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 tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].videos.removeAllWebTorrentFiles({ 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 });
                (0, chai_1.expect)(videoDetails.files).to.have.lengthOf(5);
                (0, chai_1.expect)(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                (0, chai_1.expect)(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
        });
    });
    it('Should only generate WebTorrent', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].videos.removeHLSPlaylist({ 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 });
                (0, chai_1.expect)(videoDetails.files).to.have.lengthOf(5);
                (0, chai_1.expect)(videoDetails.streamingPlaylists).to.have.lengthOf(0);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
            }
        });
    });
    it('Should correctly update HLS playlist on resolution change', function () {
        return 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 });
                (0, chai_1.expect)(videoDetails.files).to.have.lengthOf(1);
                (0, chai_1.expect)(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                (0, chai_1.expect)(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1);
                if (objectStorage)
                    yield checkFilesInObjectStorage(videoDetails);
                shouldBeDeleted = [
                    videoDetails.streamingPlaylists[0].files[0].fileUrl,
                    videoDetails.streamingPlaylists[0].playlistUrl,
                    videoDetails.streamingPlaylists[0].segmentsSha256Url
                ];
            }
            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 });
                (0, chai_1.expect)(videoDetails.streamingPlaylists).to.have.lengthOf(1);
                (0, chai_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 });
                    (0, chai_1.expect)(Object.keys(shaBody)).to.have.lengthOf(5);
                }
            }
        });
    });
    it('Should have correctly deleted previous files', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (const fileUrl of shouldBeDeleted) {
                yield (0, server_commands_1.makeRawRequest)({ url: fileUrl, expectedStatus: 404 });
            }
        });
    });
    it('Should not have updated published at attributes', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const video = yield servers[0].videos.get({ id: videoUUID });
            (0, chai_1.expect)(video.publishedAt).to.equal(publishedAt);
        });
    });
    after(function () {
        return 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.areMockObjectStorageTestsDisabled)())
            return;
        runTests(true);
    });
});
//# sourceMappingURL=create-transcoding.js.map