"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const core_utils_1 = require("../../../shared/core-utils");
const server_commands_1 = require("../../../shared/server-commands");
const shared_1 = require("../shared");
function checkFiles(origin, video, inObjectStorage) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        for (const file of video.files) {
            const start = inObjectStorage
                ? server_commands_1.ObjectStorageCommand.getWebTorrentBaseUrl()
                : origin.url;
            (0, shared_1.expectStartWith)(file.fileUrl, start);
            yield (0, server_commands_1.makeRawRequest)(file.fileUrl, 200);
        }
        const start = inObjectStorage
            ? server_commands_1.ObjectStorageCommand.getPlaylistBaseUrl()
            : origin.url;
        const hls = video.streamingPlaylists[0];
        (0, shared_1.expectStartWith)(hls.playlistUrl, start);
        (0, shared_1.expectStartWith)(hls.segmentsSha256Url, start);
        for (const file of hls.files) {
            (0, shared_1.expectStartWith)(file.fileUrl, start);
            yield (0, server_commands_1.makeRawRequest)(file.fileUrl, 200);
        }
    });
}
describe('Test create move video storage job', function () {
    if ((0, core_utils_1.areObjectStorageTestsDisabled)())
        return;
    let servers = [];
    const uuids = [];
    before(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(360000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            yield server_commands_1.ObjectStorageCommand.prepareDefaultBuckets();
            yield servers[0].config.enableTranscoding();
            for (let i = 0; i < 3; i++) {
                const { uuid } = yield servers[0].videos.upload({ attributes: { name: 'video' + i } });
                uuids.push(uuid);
            }
            yield (0, server_commands_1.waitJobs)(servers);
            yield servers[0].kill();
            yield servers[0].run(server_commands_1.ObjectStorageCommand.getDefaultConfig());
        });
    });
    it('Should move only one file', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(120000);
            const command = `npm run create-move-video-storage-job -- --to-object-storage -v ${uuids[1]}`;
            yield servers[0].cli.execWithEnv(command, server_commands_1.ObjectStorageCommand.getDefaultConfig());
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const video = yield server.videos.get({ id: uuids[1] });
                yield checkFiles(servers[0], video, true);
                for (const id of [uuids[0], uuids[2]]) {
                    const video = yield server.videos.get({ id });
                    yield checkFiles(servers[0], video, false);
                }
            }
        });
    });
    it('Should move all files', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(120000);
            const command = `npm run create-move-video-storage-job -- --to-object-storage --all-videos`;
            yield servers[0].cli.execWithEnv(command, server_commands_1.ObjectStorageCommand.getDefaultConfig());
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                for (const id of [uuids[0], uuids[2]]) {
                    const video = yield server.videos.get({ id });
                    yield checkFiles(servers[0], video, true);
                }
            }
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=create-move-video-storage-job.js.map