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 { areObjectStorageTestsDisabled } 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 (video: VideoDetails) {
21 for (const file of video.files) {
22 expectStartWith(file.fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
23 await makeRawRequest(file.fileUrl, 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, ObjectStorageCommand.getPlaylistBaseUrl())
31 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
34 expectStartWith(hlsPlaylist.playlistUrl, ObjectStorageCommand.getPlaylistBaseUrl())
35 await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200)
37 expectStartWith(hlsPlaylist.segmentsSha256Url, ObjectStorageCommand.getPlaylistBaseUrl())
38 await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200)
41 function runTests (objectStorage: boolean) {
42 let servers: PeerTubeServer[] = []
44 let publishedAt: string
46 let shouldBeDeleted: string[]
48 before(async function () {
51 const config = objectStorage
52 ? ObjectStorageCommand.getDefaultConfig()
55 // Run server 2 to have transcoding enabled
56 servers = await createMultipleServers(2, config)
57 await setAccessTokensToServers(servers)
59 await servers[0].config.disableTranscoding()
61 await doubleFollow(servers[0], servers[1])
63 if (objectStorage) await ObjectStorageCommand.prepareDefaultBuckets()
65 const { shortUUID } = await servers[0].videos.quickUpload({ name: 'video' })
68 await waitJobs(servers)
70 const video = await servers[0].videos.get({ id: videoUUID })
71 publishedAt = video.publishedAt as string
73 await servers[0].config.enableTranscoding()
76 it('Should generate HLS', async function () {
79 await servers[0].videos.runTranscoding({
81 transcodingType: 'hls'
84 await waitJobs(servers)
85 await expectNoFailedTranscodingJob(servers[0])
87 for (const server of servers) {
88 const videoDetails = await server.videos.get({ id: videoUUID })
90 expect(videoDetails.files).to.have.lengthOf(1)
91 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
92 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
94 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
98 it('Should generate WebTorrent', async function () {
101 await servers[0].videos.runTranscoding({
103 transcodingType: 'webtorrent'
106 await waitJobs(servers)
108 for (const server of servers) {
109 const videoDetails = await server.videos.get({ id: videoUUID })
111 expect(videoDetails.files).to.have.lengthOf(5)
112 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
113 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
115 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
119 it('Should generate WebTorrent from HLS only video', async function () {
122 await servers[0].videos.removeAllWebTorrentFiles({ videoId: videoUUID })
123 await waitJobs(servers)
125 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
126 await waitJobs(servers)
128 for (const server of servers) {
129 const videoDetails = await server.videos.get({ id: videoUUID })
131 expect(videoDetails.files).to.have.lengthOf(5)
132 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
133 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
135 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
139 it('Should only generate WebTorrent', async function () {
142 await servers[0].videos.removeHLSPlaylist({ videoId: videoUUID })
143 await waitJobs(servers)
145 await servers[0].videos.runTranscoding({ videoId: videoUUID, transcodingType: 'webtorrent' })
146 await waitJobs(servers)
148 for (const server of servers) {
149 const videoDetails = await server.videos.get({ id: videoUUID })
151 expect(videoDetails.files).to.have.lengthOf(5)
152 expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
154 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
158 it('Should correctly update HLS playlist on resolution change', async function () {
161 await servers[0].config.updateExistingSubConfig({
165 resolutions: ConfigCommand.getCustomConfigResolutions(false),
177 const { uuid } = await servers[0].videos.quickUpload({ name: 'quick' })
179 await waitJobs(servers)
181 for (const server of servers) {
182 const videoDetails = await server.videos.get({ id: uuid })
184 expect(videoDetails.files).to.have.lengthOf(1)
185 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
186 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(1)
188 if (objectStorage) await checkFilesInObjectStorage(videoDetails)
191 videoDetails.streamingPlaylists[0].files[0].fileUrl,
192 videoDetails.streamingPlaylists[0].playlistUrl,
193 videoDetails.streamingPlaylists[0].segmentsSha256Url
197 await servers[0].config.updateExistingSubConfig({
201 resolutions: ConfigCommand.getCustomConfigResolutions(true),
213 await servers[0].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
214 await waitJobs(servers)
216 for (const server of servers) {
217 const videoDetails = await server.videos.get({ id: uuid })
219 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
220 expect(videoDetails.streamingPlaylists[0].files).to.have.lengthOf(5)
223 await checkFilesInObjectStorage(videoDetails)
225 const hlsPlaylist = videoDetails.streamingPlaylists[0]
226 const resolutions = hlsPlaylist.files.map(f => f.resolution.id)
227 await checkResolutionsInMasterPlaylist({ server: servers[0], playlistUrl: hlsPlaylist.playlistUrl, resolutions })
229 const shaBody = await servers[0].streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
230 expect(Object.keys(shaBody)).to.have.lengthOf(5)
235 it('Should have correctly deleted previous files', async function () {
236 for (const fileUrl of shouldBeDeleted) {
237 await makeRawRequest(fileUrl, HttpStatusCode.NOT_FOUND_404)
241 it('Should not have updated published at attributes', async function () {
242 const video = await servers[0].videos.get({ id: videoUUID })
244 expect(video.publishedAt).to.equal(publishedAt)
247 after(async function () {
248 await cleanupTests(servers)
252 describe('Test create transcoding jobs from API', function () {
254 describe('On filesystem', function () {
258 describe('On object storage', function () {
259 if (areObjectStorageTestsDisabled()) return