1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { checkResolutionsInMasterPlaylist, expectStartWith } from '@server/tests/shared'
6 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
7 import { HttpStatusCode, VideoDetails } from '@shared/models'
11 createMultipleServers,
13 expectNoFailedTranscodingJob,
17 setAccessTokensToServers,
19 } from '@shared/server-commands'
21 const expect = chai.expect
23 async function checkFilesInObjectStorage (video: VideoDetails) {
24 for (const file of video.files) {
25 expectStartWith(file.fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
26 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
29 if (video.streamingPlaylists.length === 0) return
31 const hlsPlaylist = video.streamingPlaylists[0]
32 for (const file of hlsPlaylist.files) {
33 expectStartWith(file.fileUrl, ObjectStorageCommand.getPlaylistBaseUrl())
34 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
37 expectStartWith(hlsPlaylist.playlistUrl, ObjectStorageCommand.getPlaylistBaseUrl())
38 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
40 expectStartWith(hlsPlaylist.segmentsSha256Url, ObjectStorageCommand.getPlaylistBaseUrl())
41 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
44 function runTests (objectStorage: boolean) {
45 let servers: PeerTubeServer[] = []
47 let publishedAt: string
49 before(async function () {
52 const config = objectStorage
53 ? ObjectStorageCommand.getDefaultConfig()
56 // Run server 2 to have transcoding enabled
57 servers = await createMultipleServers(2, config)
58 await setAccessTokensToServers(servers)
60 await servers[0].config.disableTranscoding()
62 await doubleFollow(servers[0], servers[1])
64 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
66 const { shortUUID } = await servers[0].videos.quickUpload({ name: 'video' })
69 await waitJobs(servers)
71 const video = await servers[0].videos.get({ id: videoUUID })
72 publishedAt = video.publishedAt as string
74 await servers[0].config.enableTranscoding()
77 it('Should generate HLS', async function () {
80 await servers[0].videos.runTranscoding({
82 transcodingType: 'hls'
85 await waitJobs(servers)
86 await expectNoFailedTranscodingJob(servers[0])
88 for (const server of servers) {
89 const videoDetails = await server.videos.get({ id: videoUUID })
91 expect(videoDetails.files).to.have.lengthOf(1)
92 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
93 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
95 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
99 it('Should generate WebTorrent', async function () {
102 await servers[0].videos.runTranscoding({
104 transcodingType: 'webtorrent'
107 await waitJobs(servers)
109 for (const server of servers) {
110 const videoDetails = await server.videos.get({ id: videoUUID })
112 expect(videoDetails.files).to.have.lengthOf(5)
113 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
114 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
116 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
120 it('Should generate WebTorrent from HLS only video', async function () {
123 await servers[0].videos.removeWebTorrentFiles({ videoId: videoUUID })
124 await waitJobs(servers)
126 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
127 await waitJobs(servers)
129 for (const server of servers) {
130 const videoDetails = await server.videos.get({ id: videoUUID })
132 expect(videoDetails.files).to.have.lengthOf(5)
133 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
134 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
136 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
140 it('Should only generate WebTorrent', async function () {
143 await servers[0].videos.removeHLSFiles({ videoId: videoUUID })
144 await waitJobs(servers)
146 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
147 await waitJobs(servers)
149 for (const server of servers) {
150 const videoDetails = await server.videos.get({ id: videoUUID })
152 expect(videoDetails.files).to.have.lengthOf(5)
153 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
155 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
159 it('Should correctly update HLS playlist on resolution change', async function () {
162 await servers[0].config.updateExistingSubConfig({
166 resolutions: ConfigCommand.getCustomConfigResolutions(false),
178 const { uuid } = await servers[0].videos.quickUpload({ name: 'quick' })
180 await waitJobs(servers)
182 for (const server of servers) {
183 const videoDetails = await server.videos.get({ id: uuid })
185 expect(videoDetails.files).to.have.lengthOf(1)
186 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
187 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1)
189 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
192 await servers[0].config.updateExistingSubConfig({
196 resolutions: ConfigCommand.getCustomConfigResolutions(true),
208 await servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
209 await waitJobs(servers)
211 for (const server of servers) {
212 const videoDetails = await server.videos.get({ id: uuid })
214 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
215 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
218 await checkFilesInObjectStorage(videoDetails)
220 const hlsPlaylist = videoDetails.streamingPlaylists[0]
221 const resolutions = hlsPlaylist.files.map(f => f.resolution.id)
222 await checkResolutionsInMasterPlaylist({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions })
224 const shaBody = await servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
225 expect(Object.keys(shaBody)).to.have.lengthOf(5)
230 it('Should not have updated published at attributes', async function () {
231 const video = await servers[0].videos.get({ id: videoUUID })
233 expect(video.publishedAt).to.equal(publishedAt)
236 after(async function () {
237 await cleanupTests(servers)
241 describe('Test create transcoding jobs from API', function () {
243 describe('On filesystem', function () {
247 describe('On object storage', function () {
248 if (areObjectStorageTestsDisabled()) return