"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test channel synchronizations', function () {
    if ((0, core_utils_1.areHttpImportTestsDisabled)())
        return;
    function runSuite(mode) {
        describe('Sync using ' + mode, function () {
            let servers;
            let startTestDate;
            let rootChannelSyncId;
            const userInfo = {
                accessToken: '',
                username: 'user1',
                channelName: 'user1_channel',
                channelId: -1,
                syncId: -1
            };
            function changeDateForSync(channelSyncId, newDate) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].sql.updateQuery(`UPDATE "videoChannelSync" ` +
                        `SET "createdAt"='${newDate}', "lastSyncAt"='${newDate}' ` +
                        `WHERE id=${channelSyncId}`);
                });
            }
            function listAllVideosOfChannel(channelName) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    return servers[0].videos.listByChannel({
                        handle: channelName,
                        include: 1
                    });
                });
            }
            function forceSyncAll(videoChannelSyncId, fromDate = '1970-01-01') {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield changeDateForSync(videoChannelSyncId, fromDate);
                    yield servers[0].debug.sendCommand({
                        body: {
                            command: 'process-video-channel-sync-latest'
                        }
                    });
                    yield (0, server_commands_1.waitJobs)(servers);
                });
            }
            before(function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(240000);
                    startTestDate = new Date();
                    servers = yield (0, server_commands_1.createMultipleServers)(2, (0, server_commands_1.getServerImportConfig)(mode));
                    yield (0, server_commands_1.setAccessTokensToServers)(servers);
                    yield (0, server_commands_1.setDefaultVideoChannel)(servers);
                    yield (0, server_commands_1.setDefaultChannelAvatar)(servers);
                    yield (0, server_commands_1.setDefaultAccountAvatar)(servers);
                    yield servers[0].config.enableChannelSync();
                    {
                        userInfo.accessToken = yield servers[0].users.generateUserAndToken(userInfo.username);
                        const { videoChannels } = yield servers[0].users.getMyInfo({ token: userInfo.accessToken });
                        userInfo.channelId = videoChannels[0].id;
                    }
                });
            });
            it('Should fetch the latest channel videos of a remote channel', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    {
                        const { video } = yield servers[0].imports.importVideo({
                            attributes: {
                                channelId: servers[0].store.channel.id,
                                privacy: 1,
                                targetUrl: shared_1.FIXTURE_URLS.youtube
                            }
                        });
                        (0, chai_1.expect)(video.name).to.equal('small video - youtube');
                        (0, chai_1.expect)(video.waitTranscoding).to.be.true;
                        const { total } = yield listAllVideosOfChannel('root_channel');
                        (0, chai_1.expect)(total).to.equal(1);
                    }
                    const { videoChannelSync } = yield servers[0].channelSyncs.create({
                        attributes: {
                            externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                            videoChannelId: servers[0].store.channel.id
                        }
                    });
                    rootChannelSyncId = videoChannelSync.id;
                    yield forceSyncAll(rootChannelSyncId);
                    {
                        const { total, data } = yield listAllVideosOfChannel('root_channel');
                        (0, chai_1.expect)(total).to.equal(2);
                        (0, chai_1.expect)(data[0].name).to.equal('test');
                        (0, chai_1.expect)(data[0].waitTranscoding).to.be.true;
                    }
                });
            });
            it('Should add another synchronization', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const externalChannelUrl = shared_1.FIXTURE_URLS.youtubeChannel + '?foo=bar';
                    const { videoChannelSync } = yield servers[0].channelSyncs.create({
                        attributes: {
                            externalChannelUrl,
                            videoChannelId: servers[0].store.channel.id
                        }
                    });
                    (0, chai_1.expect)(videoChannelSync.externalChannelUrl).to.equal(externalChannelUrl);
                    (0, chai_1.expect)(videoChannelSync.channel.id).to.equal(servers[0].store.channel.id);
                    (0, chai_1.expect)(videoChannelSync.channel.name).to.equal('root_channel');
                    (0, chai_1.expect)(videoChannelSync.state.id).to.equal(1);
                    (0, chai_1.expect)(new Date(videoChannelSync.createdAt)).to.be.above(startTestDate).and.to.be.at.most(new Date());
                });
            });
            it('Should add a synchronization for another user', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { videoChannelSync } = yield servers[0].channelSyncs.create({
                        attributes: {
                            externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel + '?baz=qux',
                            videoChannelId: userInfo.channelId
                        },
                        token: userInfo.accessToken
                    });
                    userInfo.syncId = videoChannelSync.id;
                });
            });
            it('Should not import a channel if not asked', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield (0, server_commands_1.waitJobs)(servers);
                    const { data } = yield servers[0].channelSyncs.listByAccount({ accountName: userInfo.username });
                    (0, chai_1.expect)(data[0].state).to.contain({
                        id: 1,
                        label: 'Waiting first run'
                    });
                });
            });
            it('Should only fetch the videos newer than the creation date', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    yield forceSyncAll(userInfo.syncId, '2019-03-01');
                    const { data, total } = yield listAllVideosOfChannel(userInfo.channelName);
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data[0].name).to.equal('test');
                });
            });
            it('Should list channel synchronizations', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    {
                        const { total, data } = yield servers[0].channelSyncs.listByAccount({ accountName: 'root' });
                        (0, chai_1.expect)(total).to.equal(2);
                        (0, chai_1.expect)(data[0]).to.deep.contain({
                            externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                            state: {
                                id: 3,
                                label: 'Synchronized'
                            }
                        });
                        (0, chai_1.expect)(new Date(data[0].lastSyncAt)).to.be.greaterThan(startTestDate);
                        (0, chai_1.expect)(data[0].channel).to.contain({ id: servers[0].store.channel.id });
                        (0, chai_1.expect)(data[1]).to.contain({ externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel + '?foo=bar' });
                    }
                    {
                        const { total, data } = yield servers[0].channelSyncs.listByAccount({ accountName: userInfo.username });
                        (0, chai_1.expect)(total).to.equal(1);
                        (0, chai_1.expect)(data[0]).to.deep.contain({
                            externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel + '?baz=qux',
                            state: {
                                id: 3,
                                label: 'Synchronized'
                            }
                        });
                    }
                });
            });
            it('Should list imports of a channel synchronization', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].imports.getMyVideoImports({ videoChannelSyncId: rootChannelSyncId });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                    (0, chai_1.expect)(data[0].video.name).to.equal('test');
                });
            });
            it('Should remove user\'s channel synchronizations', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].channelSyncs.delete({ channelSyncId: userInfo.syncId });
                    const { total } = yield servers[0].channelSyncs.listByAccount({ accountName: userInfo.username });
                    (0, chai_1.expect)(total).to.equal(0);
                });
            });
            it('Should fetch the latest videos of a youtube playlist', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    const { id: channelId } = yield servers[0].channels.create({
                        attributes: {
                            name: 'channel2'
                        }
                    });
                    const { videoChannelSync: { id: videoChannelSyncId } } = yield servers[0].channelSyncs.create({
                        attributes: {
                            externalChannelUrl: shared_1.FIXTURE_URLS.youtubePlaylist,
                            videoChannelId: channelId
                        }
                    });
                    yield forceSyncAll(videoChannelSyncId);
                    {
                        const { total, data } = yield listAllVideosOfChannel('channel2');
                        (0, chai_1.expect)(total).to.equal(2);
                        (0, chai_1.expect)(data[0].name).to.equal('test');
                        (0, chai_1.expect)(data[1].name).to.equal('small video - youtube');
                    }
                });
            });
            after(function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield (0, server_commands_1.killallServers)(servers);
                });
            });
        });
    }
    runSuite('youtube-dl');
    runSuite('yt-dlp');
});
//# sourceMappingURL=video-channel-syncs.js.map