"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const core_utils_1 = require("../../../shared/core-utils");
const extra_utils_1 = require("../../../shared/extra-utils");
const server_commands_1 = require("../../../shared/server-commands");
function countFiles(server, directory) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const files = yield (0, fs_extra_1.readdir)(server.servers.buildDirectory(directory));
        return files.length;
    });
}
function assertNotExists(server, directory, substring) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const files = yield (0, fs_extra_1.readdir)(server.servers.buildDirectory(directory));
        for (const f of files) {
            (0, chai_1.expect)(f).to.not.contain(substring);
        }
    });
}
function assertCountAreOkay(servers) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const server of servers) {
            const videosCount = yield countFiles(server, 'videos');
            (0, chai_1.expect)(videosCount).to.equal(9);
            const privateVideosCount = yield countFiles(server, 'videos/private');
            (0, chai_1.expect)(privateVideosCount).to.equal(4);
            const torrentsCount = yield countFiles(server, 'torrents');
            (0, chai_1.expect)(torrentsCount).to.equal(24);
            const previewsCount = yield countFiles(server, 'previews');
            (0, chai_1.expect)(previewsCount).to.equal(3);
            const thumbnailsCount = yield countFiles(server, 'thumbnails');
            (0, chai_1.expect)(thumbnailsCount).to.equal(7);
            const avatarsCount = yield countFiles(server, 'avatars');
            (0, chai_1.expect)(avatarsCount).to.equal(4);
            const hlsRootCount = yield countFiles(server, (0, path_1.join)('streaming-playlists', 'hls'));
            (0, chai_1.expect)(hlsRootCount).to.equal(3);
            const hlsPrivateRootCount = yield countFiles(server, (0, path_1.join)('streaming-playlists', 'hls', 'private'));
            (0, chai_1.expect)(hlsPrivateRootCount).to.equal(1);
        }
    });
}
describe('Test prune storage scripts', function () {
    let servers;
    const badNames = {};
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, server_commands_1.createMultipleServers)(2, { transcoding: { enabled: true } });
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.setDefaultVideoChannel)(servers);
            for (const server of servers) {
                yield server.videos.upload({ attributes: { name: 'video 1', privacy: 1 } });
                yield server.videos.upload({ attributes: { name: 'video 2', privacy: 1 } });
                yield server.videos.upload({ attributes: { name: 'video 3', privacy: 3 } });
                yield server.users.updateMyAvatar({ fixture: 'avatar.png' });
                yield server.playlists.create({
                    attributes: {
                        displayName: 'playlist',
                        privacy: 1,
                        videoChannelId: server.store.channel.id,
                        thumbnailfile: 'thumbnail.jpg'
                    }
                });
            }
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            {
                const account = yield servers[0].accounts.get({ accountName: 'root@' + servers[1].host });
                for (const avatar of account.avatars) {
                    yield (0, server_commands_1.makeGetRequest)({
                        url: servers[0].url,
                        path: avatar.path,
                        expectedStatus: 200
                    });
                }
            }
            {
                const account = yield servers[1].accounts.get({ accountName: 'root@' + servers[0].host });
                for (const avatar of account.avatars) {
                    yield (0, server_commands_1.makeGetRequest)({
                        url: servers[1].url,
                        path: avatar.path,
                        expectedStatus: 200
                    });
                }
            }
            yield (0, core_utils_1.wait)(1000);
            yield (0, server_commands_1.waitJobs)(servers);
            yield (0, server_commands_1.killallServers)(servers);
            yield (0, core_utils_1.wait)(1000);
        });
    });
    it('Should have the files on the disk', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield assertCountAreOkay(servers);
        });
    });
    it('Should create some dirty files', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (let i = 0; i < 2; i++) {
                {
                    const basePublic = servers[0].servers.buildDirectory('videos');
                    const basePrivate = servers[0].servers.buildDirectory((0, path_1.join)('videos', 'private'));
                    const n1 = (0, extra_utils_1.buildUUID)() + '.mp4';
                    const n2 = (0, extra_utils_1.buildUUID)() + '.webm';
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(basePublic, n1));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(basePublic, n2));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(basePrivate, n1));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(basePrivate, n2));
                    badNames['videos'] = [n1, n2];
                }
                {
                    const base = servers[0].servers.buildDirectory('torrents');
                    const n1 = (0, extra_utils_1.buildUUID)() + '-240.torrent';
                    const n2 = (0, extra_utils_1.buildUUID)() + '-480.torrent';
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n1));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n2));
                    badNames['torrents'] = [n1, n2];
                }
                {
                    const base = servers[0].servers.buildDirectory('thumbnails');
                    const n1 = (0, extra_utils_1.buildUUID)() + '.jpg';
                    const n2 = (0, extra_utils_1.buildUUID)() + '.jpg';
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n1));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n2));
                    badNames['thumbnails'] = [n1, n2];
                }
                {
                    const base = servers[0].servers.buildDirectory('previews');
                    const n1 = (0, extra_utils_1.buildUUID)() + '.jpg';
                    const n2 = (0, extra_utils_1.buildUUID)() + '.jpg';
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n1));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n2));
                    badNames['previews'] = [n1, n2];
                }
                {
                    const base = servers[0].servers.buildDirectory('avatars');
                    const n1 = (0, extra_utils_1.buildUUID)() + '.png';
                    const n2 = (0, extra_utils_1.buildUUID)() + '.jpg';
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n1));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(base, n2));
                    badNames['avatars'] = [n1, n2];
                }
                {
                    const directory = (0, path_1.join)('streaming-playlists', 'hls');
                    const basePublic = servers[0].servers.buildDirectory(directory);
                    const basePrivate = servers[0].servers.buildDirectory((0, path_1.join)(directory, 'private'));
                    const n1 = (0, extra_utils_1.buildUUID)();
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(basePublic, n1));
                    yield (0, fs_extra_1.createFile)((0, path_1.join)(basePrivate, n1));
                    badNames[directory] = [n1];
                }
            }
        });
    });
    it('Should run prune storage', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(30000);
            const env = servers[0].cli.getEnv();
            yield server_commands_1.CLICommand.exec(`echo y | ${env} npm run prune-storage`);
        });
    });
    it('Should have removed files', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield assertCountAreOkay(servers);
            for (const directory of Object.keys(badNames)) {
                for (const name of badNames[directory]) {
                    yield assertNotExists(servers[0], directory, name);
                }
            }
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=prune-storage.js.map