1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
5 import { HttpStatusCode, VideoFile } from '@shared/models'
13 setAccessTokensToServers,
15 } from '@shared/server-commands'
16 import { checkResolutionsInMasterPlaylist, expectStartWith } from '../shared'
18 const expect = chai.expect
20 async function checkFilesInObjectStorage (files: VideoFile[], type: 'webtorrent' | 'playlist') {
21 for (const file of files) {
22 const shouldStartWith = type === 'webtorrent'
23 ? ObjectStorageCommand.getWebTorrentBaseUrl()
24 : ObjectStorageCommand.getPlaylistBaseUrl()
26 expectStartWith(file.fileUrl, shouldStartWith)
28 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
32 function runTests (objectStorage: boolean) {
33 let servers: PeerTubeServer[] = []
34 const videosUUID: string[] = []
35 const publishedAt: 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 } })
57 await waitJobs(servers)
59 const video = await servers[0].videos.get({ id: uuid })
60 publishedAt.push(video.publishedAt as string)
65 videosUUID.push(shortUUID)
69 await waitJobs(servers)
72 it('Should have two video files on each server', async function () {
75 for (const server of servers) {
76 const { data } = await server.videos.list()
77 expect(data).to.have.lengthOf(videosUUID.length)
79 for (const video of data) {
80 const videoDetail = await server.videos.get({ id: video.uuid })
81 expect(videoDetail.files).to.have.lengthOf(1)
82 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
87 it('Should run a transcoding job on video 2', async function () {
90 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
91 await waitJobs(servers)
93 for (const server of servers) {
94 const { data } = await server.videos.list()
96 let infoHashes: { [id: number]: string }
98 for (const video of data) {
99 const videoDetails = await server.videos.get({ id: video.uuid })
101 if (video.shortUUID === videosUUID[1] || video.uuid === videosUUID[1]) {
102 expect(videoDetails.files).to.have.lengthOf(4)
103 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
105 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
110 for (const file of videoDetails.files) {
111 infoHashes[file.resolution.id.toString()] = file.magnetUri
114 for (const resolution of Object.keys(infoHashes)) {
115 const file = videoDetails.files.find(f => f.resolution.id.toString() === resolution)
116 expect(file.magnetUri).to.equal(infoHashes[resolution])
120 expect(videoDetails.files).to.have.lengthOf(1)
121 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
127 it('Should run a transcoding job on video 1 with resolution', async function () {
130 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)
132 await waitJobs(servers)
134 for (const server of servers) {
135 const { data } = await server.videos.list()
136 expect(data).to.have.lengthOf(videosUUID.length)
138 const videoDetails = await server.videos.get({ id: videosUUID[0] })
140 expect(videoDetails.files).to.have.lengthOf(2)
141 expect(videoDetails.files[0].resolution.id).to.equal(720)
142 expect(videoDetails.files[1].resolution.id).to.equal(480)
144 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
146 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
150 it('Should generate an HLS resolution', async function () {
153 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
155 await waitJobs(servers)
157 for (const server of servers) {
158 const videoDetails = await server.videos.get({ id: videosUUID[2] })
160 expect(videoDetails.files).to.have.lengthOf(1)
161 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
163 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
165 const hlsPlaylist = videoDetails.streamingPlaylists[0]
167 const files = hlsPlaylist.files
168 expect(files).to.have.lengthOf(1)
169 expect(files[0].resolution.id).to.equal(480)
172 await checkFilesInObjectStorage(files, 'playlist')
174 const resolutions = files.map(f => f.resolution.id)
175 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
180 it('Should not duplicate an HLS resolution', async function () {
183 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
185 await waitJobs(servers)
187 for (const server of servers) {
188 const videoDetails = await server.videos.get({ id: videosUUID[2] })
190 const files = videoDetails.streamingPlaylists[0].files
191 expect(files).to.have.lengthOf(1)
192 expect(files[0].resolution.id).to.equal(480)
194 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
198 it('Should generate all HLS resolutions', async function () {
201 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
203 await waitJobs(servers)
205 for (const server of servers) {
206 const videoDetails = await server.videos.get({ id: videosUUID[3] })
208 expect(videoDetails.files).to.have.lengthOf(1)
209 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
211 const files = videoDetails.streamingPlaylists[0].files
212 expect(files).to.have.lengthOf(4)
214 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
218 it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
221 await servers[0].config.enableTranscoding()
222 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
224 await waitJobs(servers)
226 for (const server of servers) {
227 const videoDetails = await server.videos.get({ id: videosUUID[4] })
229 expect(videoDetails.files).to.have.lengthOf(5)
230 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
231 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
234 await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
235 await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist')
240 it('Should not have updated published at attributes', async function () {
241 for (const id of videosUUID) {
242 const video = await servers[0].videos.get({ id })
244 expect(publishedAt.some(p => video.publishedAt === p)).to.be.true
248 after(async function () {
249 await cleanupTests(servers)
253 describe('Test create transcoding jobs', function () {
255 describe('On filesystem', function () {
259 describe('On object storage', function () {
260 if (areObjectStorageTestsDisabled()) return