1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { pathExists, readdir, remove } from 'fs-extra'
6 import { join } from 'path'
7 import { FIXTURE_URLS, testCaptionFile, testImage } from '@server/tests/shared'
8 import { areHttpImportTestsDisabled } from '@shared/core-utils'
9 import { CustomConfig, HttpStatusCode, Video, VideoImportState, VideoPrivacy, VideoResolution, VideoState } from '@shared/models'
12 createMultipleServers,
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 () {
90 servers = await createMultipleServers(2, {
95 url: mode === 'youtube-dl'
96 ? 'https://yt-dl.org/downloads/latest/youtube-dl'
97 : 'https://api.github.com/repos/yt-dlp/yt-dlp/releases',
106 await setAccessTokensToServers(servers)
107 await setDefaultVideoChannel(servers)
109 for (const server of servers) {
110 await server.config.updateExistingSubConfig({
113 alwaysTranscodeOriginalResolution: false
119 await doubleFollow(servers[0], servers[1])
122 it('Should import videos on server 1', async function () {
125 const baseAttributes = {
126 channelId: servers[0].store.channel.id,
127 privacy: VideoPrivacy.PUBLIC
131 const attributes = { ...baseAttributes, targetUrl: FIXTURE_URLS.youtube }
132 const { video } = await servers[0].imports.importVideo({ attributes })
133 expect(video.name).to.equal('small video - youtube')
136 expect(video.thumbnailPath).to.match(new RegExp(`^/static/thumbnails/.+.jpg$`))
137 expect(video.previewPath).to.match(new RegExp(`^/lazy-static/previews/.+.jpg$`))
139 const suffix = mode === 'yt-dlp'
143 await testImage(servers[0].url, 'video_import_thumbnail' + suffix, video.thumbnailPath)
144 await testImage(servers[0].url, 'video_import_preview' + suffix, video.previewPath)
147 const bodyCaptions = await servers[0].captions.list({ videoId: video.id })
148 const videoCaptions = bodyCaptions.data
149 expect(videoCaptions).to.have.lengthOf(2)
152 const enCaption = videoCaptions.find(caption => caption.language.id === 'en')
153 expect(enCaption).to.exist
154 expect(enCaption.language.label).to.equal('English')
155 expect(enCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-en.vtt$`))
157 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+` +
158 `(Language: en[ \n]+)?` +
159 `00:00:01.600 --> 00:00:04.200( position:\\d+% line:\\d+%)?[ \n]+English \\(US\\)[ \n]+` +
160 `00:00:05.900 --> 00:00:07.999( position:\\d+% line:\\d+%)?[ \n]+This is a subtitle in American English[ \n]+` +
161 `00:00:10.000 --> 00:00:14.000( position:\\d+% line:\\d+%)?[ \n]+Adding subtitles is very easy to do`
162 await testCaptionFile(servers[0].url, enCaption.captionPath, new RegExp(regex))
166 const frCaption = videoCaptions.find(caption => caption.language.id === 'fr')
167 expect(frCaption).to.exist
168 expect(frCaption.language.label).to.equal('French')
169 expect(frCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-fr.vtt`))
171 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+` +
172 `(Language: fr[ \n]+)?` +
173 `00:00:01.600 --> 00:00:04.200( position:\\d+% line:\\d+%)?[ \n]+Français \\(FR\\)[ \n]+` +
174 `00:00:05.900 --> 00:00:07.999( position:\\d+% line:\\d+%)?[ \n]+C'est un sous-titre français[ \n]+` +
175 `00:00:10.000 --> 00:00:14.000( position:\\d+% line:\\d+%)?[ \n]+Ajouter un sous-titre est vraiment facile`
177 await testCaptionFile(servers[0].url, frCaption.captionPath, new RegExp(regex))
184 magnetUri: FIXTURE_URLS.magnet,
185 description: 'this is a super torrent description',
186 tags: [ 'tag_torrent1', 'tag_torrent2' ]
188 const { video } = await servers[0].imports.importVideo({ attributes })
189 expect(video.name).to.equal('super peertube2 video')
195 torrentfile: 'video-720p.torrent' as any,
196 description: 'this is a super torrent description',
197 tags: [ 'tag_torrent1', 'tag_torrent2' ]
199 const { video } = await servers[0].imports.importVideo({ attributes })
200 expect(video.name).to.equal('你好 世界 720p.mp4')
204 it('Should list the videos to import in my videos on server 1', async function () {
205 const { total, data } = await servers[0].videos.listMyVideos({ sort: 'createdAt' })
207 expect(total).to.equal(3)
209 expect(data).to.have.lengthOf(3)
210 expect(data[0].name).to.equal('small video - youtube')
211 expect(data[1].name).to.equal('super peertube2 video')
212 expect(data[2].name).to.equal('你好 世界 720p.mp4')
215 it('Should list the videos to import in my imports on server 1', async function () {
216 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ sort: '-createdAt' })
217 expect(total).to.equal(3)
219 expect(videoImports).to.have.lengthOf(3)
221 expect(videoImports[2].targetUrl).to.equal(FIXTURE_URLS.youtube)
222 expect(videoImports[2].magnetUri).to.be.null
223 expect(videoImports[2].torrentName).to.be.null
224 expect(videoImports[2].video.name).to.equal('small video - youtube')
226 expect(videoImports[1].targetUrl).to.be.null
227 expect(videoImports[1].magnetUri).to.equal(FIXTURE_URLS.magnet)
228 expect(videoImports[1].torrentName).to.be.null
229 expect(videoImports[1].video.name).to.equal('super peertube2 video')
231 expect(videoImports[0].targetUrl).to.be.null
232 expect(videoImports[0].magnetUri).to.be.null
233 expect(videoImports[0].torrentName).to.equal('video-720p.torrent')
234 expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
237 it('Should filter my imports on target URL', async function () {
238 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ targetUrl: FIXTURE_URLS.youtube })
239 expect(total).to.equal(1)
240 expect(videoImports).to.have.lengthOf(1)
242 expect(videoImports[0].targetUrl).to.equal(FIXTURE_URLS.youtube)
245 it('Should have the video listed on the two instances', async function () {
246 this.timeout(120_000)
248 await waitJobs(servers)
250 for (const server of servers) {
251 const { total, data } = await server.videos.list()
252 expect(total).to.equal(3)
253 expect(data).to.have.lengthOf(3)
255 const [ videoHttp, videoMagnet, videoTorrent ] = data
256 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
260 it('Should import a video on server 2 with some fields', async function () {
264 targetUrl: FIXTURE_URLS.youtube,
265 channelId: servers[1].store.channel.id,
266 privacy: VideoPrivacy.PUBLIC,
270 name: 'my super name',
271 description: 'my super description',
272 tags: [ 'supertag1', 'supertag2' ]
274 const { video } = await servers[1].imports.importVideo({ attributes })
275 expect(video.name).to.equal('my super name')
278 it('Should have the videos listed on the two instances', async function () {
279 this.timeout(120_000)
281 await waitJobs(servers)
283 for (const server of servers) {
284 const { total, data } = await server.videos.list()
285 expect(total).to.equal(4)
286 expect(data).to.have.lengthOf(4)
288 await checkVideoServer2(server, data[0].uuid)
290 const [ , videoHttp, videoMagnet, videoTorrent ] = data
291 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
295 it('Should import a video that will be transcoded', async function () {
296 this.timeout(240_000)
299 name: 'transcoded video',
300 magnetUri: FIXTURE_URLS.magnet,
301 channelId: servers[1].store.channel.id,
302 privacy: VideoPrivacy.PUBLIC
304 const { video } = await servers[1].imports.importVideo({ attributes })
305 const videoUUID = video.uuid
307 await waitJobs(servers)
309 for (const server of servers) {
310 const video = await server.videos.get({ id: videoUUID })
312 expect(video.name).to.equal('transcoded video')
313 expect(video.files).to.have.lengthOf(4)
317 it('Should import no HDR version on a HDR video', async function () {
318 this.timeout(300_000)
320 const config: DeepPartial<CustomConfig> = {
330 '1080p': false, // the resulting resolution shouldn't be higher than this, and not vp9.2/av01
334 webtorrent: { enabled: true },
335 hls: { enabled: false }
338 await servers[0].config.updateExistingSubConfig({ newConfig: config })
342 targetUrl: FIXTURE_URLS.youtubeHDR,
343 channelId: servers[0].store.channel.id,
344 privacy: VideoPrivacy.PUBLIC
346 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
347 const videoUUID = videoImported.uuid
349 await waitJobs(servers)
352 const video = await servers[0].videos.get({ id: videoUUID })
353 expect(video.name).to.equal('hdr video')
354 const maxResolution = Math.max.apply(Math, video.files.map(function (o) { return o.resolution.id }))
355 expect(maxResolution, 'expected max resolution not met').to.equals(VideoResolution.H_240P)
358 it('Should not import resolution higher than enabled transcoding resolution', async function () {
359 this.timeout(300_000)
361 const config: DeepPartial<CustomConfig> = {
375 alwaysTranscodeOriginalResolution: false
378 await servers[0].config.updateExistingSubConfig({ newConfig: config })
381 name: 'small resolution video',
382 targetUrl: FIXTURE_URLS.youtube,
383 channelId: servers[0].store.channel.id,
384 privacy: VideoPrivacy.PUBLIC
386 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
387 const videoUUID = videoImported.uuid
389 await waitJobs(servers)
392 const video = await servers[0].videos.get({ id: videoUUID })
393 expect(video.name).to.equal('small resolution video')
394 expect(video.files).to.have.lengthOf(1)
395 expect(video.files[0].resolution.id).to.equal(144)
398 it('Should import resolution higher than enabled transcoding resolution', async function () {
399 this.timeout(300_000)
401 const config: DeepPartial<CustomConfig> = {
403 alwaysTranscodeOriginalResolution: true
406 await servers[0].config.updateExistingSubConfig({ newConfig: config })
409 name: 'bigger resolution video',
410 targetUrl: FIXTURE_URLS.youtube,
411 channelId: servers[0].store.channel.id,
412 privacy: VideoPrivacy.PUBLIC
414 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
415 const videoUUID = videoImported.uuid
417 await waitJobs(servers)
420 const video = await servers[0].videos.get({ id: videoUUID })
421 expect(video.name).to.equal('bigger resolution video')
423 expect(video.files).to.have.lengthOf(2)
424 expect(video.files.find(f => f.resolution.id === 240)).to.exist
425 expect(video.files.find(f => f.resolution.id === 144)).to.exist
428 it('Should import a peertube video', async function () {
429 this.timeout(120_000)
431 const toTest = [ FIXTURE_URLS.peertube_long ]
433 // TODO: include peertube_short when https://github.com/ytdl-org/youtube-dl/pull/29475 is merged
434 if (mode === 'yt-dlp') {
435 toTest.push(FIXTURE_URLS.peertube_short)
438 for (const targetUrl of toTest) {
439 await servers[0].config.disableTranscoding()
443 channelId: servers[0].store.channel.id,
444 privacy: VideoPrivacy.PUBLIC
446 const { video } = await servers[0].imports.importVideo({ attributes })
447 const videoUUID = video.uuid
449 await waitJobs(servers)
451 for (const server of servers) {
452 const video = await server.videos.get({ id: videoUUID })
454 expect(video.name).to.equal('E2E tests')
459 after(async function () {
460 await cleanupTests(servers)
465 runSuite('youtube-dl')
469 describe('Delete/cancel an import', function () {
470 let server: PeerTubeServer
472 let finishedImportId: number
473 let finishedVideo: Video
474 let pendingImportId: number
476 async function importVideo (name: string) {
477 const attributes = { name, channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
478 const res = await server.imports.importVideo({ attributes })
483 before(async function () {
484 this.timeout(120_000)
486 server = await createSingleServer(1)
488 await setAccessTokensToServers([ server ])
489 await setDefaultVideoChannel([ server ])
491 finishedImportId = await importVideo('finished')
492 await waitJobs([ server ])
494 await server.jobs.pauseJobQueue()
495 pendingImportId = await importVideo('pending')
497 const { data } = await server.imports.getMyVideoImports()
498 expect(data).to.have.lengthOf(2)
500 finishedVideo = data.find(i => i.id === finishedImportId).video
503 it('Should delete a video import', async function () {
504 await server.imports.delete({ importId: finishedImportId })
506 const { data } = await server.imports.getMyVideoImports()
507 expect(data).to.have.lengthOf(1)
508 expect(data[0].id).to.equal(pendingImportId)
509 expect(data[0].state.id).to.equal(VideoImportState.PENDING)
512 it('Should not have deleted the associated video', async function () {
513 const video = await server.videos.get({ id: finishedVideo.id, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
514 expect(video.name).to.equal('finished')
515 expect(video.state.id).to.equal(VideoState.PUBLISHED)
518 it('Should cancel a video import', async function () {
519 await server.imports.cancel({ importId: pendingImportId })
521 const { data } = await server.imports.getMyVideoImports()
522 expect(data).to.have.lengthOf(1)
523 expect(data[0].id).to.equal(pendingImportId)
524 expect(data[0].state.id).to.equal(VideoImportState.CANCELLED)
527 it('Should not have processed the cancelled video import', async function () {
530 await server.jobs.resumeJobQueue()
532 await waitJobs([ server ])
534 const { data } = await server.imports.getMyVideoImports()
535 expect(data).to.have.lengthOf(1)
536 expect(data[0].id).to.equal(pendingImportId)
537 expect(data[0].state.id).to.equal(VideoImportState.CANCELLED)
538 expect(data[0].video.state.id).to.equal(VideoState.TO_IMPORT)
541 it('Should delete the cancelled video import', async function () {
542 await server.imports.delete({ importId: pendingImportId })
543 const { data } = await server.imports.getMyVideoImports()
544 expect(data).to.have.lengthOf(0)
547 after(async function () {
548 await cleanupTests([ server ])
552 describe('Auto update', function () {
553 let server: PeerTubeServer
555 function quickPeerTubeImport () {
557 targetUrl: FIXTURE_URLS.peertube_long,
558 channelId: server.store.channel.id,
559 privacy: VideoPrivacy.PUBLIC
562 return server.imports.importVideo({ attributes })
565 async function testBinaryUpdate (releaseUrl: string, releaseName: string) {
566 await remove(join(server.servers.buildDirectory('bin'), releaseName))
573 youtube_dl_release: {
582 await quickPeerTubeImport()
584 const base = server.servers.buildDirectory('bin')
585 const content = await readdir(base)
586 const binaryPath = join(base, releaseName)
588 expect(await pathExists(binaryPath), `${binaryPath} does not exist in ${base} (${content.join(', ')})`).to.be.true
591 before(async function () {
595 server = await createSingleServer(1)
597 await setAccessTokensToServers([ server ])
598 await setDefaultVideoChannel([ server ])
601 it('Should update youtube-dl from github URL', async function () {
602 this.timeout(120_000)
604 await testBinaryUpdate('https://api.github.com/repos/ytdl-org/youtube-dl/releases', 'youtube-dl')
607 it('Should update youtube-dl from raw URL', async function () {
608 this.timeout(120_000)
610 await testBinaryUpdate('https://yt-dl.org/downloads/latest/youtube-dl', 'youtube-dl')
613 it('Should update youtube-dl from youtube-dl fork', async function () {
614 this.timeout(120_000)
616 await testBinaryUpdate('https://api.github.com/repos/yt-dlp/yt-dlp/releases', 'yt-dlp')
619 after(async function () {
620 await cleanupTests([ server ])