1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { FIXTURE_URLS } from '@server/tests/shared'
5 import { areHttpImportTestsDisabled } from '@shared/core-utils'
6 import { VideoChannelSyncState, VideoInclude, VideoPrivacy } from '@shared/models'
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultChannelAvatar,
15 setDefaultVideoChannel,
17 } from '@shared/server-commands'
19 describe('Test channel synchronizations', function () {
20 if (areHttpImportTestsDisabled()) return
22 function runSuite (mode: 'youtube-dl' | 'yt-dlp') {
24 describe('Sync using ' + mode, function () {
25 let servers: PeerTubeServer[]
27 let startTestDate: Date
29 let rootChannelSyncId: number
33 channelName: 'user1_channel',
38 async function changeDateForSync (channelSyncId: number, newDate: string) {
39 await servers[0].sql.updateQuery(
40 `UPDATE "videoChannelSync" ` +
41 `SET "createdAt"='${newDate}', "lastSyncAt"='${newDate}' ` +
42 `WHERE id=${channelSyncId}`
46 async function listAllVideosOfChannel (channelName: string) {
47 return servers[0].videos.listByChannel({
49 include: VideoInclude.NOT_PUBLISHED_STATE
53 async function forceSyncAll (videoChannelSyncId: number, fromDate = '1970-01-01') {
54 await changeDateForSync(videoChannelSyncId, fromDate)
56 await servers[0].debug.sendCommand({
58 command: 'process-video-channel-sync-latest'
62 await waitJobs(servers)
65 before(async function () {
68 startTestDate = new Date()
70 servers = await createMultipleServers(2, getServerImportConfig(mode))
72 await setAccessTokensToServers(servers)
73 await setDefaultVideoChannel(servers)
74 await setDefaultChannelAvatar(servers)
75 await setDefaultAccountAvatar(servers)
77 await servers[0].config.enableChannelSync()
80 userInfo.accessToken = await servers[0].users.generateUserAndToken(userInfo.username)
82 const { videoChannels } = await servers[0].users.getMyInfo({ token: userInfo.accessToken })
83 userInfo.channelId = videoChannels[0].id
87 it('Should fetch the latest channel videos of a remote channel', async function () {
91 const { video } = await servers[0].imports.importVideo({
93 channelId: servers[0].store.channel.id,
94 privacy: VideoPrivacy.PUBLIC,
95 targetUrl: FIXTURE_URLS.youtube
99 expect(video.name).to.equal('small video - youtube')
100 expect(video.waitTranscoding).to.be.true
102 const { total } = await listAllVideosOfChannel('root_channel')
103 expect(total).to.equal(1)
106 const { videoChannelSync } = await servers[0].channelSyncs.create({
108 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
109 videoChannelId: servers[0].store.channel.id
112 rootChannelSyncId = videoChannelSync.id
114 await forceSyncAll(rootChannelSyncId)
117 const { total, data } = await listAllVideosOfChannel('root_channel')
118 expect(total).to.equal(2)
119 expect(data[0].name).to.equal('test')
120 expect(data[0].waitTranscoding).to.be.true
124 it('Should add another synchronization', async function () {
125 const externalChannelUrl = FIXTURE_URLS.youtubeChannel + '?foo=bar'
127 const { videoChannelSync } = await servers[0].channelSyncs.create({
130 videoChannelId: servers[0].store.channel.id
134 expect(videoChannelSync.externalChannelUrl).to.equal(externalChannelUrl)
135 expect(videoChannelSync.channel.id).to.equal(servers[0].store.channel.id)
136 expect(videoChannelSync.channel.name).to.equal('root_channel')
137 expect(videoChannelSync.state.id).to.equal(VideoChannelSyncState.WAITING_FIRST_RUN)
138 expect(new Date(videoChannelSync.createdAt)).to.be.above(startTestDate).and.to.be.at.most(new Date())
141 it('Should add a synchronization for another user', async function () {
142 const { videoChannelSync } = await servers[0].channelSyncs.create({
144 externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?baz=qux',
145 videoChannelId: userInfo.channelId
147 token: userInfo.accessToken
149 userInfo.syncId = videoChannelSync.id
152 it('Should not import a channel if not asked', async function () {
153 await waitJobs(servers)
155 const { data } = await servers[0].channelSyncs.listByAccount({ accountName: userInfo.username })
157 expect(data[0].state).to.contain({
158 id: VideoChannelSyncState.WAITING_FIRST_RUN,
159 label: 'Waiting first run'
163 it('Should only fetch the videos newer than the creation date', async function () {
164 this.timeout(120_000)
166 await forceSyncAll(userInfo.syncId, '2019-03-01')
168 const { data, total } = await listAllVideosOfChannel(userInfo.channelName)
170 expect(total).to.equal(1)
171 expect(data[0].name).to.equal('test')
174 it('Should list channel synchronizations', async function () {
177 const { total, data } = await servers[0].channelSyncs.listByAccount({ accountName: 'root' })
178 expect(total).to.equal(2)
180 expect(data[0]).to.deep.contain({
181 externalChannelUrl: FIXTURE_URLS.youtubeChannel,
183 id: VideoChannelSyncState.SYNCED,
184 label: 'Synchronized'
188 expect(new Date(data[0].lastSyncAt)).to.be.greaterThan(startTestDate)
190 expect(data[0].channel).to.contain({ id: servers[0].store.channel.id })
191 expect(data[1]).to.contain({ externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?foo=bar' })
196 const { total, data } = await servers[0].channelSyncs.listByAccount({ accountName: userInfo.username })
197 expect(total).to.equal(1)
198 expect(data[0]).to.deep.contain({
199 externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?baz=qux',
201 id: VideoChannelSyncState.SYNCED,
202 label: 'Synchronized'
208 it('Should list imports of a channel synchronization', async function () {
209 const { total, data } = await servers[0].imports.getMyVideoImports({ videoChannelSyncId: rootChannelSyncId })
211 expect(total).to.equal(1)
212 expect(data).to.have.lengthOf(1)
213 expect(data[0].video.name).to.equal('test')
216 it('Should remove user\'s channel synchronizations', async function () {
217 await servers[0].channelSyncs.delete({ channelSyncId: userInfo.syncId })
219 const { total } = await servers[0].channelSyncs.listByAccount({ accountName: userInfo.username })
220 expect(total).to.equal(0)
223 // FIXME: youtube-dl/yt-dlp doesn't work when speicifying a port after the hostname
224 // it('Should import a remote PeerTube channel', async function () {
225 // this.timeout(240_000)
227 // await servers[1].videos.quickUpload({ name: 'remote 1' })
228 // await waitJobs(servers)
230 // const { videoChannelSync } = await servers[0].channelSyncs.create({
232 // externalChannelUrl: servers[1].url + '/c/root_channel',
233 // videoChannelId: userInfo.channelId
235 // token: userInfo.accessToken
237 // await servers[0].channels.importVideos({
238 // channelName: userInfo.channelName,
239 // externalChannelUrl: servers[1].url + '/c/root_channel',
240 // videoChannelSyncId: videoChannelSync.id,
241 // token: userInfo.accessToken
244 // await waitJobs(servers)
246 // const { data, total } = await servers[0].videos.listByChannel({
247 // handle: userInfo.channelName,
248 // include: VideoInclude.NOT_PUBLISHED_STATE
251 // expect(total).to.equal(2)
252 // expect(data[0].name).to.equal('remote 1')
255 // it('Should keep synced a remote PeerTube channel', async function () {
256 // this.timeout(240_000)
258 // await servers[1].videos.quickUpload({ name: 'remote 2' })
259 // await waitJobs(servers)
261 // await servers[0].debug.sendCommand({
263 // command: 'process-video-channel-sync-latest'
267 // await waitJobs(servers)
269 // const { data, total } = await servers[0].videos.listByChannel({
270 // handle: userInfo.channelName,
271 // include: VideoInclude.NOT_PUBLISHED_STATE
273 // expect(total).to.equal(2)
274 // expect(data[0].name).to.equal('remote 2')
277 it('Should fetch the latest videos of a youtube playlist', async function () {
278 this.timeout(120_000)
280 const { id: channelId } = await servers[0].channels.create({
286 const { videoChannelSync: { id: videoChannelSyncId } } = await servers[0].channelSyncs.create({
288 externalChannelUrl: FIXTURE_URLS.youtubePlaylist,
289 videoChannelId: channelId
293 await forceSyncAll(videoChannelSyncId)
297 const { total, data } = await listAllVideosOfChannel('channel2')
298 expect(total).to.equal(2)
299 expect(data[0].name).to.equal('test')
300 expect(data[1].name).to.equal('small video - youtube')
304 after(async function () {
305 await killallServers(servers)
310 // FIXME: suite is broken with youtube-dl
311 // runSuite('youtube-dl')