1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { pathExists, readdir, remove } from 'fs-extra'
5 import { join } from 'path'
6 import { FIXTURE_URLS, testCaptionFile, testImage } from '@server/tests/shared'
7 import { areHttpImportTestsDisabled } from '@shared/core-utils'
8 import { CustomConfig, HttpStatusCode, Video, VideoImportState, VideoPrivacy, VideoResolution, VideoState } from '@shared/models'
11 createMultipleServers,
14 getServerImportConfig,
16 setAccessTokensToServers,
17 setDefaultVideoChannel,
19 } from '@shared/server-commands'
20 import { DeepPartial } from '@shared/typescript-utils'
22 async function checkVideosServer1 (server: PeerTubeServer, idHttp: string, idMagnet: string, idTorrent: string) {
23 const videoHttp = await server.videos.get({ id: idHttp })
25 expect(videoHttp.name).to.equal('small video - youtube')
26 // FIXME: youtube-dl seems broken
27 // expect(videoHttp.category.label).to.equal('News & Politics')
28 // expect(videoHttp.licence.label).to.equal('Attribution')
29 expect(videoHttp.language.label).to.equal('Unknown')
30 expect(videoHttp.nsfw).to.be.false
31 expect(videoHttp.description).to.equal('this is a super description')
32 expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ])
33 expect(videoHttp.files).to.have.lengthOf(1)
35 const originallyPublishedAt = new Date(videoHttp.originallyPublishedAt)
36 expect(originallyPublishedAt.getDate()).to.equal(14)
37 expect(originallyPublishedAt.getMonth()).to.equal(0)
38 expect(originallyPublishedAt.getFullYear()).to.equal(2019)
40 const videoMagnet = await server.videos.get({ id: idMagnet })
41 const videoTorrent = await server.videos.get({ id: idTorrent })
43 for (const video of [ videoMagnet, videoTorrent ]) {
44 expect(video.category.label).to.equal('Misc')
45 expect(video.licence.label).to.equal('Unknown')
46 expect(video.language.label).to.equal('Unknown')
47 expect(video.nsfw).to.be.false
48 expect(video.description).to.equal('this is a super torrent description')
49 expect(video.tags).to.deep.equal([ 'tag_torrent1', 'tag_torrent2' ])
50 expect(video.files).to.have.lengthOf(1)
53 expect(videoTorrent.name).to.contain('你好 世界 720p.mp4')
54 expect(videoMagnet.name).to.contain('super peertube2 video')
56 const bodyCaptions = await server.captions.list({ videoId: idHttp })
57 expect(bodyCaptions.total).to.equal(2)
60 async function checkVideoServer2 (server: PeerTubeServer, id: number | string) {
61 const video = await server.videos.get({ id })
63 expect(video.name).to.equal('my super name')
64 expect(video.category.label).to.equal('Entertainment')
65 expect(video.licence.label).to.equal('Public Domain Dedication')
66 expect(video.language.label).to.equal('English')
67 expect(video.nsfw).to.be.false
68 expect(video.description).to.equal('my super description')
69 expect(video.tags).to.deep.equal([ 'supertag1', 'supertag2' ])
71 expect(video.files).to.have.lengthOf(1)
73 const bodyCaptions = await server.captions.list({ videoId: id })
74 expect(bodyCaptions.total).to.equal(2)
77 describe('Test video imports', function () {
79 if (areHttpImportTestsDisabled()) return
81 function runSuite (mode: 'youtube-dl' | 'yt-dlp') {
83 describe('Import ' + mode, function () {
84 let servers: PeerTubeServer[] = []
86 before(async function () {
89 servers = await createMultipleServers(2, getServerImportConfig(mode))
91 await setAccessTokensToServers(servers)
92 await setDefaultVideoChannel(servers)
94 for (const server of servers) {
95 await server.config.updateExistingSubConfig({
98 alwaysTranscodeOriginalResolution: false
104 await doubleFollow(servers[0], servers[1])
107 it('Should import videos on server 1', async function () {
110 const baseAttributes = {
111 channelId: servers[0].store.channel.id,
112 privacy: VideoPrivacy.PUBLIC
116 const attributes = { ...baseAttributes, targetUrl: FIXTURE_URLS.youtube }
117 const { video } = await servers[0].imports.importVideo({ attributes })
118 expect(video.name).to.equal('small video - youtube')
121 expect(video.thumbnailPath).to.match(new RegExp(`^/static/thumbnails/.+.jpg$`))
122 expect(video.previewPath).to.match(new RegExp(`^/lazy-static/previews/.+.jpg$`))
124 const suffix = mode === 'yt-dlp'
128 await testImage(servers[0].url, 'video_import_thumbnail' + suffix, video.thumbnailPath)
129 await testImage(servers[0].url, 'video_import_preview' + suffix, video.previewPath)
132 const bodyCaptions = await servers[0].captions.list({ videoId: video.id })
133 const videoCaptions = bodyCaptions.data
134 expect(videoCaptions).to.have.lengthOf(2)
137 const enCaption = videoCaptions.find(caption => caption.language.id === 'en')
138 expect(enCaption).to.exist
139 expect(enCaption.language.label).to.equal('English')
140 expect(enCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-en.vtt$`))
142 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+` +
143 `(Language: en[ \n]+)?` +
144 `00:00:01.600 --> 00:00:04.200( position:\\d+% line:\\d+%)?[ \n]+English \\(US\\)[ \n]+` +
145 `00:00:05.900 --> 00:00:07.999( position:\\d+% line:\\d+%)?[ \n]+This is a subtitle in American English[ \n]+` +
146 `00:00:10.000 --> 00:00:14.000( position:\\d+% line:\\d+%)?[ \n]+Adding subtitles is very easy to do`
147 await testCaptionFile(servers[0].url, enCaption.captionPath, new RegExp(regex))
151 const frCaption = videoCaptions.find(caption => caption.language.id === 'fr')
152 expect(frCaption).to.exist
153 expect(frCaption.language.label).to.equal('French')
154 expect(frCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-fr.vtt`))
156 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+` +
157 `(Language: fr[ \n]+)?` +
158 `00:00:01.600 --> 00:00:04.200( position:\\d+% line:\\d+%)?[ \n]+Français \\(FR\\)[ \n]+` +
159 `00:00:05.900 --> 00:00:07.999( position:\\d+% line:\\d+%)?[ \n]+C'est un sous-titre français[ \n]+` +
160 `00:00:10.000 --> 00:00:14.000( position:\\d+% line:\\d+%)?[ \n]+Ajouter un sous-titre est vraiment facile`
162 await testCaptionFile(servers[0].url, frCaption.captionPath, new RegExp(regex))
169 magnetUri: FIXTURE_URLS.magnet,
170 description: 'this is a super torrent description',
171 tags: [ 'tag_torrent1', 'tag_torrent2' ]
173 const { video } = await servers[0].imports.importVideo({ attributes })
174 expect(video.name).to.equal('super peertube2 video')
180 torrentfile: 'video-720p.torrent' as any,
181 description: 'this is a super torrent description',
182 tags: [ 'tag_torrent1', 'tag_torrent2' ]
184 const { video } = await servers[0].imports.importVideo({ attributes })
185 expect(video.name).to.equal('你好 世界 720p.mp4')
189 it('Should list the videos to import in my videos on server 1', async function () {
190 const { total, data } = await servers[0].videos.listMyVideos({ sort: 'createdAt' })
192 expect(total).to.equal(3)
194 expect(data).to.have.lengthOf(3)
195 expect(data[0].name).to.equal('small video - youtube')
196 expect(data[1].name).to.equal('super peertube2 video')
197 expect(data[2].name).to.equal('你好 世界 720p.mp4')
200 it('Should list the videos to import in my imports on server 1', async function () {
201 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ sort: '-createdAt' })
202 expect(total).to.equal(3)
204 expect(videoImports).to.have.lengthOf(3)
206 expect(videoImports[2].targetUrl).to.equal(FIXTURE_URLS.youtube)
207 expect(videoImports[2].magnetUri).to.be.null
208 expect(videoImports[2].torrentName).to.be.null
209 expect(videoImports[2].video.name).to.equal('small video - youtube')
211 expect(videoImports[1].targetUrl).to.be.null
212 expect(videoImports[1].magnetUri).to.equal(FIXTURE_URLS.magnet)
213 expect(videoImports[1].torrentName).to.be.null
214 expect(videoImports[1].video.name).to.equal('super peertube2 video')
216 expect(videoImports[0].targetUrl).to.be.null
217 expect(videoImports[0].magnetUri).to.be.null
218 expect(videoImports[0].torrentName).to.equal('video-720p.torrent')
219 expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
222 it('Should filter my imports on target URL', async function () {
223 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ targetUrl: FIXTURE_URLS.youtube })
224 expect(total).to.equal(1)
225 expect(videoImports).to.have.lengthOf(1)
227 expect(videoImports[0].targetUrl).to.equal(FIXTURE_URLS.youtube)
230 it('Should search in my imports', async function () {
231 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ search: 'peertube2' })
232 expect(total).to.equal(1)
233 expect(videoImports).to.have.lengthOf(1)
235 expect(videoImports[0].magnetUri).to.equal(FIXTURE_URLS.magnet)
236 expect(videoImports[0].video.name).to.equal('super peertube2 video')
239 it('Should have the video listed on the two instances', async function () {
240 this.timeout(120_000)
242 await waitJobs(servers)
244 for (const server of servers) {
245 const { total, data } = await server.videos.list()
246 expect(total).to.equal(3)
247 expect(data).to.have.lengthOf(3)
249 const [ videoHttp, videoMagnet, videoTorrent ] = data
250 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
254 it('Should import a video on server 2 with some fields', async function () {
258 targetUrl: FIXTURE_URLS.youtube,
259 channelId: servers[1].store.channel.id,
260 privacy: VideoPrivacy.PUBLIC,
264 name: 'my super name',
265 description: 'my super description',
266 tags: [ 'supertag1', 'supertag2' ]
268 const { video } = await servers[1].imports.importVideo({ attributes })
269 expect(video.name).to.equal('my super name')
272 it('Should have the videos listed on the two instances', async function () {
273 this.timeout(120_000)
275 await waitJobs(servers)
277 for (const server of servers) {
278 const { total, data } = await server.videos.list()
279 expect(total).to.equal(4)
280 expect(data).to.have.lengthOf(4)
282 await checkVideoServer2(server, data[0].uuid)
284 const [ , videoHttp, videoMagnet, videoTorrent ] = data
285 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
289 it('Should import a video that will be transcoded', async function () {
290 this.timeout(240_000)
293 name: 'transcoded video',
294 magnetUri: FIXTURE_URLS.magnet,
295 channelId: servers[1].store.channel.id,
296 privacy: VideoPrivacy.PUBLIC
298 const { video } = await servers[1].imports.importVideo({ attributes })
299 const videoUUID = video.uuid
301 await waitJobs(servers)
303 for (const server of servers) {
304 const video = await server.videos.get({ id: videoUUID })
306 expect(video.name).to.equal('transcoded video')
307 expect(video.files).to.have.lengthOf(4)
311 it('Should import no HDR version on a HDR video', async function () {
312 this.timeout(300_000)
314 const config: DeepPartial<CustomConfig> = {
324 '1080p': false, // the resulting resolution shouldn't be higher than this, and not vp9.2/av01
328 webtorrent: { enabled: true },
329 hls: { enabled: false }
332 await servers[0].config.updateExistingSubConfig({ newConfig: config })
336 targetUrl: FIXTURE_URLS.youtubeHDR,
337 channelId: servers[0].store.channel.id,
338 privacy: VideoPrivacy.PUBLIC
340 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
341 const videoUUID = videoImported.uuid
343 await waitJobs(servers)
346 const video = await servers[0].videos.get({ id: videoUUID })
347 expect(video.name).to.equal('hdr video')
348 const maxResolution = Math.max.apply(Math, video.files.map(function (o) { return o.resolution.id }))
349 expect(maxResolution, 'expected max resolution not met').to.equals(VideoResolution.H_240P)
352 it('Should not import resolution higher than enabled transcoding resolution', async function () {
353 this.timeout(300_000)
355 const config: DeepPartial<CustomConfig> = {
369 alwaysTranscodeOriginalResolution: false
372 await servers[0].config.updateExistingSubConfig({ newConfig: config })
375 name: 'small resolution video',
376 targetUrl: FIXTURE_URLS.youtube,
377 channelId: servers[0].store.channel.id,
378 privacy: VideoPrivacy.PUBLIC
380 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
381 const videoUUID = videoImported.uuid
383 await waitJobs(servers)
386 const video = await servers[0].videos.get({ id: videoUUID })
387 expect(video.name).to.equal('small resolution video')
388 expect(video.files).to.have.lengthOf(1)
389 expect(video.files[0].resolution.id).to.equal(144)
392 it('Should import resolution higher than enabled transcoding resolution', async function () {
393 this.timeout(300_000)
395 const config: DeepPartial<CustomConfig> = {
397 alwaysTranscodeOriginalResolution: true
400 await servers[0].config.updateExistingSubConfig({ newConfig: config })
403 name: 'bigger resolution video',
404 targetUrl: FIXTURE_URLS.youtube,
405 channelId: servers[0].store.channel.id,
406 privacy: VideoPrivacy.PUBLIC
408 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
409 const videoUUID = videoImported.uuid
411 await waitJobs(servers)
414 const video = await servers[0].videos.get({ id: videoUUID })
415 expect(video.name).to.equal('bigger resolution video')
417 expect(video.files).to.have.lengthOf(2)
418 expect(video.files.find(f => f.resolution.id === 240)).to.exist
419 expect(video.files.find(f => f.resolution.id === 144)).to.exist
422 it('Should import a peertube video', async function () {
423 this.timeout(120_000)
425 const toTest = [ FIXTURE_URLS.peertube_long ]
427 // TODO: include peertube_short when https://github.com/ytdl-org/youtube-dl/pull/29475 is merged
428 if (mode === 'yt-dlp') {
429 toTest.push(FIXTURE_URLS.peertube_short)
432 for (const targetUrl of toTest) {
433 await servers[0].config.disableTranscoding()
437 channelId: servers[0].store.channel.id,
438 privacy: VideoPrivacy.PUBLIC
440 const { video } = await servers[0].imports.importVideo({ attributes })
441 const videoUUID = video.uuid
443 await waitJobs(servers)
445 for (const server of servers) {
446 const video = await server.videos.get({ id: videoUUID })
448 expect(video.name).to.equal('E2E tests')
453 after(async function () {
454 await cleanupTests(servers)
459 runSuite('youtube-dl')
463 describe('Delete/cancel an import', function () {
464 let server: PeerTubeServer
466 let finishedImportId: number
467 let finishedVideo: Video
468 let pendingImportId: number
470 async function importVideo (name: string) {
471 const attributes = { name, channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
472 const res = await server.imports.importVideo({ attributes })
477 before(async function () {
478 this.timeout(120_000)
480 server = await createSingleServer(1)
482 await setAccessTokensToServers([ server ])
483 await setDefaultVideoChannel([ server ])
485 finishedImportId = await importVideo('finished')
486 await waitJobs([ server ])
488 await server.jobs.pauseJobQueue()
489 pendingImportId = await importVideo('pending')
491 const { data } = await server.imports.getMyVideoImports()
492 expect(data).to.have.lengthOf(2)
494 finishedVideo = data.find(i => i.id === finishedImportId).video
497 it('Should delete a video import', async function () {
498 await server.imports.delete({ importId: finishedImportId })
500 const { data } = await server.imports.getMyVideoImports()
501 expect(data).to.have.lengthOf(1)
502 expect(data[0].id).to.equal(pendingImportId)
503 expect(data[0].state.id).to.equal(VideoImportState.PENDING)
506 it('Should not have deleted the associated video', async function () {
507 const video = await server.videos.get({ id: finishedVideo.id, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
508 expect(video.name).to.equal('finished')
509 expect(video.state.id).to.equal(VideoState.PUBLISHED)
512 it('Should cancel a video import', async function () {
513 await server.imports.cancel({ importId: pendingImportId })
515 const { data } = await server.imports.getMyVideoImports()
516 expect(data).to.have.lengthOf(1)
517 expect(data[0].id).to.equal(pendingImportId)
518 expect(data[0].state.id).to.equal(VideoImportState.CANCELLED)
521 it('Should not have processed the cancelled video import', async function () {
524 await server.jobs.resumeJobQueue()
526 await waitJobs([ server ])
528 const { data } = await server.imports.getMyVideoImports()
529 expect(data).to.have.lengthOf(1)
530 expect(data[0].id).to.equal(pendingImportId)
531 expect(data[0].state.id).to.equal(VideoImportState.CANCELLED)
532 expect(data[0].video.state.id).to.equal(VideoState.TO_IMPORT)
535 it('Should delete the cancelled video import', async function () {
536 await server.imports.delete({ importId: pendingImportId })
537 const { data } = await server.imports.getMyVideoImports()
538 expect(data).to.have.lengthOf(0)
541 after(async function () {
542 await cleanupTests([ server ])
546 describe('Auto update', function () {
547 let server: PeerTubeServer
549 function quickPeerTubeImport () {
551 targetUrl: FIXTURE_URLS.peertube_long,
552 channelId: server.store.channel.id,
553 privacy: VideoPrivacy.PUBLIC
556 return server.imports.importVideo({ attributes })
559 async function testBinaryUpdate (releaseUrl: string, releaseName: string) {
560 await remove(join(server.servers.buildDirectory('bin'), releaseName))
567 youtube_dl_release: {
576 await quickPeerTubeImport()
578 const base = server.servers.buildDirectory('bin')
579 const content = await readdir(base)
580 const binaryPath = join(base, releaseName)
582 expect(await pathExists(binaryPath), `${binaryPath} does not exist in ${base} (${content.join(', ')})`).to.be.true
585 before(async function () {
589 server = await createSingleServer(1)
591 await setAccessTokensToServers([ server ])
592 await setDefaultVideoChannel([ server ])
595 it('Should update youtube-dl from github URL', async function () {
596 this.timeout(120_000)
598 await testBinaryUpdate('https://api.github.com/repos/ytdl-org/youtube-dl/releases', 'youtube-dl')
601 it('Should update youtube-dl from raw URL', async function () {
602 this.timeout(120_000)
604 await testBinaryUpdate('https://yt-dl.org/downloads/latest/youtube-dl', 'youtube-dl')
607 it('Should update youtube-dl from youtube-dl fork', async function () {
608 this.timeout(120_000)
610 await testBinaryUpdate('https://api.github.com/repos/yt-dlp/yt-dlp/releases', 'yt-dlp')
613 after(async function () {
614 await cleanupTests([ server ])