]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/cli/optimize-old-videos.ts
Refactor requests
[github/Chocobozzz/PeerTube.git] / server / tests / cli / optimize-old-videos.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
74cd011b
C
2
3import 'mocha'
4import * as chai from 'chai'
daf6e480 5import { join } from 'path'
74cd011b 6import {
7c3b7976 7 cleanupTests,
74cd011b 8 doubleFollow,
254d3579 9 createMultipleServers,
7243f84d 10 generateHighBitrateVideo,
254d3579 11 PeerTubeServer,
74cd011b 12 setAccessTokensToServers,
d23dd9fb
C
13 wait,
14 waitJobs
15} from '@shared/extra-utils'
16import { getMaxBitrate, VideoResolution } from '@shared/models'
daf6e480 17import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../../helpers/ffprobe-utils'
74dc3bca 18import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants'
74cd011b
C
19
20const expect = chai.expect
21
22describe('Test optimize old videos', function () {
254d3579 23 let servers: PeerTubeServer[] = []
74cd011b
C
24
25 before(async function () {
26 this.timeout(200000)
27
74cd011b 28 // Run server 2 to have transcoding enabled
254d3579 29 servers = await createMultipleServers(2)
74cd011b
C
30 await setAccessTokensToServers(servers)
31
32 await doubleFollow(servers[0], servers[1])
33
34 let tempFixturePath: string
35
36 {
37 tempFixturePath = await generateHighBitrateVideo()
38
39 const bitrate = await getVideoFileBitrate(tempFixturePath)
941c5eac 40 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 25, VIDEO_TRANSCODING_FPS))
74cd011b
C
41 }
42
43 // Upload two videos for our needs
89d241a7
C
44 await servers[0].videos.upload({ attributes: { name: 'video1', fixture: tempFixturePath } })
45 await servers[0].videos.upload({ attributes: { name: 'video2', fixture: tempFixturePath } })
74cd011b
C
46
47 await waitJobs(servers)
48 })
49
50 it('Should have two video files on each server', async function () {
51 this.timeout(30000)
52
53 for (const server of servers) {
89d241a7 54 const { data } = await server.videos.list()
d23dd9fb
C
55 expect(data).to.have.lengthOf(2)
56
57 for (const video of data) {
89d241a7 58 const videoDetails = await server.videos.get({ id: video.uuid })
d23dd9fb 59 expect(videoDetails.files).to.have.lengthOf(1)
74cd011b
C
60 }
61 }
62 })
63
64 it('Should run optimize script', async function () {
5b77537c 65 this.timeout(200000)
74cd011b 66
89d241a7 67 await servers[0].cli.execWithEnv('npm run optimize-old-videos')
74cd011b
C
68 await waitJobs(servers)
69
70 for (const server of servers) {
89d241a7 71 const { data } = await server.videos.list()
d23dd9fb 72 expect(data).to.have.lengthOf(2)
74cd011b 73
d23dd9fb 74 for (const video of data) {
89d241a7 75 await server.videos.view({ id: video.uuid })
74cd011b
C
76
77 // Refresh video
78 await waitJobs(servers)
79 await wait(5000)
80 await waitJobs(servers)
81
89d241a7 82 const videoDetails = await server.videos.get({ id: video.uuid })
74cd011b 83
d23dd9fb
C
84 expect(videoDetails.files).to.have.lengthOf(1)
85 const file = videoDetails.files[0]
74cd011b 86
941c5eac 87 expect(file.size).to.be.below(8000000)
74cd011b 88
89d241a7 89 const path = servers[0].servers.buildDirectory(join('videos', video.uuid + '-' + file.resolution.id + '.mp4'))
74cd011b
C
90 const bitrate = await getVideoFileBitrate(path)
91 const fps = await getVideoFileFPS(path)
92 const resolution = await getVideoFileResolution(path)
93
94 expect(resolution.videoFileResolution).to.equal(file.resolution.id)
95 expect(bitrate).to.be.below(getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
96 }
97 }
98 })
99
7c3b7976
C
100 after(async function () {
101 await cleanupTests(servers)
74cd011b
C
102 })
103})