"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.testVideoResolutions = exports.checkLiveCleanup = void 0;
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const server_commands_1 = require("../../../shared/server-commands");
const streaming_playlists_1 = require("./streaming-playlists");
const extra_utils_1 = require("../../../shared/extra-utils");
function checkLiveCleanup(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { server, videoUUID, permanent, savedResolutions = [] } = options;
        const basePath = server.servers.buildDirectory('streaming-playlists');
        const hlsPath = (0, path_1.join)(basePath, 'hls', videoUUID);
        if (permanent) {
            if (!(yield (0, fs_extra_1.pathExists)(hlsPath)))
                return;
            const files = yield (0, fs_extra_1.readdir)(hlsPath);
            (0, chai_1.expect)(files).to.have.lengthOf(0);
            return;
        }
        if (savedResolutions.length === 0) {
            return checkUnsavedLiveCleanup(server, videoUUID, hlsPath);
        }
        return checkSavedLiveCleanup(hlsPath, savedResolutions);
    });
}
exports.checkLiveCleanup = checkLiveCleanup;
function testVideoResolutions(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { originServer, servers, liveVideoId, resolutions, transcoded, objectStorage } = options;
        for (const server of servers) {
            const { data } = yield server.videos.list();
            (0, chai_1.expect)(data.find(v => v.uuid === liveVideoId)).to.exist;
            const video = yield server.videos.get({ id: liveVideoId });
            (0, chai_1.expect)(video.streamingPlaylists).to.have.lengthOf(1);
            const hlsPlaylist = video.streamingPlaylists.find(s => s.type === 1);
            (0, chai_1.expect)(hlsPlaylist).to.exist;
            (0, chai_1.expect)(hlsPlaylist.files).to.have.lengthOf(0);
            yield (0, streaming_playlists_1.checkResolutionsInMasterPlaylist)({
                server,
                playlistUrl: hlsPlaylist.playlistUrl,
                resolutions,
                transcoded,
                withRetry: objectStorage
            });
            if (objectStorage) {
                (0, chai_1.expect)(hlsPlaylist.playlistUrl).to.contain(server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl());
            }
            for (let i = 0; i < resolutions.length; i++) {
                const segmentNum = 3;
                const segmentName = `${i}-00000${segmentNum}.ts`;
                yield originServer.live.waitUntilSegmentGeneration({
                    server: originServer,
                    videoUUID: video.uuid,
                    playlistNumber: i,
                    segment: segmentNum,
                    objectStorage
                });
                const baseUrl = objectStorage
                    ? server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl() + 'hls'
                    : originServer.url + '/static/streaming-playlists/hls';
                if (objectStorage) {
                    (0, chai_1.expect)(hlsPlaylist.segmentsSha256Url).to.contain(server_commands_1.ObjectStorageCommand.getMockPlaylistBaseUrl());
                }
                const subPlaylist = yield originServer.streamingPlaylists.get({
                    url: `${baseUrl}/${video.uuid}/${i}.m3u8`,
                    withRetry: objectStorage
                });
                (0, chai_1.expect)(subPlaylist).to.contain(segmentName);
                yield (0, streaming_playlists_1.checkLiveSegmentHash)({
                    server,
                    baseUrlSegment: baseUrl,
                    videoUUID: video.uuid,
                    segmentName,
                    hlsPlaylist
                });
                if (originServer.internalServerNumber === server.internalServerNumber) {
                    const infohash = (0, extra_utils_1.sha1)(`${2 + hlsPlaylist.playlistUrl}+V${i}`);
                    const dbInfohashes = yield originServer.sql.getPlaylistInfohash(hlsPlaylist.id);
                    (0, chai_1.expect)(dbInfohashes).to.include(infohash);
                }
            }
        }
    });
}
exports.testVideoResolutions = testVideoResolutions;
function checkSavedLiveCleanup(hlsPath, savedResolutions = []) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const files = yield (0, fs_extra_1.readdir)(hlsPath);
        (0, chai_1.expect)(files).to.have.lengthOf(savedResolutions.length * 2 + 2);
        for (const resolution of savedResolutions) {
            const fragmentedFile = files.find(f => f.endsWith(`-${resolution}-fragmented.mp4`));
            (0, chai_1.expect)(fragmentedFile).to.exist;
            const playlistFile = files.find(f => f.endsWith(`${resolution}.m3u8`));
            (0, chai_1.expect)(playlistFile).to.exist;
        }
        const masterPlaylistFile = files.find(f => f.endsWith('-master.m3u8'));
        (0, chai_1.expect)(masterPlaylistFile).to.exist;
        const shaFile = files.find(f => f.endsWith('-segments-sha256.json'));
        (0, chai_1.expect)(shaFile).to.exist;
    });
}
function checkUnsavedLiveCleanup(server, videoUUID, hlsPath) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        let live;
        try {
            live = yield server.live.get({ videoId: videoUUID });
        }
        catch (_a) { }
        if (live === null || live === void 0 ? void 0 : live.permanentLive) {
            (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(hlsPath)).to.be.true;
            const hlsFiles = yield (0, fs_extra_1.readdir)(hlsPath);
            (0, chai_1.expect)(hlsFiles).to.have.lengthOf(1);
            const replayDir = (0, path_1.join)(hlsPath, 'replay');
            (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(replayDir)).to.be.true;
            const replayFiles = yield (0, fs_extra_1.readdir)((0, path_1.join)(hlsPath, 'replay'));
            (0, chai_1.expect)(replayFiles).to.have.lengthOf(0);
            return;
        }
        (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(hlsPath)).to.be.false;
    });
}
//# sourceMappingURL=live.js.map