"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 videos import in a channel', function () {
    if ((0, core_utils_1.areHttpImportTestsDisabled)())
        return;
    function runSuite(mode) {
        describe('Import using ' + mode, function () {
            let server;
            before(function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    server = yield (0, server_commands_1.createSingleServer)(1, (0, server_commands_1.getServerImportConfig)(mode));
                    yield (0, server_commands_1.setAccessTokensToServers)([server]);
                    yield (0, server_commands_1.setDefaultVideoChannel)([server]);
                    yield server.config.enableChannelSync();
                });
            });
            it('Should import a whole channel without specifying the sync id', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(240000);
                    yield server.channels.importVideos({ channelName: server.store.channel.name, externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel });
                    yield (0, server_commands_1.waitJobs)(server);
                    const videos = yield server.videos.listByChannel({ handle: server.store.channel.name });
                    (0, chai_1.expect)(videos.total).to.equal(2);
                });
            });
            it('These imports should not have a sync id', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield server.imports.getMyVideoImports();
                    (0, chai_1.expect)(total).to.equal(2);
                    (0, chai_1.expect)(data).to.have.lengthOf(2);
                    for (const videoImport of data) {
                        (0, chai_1.expect)(videoImport.videoChannelSync).to.not.exist;
                    }
                });
            });
            it('Should import a whole channel and specifying the sync id', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(240000);
                    {
                        server.store.channel.name = 'channel2';
                        const { id } = yield server.channels.create({ attributes: { name: server.store.channel.name } });
                        server.store.channel.id = id;
                    }
                    {
                        const attributes = {
                            externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                            videoChannelId: server.store.channel.id
                        };
                        const { videoChannelSync } = yield server.channelSyncs.create({ attributes });
                        server.store.videoChannelSync = videoChannelSync;
                        yield (0, server_commands_1.waitJobs)(server);
                    }
                    yield server.channels.importVideos({
                        channelName: server.store.channel.name,
                        externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                        videoChannelSyncId: server.store.videoChannelSync.id
                    });
                    yield (0, server_commands_1.waitJobs)(server);
                });
            });
            it('These imports should have a sync id', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield server.imports.getMyVideoImports();
                    (0, chai_1.expect)(total).to.equal(4);
                    (0, chai_1.expect)(data).to.have.lengthOf(4);
                    const importsWithSyncId = data.filter(i => !!i.videoChannelSync);
                    (0, chai_1.expect)(importsWithSyncId).to.have.lengthOf(2);
                    for (const videoImport of importsWithSyncId) {
                        (0, chai_1.expect)(videoImport.videoChannelSync).to.exist;
                        (0, chai_1.expect)(videoImport.videoChannelSync.id).to.equal(server.store.videoChannelSync.id);
                    }
                });
            });
            it('Should be able to filter imports by this sync id', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield server.imports.getMyVideoImports({ videoChannelSyncId: server.store.videoChannelSync.id });
                    (0, chai_1.expect)(total).to.equal(2);
                    (0, chai_1.expect)(data).to.have.lengthOf(2);
                    for (const videoImport of data) {
                        (0, chai_1.expect)(videoImport.videoChannelSync).to.exist;
                        (0, chai_1.expect)(videoImport.videoChannelSync.id).to.equal(server.store.videoChannelSync.id);
                    }
                });
            });
            it('Should limit max amount of videos synced on full sync', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(240000);
                    yield server.kill();
                    yield server.run({
                        import: {
                            video_channel_synchronization: {
                                full_sync_videos_limit: 1
                            }
                        }
                    });
                    const { id } = yield server.channels.create({ attributes: { name: 'channel3' } });
                    const channel3Id = id;
                    const { videoChannelSync } = yield server.channelSyncs.create({
                        attributes: {
                            externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                            videoChannelId: channel3Id
                        }
                    });
                    const syncId = videoChannelSync.id;
                    yield (0, server_commands_1.waitJobs)(server);
                    yield server.channels.importVideos({
                        channelName: 'channel3',
                        externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                        videoChannelSyncId: syncId
                    });
                    yield (0, server_commands_1.waitJobs)(server);
                    const { total, data } = yield server.videos.listByChannel({ handle: 'channel3' });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            after(function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield (server === null || server === void 0 ? void 0 : server.kill());
                });
            });
        });
    }
    runSuite('yt-dlp');
});
//# sourceMappingURL=channel-import-videos.js.map