1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { checkResolutionsInMasterPlaylist, expectStartWith } from '@server/tests/shared'
5 import { areMockObjectStorageTestsDisabled } from '@shared/core-utils'
6 import { HttpStatusCode, VideoDetails } from '@shared/models'
10 createMultipleServers,
12 expectNoFailedTranscodingJob,
16 setAccessTokensToServers,
18 } from '@shared/server-commands'
20 async function checkFilesInObjectStorage (objectStorage: ObjectStorageCommand, video: VideoDetails) {
21 for (const file of video.files) {
22 expectStartWith(file.fileUrl, objectStorage.getMockWebVideosBaseUrl())
23 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
26 if (video.streamingPlaylists.length === 0) return
28 const hlsPlaylist = video.streamingPlaylists[0]
29 for (const file of hlsPlaylist.files) {
30 expectStartWith(file.fileUrl, objectStorage.getMockPlaylistBaseUrl())
31 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
34 expectStartWith(hlsPlaylist.playlistUrl, objectStorage.getMockPlaylistBaseUrl())
35 await makeRawRequest({ url: hlsPlaylist.playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
37 expectStartWith(hlsPlaylist.segmentsSha256Url, objectStorage.getMockPlaylistBaseUrl())
38 await makeRawRequest({ url: hlsPlaylist.segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
41 function runTests (enableObjectStorage: boolean) {
42 let servers: PeerTubeServer[] = []
44 let publishedAt: string
46 let shouldBeDeleted: string[]
47 const objectStorage = new ObjectStorageCommand()
49 before(async function () {
52 const config = enableObjectStorage
53 ? objectStorage.getDefaultMockConfig()
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 (enableObjectStorage) await objectStorage.prepareDefaultMockBuckets()
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 (enableObjectStorage) await checkFilesInObjectStorage(objectStorage, 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 (enableObjectStorage) await checkFilesInObjectStorage(objectStorage, videoDetails)
120 it('Should generate WebTorrent from HLS only video', async function () {
123 await servers[0].videos.removeAllWebTorrentFiles({ 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 (enableObjectStorage) await checkFilesInObjectStorage(objectStorage, videoDetails)
140 it('Should only generate WebTorrent', async function () {
143 await servers[0].videos.removeHLSPlaylist({ 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 (enableObjectStorage) await checkFilesInObjectStorage(objectStorage, 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 (enableObjectStorage) await checkFilesInObjectStorage(objectStorage, videoDetails)
192 videoDetails.streamingPlaylists[0].files[0].fileUrl,
193 videoDetails.streamingPlaylists[0].playlistUrl,
194 videoDetails.streamingPlaylists[0].segmentsSha256Url
198 await servers[0].config.updateExistingSubConfig({
202 resolutions: ConfigCommand.getCustomConfigResolutions(true),
214 await servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
215 await waitJobs(servers)
217 for (const server of servers) {
218 const videoDetails = await server.videos.get({ id: uuid })
220 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
221 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
223 if (enableObjectStorage) {
224 await checkFilesInObjectStorage(objectStorage, videoDetails)
226 const hlsPlaylist = videoDetails.streamingPlaylists[0]
227 const resolutions = hlsPlaylist.files.map(f => f.resolution.id)
228 await checkResolutionsInMasterPlaylist({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions })
230 const shaBody = await servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url, withRetry: true })
231 expect(Object.keys(shaBody)).to.have.lengthOf(5)
236 it('Should have correctly deleted previous files', async function () {
237 for (const fileUrl of shouldBeDeleted) {
238 await makeRawRequest({ url: fileUrl, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
242 it('Should not have updated published at attributes', async function () {
243 const video = await servers[0].videos.get({ id: videoUUID })
245 expect(video.publishedAt).to.equal(publishedAt)
248 after(async function () {
249 if (objectStorage) await objectStorage.cleanupMock()
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 (areMockObjectStorageTestsDisabled()) return