2 import { expect } from 'chai'
3 import { FIXTURE_URLS } from '@server/tests/shared'
4 import { areHttpImportTestsDisabled } from '@shared/core-utils'
5 import { HttpStatusCode, VideoChannelSyncState, VideoInclude, VideoPrivacy } from '@shared/models'
11 setAccessTokensToServers,
12 setDefaultAccountAvatar,
13 setDefaultChannelAvatar,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
18 describe('Test channel synchronizations', function () {
19 if (areHttpImportTestsDisabled()) return
21 function runSuite (mode: 'youtube-dl' | 'yt-dlp') {
23 describe('Sync using ' + mode, function () {
24 let server: PeerTubeServer
25 let command: ChannelSyncsCommand
27 let startTestDate: Date
29 let rootChannelSyncId: number
33 channelName: 'user1_channel',
38 async function changeDateForSync (channelSyncId: number, newDate: string) {
39 await server.sql.updateQuery(
40 `UPDATE "videoChannelSync" ` +
41 `SET "createdAt"='${newDate}', "lastSyncAt"='${newDate}' ` +
42 `WHERE id=${channelSyncId}`
46 before(async function () {
49 startTestDate = new Date()
51 server = await createSingleServer(1, getServerImportConfig(mode))
53 await setAccessTokensToServers([ server ])
54 await setDefaultVideoChannel([ server ])
55 await setDefaultChannelAvatar([ server ])
56 await setDefaultAccountAvatar([ server ])
58 await server.config.enableChannelSync()
60 command = server.channelSyncs
63 userInfo.accessToken = await server.users.generateUserAndToken(userInfo.username)
65 const { videoChannels } = await server.users.getMyInfo({ token: userInfo.accessToken })
66 userInfo.channelId = videoChannels[0].id
70 it('Should fetch the latest channel videos of a remote channel', async function () {
74 const { video } = await server.imports.importVideo({
76 channelId: server.store.channel.id,
77 privacy: VideoPrivacy.PUBLIC,
78 targetUrl: FIXTURE_URLS.youtube
82 expect(video.name).to.equal('small video - youtube')
84 const { total } = await server.videos.listByChannel({ handle: 'root_channel', include: VideoInclude.NOT_PUBLISHED_STATE })
85 expect(total).to.equal(1)
88 const { videoChannelSync } = await command.create({
90 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
91 videoChannelId: server.store.channel.id
93 token: server.accessToken,
94 expectedStatus: HttpStatusCode.OK_200
96 rootChannelSyncId = videoChannelSync.id
98 // Ensure any missing video not already fetched will be considered as new
99 await changeDateForSync(videoChannelSync.id, '1970-01-01')
101 await server.debug.sendCommand({
103 command: 'process-video-channel-sync-latest'
108 await waitJobs(server)
110 const { total, data } = await server.videos.listByChannel({ handle: 'root_channel', include: VideoInclude.NOT_PUBLISHED_STATE })
111 expect(total).to.equal(2)
112 expect(data[0].name).to.equal('test')
116 it('Should add another synchronization', async function () {
117 const externalChannelUrl = FIXTURE_URLS.youtubeChannel + '?foo=bar'
119 const { videoChannelSync } = await command.create({
122 videoChannelId: server.store.channel.id
124 token: server.accessToken,
125 expectedStatus: HttpStatusCode.OK_200
128 expect(videoChannelSync.externalChannelUrl).to.equal(externalChannelUrl)
129 expect(videoChannelSync.channel).to.include({
130 id: server.store.channel.id,
133 expect(videoChannelSync.state.id).to.equal(VideoChannelSyncState.WAITING_FIRST_RUN)
134 expect(new Date(videoChannelSync.createdAt)).to.be.above(startTestDate).and.to.be.at.most(new Date())
137 it('Should add a synchronization for another user', async function () {
138 const { videoChannelSync } = await command.create({
140 externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?baz=qux',
141 videoChannelId: userInfo.channelId
143 token: userInfo.accessToken
145 userInfo.syncId = videoChannelSync.id
148 it('Should not import a channel if not asked', async function () {
149 await waitJobs(server)
151 const { data } = await command.listByAccount({ accountName: userInfo.username })
153 expect(data[0].state).to.contain({
154 id: VideoChannelSyncState.WAITING_FIRST_RUN,
155 label: 'Waiting first run'
159 it('Should only fetch the videos newer than the creation date', async function () {
160 this.timeout(120_000)
162 await changeDateForSync(userInfo.syncId, '2019-03-01')
164 await server.debug.sendCommand({
166 command: 'process-video-channel-sync-latest'
170 await waitJobs(server)
172 const { data, total } = await server.videos.listByChannel({
173 handle: userInfo.channelName,
174 include: VideoInclude.NOT_PUBLISHED_STATE
177 expect(total).to.equal(1)
178 expect(data[0].name).to.equal('test')
181 it('Should list channel synchronizations', async function () {
184 const { total, data } = await command.listByAccount({ accountName: 'root' })
185 expect(total).to.equal(2)
187 expect(data[0]).to.deep.contain({
188 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
190 id: VideoChannelSyncState.SYNCED,
191 label: 'Synchronized'
195 expect(new Date(data[0].lastSyncAt)).to.be.greaterThan(startTestDate)
197 expect(data[0].channel).to.contain({ id: server.store.channel.id })
198 expect(data[1]).to.contain({ externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?foo=bar' })
203 const { total, data } = await command.listByAccount({ accountName: userInfo.username })
204 expect(total).to.equal(1)
205 expect(data[0]).to.deep.contain({
206 externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?baz=qux',
208 id: VideoChannelSyncState.SYNCED,
209 label: 'Synchronized'
215 it('Should list imports of a channel synchronization', async function () {
216 const { total, data } = await server.imports.getMyVideoImports({ videoChannelSyncId: rootChannelSyncId })
218 expect(total).to.equal(1)
219 expect(data).to.have.lengthOf(1)
220 expect(data[0].video.name).to.equal('test')
223 it('Should remove user\'s channel synchronizations', async function () {
224 await command.delete({ channelSyncId: userInfo.syncId })
226 const { total } = await command.listByAccount({ accountName: userInfo.username })
227 expect(total).to.equal(0)
230 after(async function () {
236 runSuite('youtube-dl')