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 let shouldBeDeleted: string[]
51 before(async function () {
54 const config = objectStorage
55 ? ObjectStorageCommand.getDefaultConfig()
58 // Run server 2 to have transcoding enabled
59 servers = await createMultipleServers(2, config)
60 await setAccessTokensToServers(servers)
62 await servers[0].config.disableTranscoding()
64 await doubleFollow(servers[0], servers[1])
66 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
68 const { shortUUID } = await servers[0].videos.quickUpload({ name: 'video' })
71 await waitJobs(servers)
73 const video = await servers[0].videos.get({ id: videoUUID })
74 publishedAt = video.publishedAt as string
76 await servers[0].config.enableTranscoding()
79 it('Should generate HLS', async function () {
82 await servers[0].videos.runTranscoding({
84 transcodingType: 'hls'
87 await waitJobs(servers)
88 await expectNoFailedTranscodingJob(servers[0])
90 for (const server of servers) {
91 const videoDetails = await server.videos.get({ id: videoUUID })
93 expect(videoDetails.files).to.have.lengthOf(1)
94 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
95 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
97 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
101 it('Should generate WebTorrent', async function () {
104 await servers[0].videos.runTranscoding({
106 transcodingType: 'webtorrent'
109 await waitJobs(servers)
111 for (const server of servers) {
112 const videoDetails = await server.videos.get({ id: videoUUID })
114 expect(videoDetails.files).to.have.lengthOf(5)
115 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
116 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
118 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
122 it('Should generate WebTorrent from HLS only video', async function () {
125 await servers[0].videos.removeAllWebTorrentFiles({ videoId: videoUUID })
126 await waitJobs(servers)
128 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
129 await waitJobs(servers)
131 for (const server of servers) {
132 const videoDetails = await server.videos.get({ id: videoUUID })
134 expect(videoDetails.files).to.have.lengthOf(5)
135 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
136 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
138 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
142 it('Should only generate WebTorrent', async function () {
145 await servers[0].videos.removeHLSPlaylist({ videoId: videoUUID })
146 await waitJobs(servers)
148 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
149 await waitJobs(servers)
151 for (const server of servers) {
152 const videoDetails = await server.videos.get({ id: videoUUID })
154 expect(videoDetails.files).to.have.lengthOf(5)
155 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
157 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
161 it('Should correctly update HLS playlist on resolution change', async function () {
164 await servers[0].config.updateExistingSubConfig({
168 resolutions: ConfigCommand.getCustomConfigResolutions(false),
180 const { uuid } = await servers[0].videos.quickUpload({ name: 'quick' })
182 await waitJobs(servers)
184 for (const server of servers) {
185 const videoDetails = await server.videos.get({ id: uuid })
187 expect(videoDetails.files).to.have.lengthOf(1)
188 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
189 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1)
191 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
194 videoDetails.streamingPlaylists[0].files[0].fileUrl,
195 videoDetails.streamingPlaylists[0].playlistUrl,
196 videoDetails.streamingPlaylists[0].segmentsSha256Url
200 await servers[0].config.updateExistingSubConfig({
204 resolutions: ConfigCommand.getCustomConfigResolutions(true),
216 await servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
217 await waitJobs(servers)
219 for (const server of servers) {
220 const videoDetails = await server.videos.get({ id: uuid })
222 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
223 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
226 await checkFilesInObjectStorage(videoDetails)
228 const hlsPlaylist = videoDetails.streamingPlaylists[0]
229 const resolutions = hlsPlaylist.files.map(f => f.resolution.id)
230 await checkResolutionsInMasterPlaylist({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions })
232 const shaBody = await servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
233 expect(Object.keys(shaBody)).to.have.lengthOf(5)
238 it('Should have correctly deleted previous files', async function () {
239 for (const fileUrl of shouldBeDeleted) {
240 await makeRawRequest(fileUrl, HttpStatusCode.NOT_FOUND_404)
244 it('Should not have updated published at attributes', async function () {
245 const video = await servers[0].videos.get({ id: videoUUID })
247 expect(video.publishedAt).to.equal(publishedAt)
250 after(async function () {
251 await cleanupTests(servers)
255 describe('Test create transcoding jobs from API', function () {
257 describe('On filesystem', function () {
261 describe('On object storage', function () {
262 if (areObjectStorageTestsDisabled()) return