1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } 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 async function checkFilesInObjectStorage (files: VideoFile[], type: 'webtorrent' | 'playlist') {
19 for (const file of files) {
20 const shouldStartWith = type === 'webtorrent'
21 ? ObjectStorageCommand.getWebTorrentBaseUrl()
22 : ObjectStorageCommand.getPlaylistBaseUrl()
24 expectStartWith(file.fileUrl, shouldStartWith)
26 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
30 function runTests (objectStorage: boolean) {
31 let servers: PeerTubeServer[] = []
32 const videosUUID: string[] = []
33 const publishedAt: string[] = []
35 before(async function () {
38 const config = objectStorage
39 ? ObjectStorageCommand.getDefaultConfig()
42 // Run server 2 to have transcoding enabled
43 servers = await createMultipleServers(2, config)
44 await setAccessTokensToServers(servers)
46 await servers[0].config.disableTranscoding()
48 await doubleFollow(servers[0], servers[1])
50 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
52 for (let i = 1; i <= 5; i++) {
53 const { uuid, shortUUID } = await servers[0].videos.upload({ attributes: { name: 'video' + i } })
55 await waitJobs(servers)
57 const video = await servers[0].videos.get({ id: uuid })
58 publishedAt.push(video.publishedAt as string)
63 videosUUID.push(shortUUID)
67 await waitJobs(servers)
70 it('Should have two video files on each server', async function () {
73 for (const server of servers) {
74 const { data } = await server.videos.list()
75 expect(data).to.have.lengthOf(videosUUID.length)
77 for (const video of data) {
78 const videoDetail = await server.videos.get({ id: video.uuid })
79 expect(videoDetail.files).to.have.lengthOf(1)
80 expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
85 it('Should run a transcoding job on video 2', async function () {
88 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[1]}`)
89 await waitJobs(servers)
91 for (const server of servers) {
92 const { data } = await server.videos.list()
94 let infoHashes: { [id: number]: string }
96 for (const video of data) {
97 const videoDetails = await server.videos.get({ id: video.uuid })
99 if (video.shortUUID === videosUUID[1] || video.uuid === videosUUID[1]) {
100 expect(videoDetails.files).to.have.lengthOf(4)
101 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
103 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
108 for (const file of videoDetails.files) {
109 infoHashes[file.resolution.id.toString()] = file.magnetUri
112 for (const resolution of Object.keys(infoHashes)) {
113 const file = videoDetails.files.find(f => f.resolution.id.toString() === resolution)
114 expect(file.magnetUri).to.equal(infoHashes[resolution])
118 expect(videoDetails.files).to.have.lengthOf(1)
119 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
125 it('Should run a transcoding job on video 1 with resolution', async function () {
128 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`)
130 await waitJobs(servers)
132 for (const server of servers) {
133 const { data } = await server.videos.list()
134 expect(data).to.have.lengthOf(videosUUID.length)
136 const videoDetails = await server.videos.get({ id: videosUUID[0] })
138 expect(videoDetails.files).to.have.lengthOf(2)
139 expect(videoDetails.files[0].resolution.id).to.equal(720)
140 expect(videoDetails.files[1].resolution.id).to.equal(480)
142 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
144 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
148 it('Should generate an HLS resolution', async function () {
151 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
153 await waitJobs(servers)
155 for (const server of servers) {
156 const videoDetails = await server.videos.get({ id: videosUUID[2] })
158 expect(videoDetails.files).to.have.lengthOf(1)
159 if (objectStorage) await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
161 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
163 const hlsPlaylist = videoDetails.streamingPlaylists[0]
165 const files = hlsPlaylist.files
166 expect(files).to.have.lengthOf(1)
167 expect(files[0].resolution.id).to.equal(480)
170 await checkFilesInObjectStorage(files, 'playlist')
172 const resolutions = files.map(f => f.resolution.id)
173 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
178 it('Should not duplicate an HLS resolution', async function () {
181 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`)
183 await waitJobs(servers)
185 for (const server of servers) {
186 const videoDetails = await server.videos.get({ id: videosUUID[2] })
188 const files = videoDetails.streamingPlaylists[0].files
189 expect(files).to.have.lengthOf(1)
190 expect(files[0].resolution.id).to.equal(480)
192 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
196 it('Should generate all HLS resolutions', async function () {
199 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`)
201 await waitJobs(servers)
203 for (const server of servers) {
204 const videoDetails = await server.videos.get({ id: videosUUID[3] })
206 expect(videoDetails.files).to.have.lengthOf(1)
207 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
209 const files = videoDetails.streamingPlaylists[0].files
210 expect(files).to.have.lengthOf(4)
212 if (objectStorage) await checkFilesInObjectStorage(files, 'playlist')
216 it('Should optimize the video file and generate HLS videos if enabled in config', async function () {
219 await servers[0].config.enableTranscoding()
220 await servers[0].cli.execWithEnv(`npm run create-transcoding-job -- -v ${videosUUID[4]}`)
222 await waitJobs(servers)
224 for (const server of servers) {
225 const videoDetails = await server.videos.get({ id: videosUUID[4] })
227 expect(videoDetails.files).to.have.lengthOf(5)
228 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
229 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
232 await checkFilesInObjectStorage(videoDetails.files, 'webtorrent')
233 await checkFilesInObjectStorage(videoDetails.streamingPlaylists[0].files, 'playlist')
238 it('Should not have updated published at attributes', async function () {
239 for (const id of videosUUID) {
240 const video = await servers[0].videos.get({ id })
242 expect(publishedAt.some(p => video.publishedAt === p)).to.be.true
246 after(async function () {
247 await cleanupTests(servers)
251 describe('Test create transcoding jobs', function () {
253 describe('On filesystem', function () {
257 describe('On object storage', function () {
258 if (areObjectStorageTestsDisabled()) return