1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode, VideoFile } from '@shared/models'
7 areObjectStorageTestsDisabled,
15 setAccessTokensToServers,
17 } from '../../../shared/extra-utils'
19 const expect = chai.expect
21 async function checkFilesInObjectStorage (files: VideoFile[], type: 'webtorrent' | 'playlist') {
22 for (const file of files) {
23 const shouldStartWith = type === 'webtorrent'
24 ? ObjectStorageCommand.getWebTorrentBaseUrl()
25 : ObjectStorageCommand.getPlaylistBaseUrl()
27 expectStartWith(file.fileUrl, shouldStartWith)
29 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
33 function runTests (objectStorage: boolean) {
34 let servers: PeerTubeServer[] = []
35 const videosUUID: string[] = []
37 before(async function () {
40 const config = objectStorage
41 ? ObjectStorageCommand.getDefaultConfig()
44 // Run server 2 to have transcoding enabled
45 servers = await createMultipleServers(2, config)
46 await setAccessTokensToServers(servers)
48 await servers[0].config.disableTranscoding()
50 await doubleFollow(servers[0], servers[1])
52 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
54 for (let i = 1; i <= 5; i++) {
55 const { uuid, shortUUID } = await servers[0].videos.upload({ attributes: { name: 'video' + i } })
60 videosUUID.push(shortUUID)
64 await waitJobs(servers)
67 it('Should have two video files on each server', async function () {
70 for (const server of servers) {
71 const { data } = await server.videos.list()
72 expect(data).to.have.lengthOf(videosUUID.length)
74 for (const video of data) {
75 const videoDetail = await server.videos.get({ id: video.uuid })
76 expect(videoDetail.files).to.have.lengthOf(1)
77 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
82 it('Should run a transcoding job on video 2', async function () {
85 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
86 await waitJobs(servers)
88 for (const server of servers) {
89 const { data } = await server.videos.list()
91 let infoHashes: { [id: number]: string }
93 for (const video of data) {
94 const videoDetails = await server.videos.get({ id: video.uuid })
96 if (video.shortUUID === videosUUID[1] || video.uuid === videosUUID[1]) {
97 expect(videoDetails.files).to.have.lengthOf(4)
98 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
100 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
105 for (const file of videoDetails.files) {
106 infoHashes[file.resolution.id.toString()] = file.magnetUri
109 for (const resolution of Object.keys(infoHashes)) {
110 const file = videoDetails.files.find(f => f.resolution.id.toString() === resolution)
111 expect(file.magnetUri).to.equal(infoHashes[resolution])
115 expect(videoDetails.files).to.have.lengthOf(1)
116 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
122 it('Should run a transcoding job on video 1 with resolution', async function () {
125 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)
127 await waitJobs(servers)
129 for (const server of servers) {
130 const { data } = await server.videos.list()
131 expect(data).to.have.lengthOf(videosUUID.length)
133 const videoDetails = await server.videos.get({ id: videosUUID[0] })
135 expect(videoDetails.files).to.have.lengthOf(2)
136 expect(videoDetails.files[0].resolution.id).to.equal(720)
137 expect(videoDetails.files[1].resolution.id).to.equal(480)
139 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
141 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
145 it('Should generate an HLS resolution', async function () {
148 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
150 await waitJobs(servers)
152 for (const server of servers) {
153 const videoDetails = await server.videos.get({ id: videosUUID[2] })
155 expect(videoDetails.files).to.have.lengthOf(1)
156 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
158 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
160 const files = videoDetails.streamingPlaylists[0].files
161 expect(files).to.have.lengthOf(1)
162 expect(files[0].resolution.id).to.equal(480)
164 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
168 it('Should not duplicate an HLS resolution', async function () {
171 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
173 await waitJobs(servers)
175 for (const server of servers) {
176 const videoDetails = await server.videos.get({ id: videosUUID[2] })
178 const files = videoDetails.streamingPlaylists[0].files
179 expect(files).to.have.lengthOf(1)
180 expect(files[0].resolution.id).to.equal(480)
182 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
186 it('Should generate all HLS resolutions', async function () {
189 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
191 await waitJobs(servers)
193 for (const server of servers) {
194 const videoDetails = await server.videos.get({ id: videosUUID[3] })
196 expect(videoDetails.files).to.have.lengthOf(1)
197 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
199 const files = videoDetails.streamingPlaylists[0].files
200 expect(files).to.have.lengthOf(4)
202 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
206 it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
209 await servers[0].config.enableTranscoding()
210 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
212 await waitJobs(servers)
214 for (const server of servers) {
215 const videoDetails = await server.videos.get({ id: videosUUID[4] })
217 expect(videoDetails.files).to.have.lengthOf(4)
218 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
219 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(4)
222 await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
223 await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist')
228 after(async function () {
229 await cleanupTests(servers)
233 describe('Test create transcoding jobs', function () {
235 describe('On filesystem', function () {
239 describe('On object storage', function () {
240 if (areObjectStorageTestsDisabled()) return