"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 server_commands_1 = require("../../../shared/server-commands");
const shared_1 = require("../shared");
function postCommand(server, command, bodyArg) {
    const body = { command };
    if (bodyArg)
        Object.assign(body, bodyArg);
    return (0, server_commands_1.makePostBodyRequest)({
        url: server.url,
        path: '/plugins/test-four/router/commander',
        fields: body,
        expectedStatus: 204
    });
}
describe('Test plugin helpers', function () {
    let servers;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            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 servers[0].plugins.install({ path: server_commands_1.PluginsCommand.getPluginTestPath('-four') });
        });
    });
    describe('Logger', function () {
        it('Should have logged things', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].servers.waitUntilLog(servers[0].host + ' peertube-plugin-test-four', 1, false);
                yield servers[0].servers.waitUntilLog('Hello world from plugin four', 1);
            });
        });
    });
    describe('Database', function () {
        it('Should have made a query', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].servers.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`);
            });
        });
    });
    describe('Config', function () {
        it('Should have the correct webserver url', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].servers.waitUntilLog(`server url is ${servers[0].url}`);
            });
        });
        it('Should have the correct config', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const res = yield (0, server_commands_1.makeGetRequest)({
                    url: servers[0].url,
                    path: '/plugins/test-four/router/server-config',
                    expectedStatus: 200
                });
                (0, chai_1.expect)(res.body.serverConfig).to.exist;
                (0, chai_1.expect)(res.body.serverConfig.instance.name).to.equal('PeerTube');
            });
        });
    });
    describe('Server', function () {
        it('Should get the server actor', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].servers.waitUntilLog('server actor name is peertube');
            });
        });
    });
    describe('Socket', function () {
        it('Should sendNotification without any exceptions', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
            const user = yield servers[0].users.create({ username: 'notis_redding', password: 'secret1234?' });
            yield (0, server_commands_1.makePostBodyRequest)({
                url: servers[0].url,
                path: '/plugins/test-four/router/send-notification',
                fields: {
                    userId: user.id
                },
                expectedStatus: 201
            });
        }));
        it('Should sendVideoLiveNewState without any exceptions', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
            const res = yield servers[0].videos.quickUpload({ name: 'video server 1' });
            yield (0, server_commands_1.makePostBodyRequest)({
                url: servers[0].url,
                path: '/plugins/test-four/router/send-video-live-new-state/' + res.uuid,
                expectedStatus: 201
            });
            yield servers[0].videos.remove({ id: res.uuid });
        }));
    });
    describe('Plugin', function () {
        it('Should get the base static route', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const res = yield (0, server_commands_1.makeGetRequest)({
                    url: servers[0].url,
                    path: '/plugins/test-four/router/static-route',
                    expectedStatus: 200
                });
                (0, chai_1.expect)(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/');
            });
        });
        it('Should get the base static route', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const baseRouter = '/plugins/test-four/0.0.1/router/';
                const res = yield (0, server_commands_1.makeGetRequest)({
                    url: servers[0].url,
                    path: baseRouter + 'router-route',
                    expectedStatus: 200
                });
                (0, chai_1.expect)(res.body.routerRoute).to.equal(baseRouter);
            });
        });
    });
    describe('User', function () {
        let rootId;
        it('Should not get a user if not authenticated', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: servers[0].url,
                    path: '/plugins/test-four/router/user',
                    expectedStatus: 404
                });
            });
        });
        it('Should get a user if authenticated', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const res = yield (0, server_commands_1.makeGetRequest)({
                    url: servers[0].url,
                    token: servers[0].accessToken,
                    path: '/plugins/test-four/router/user',
                    expectedStatus: 200
                });
                (0, chai_1.expect)(res.body.username).to.equal('root');
                (0, chai_1.expect)(res.body.displayName).to.equal('root');
                (0, chai_1.expect)(res.body.isAdmin).to.be.true;
                (0, chai_1.expect)(res.body.isModerator).to.be.false;
                (0, chai_1.expect)(res.body.isUser).to.be.false;
                rootId = res.body.id;
            });
        });
        it('Should load a user by id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                {
                    const res = yield (0, server_commands_1.makeGetRequest)({
                        url: servers[0].url,
                        path: '/plugins/test-four/router/user/' + rootId,
                        expectedStatus: 200
                    });
                    (0, chai_1.expect)(res.body.username).to.equal('root');
                }
                {
                    yield (0, server_commands_1.makeGetRequest)({
                        url: servers[0].url,
                        path: '/plugins/test-four/router/user/42',
                        expectedStatus: 404
                    });
                }
            });
        });
    });
    describe('Moderation', function () {
        let videoUUIDServer1;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                {
                    const res = yield servers[0].videos.quickUpload({ name: 'video server 1' });
                    videoUUIDServer1 = res.uuid;
                }
                {
                    yield servers[1].videos.quickUpload({ name: 'video server 2' });
                }
                yield (0, server_commands_1.waitJobs)(servers);
                const { data } = yield servers[0].videos.list();
                (0, chai_1.expect)(data).to.have.lengthOf(2);
            });
        });
        it('Should mute server 2', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(10000);
                yield postCommand(servers[0], 'blockServer', { hostToBlock: servers[1].host });
                const { data } = yield servers[0].videos.list();
                (0, chai_1.expect)(data).to.have.lengthOf(1);
                (0, chai_1.expect)(data[0].name).to.equal('video server 1');
            });
        });
        it('Should unmute server 2', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield postCommand(servers[0], 'unblockServer', { hostToUnblock: servers[1].host });
                const { data } = yield servers[0].videos.list();
                (0, chai_1.expect)(data).to.have.lengthOf(2);
            });
        });
        it('Should mute account of server 2', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield postCommand(servers[0], 'blockAccount', { handleToBlock: `root@${servers[1].host}` });
                const { data } = yield servers[0].videos.list();
                (0, chai_1.expect)(data).to.have.lengthOf(1);
                (0, chai_1.expect)(data[0].name).to.equal('video server 1');
            });
        });
        it('Should unmute account of server 2', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@${servers[1].host}` });
                const { data } = yield servers[0].videos.list();
                (0, chai_1.expect)(data).to.have.lengthOf(2);
            });
        });
        it('Should blacklist video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(10000);
                yield postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const { data } = yield server.videos.list();
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                    (0, chai_1.expect)(data[0].name).to.equal('video server 2');
                }
            });
        });
        it('Should unblacklist video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(10000);
                yield postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const { data } = yield server.videos.list();
                    (0, chai_1.expect)(data).to.have.lengthOf(2);
                }
            });
        });
    });
    describe('Videos', function () {
        let videoUUID;
        let videoPath;
        before(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(240000);
            yield servers[0].config.enableTranscoding();
            const res = yield servers[0].videos.quickUpload({ name: 'video1' });
            videoUUID = res.uuid;
            yield (0, server_commands_1.waitJobs)(servers);
        }));
        it('Should get video files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { body } = yield (0, server_commands_1.makeGetRequest)({
                    url: servers[0].url,
                    path: '/plugins/test-four/router/video-files/' + videoUUID,
                    expectedStatus: 200
                });
                {
                    (0, chai_1.expect)(body.webtorrent.videoFiles).to.be.an('array');
                    (0, chai_1.expect)(body.hls.videoFiles).to.be.an('array');
                    for (const resolution of [144, 240, 360, 480, 720]) {
                        for (const files of [body.webtorrent.videoFiles, body.hls.videoFiles]) {
                            const file = files.find(f => f.resolution === resolution);
                            (0, chai_1.expect)(file).to.exist;
                            (0, chai_1.expect)(file.size).to.be.a('number');
                            (0, chai_1.expect)(file.fps).to.equal(25);
                            (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(file.path)).to.be.true;
                            yield (0, server_commands_1.makeRawRequest)({ url: file.url, expectedStatus: 200 });
                        }
                    }
                    videoPath = body.webtorrent.videoFiles[0].path;
                }
                {
                    (0, chai_1.expect)(body.thumbnails).to.be.an('array');
                    const miniature = body.thumbnails.find(t => t.type === 1);
                    (0, chai_1.expect)(miniature).to.exist;
                    (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(miniature.path)).to.be.true;
                    yield (0, server_commands_1.makeRawRequest)({ url: miniature.url, expectedStatus: 200 });
                    const preview = body.thumbnails.find(t => t.type === 2);
                    (0, chai_1.expect)(preview).to.exist;
                    (0, chai_1.expect)(yield (0, fs_extra_1.pathExists)(preview.path)).to.be.true;
                    yield (0, server_commands_1.makeRawRequest)({ url: preview.url, expectedStatus: 200 });
                }
            });
        });
        it('Should probe a file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { body } = yield (0, server_commands_1.makeGetRequest)({
                    url: servers[0].url,
                    path: '/plugins/test-four/router/ffprobe',
                    query: {
                        path: videoPath
                    },
                    expectedStatus: 200
                });
                (0, chai_1.expect)(body.streams).to.be.an('array');
                (0, chai_1.expect)(body.streams).to.have.lengthOf(2);
            });
        });
        it('Should remove a video after a view', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(40000);
                const video = yield servers[0].videos.get({ id: videoUUID });
                yield servers[0].views.simulateView({ id: videoUUID });
                yield servers[0].servers.waitUntilLog('Video deleted by plugin four.');
                try {
                    yield servers[0].videos.get({ id: videoUUID });
                    throw new Error('Video exists');
                }
                catch (err) {
                    if (err.message.includes('exists'))
                        throw err;
                }
                yield (0, shared_1.checkVideoFilesWereRemoved)({ server: servers[0], video });
            });
        });
        it('Should have fetched the video by URL', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`);
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=plugin-helpers.js.map