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 expect(videoHttp.category.label).to.equal('News & Politics')
27 expect(videoHttp.licence.label).to.equal('Attribution')
28 expect(videoHttp.language.label).to.equal('Unknown')
29 expect(videoHttp.nsfw).to.be.false
30 expect(videoHttp.description).to.equal('this is a super description')
31 expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ])
32 expect(videoHttp.files).to.have.lengthOf(1)
34 const originallyPublishedAt = new Date(videoHttp.originallyPublishedAt)
35 expect(originallyPublishedAt.getDate()).to.equal(14)
36 expect(originallyPublishedAt.getMonth()).to.equal(0)
37 expect(originallyPublishedAt.getFullYear()).to.equal(2019)
39 const videoMagnet = await server.videos.get({ id: idMagnet })
40 const videoTorrent = await server.videos.get({ id: idTorrent })
42 for (const video of [ videoMagnet, videoTorrent ]) {
43 expect(video.category.label).to.equal('Unknown')
44 expect(video.licence.label).to.equal('Unknown')
45 expect(video.language.label).to.equal('Unknown')
46 expect(video.nsfw).to.be.false
47 expect(video.description).to.equal('this is a super torrent description')
48 expect(video.tags).to.deep.equal([ 'tag_torrent1', 'tag_torrent2' ])
49 expect(video.files).to.have.lengthOf(1)
52 expect(videoTorrent.name).to.contain('你好 世界 720p.mp4')
53 expect(videoMagnet.name).to.contain('super peertube2 video')
55 const bodyCaptions = await server.captions.list({ videoId: idHttp })
56 expect(bodyCaptions.total).to.equal(2)
59 async function checkVideoServer2 (server: PeerTubeServer, id: number | string) {
60 const video = await server.videos.get({ id })
62 expect(video.name).to.equal('my super name')
63 expect(video.category.label).to.equal('Entertainment')
64 expect(video.licence.label).to.equal('Public Domain Dedication')
65 expect(video.language.label).to.equal('English')
66 expect(video.nsfw).to.be.false
67 expect(video.description).to.equal('my super description')
68 expect(video.tags).to.deep.equal([ 'supertag1', 'supertag2' ])
70 await testImage(server.url, 'thumbnail', video.thumbnailPath)
72 expect(video.files).to.have.lengthOf(1)
74 const bodyCaptions = await server.captions.list({ videoId: id })
75 expect(bodyCaptions.total).to.equal(2)
78 describe('Test video imports', function () {
80 if (areHttpImportTestsDisabled()) return
82 function runSuite (mode: 'youtube-dl' | 'yt-dlp') {
84 describe('Import ' + mode, function () {
85 let servers: PeerTubeServer[] = []
87 before(async function () {
90 servers = await createMultipleServers(2, getServerImportConfig(mode))
92 await setAccessTokensToServers(servers)
93 await setDefaultVideoChannel(servers)
95 for (const server of servers) {
96 await server.config.updateExistingSubConfig({
99 alwaysTranscodeOriginalResolution: false
105 await doubleFollow(servers[0], servers[1])
108 it('Should import videos on server 1', async function () {
111 const baseAttributes = {
112 channelId: servers[0].store.channel.id,
113 privacy: VideoPrivacy.PUBLIC
117 const attributes = { ...baseAttributes, targetUrl: FIXTURE_URLS.youtube }
118 const { video } = await servers[0].imports.importVideo({ attributes })
119 expect(video.name).to.equal('small video - youtube')
122 expect(video.thumbnailPath).to.match(new RegExp(`^/static/thumbnails/.+.jpg$`))
123 expect(video.previewPath).to.match(new RegExp(`^/lazy-static/previews/.+.jpg$`))
125 const suffix = mode === 'yt-dlp'
129 await testImage(servers[0].url, 'video_import_thumbnail' + suffix, video.thumbnailPath)
130 await testImage(servers[0].url, 'video_import_preview' + suffix, video.previewPath)
133 const bodyCaptions = await servers[0].captions.list({ videoId: video.id })
134 const videoCaptions = bodyCaptions.data
135 expect(videoCaptions).to.have.lengthOf(2)
138 const enCaption = videoCaptions.find(caption => caption.language.id === 'en')
139 expect(enCaption).to.exist
140 expect(enCaption.language.label).to.equal('English')
141 expect(enCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-en.vtt$`))
143 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+` +
144 `(Language: en[ \n]+)?` +
145 `00:00:01.600 --> 00:00:04.200( position:\\d+% line:\\d+%)?[ \n]+English \\(US\\)[ \n]+` +
146 `00:00:05.900 --> 00:00:07.999( position:\\d+% line:\\d+%)?[ \n]+This is a subtitle in American English[ \n]+` +
147 `00:00:10.000 --> 00:00:14.000( position:\\d+% line:\\d+%)?[ \n]+Adding subtitles is very easy to do`
148 await testCaptionFile(servers[0].url, enCaption.captionPath, new RegExp(regex))
152 const frCaption = videoCaptions.find(caption => caption.language.id === 'fr')
153 expect(frCaption).to.exist
154 expect(frCaption.language.label).to.equal('French')
155 expect(frCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-fr.vtt`))
157 const regex = `WEBVTT[ \n]+Kind: captions[ \n]+` +
158 `(Language: fr[ \n]+)?` +
159 `00:00:01.600 --> 00:00:04.200( position:\\d+% line:\\d+%)?[ \n]+Français \\(FR\\)[ \n]+` +
160 `00:00:05.900 --> 00:00:07.999( position:\\d+% line:\\d+%)?[ \n]+C'est un sous-titre français[ \n]+` +
161 `00:00:10.000 --> 00:00:14.000( position:\\d+% line:\\d+%)?[ \n]+Ajouter un sous-titre est vraiment facile`
163 await testCaptionFile(servers[0].url, frCaption.captionPath, new RegExp(regex))
170 magnetUri: FIXTURE_URLS.magnet,
171 description: 'this is a super torrent description',
172 tags: [ 'tag_torrent1', 'tag_torrent2' ]
174 const { video } = await servers[0].imports.importVideo({ attributes })
175 expect(video.name).to.equal('super peertube2 video')
181 torrentfile: 'video-720p.torrent' as any,
182 description: 'this is a super torrent description',
183 tags: [ 'tag_torrent1', 'tag_torrent2' ]
185 const { video } = await servers[0].imports.importVideo({ attributes })
186 expect(video.name).to.equal('你好 世界 720p.mp4')
190 it('Should list the videos to import in my videos on server 1', async function () {
191 const { total, data } = await servers[0].videos.listMyVideos({ sort: 'createdAt' })
193 expect(total).to.equal(3)
195 expect(data).to.have.lengthOf(3)
196 expect(data[0].name).to.equal('small video - youtube')
197 expect(data[1].name).to.equal('super peertube2 video')
198 expect(data[2].name).to.equal('你好 世界 720p.mp4')
201 it('Should list the videos to import in my imports on server 1', async function () {
202 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ sort: '-createdAt' })
203 expect(total).to.equal(3)
205 expect(videoImports).to.have.lengthOf(3)
207 expect(videoImports[2].targetUrl).to.equal(FIXTURE_URLS.youtube)
208 expect(videoImports[2].magnetUri).to.be.null
209 expect(videoImports[2].torrentName).to.be.null
210 expect(videoImports[2].video.name).to.equal('small video - youtube')
212 expect(videoImports[1].targetUrl).to.be.null
213 expect(videoImports[1].magnetUri).to.equal(FIXTURE_URLS.magnet)
214 expect(videoImports[1].torrentName).to.be.null
215 expect(videoImports[1].video.name).to.equal('super peertube2 video')
217 expect(videoImports[0].targetUrl).to.be.null
218 expect(videoImports[0].magnetUri).to.be.null
219 expect(videoImports[0].torrentName).to.equal('video-720p.torrent')
220 expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
223 it('Should filter my imports on target URL', async function () {
224 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ targetUrl: FIXTURE_URLS.youtube })
225 expect(total).to.equal(1)
226 expect(videoImports).to.have.lengthOf(1)
228 expect(videoImports[0].targetUrl).to.equal(FIXTURE_URLS.youtube)
231 it('Should search in my imports', async function () {
232 const { total, data: videoImports } = await servers[0].imports.getMyVideoImports({ search: 'peertube2' })
233 expect(total).to.equal(1)
234 expect(videoImports).to.have.lengthOf(1)
236 expect(videoImports[0].magnetUri).to.equal(FIXTURE_URLS.magnet)
237 expect(videoImports[0].video.name).to.equal('super peertube2 video')
240 it('Should have the video listed on the two instances', async function () {
241 this.timeout(120_000)
243 await waitJobs(servers)
245 for (const server of servers) {
246 const { total, data } = await server.videos.list()
247 expect(total).to.equal(3)
248 expect(data).to.have.lengthOf(3)
250 const [ videoHttp, videoMagnet, videoTorrent ] = data
251 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
255 it('Should import a video on server 2 with some fields', async function () {
258 const { video } = await servers[1].imports.importVideo({
260 targetUrl: FIXTURE_URLS.youtube,
261 channelId: servers[1].store.channel.id,
262 privacy: VideoPrivacy.PUBLIC,
266 name: 'my super name',
267 description: 'my super description',
268 tags: [ 'supertag1', 'supertag2' ],
269 thumbnailfile: 'thumbnail.jpg'
272 expect(video.name).to.equal('my super name')
275 it('Should have the videos listed on the two instances', async function () {
276 this.timeout(120_000)
278 await waitJobs(servers)
280 for (const server of servers) {
281 const { total, data } = await server.videos.list()
282 expect(total).to.equal(4)
283 expect(data).to.have.lengthOf(4)
285 await checkVideoServer2(server, data[0].uuid)
287 const [ , videoHttp, videoMagnet, videoTorrent ] = data
288 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
292 it('Should import a video that will be transcoded', async function () {
293 this.timeout(240_000)
296 name: 'transcoded video',
297 magnetUri: FIXTURE_URLS.magnet,
298 channelId: servers[1].store.channel.id,
299 privacy: VideoPrivacy.PUBLIC
301 const { video } = await servers[1].imports.importVideo({ attributes })
302 const videoUUID = video.uuid
304 await waitJobs(servers)
306 for (const server of servers) {
307 const video = await server.videos.get({ id: videoUUID })
309 expect(video.name).to.equal('transcoded video')
310 expect(video.files).to.have.lengthOf(4)
314 it('Should import no HDR version on a HDR video', async function () {
315 this.timeout(300_000)
317 const config: DeepPartial<CustomConfig> = {
327 '1080p': false, // the resulting resolution shouldn't be higher than this, and not vp9.2/av01
331 webtorrent: { enabled: true },
332 hls: { enabled: false }
335 await servers[0].config.updateExistingSubConfig({ newConfig: config })
339 targetUrl: FIXTURE_URLS.youtubeHDR,
340 channelId: servers[0].store.channel.id,
341 privacy: VideoPrivacy.PUBLIC
343 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
344 const videoUUID = videoImported.uuid
346 await waitJobs(servers)
349 const video = await servers[0].videos.get({ id: videoUUID })
350 expect(video.name).to.equal('hdr video')
351 const maxResolution = Math.max.apply(Math, video.files.map(function (o) { return o.resolution.id }))
352 expect(maxResolution, 'expected max resolution not met').to.equals(VideoResolution.H_240P)
355 it('Should not import resolution higher than enabled transcoding resolution', async function () {
356 this.timeout(300_000)
358 const config: DeepPartial<CustomConfig> = {
372 alwaysTranscodeOriginalResolution: false
375 await servers[0].config.updateExistingSubConfig({ newConfig: config })
378 name: 'small resolution video',
379 targetUrl: FIXTURE_URLS.youtube,
380 channelId: servers[0].store.channel.id,
381 privacy: VideoPrivacy.PUBLIC
383 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
384 const videoUUID = videoImported.uuid
386 await waitJobs(servers)
389 const video = await servers[0].videos.get({ id: videoUUID })
390 expect(video.name).to.equal('small resolution video')
391 expect(video.files).to.have.lengthOf(1)
392 expect(video.files[0].resolution.id).to.equal(144)
395 it('Should import resolution higher than enabled transcoding resolution', async function () {
396 this.timeout(300_000)
398 const config: DeepPartial<CustomConfig> = {
400 alwaysTranscodeOriginalResolution: true
403 await servers[0].config.updateExistingSubConfig({ newConfig: config })
406 name: 'bigger resolution video',
407 targetUrl: FIXTURE_URLS.youtube,
408 channelId: servers[0].store.channel.id,
409 privacy: VideoPrivacy.PUBLIC
411 const { video: videoImported } = await servers[0].imports.importVideo({ attributes })
412 const videoUUID = videoImported.uuid
414 await waitJobs(servers)
417 const video = await servers[0].videos.get({ id: videoUUID })
418 expect(video.name).to.equal('bigger resolution video')
420 expect(video.files).to.have.lengthOf(2)
421 expect(video.files.find(f => f.resolution.id === 240)).to.exist
422 expect(video.files.find(f => f.resolution.id === 144)).to.exist
425 it('Should import a peertube video', async function () {
426 this.timeout(120_000)
428 const toTest = [ FIXTURE_URLS.peertube_long ]
430 // TODO: include peertube_short when https://github.com/ytdl-org/youtube-dl/pull/29475 is merged
431 if (mode === 'yt-dlp') {
432 toTest.push(FIXTURE_URLS.peertube_short)
435 for (const targetUrl of toTest) {
436 await servers[0].config.disableTranscoding()
440 channelId: servers[0].store.channel.id,
441 privacy: VideoPrivacy.PUBLIC
443 const { video } = await servers[0].imports.importVideo({ attributes })
444 const videoUUID = video.uuid
446 await waitJobs(servers)
448 for (const server of servers) {
449 const video = await server.videos.get({ id: videoUUID })
451 expect(video.name).to.equal('E2E tests')
456 after(async function () {
457 await cleanupTests(servers)
462 // FIXME: youtube-dl seems broken
463 // runSuite('youtube-dl')
467 describe('Delete/cancel an import', function () {
468 let server: PeerTubeServer
470 let finishedImportId: number
471 let finishedVideo: Video
472 let pendingImportId: number
474 async function importVideo (name: string) {
475 const attributes = { name, channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo }
476 const res = await server.imports.importVideo({ attributes })
481 before(async function () {
482 this.timeout(120_000)
484 server = await createSingleServer(1)
486 await setAccessTokensToServers([ server ])
487 await setDefaultVideoChannel([ server ])
489 finishedImportId = await importVideo('finished')
490 await waitJobs([ server ])
492 await server.jobs.pauseJobQueue()
493 pendingImportId = await importVideo('pending')
495 const { data } = await server.imports.getMyVideoImports()
496 expect(data).to.have.lengthOf(2)
498 finishedVideo = data.find(i => i.id === finishedImportId).video
501 it('Should delete a video import', async function () {
502 await server.imports.delete({ importId: finishedImportId })
504 const { data } = await server.imports.getMyVideoImports()
505 expect(data).to.have.lengthOf(1)
506 expect(data[0].id).to.equal(pendingImportId)
507 expect(data[0].state.id).to.equal(VideoImportState.PENDING)
510 it('Should not have deleted the associated video', async function () {
511 const video = await server.videos.get({ id: finishedVideo.id, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
512 expect(video.name).to.equal('finished')
513 expect(video.state.id).to.equal(VideoState.PUBLISHED)
516 it('Should cancel a video import', async function () {
517 await server.imports.cancel({ importId: pendingImportId })
519 const { data } = await server.imports.getMyVideoImports()
520 expect(data).to.have.lengthOf(1)
521 expect(data[0].id).to.equal(pendingImportId)
522 expect(data[0].state.id).to.equal(VideoImportState.CANCELLED)
525 it('Should not have processed the cancelled video import', async function () {
528 await server.jobs.resumeJobQueue()
530 await waitJobs([ server ])
532 const { data } = await server.imports.getMyVideoImports()
533 expect(data).to.have.lengthOf(1)
534 expect(data[0].id).to.equal(pendingImportId)
535 expect(data[0].state.id).to.equal(VideoImportState.CANCELLED)
536 expect(data[0].video.state.id).to.equal(VideoState.TO_IMPORT)
539 it('Should delete the cancelled video import', async function () {
540 await server.imports.delete({ importId: pendingImportId })
541 const { data } = await server.imports.getMyVideoImports()
542 expect(data).to.have.lengthOf(0)
545 after(async function () {
546 await cleanupTests([ server ])
550 describe('Auto update', function () {
551 let server: PeerTubeServer
553 function quickPeerTubeImport () {
555 targetUrl: FIXTURE_URLS.peertube_long,
556 channelId: server.store.channel.id,
557 privacy: VideoPrivacy.PUBLIC
560 return server.imports.importVideo({ attributes })
563 async function testBinaryUpdate (releaseUrl: string, releaseName: string) {
564 await remove(join(server.servers.buildDirectory('bin'), releaseName))
571 youtube_dl_release: {
580 await quickPeerTubeImport()
582 const base = server.servers.buildDirectory('bin')
583 const content = await readdir(base)
584 const binaryPath = join(base, releaseName)
586 expect(await pathExists(binaryPath), `${binaryPath} does not exist in ${base} (${content.join(', ')})`).to.be.true
589 before(async function () {
593 server = await createSingleServer(1)
595 await setAccessTokensToServers([ server ])
596 await setDefaultVideoChannel([ server ])
599 it('Should update youtube-dl from github URL', async function () {
600 this.timeout(120_000)
602 await testBinaryUpdate('https://api.github.com/repos/ytdl-org/youtube-dl/releases', 'youtube-dl')
605 it('Should update youtube-dl from raw URL', async function () {
606 this.timeout(120_000)
608 await testBinaryUpdate('https://yt-dl.org/downloads/latest/youtube-dl', 'youtube-dl')
611 it('Should update youtube-dl from youtube-dl fork', async function () {
612 this.timeout(120_000)
614 await testBinaryUpdate('https://api.github.com/repos/yt-dlp/yt-dlp/releases', 'yt-dlp')
617 after(async function () {
618 await cleanupTests([ server ])