1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import * as ffmpeg from 'fluent-ffmpeg'
5 import { ensureDir, exists, pathExists, readFile, stat } from 'fs-extra'
6 import { basename, dirname, isAbsolute, join, resolve } from 'path'
7 import * as request from 'supertest'
8 import * as WebTorrent from 'webtorrent'
9 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
11 const expect = chai.expect
12 let webtorrent: WebTorrent.Instance
14 function immutableAssign<T, U> (target: T, source: U) {
15 return Object.assign<{}, T, U>({}, target, source)
18 // Default interval -> 5 minutes
19 function dateIsValid (dateString: string, interval = 300000) {
20 const dateToCheck = new Date(dateString)
21 const now = new Date()
23 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
26 function wait (milliseconds: number) {
27 return new Promise(resolve => setTimeout(resolve, milliseconds))
30 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
31 const WebTorrent = require('webtorrent')
33 if (!webtorrent) webtorrent = new WebTorrent()
34 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
36 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
41 let root = join(__dirname, '..', '..', '..')
43 if (basename(root) === 'dist') root = resolve(root, '..')
48 function buildServerDirectory (server: { internalServerNumber: number }, directory: string) {
49 return join(root(), 'test' + server.internalServerNumber, directory)
52 async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
53 const res = await request(url)
55 .expect(HttpStatusCode.OK_200)
59 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
60 const minLength = body.length - ((30 * body.length) / 100)
61 const maxLength = body.length + ((30 * body.length) / 100)
63 expect(data.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
64 expect(data.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
67 async function testFileExistsOrNot (server: { internalServerNumber: number }, directory: string, filePath: string, exist: boolean) {
68 const base = buildServerDirectory(server, directory)
70 expect(await pathExists(join(base, filePath))).to.equal(exist)
73 function isGithubCI () {
74 return !!process.env.GITHUB_WORKSPACE
77 function buildAbsoluteFixturePath (path: string, customCIPath = false) {
78 if (isAbsolute(path)) return path
80 if (customCIPath && process.env.GITHUB_WORKSPACE) {
81 return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
84 return join(root(), 'server', 'tests', 'fixtures', path)
87 function areHttpImportTestsDisabled () {
88 const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true'
90 if (disabled) console.log('Import tests are disabled')
95 async function generateHighBitrateVideo () {
96 const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
98 await ensureDir(dirname(tempFixturePath))
100 const exists = await pathExists(tempFixturePath)
102 console.log('Generating high bitrate video.')
104 // Generate a random, high bitrate video on the fly, so we don't have to include
105 // a large file in the repo. The video needs to have a certain minimum length so
106 // that FFmpeg properly applies bitrate limits.
107 // https://stackoverflow.com/a/15795112
108 return new Promise<string>((res, rej) => {
110 .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
111 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
112 .outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
113 .output(tempFixturePath)
115 .on('end', () => res(tempFixturePath))
120 return tempFixturePath
123 async function generateVideoWithFramerate (fps = 60) {
124 const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true)
126 await ensureDir(dirname(tempFixturePath))
128 const exists = await pathExists(tempFixturePath)
130 console.log('Generating video with framerate %d.', fps)
132 return new Promise<string>((res, rej) => {
134 .outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ])
135 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
136 .outputOptions([ `-r ${fps}` ])
137 .output(tempFixturePath)
139 .on('end', () => res(tempFixturePath))
144 return tempFixturePath
147 async function getFileSize (path: string) {
148 const stats = await stat(path)
153 // ---------------------------------------------------------------------------
158 areHttpImportTestsDisabled,
159 buildServerDirectory,
165 buildAbsoluteFixturePath,
168 generateHighBitrateVideo,
169 generateVideoWithFramerate