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[] = []
36 const publishedAt: string[] = []
38 before(async function () {
41 const config = objectStorage
42 ? ObjectStorageCommand.getDefaultConfig()
45 // Run server 2 to have transcoding enabled
46 servers = await createMultipleServers(2, config)
47 await setAccessTokensToServers(servers)
49 await servers[0].config.disableTranscoding()
51 await doubleFollow(servers[0], servers[1])
53 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
55 for (let i = 1; i <= 5; i++) {
56 const { uuid, shortUUID } = await servers[0].videos.upload({ attributes: { name: 'video' + i } })
58 await waitJobs(servers)
60 const video = await servers[0].videos.get({ id: uuid })
61 publishedAt.push(video.publishedAt as string)
66 videosUUID.push(shortUUID)
70 await waitJobs(servers)
73 it('Should have two video files on each server', async function () {
76 for (const server of servers) {
77 const { data } = await server.videos.list()
78 expect(data).to.have.lengthOf(videosUUID.length)
80 for (const video of data) {
81 const videoDetail = await server.videos.get({ id: video.uuid })
82 expect(videoDetail.files).to.have.lengthOf(1)
83 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
88 it('Should run a transcoding job on video 2', async function () {
91 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
92 await waitJobs(servers)
94 for (const server of servers) {
95 const { data } = await server.videos.list()
97 let infoHashes: { [id: number]: string }
99 for (const video of data) {
100 const videoDetails = await server.videos.get({ id: video.uuid })
102 if (video.shortUUID === videosUUID[1] || video.uuid === videosUUID[1]) {
103 expect(videoDetails.files).to.have.lengthOf(4)
104 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
106 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
111 for (const file of videoDetails.files) {
112 infoHashes[file.resolution.id.toString()] = file.magnetUri
115 for (const resolution of Object.keys(infoHashes)) {
116 const file = videoDetails.files.find(f => f.resolution.id.toString() === resolution)
117 expect(file.magnetUri).to.equal(infoHashes[resolution])
121 expect(videoDetails.files).to.have.lengthOf(1)
122 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
128 it('Should run a transcoding job on video 1 with resolution', async function () {
131 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)
133 await waitJobs(servers)
135 for (const server of servers) {
136 const { data } = await server.videos.list()
137 expect(data).to.have.lengthOf(videosUUID.length)
139 const videoDetails = await server.videos.get({ id: videosUUID[0] })
141 expect(videoDetails.files).to.have.lengthOf(2)
142 expect(videoDetails.files[0].resolution.id).to.equal(720)
143 expect(videoDetails.files[1].resolution.id).to.equal(480)
145 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
147 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
151 it('Should generate an HLS resolution', async function () {
154 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
156 await waitJobs(servers)
158 for (const server of servers) {
159 const videoDetails = await server.videos.get({ id: videosUUID[2] })
161 expect(videoDetails.files).to.have.lengthOf(1)
162 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
164 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
166 const files = videoDetails.streamingPlaylists[0].files
167 expect(files).to.have.lengthOf(1)
168 expect(files[0].resolution.id).to.equal(480)
170 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
174 it('Should not duplicate an HLS resolution', async function () {
177 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
179 await waitJobs(servers)
181 for (const server of servers) {
182 const videoDetails = await server.videos.get({ id: videosUUID[2] })
184 const files = videoDetails.streamingPlaylists[0].files
185 expect(files).to.have.lengthOf(1)
186 expect(files[0].resolution.id).to.equal(480)
188 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
192 it('Should generate all HLS resolutions', async function () {
195 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
197 await waitJobs(servers)
199 for (const server of servers) {
200 const videoDetails = await server.videos.get({ id: videosUUID[3] })
202 expect(videoDetails.files).to.have.lengthOf(1)
203 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
205 const files = videoDetails.streamingPlaylists[0].files
206 expect(files).to.have.lengthOf(4)
208 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
212 it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
215 await servers[0].config.enableTranscoding()
216 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
218 await waitJobs(servers)
220 for (const server of servers) {
221 const videoDetails = await server.videos.get({ id: videosUUID[4] })
223 expect(videoDetails.files).to.have.lengthOf(5)
224 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
225 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
228 await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
229 await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist')
234 it('Should not have updated published at attributes', async function () {
235 for (const id of videosUUID) {
236 const video = await servers[0].videos.get({ id })
238 expect(publishedAt.some(p => video.publishedAt === p)).to.be.true
242 after(async function () {
243 await cleanupTests(servers)
247 describe('Test create transcoding jobs', function () {
249 describe('On filesystem', function () {
253 describe('On object storage', function () {
254 if (areObjectStorageTestsDisabled()) return