"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test users with multiple servers', function () {
    let servers = [];
    let user;
    let userId;
    let videoUUID;
    let userAccessToken;
    let userAvatarFilenames;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, server_commands_1.createMultipleServers)(3);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.setDefaultChannelAvatar)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[2]);
            yield (0, server_commands_1.doubleFollow)(servers[1], servers[2]);
            yield servers[0].videos.upload();
            {
                const username = 'user1';
                const created = yield servers[0].users.create({ username });
                userId = created.id;
                userAccessToken = yield servers[0].login.getAccessToken(username);
            }
            {
                const { uuid } = yield servers[0].videos.upload({ token: userAccessToken });
                videoUUID = uuid;
                yield (0, server_commands_1.waitJobs)(servers);
                yield (0, shared_1.saveVideoInServers)(servers, videoUUID);
            }
        });
    });
    it('Should be able to update my display name', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(10000);
            yield servers[0].users.updateMe({ displayName: 'my super display name' });
            user = yield servers[0].users.getMyInfo();
            (0, chai_1.expect)(user.account.displayName).to.equal('my super display name');
            yield (0, server_commands_1.waitJobs)(servers);
        });
    });
    it('Should be able to update my description', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(10000);
            yield servers[0].users.updateMe({ description: 'my super description updated' });
            user = yield servers[0].users.getMyInfo();
            (0, chai_1.expect)(user.account.displayName).to.equal('my super display name');
            (0, chai_1.expect)(user.account.description).to.equal('my super description updated');
            yield (0, server_commands_1.waitJobs)(servers);
        });
    });
    it('Should be able to update my avatar', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(10000);
            const fixture = 'avatar2.png';
            yield servers[0].users.updateMyAvatar({ fixture });
            user = yield servers[0].users.getMyInfo();
            userAvatarFilenames = user.account.avatars.map(({ path }) => path);
            for (const avatar of user.account.avatars) {
                yield (0, shared_1.testImage)(servers[0].url, `avatar2-resized-${avatar.width}x${avatar.width}`, avatar.path, '.png');
            }
            yield (0, server_commands_1.waitJobs)(servers);
        });
    });
    it('Should have updated my profile on other servers too', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let createdAt;
            for (const server of servers) {
                const body = yield server.accounts.list({ sort: '-createdAt' });
                const resList = body.data.find(a => a.name === 'root' && a.host === servers[0].host);
                (0, chai_1.expect)(resList).not.to.be.undefined;
                const account = yield server.accounts.get({ accountName: resList.name + '@' + resList.host });
                if (!createdAt)
                    createdAt = account.createdAt;
                (0, chai_1.expect)(account.name).to.equal('root');
                (0, chai_1.expect)(account.host).to.equal(servers[0].host);
                (0, chai_1.expect)(account.displayName).to.equal('my super display name');
                (0, chai_1.expect)(account.description).to.equal('my super description updated');
                (0, chai_1.expect)(createdAt).to.equal(account.createdAt);
                if (server.serverNumber === 1) {
                    (0, chai_1.expect)(account.userId).to.be.a('number');
                }
                else {
                    (0, chai_1.expect)(account.userId).to.be.undefined;
                }
                for (const avatar of account.avatars) {
                    yield (0, shared_1.testImage)(server.url, `avatar2-resized-${avatar.width}x${avatar.width}`, avatar.path, '.png');
                }
            }
        });
    });
    it('Should list account videos', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (const server of servers) {
                const { total, data } = yield server.videos.listByAccount({ handle: 'user1@' + servers[0].host });
                (0, chai_1.expect)(total).to.equal(1);
                (0, chai_1.expect)(data).to.be.an('array');
                (0, chai_1.expect)(data).to.have.lengthOf(1);
                (0, chai_1.expect)(data[0].uuid).to.equal(videoUUID);
            }
        });
    });
    it('Should search through account videos', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(10000);
            const created = yield servers[0].videos.upload({ token: userAccessToken, attributes: { name: 'Kami no chikara' } });
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const { total, data } = yield server.videos.listByAccount({ handle: 'user1@' + servers[0].host, search: 'Kami' });
                (0, chai_1.expect)(total).to.equal(1);
                (0, chai_1.expect)(data).to.be.an('array');
                (0, chai_1.expect)(data).to.have.lengthOf(1);
                (0, chai_1.expect)(data[0].uuid).to.equal(created.uuid);
            }
        });
    });
    it('Should remove the user', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(10000);
            for (const server of servers) {
                const body = yield server.accounts.list({ sort: '-createdAt' });
                const accountDeleted = body.data.find(a => a.name === 'user1' && a.host === servers[0].host);
                (0, chai_1.expect)(accountDeleted).not.to.be.undefined;
                const { data } = yield server.channels.list();
                const videoChannelDeleted = data.find(a => a.displayName === 'Main user1 channel' && a.host === servers[0].host);
                (0, chai_1.expect)(videoChannelDeleted).not.to.be.undefined;
            }
            yield servers[0].users.remove({ userId });
            yield (0, server_commands_1.waitJobs)(servers);
            for (const server of servers) {
                const body = yield server.accounts.list({ sort: '-createdAt' });
                const accountDeleted = body.data.find(a => a.name === 'user1' && a.host === servers[0].host);
                (0, chai_1.expect)(accountDeleted).to.be.undefined;
                const { data } = yield server.channels.list();
                const videoChannelDeleted = data.find(a => a.name === 'Main user1 channel' && a.host === servers[0].host);
                (0, chai_1.expect)(videoChannelDeleted).to.be.undefined;
            }
        });
    });
    it('Should not have actor files', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const server of servers) {
            for (const userAvatarFilename of userAvatarFilenames) {
                yield (0, shared_1.checkActorFilesWereRemoved)(userAvatarFilename, server);
            }
        }
    }));
    it('Should not have video files', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const server of servers) {
            yield (0, shared_1.checkVideoFilesWereRemoved)({ server, video: server.store.videoDetails });
        }
    }));
    it('Should have an empty tmp directory', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (const server of servers) {
                yield (0, shared_1.checkTmpIsEmpty)(server);
            }
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=users-multiple-servers.js.map