"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = require("path");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
const constants_1 = require("../../../initializers/constants");
describe('Test HLS videos', function () {
    let servers = [];
    function runTestSuite(hlsOnly, objectStorageBaseUrl) {
        const videoUUIDs = [];
        it('Should upload a video and transcode it to HLS', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } });
                videoUUIDs.push(uuid);
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.completeCheckHlsPlaylist)({ servers, videoUUID: uuid, hlsOnly, objectStorageBaseUrl });
            });
        });
        it('Should upload an audio file and transcode it to HLS', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } });
                videoUUIDs.push(uuid);
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.completeCheckHlsPlaylist)({
                    servers,
                    videoUUID: uuid,
                    hlsOnly,
                    resolutions: [constants_1.DEFAULT_AUDIO_RESOLUTION, 360, 240],
                    objectStorageBaseUrl
                });
            });
        });
        it('Should update the video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[0].videos.update({ id: videoUUIDs[0], attributes: { name: 'video 1 updated' } });
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.completeCheckHlsPlaylist)({ servers, videoUUID: videoUUIDs[0], hlsOnly, objectStorageBaseUrl });
            });
        });
        it('Should delete videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(10000);
                for (const uuid of videoUUIDs) {
                    yield servers[0].videos.remove({ id: uuid });
                }
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    for (const uuid of videoUUIDs) {
                        yield server.videos.get({ id: uuid, expectedStatus: 404 });
                    }
                }
            });
        });
        it('Should have the playlists/segment deleted from the disk', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    yield (0, shared_1.checkDirectoryIsEmpty)(server, 'videos', ['private']);
                    yield (0, shared_1.checkDirectoryIsEmpty)(server, (0, path_1.join)('videos', 'private'));
                    yield (0, shared_1.checkDirectoryIsEmpty)(server, (0, path_1.join)('streaming-playlists', 'hls'), ['private']);
                    yield (0, shared_1.checkDirectoryIsEmpty)(server, (0, path_1.join)('streaming-playlists', 'hls', 'private'));
                }
            });
        });
        it('Should have an empty tmp directory', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const server of servers) {
                    yield (0, shared_1.checkTmpIsEmpty)(server);
                }
            });
        });
    }
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            const configOverride = {
                transcoding: {
                    enabled: true,
                    allow_audio_files: true,
                    hls: {
                        enabled: true
                    }
                }
            };
            servers = yield (0, server_commands_1.createMultipleServers)(2, configOverride);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        });
    });
    describe('With WebTorrent & HLS enabled', function () {
        runTestSuite(false);
    });
    describe('With only HLS enabled', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].config.updateCustomSubConfig({
                    newConfig: {
                        transcoding: {
                            enabled: true,
                            allowAudioFiles: true,
                            resolutions: {
                                '144p': false,
                                '240p': true,
                                '360p': true,
                                '480p': true,
                                '720p': true,
                                '1080p': true,
                                '1440p': true,
                                '2160p': true
                            },
                            hls: {
                                enabled: true
                            },
                            webtorrent: {
                                enabled: false
                            }
                        }
                    }
                });
            });
        });
        runTestSuite(true);
    });
    describe('With object storage enabled', function () {
        if ((0, core_utils_1.areMockObjectStorageTestsDisabled)())
            return;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const configOverride = server_commands_1.ObjectStorageCommand.getDefaultMockConfig();
                yield server_commands_1.ObjectStorageCommand.prepareDefaultMockBuckets();
                yield servers[0].kill();
                yield servers[0].run(configOverride);
            });
        });
        runTestSuite(true, server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl());
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=hls.js.map