"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const server_commands_1 = require("../../../shared/server-commands");
describe('Official plugin Akismet', function () {
    let servers;
    let videoUUID;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(30000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield servers[0].plugins.install({
                npmName: 'peertube-plugin-akismet'
            });
            if (!process.env.AKISMET_KEY)
                throw new Error('Missing AKISMET_KEY from env');
            yield servers[0].plugins.updateSettings({
                npmName: 'peertube-plugin-akismet',
                settings: {
                    'akismet-api-key': process.env.AKISMET_KEY
                }
            });
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        });
    });
    describe('Local threads/replies', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video 1' });
                videoUUID = uuid;
            });
        });
        it('Should not detect a thread as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].comments.createThread({ videoId: videoUUID, text: 'comment' });
            });
        });
        it('Should not detect a reply as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].comments.addReplyToLastThread({ text: 'reply' });
            });
        });
        it('Should detect a thread as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].comments.createThread({
                    videoId: videoUUID,
                    text: 'akismet-guaranteed-spam',
                    expectedStatus: 403
                });
            });
        });
        it('Should detect a thread as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].comments.createThread({ videoId: videoUUID, text: 'comment' });
                yield servers[0].comments.addReplyToLastThread({ text: 'akismet-guaranteed-spam', expectedStatus: 403 });
            });
        });
    });
    describe('Remote threads/replies', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video 1' });
                videoUUID = uuid;
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should not detect a thread as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[1].comments.createThread({ videoId: videoUUID, text: 'remote comment 1' });
                yield (0, server_commands_1.waitJobs)(servers);
                const { data } = yield servers[0].comments.listThreads({ videoId: videoUUID });
                (0, chai_1.expect)(data).to.have.lengthOf(1);
            });
        });
        it('Should not detect a reply as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[1].comments.addReplyToLastThread({ text: 'I agree with you' });
                yield (0, server_commands_1.waitJobs)(servers);
                const { data } = yield servers[0].comments.listThreads({ videoId: videoUUID });
                (0, chai_1.expect)(data).to.have.lengthOf(1);
                const tree = yield servers[0].comments.getThread({ videoId: videoUUID, threadId: data[0].id });
                (0, chai_1.expect)(tree.children).to.have.lengthOf(1);
            });
        });
        it('Should detect a thread as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[1].comments.createThread({ videoId: videoUUID, text: 'akismet-guaranteed-spam' });
                yield (0, server_commands_1.waitJobs)(servers);
                const { data } = yield servers[0].comments.listThreads({ videoId: videoUUID });
                (0, chai_1.expect)(data).to.have.lengthOf(1);
            });
        });
        it('Should detect a thread as spam', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[1].comments.addReplyToLastThread({ text: 'akismet-guaranteed-spam' });
                yield (0, server_commands_1.waitJobs)(servers);
                const { data } = yield servers[0].comments.listThreads({ videoId: videoUUID });
                (0, chai_1.expect)(data).to.have.lengthOf(1);
                const thread = data[0];
                const tree = yield servers[0].comments.getThread({ videoId: videoUUID, threadId: thread.id });
                (0, chai_1.expect)(tree.children).to.have.lengthOf(1);
            });
        });
    });
    describe('Signup', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].config.updateExistingSubConfig({
                    newConfig: {
                        signup: {
                            enabled: true
                        }
                    }
                });
            });
        });
        it('Should allow signup', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].users.register({
                    username: 'user1',
                    displayName: 'user 1'
                });
            });
        });
        it('Should detect a signup as SPAM', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].users.register({
                    username: 'user2',
                    displayName: 'user 2',
                    email: 'akismet-guaranteed-spam@example.com',
                    expectedStatus: 403
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=akismet.js.map