1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
6 import { HttpStatusCode, VideoFile } from '@shared/models'
14 setAccessTokensToServers,
16 } from '@shared/server-commands'
17 import { checkResolutionsInMasterPlaylist, expectStartWith } from '../shared'
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 hlsPlaylist = videoDetails.streamingPlaylists[0]
168 const files = hlsPlaylist.files
169 expect(files).to.have.lengthOf(1)
170 expect(files[0].resolution.id).to.equal(480)
173 await checkFilesInObjectStorage(files, 'playlist')
175 const resolutions = files.map(f => f.resolution.id)
176 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
181 it('Should not duplicate an HLS resolution', async function () {
184 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
186 await waitJobs(servers)
188 for (const server of servers) {
189 const videoDetails = await server.videos.get({ id: videosUUID[2] })
191 const files = videoDetails.streamingPlaylists[0].files
192 expect(files).to.have.lengthOf(1)
193 expect(files[0].resolution.id).to.equal(480)
195 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
199 it('Should generate all HLS resolutions', async function () {
202 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
204 await waitJobs(servers)
206 for (const server of servers) {
207 const videoDetails = await server.videos.get({ id: videosUUID[3] })
209 expect(videoDetails.files).to.have.lengthOf(1)
210 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
212 const files = videoDetails.streamingPlaylists[0].files
213 expect(files).to.have.lengthOf(4)
215 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
219 it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
222 await servers[0].config.enableTranscoding()
223 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
225 await waitJobs(servers)
227 for (const server of servers) {
228 const videoDetails = await server.videos.get({ id: videosUUID[4] })
230 expect(videoDetails.files).to.have.lengthOf(5)
231 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
232 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
235 await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
236 await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist')
241 it('Should not have updated published at attributes', async function () {
242 for (const id of videosUUID) {
243 const video = await servers[0].videos.get({ id })
245 expect(publishedAt.some(p => video.publishedAt === p)).to.be.true
249 after(async function () {
250 await cleanupTests(servers)
254 describe('Test create transcoding jobs', function () {
256 describe('On filesystem', function () {
260 describe('On object storage', function () {
261 if (areObjectStorageTestsDisabled()) return