"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const chai = (0, tslib_1.__importStar)(require("chai"));
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
const expect = chai.expect;
describe('Test ActivityPub videos search', function () {
    let servers;
    let videoServer1UUID;
    let videoServer2UUID;
    let command;
    before(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            {
                const { uuid } = yield servers[0].videos.upload({ attributes: { name: 'video 1 on server 1' } });
                videoServer1UUID = uuid;
            }
            {
                const { uuid } = yield servers[1].videos.upload({ attributes: { name: 'video 1 on server 2' } });
                videoServer2UUID = uuid;
            }
            yield (0, server_commands_1.waitJobs)(servers);
            command = servers[0].search;
        });
    });
    it('Should not find a remote video', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            {
                const search = servers[1].url + '/videos/watch/43';
                const body = yield command.searchVideos({ search, token: servers[0].accessToken });
                expect(body.total).to.equal(0);
                expect(body.data).to.be.an('array');
                expect(body.data).to.have.lengthOf(0);
            }
            {
                const search = servers[1].url + '/videos/watch/' + videoServer2UUID;
                const body = yield command.searchVideos({ search });
                expect(body.total).to.equal(0);
                expect(body.data).to.be.an('array');
                expect(body.data).to.have.lengthOf(0);
            }
        });
    });
    it('Should search a local video', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const search = servers[0].url + '/videos/watch/' + videoServer1UUID;
            const body = yield command.searchVideos({ search });
            expect(body.total).to.equal(1);
            expect(body.data).to.be.an('array');
            expect(body.data).to.have.lengthOf(1);
            expect(body.data[0].name).to.equal('video 1 on server 1');
        });
    });
    it('Should search a local video with an alternative URL', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const search = servers[0].url + '/w/' + videoServer1UUID;
            const body1 = yield command.searchVideos({ search });
            const body2 = yield command.searchVideos({ search, token: servers[0].accessToken });
            for (const body of [body1, body2]) {
                expect(body.total).to.equal(1);
                expect(body.data).to.be.an('array');
                expect(body.data).to.have.lengthOf(1);
                expect(body.data[0].name).to.equal('video 1 on server 1');
            }
        });
    });
    it('Should search a local video with a query in URL', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const searches = [
                servers[0].url + '/w/' + videoServer1UUID,
                servers[0].url + '/videos/watch/' + videoServer1UUID
            ];
            for (const search of searches) {
                for (const token of [undefined, servers[0].accessToken]) {
                    const body = yield command.searchVideos({ search: search + '?startTime=4', token });
                    expect(body.total).to.equal(1);
                    expect(body.data).to.be.an('array');
                    expect(body.data).to.have.lengthOf(1);
                    expect(body.data[0].name).to.equal('video 1 on server 1');
                }
            }
        });
    });
    it('Should search a remote video', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const searches = [
                servers[1].url + '/w/' + videoServer2UUID,
                servers[1].url + '/videos/watch/' + videoServer2UUID
            ];
            for (const search of searches) {
                const body = yield command.searchVideos({ search, token: servers[0].accessToken });
                expect(body.total).to.equal(1);
                expect(body.data).to.be.an('array');
                expect(body.data).to.have.lengthOf(1);
                expect(body.data[0].name).to.equal('video 1 on server 2');
            }
        });
    });
    it('Should not list this remote video', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const { total, data } = yield servers[0].videos.list();
            expect(total).to.equal(1);
            expect(data).to.have.lengthOf(1);
            expect(data[0].name).to.equal('video 1 on server 1');
        });
    });
    it('Should update video of server 2, and refresh it on server 1', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(120000);
            const channelAttributes = {
                name: 'super_channel',
                displayName: 'super channel'
            };
            const created = yield servers[1].channels.create({ attributes: channelAttributes });
            const videoChannelId = created.id;
            const attributes = {
                name: 'updated',
                tag: ['tag1', 'tag2'],
                privacy: 2,
                channelId: videoChannelId
            };
            yield servers[1].videos.update({ id: videoServer2UUID, attributes });
            yield (0, server_commands_1.waitJobs)(servers);
            yield (0, core_utils_1.wait)(10000);
            const search = servers[1].url + '/videos/watch/' + videoServer2UUID;
            yield command.searchVideos({ search, token: servers[0].accessToken });
            yield (0, core_utils_1.wait)(5000);
            const body = yield command.searchVideos({ search, token: servers[0].accessToken });
            expect(body.total).to.equal(1);
            expect(body.data).to.have.lengthOf(1);
            const video = body.data[0];
            expect(video.name).to.equal('updated');
            expect(video.channel.name).to.equal('super_channel');
            expect(video.privacy.id).to.equal(2);
        });
    });
    it('Should delete video of server 2, and delete it on server 1', function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(120000);
            yield servers[1].videos.remove({ id: videoServer2UUID });
            yield (0, server_commands_1.waitJobs)(servers);
            yield (0, core_utils_1.wait)(10000);
            const search = servers[1].url + '/videos/watch/' + videoServer2UUID;
            yield command.searchVideos({ search, token: servers[0].accessToken });
            yield (0, core_utils_1.wait)(5000);
            const body = yield command.searchVideos({ search, token: servers[0].accessToken });
            expect(body.total).to.equal(0);
            expect(body.data).to.have.lengthOf(0);
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=search-activitypub-videos.js.map