"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 activitypub', function () {
    let servers = [];
    let video;
    let playlist;
    function testAccount(path) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const res = yield (0, server_commands_1.makeActivityPubGetRequest)(servers[0].url, path);
            const object = res.body;
            (0, chai_1.expect)(object.type).to.equal('Person');
            (0, chai_1.expect)(object.id).to.equal(servers[0].url + '/accounts/root');
            (0, chai_1.expect)(object.name).to.equal('root');
            (0, chai_1.expect)(object.preferredUsername).to.equal('root');
        });
    }
    function testChannel(path) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const res = yield (0, server_commands_1.makeActivityPubGetRequest)(servers[0].url, path);
            const object = res.body;
            (0, chai_1.expect)(object.type).to.equal('Group');
            (0, chai_1.expect)(object.id).to.equal(servers[0].url + '/video-channels/root_channel');
            (0, chai_1.expect)(object.name).to.equal('Main root channel');
            (0, chai_1.expect)(object.preferredUsername).to.equal('root_channel');
        });
    }
    function testVideo(path) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const res = yield (0, server_commands_1.makeActivityPubGetRequest)(servers[0].url, path);
            const object = res.body;
            (0, chai_1.expect)(object.type).to.equal('Video');
            (0, chai_1.expect)(object.id).to.equal(servers[0].url + '/videos/watch/' + video.uuid);
            (0, chai_1.expect)(object.name).to.equal('video');
        });
    }
    function testPlaylist(path) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const res = yield (0, server_commands_1.makeActivityPubGetRequest)(servers[0].url, path);
            const object = res.body;
            (0, chai_1.expect)(object.type).to.equal('Playlist');
            (0, chai_1.expect)(object.id).to.equal(servers[0].url + '/video-playlists/' + playlist.uuid);
            (0, chai_1.expect)(object.name).to.equal('playlist');
        });
    }
    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 (0, server_commands_1.setDefaultVideoChannel)(servers);
            {
                video = yield servers[0].videos.quickUpload({ name: 'video' });
            }
            {
                const attributes = { displayName: 'playlist', privacy: 1, videoChannelId: servers[0].store.channel.id };
                playlist = yield servers[0].playlists.create({ attributes });
            }
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        });
    });
    it('Should return the account object', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield testAccount('/accounts/root');
            yield testAccount('/a/root');
        });
    });
    it('Should return the channel object', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield testChannel('/video-channels/root_channel');
            yield testChannel('/c/root_channel');
        });
    });
    it('Should return the video object', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield testVideo('/videos/watch/' + video.id);
            yield testVideo('/videos/watch/' + video.uuid);
            yield testVideo('/videos/watch/' + video.shortUUID);
            yield testVideo('/w/' + video.id);
            yield testVideo('/w/' + video.uuid);
            yield testVideo('/w/' + video.shortUUID);
        });
    });
    it('Should return the playlist object', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield testPlaylist('/video-playlists/' + playlist.id);
            yield testPlaylist('/video-playlists/' + playlist.uuid);
            yield testPlaylist('/video-playlists/' + playlist.shortUUID);
            yield testPlaylist('/w/p/' + playlist.id);
            yield testPlaylist('/w/p/' + playlist.uuid);
            yield testPlaylist('/w/p/' + playlist.shortUUID);
            yield testPlaylist('/videos/watch/playlist/' + playlist.id);
            yield testPlaylist('/videos/watch/playlist/' + playlist.uuid);
            yield testPlaylist('/videos/watch/playlist/' + playlist.shortUUID);
        });
    });
    it('Should redirect to the origin video object', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const res = yield (0, server_commands_1.makeActivityPubGetRequest)(servers[1].url, '/videos/watch/' + video.uuid, 302);
            (0, chai_1.expect)(res.header.location).to.equal(servers[0].url + '/videos/watch/' + video.uuid);
        });
    });
    it('Should return the watch action', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(50000);
            yield servers[0].views.simulateViewer({ id: video.uuid, currentTimes: [0, 2] });
            yield (0, shared_1.processViewersStats)(servers);
            const res = yield (0, server_commands_1.makeActivityPubGetRequest)(servers[0].url, '/videos/local-viewer/1', 200);
            const object = res.body;
            (0, chai_1.expect)(object.type).to.equal('WatchAction');
            (0, chai_1.expect)(object.duration).to.equal('PT2S');
            (0, chai_1.expect)(object.actionStatus).to.equal('CompletedActionStatus');
            (0, chai_1.expect)(object.watchSections).to.have.lengthOf(1);
            (0, chai_1.expect)(object.watchSections[0].startTimestamp).to.equal(0);
            (0, chai_1.expect)(object.watchSections[0].endTimestamp).to.equal(2);
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=client.js.map