1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { checkResolutionsInMasterPlaylist, expectStartWith } from '@server/tests/shared'
5 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
6 import { HttpStatusCode, VideoDetails } from '@shared/models'
10 createMultipleServers,
12 expectNoFailedTranscodingJob,
16 setAccessTokensToServers,
18 } from '@shared/server-commands'
20 const expect = chai.expect
22 async function checkFilesInObjectStorage (video: VideoDetails) {
23 for (const file of video.files) {
24 expectStartWith(file.fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
25 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
28 if (video.streamingPlaylists.length === 0) return
30 const hlsPlaylist = video.streamingPlaylists[0]
31 for (const file of hlsPlaylist.files) {
32 expectStartWith(file.fileUrl, ObjectStorageCommand.getPlaylistBaseUrl())
33 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
36 expectStartWith(hlsPlaylist.playlistUrl, ObjectStorageCommand.getPlaylistBaseUrl())
37 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
39 expectStartWith(hlsPlaylist.segmentsSha256Url, ObjectStorageCommand.getPlaylistBaseUrl())
40 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
43 function runTests (objectStorage: boolean) {
44 let servers: PeerTubeServer[] = []
46 let publishedAt: string
48 let shouldBeDeleted: string[]
50 before(async function () {
53 const config = objectStorage
54 ? ObjectStorageCommand.getDefaultConfig()
57 // Run server 2 to have transcoding enabled
58 servers = await createMultipleServers(2, config)
59 await setAccessTokensToServers(servers)
61 await servers[0].config.disableTranscoding()
63 await doubleFollow(servers[0], servers[1])
65 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
67 const { shortUUID } = await servers[0].videos.quickUpload({ name: 'video' })
70 await waitJobs(servers)
72 const video = await servers[0].videos.get({ id: videoUUID })
73 publishedAt = video.publishedAt as string
75 await servers[0].config.enableTranscoding()
78 it('Should generate HLS', async function () {
81 await servers[0].videos.runTranscoding({
83 transcodingType: 'hls'
86 await waitJobs(servers)
87 await expectNoFailedTranscodingJob(servers[0])
89 for (const server of servers) {
90 const videoDetails = await server.videos.get({ id: videoUUID })
92 expect(videoDetails.files).to.have.lengthOf(1)
93 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
94 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
96 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
100 it('Should generate WebTorrent', async function () {
103 await servers[0].videos.runTranscoding({
105 transcodingType: 'webtorrent'
108 await waitJobs(servers)
110 for (const server of servers) {
111 const videoDetails = await server.videos.get({ id: videoUUID })
113 expect(videoDetails.files).to.have.lengthOf(5)
114 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
115 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
117 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
121 it('Should generate WebTorrent from HLS only video', async function () {
124 await servers[0].videos.removeAllWebTorrentFiles({ videoId: videoUUID })
125 await waitJobs(servers)
127 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
128 await waitJobs(servers)
130 for (const server of servers) {
131 const videoDetails = await server.videos.get({ id: videoUUID })
133 expect(videoDetails.files).to.have.lengthOf(5)
134 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
135 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
137 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
141 it('Should only generate WebTorrent', async function () {
144 await servers[0].videos.removeHLSPlaylist({ videoId: videoUUID })
145 await waitJobs(servers)
147 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
148 await waitJobs(servers)
150 for (const server of servers) {
151 const videoDetails = await server.videos.get({ id: videoUUID })
153 expect(videoDetails.files).to.have.lengthOf(5)
154 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
156 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
160 it('Should correctly update HLS playlist on resolution change', async function () {
163 await servers[0].config.updateExistingSubConfig({
167 resolutions: ConfigCommand.getCustomConfigResolutions(false),
179 const { uuid } = await servers[0].videos.quickUpload({ name: 'quick' })
181 await waitJobs(servers)
183 for (const server of servers) {
184 const videoDetails = await server.videos.get({ id: uuid })
186 expect(videoDetails.files).to.have.lengthOf(1)
187 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
188 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1)
190 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
193 videoDetails.streamingPlaylists[0].files[0].fileUrl,
194 videoDetails.streamingPlaylists[0].playlistUrl,
195 videoDetails.streamingPlaylists[0].segmentsSha256Url
199 await servers[0].config.updateExistingSubConfig({
203 resolutions: ConfigCommand.getCustomConfigResolutions(true),
215 await servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
216 await waitJobs(servers)
218 for (const server of servers) {
219 const videoDetails = await server.videos.get({ id: uuid })
221 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
222 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
225 await checkFilesInObjectStorage(videoDetails)
227 const hlsPlaylist = videoDetails.streamingPlaylists[0]
228 const resolutions = hlsPlaylist.files.map(f => f.resolution.id)
229 await checkResolutionsInMasterPlaylist({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions })
231 const shaBody = await servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
232 expect(Object.keys(shaBody)).to.have.lengthOf(5)
237 it('Should have correctly deleted previous files', async function () {
238 for (const fileUrl of shouldBeDeleted) {
239 await makeRawRequest(fileUrl, HttpStatusCode.NOT_FOUND_404)
243 it('Should not have updated published at attributes', async function () {
244 const video = await servers[0].videos.get({ id: videoUUID })
246 expect(video.publishedAt).to.equal(publishedAt)
249 after(async function () {
250 await cleanupTests(servers)
254 describe('Test create transcoding jobs from API', function () {
256 describe('On filesystem', function () {
260 describe('On object storage', function () {
261 if (areObjectStorageTestsDisabled()) return