"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const chai = (0, tslib_1.__importStar)(require("chai"));
const server_commands_1 = require("../../../../shared/server-commands");
const expect = chai.expect;
describe('Test slow follows', function () {
    let servers = [];
    let afterFollows;
    before(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(60000);
            servers = yield (0, server_commands_1.createMultipleServers)(3);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[2]);
            afterFollows = new Date();
            for (let i = 0; i < 5; i++) {
                yield servers[0].videos.quickUpload({ name: 'video ' + i });
            }
            yield (0, server_commands_1.waitJobs)(servers);
        });
    });
    it('Should only have broadcast jobs', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const { data } = yield servers[0].jobs.list({ jobType: 'activitypub-http-unicast', sort: '-createdAt' });
            for (const job of data) {
                expect(new Date(job.createdAt)).below(afterFollows);
            }
        });
    });
    it('Should process bad follower', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(30000);
            yield servers[1].kill();
            yield servers[0].videos.quickUpload({ name: 'video 6' });
            yield (0, server_commands_1.waitJobs)(servers[0]);
            afterFollows = new Date();
            const filter = (job) => new Date(job.createdAt) > afterFollows;
            yield servers[0].videos.quickUpload({ name: 'video 7' });
            yield (0, server_commands_1.waitJobs)(servers[0]);
            const resBroadcast = yield servers[0].jobs.list({ jobType: 'activitypub-http-broadcast', sort: '-createdAt' });
            const resUnicast = yield servers[0].jobs.list({ jobType: 'activitypub-http-unicast', sort: '-createdAt' });
            const broadcast = resBroadcast.data.filter(filter);
            const unicast = resUnicast.data.filter(filter);
            expect(unicast).to.have.lengthOf(2);
            expect(broadcast).to.have.lengthOf(2);
            for (const u of unicast) {
                expect(u.data.uri).to.equal(servers[1].url + '/inbox');
            }
            for (const b of broadcast) {
                expect(b.data.uris).to.have.lengthOf(1);
                expect(b.data.uris[0]).to.equal(servers[2].url + '/inbox');
            }
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=slow-follows.js.map