1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { basename, dirname, isAbsolute, join, resolve } from 'path'
5 import * as request from 'supertest'
6 import * as WebTorrent from 'webtorrent'
7 import { ensureDir, pathExists, readFile, stat } from 'fs-extra'
8 import * as ffmpeg from 'fluent-ffmpeg'
10 const expect = chai.expect
11 let webtorrent: WebTorrent.Instance
13 function immutableAssign<T, U> (target: T, source: U) {
14 return Object.assign<{}, T, U>({}, target, source)
17 // Default interval -> 5 minutes
18 function dateIsValid (dateString: string, interval = 300000) {
19 const dateToCheck = new Date(dateString)
20 const now = new Date()
22 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
25 function wait (milliseconds: number) {
26 return new Promise(resolve => setTimeout(resolve, milliseconds))
29 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
30 const WebTorrent = require('webtorrent')
32 if (!webtorrent) webtorrent = new WebTorrent()
33 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
35 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
40 let root = join(__dirname, '..', '..', '..')
42 if (basename(root) === 'dist') root = resolve(root, '..')
47 function buildServerDirectory (internalServerNumber: number, directory: string) {
48 return join(root(), 'test' + internalServerNumber, directory)
51 async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
52 const res = await request(url)
58 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
59 const minLength = body.length - ((30 * body.length) / 100)
60 const maxLength = body.length + ((30 * body.length) / 100)
62 expect(data.length).to.be.above(minLength, "the generated image is way smaller than the recorded fixture")
63 expect(data.length).to.be.below(maxLength, "the generated image is way larger than the recorded fixture")
66 function buildAbsoluteFixturePath (path: string, customCIPath = false) {
67 if (isAbsolute(path)) return path
69 if (customCIPath && process.env.GITHUB_WORKSPACE) {
70 return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
73 return join(root(), 'server', 'tests', 'fixtures', path)
76 function areHttpImportTestsDisabled () {
77 const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true'
79 if (disabled) console.log('Import tests are disabled')
84 async function generateHighBitrateVideo () {
85 const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
87 await ensureDir(dirname(tempFixturePath))
89 const exists = await pathExists(tempFixturePath)
92 // Generate a random, high bitrate video on the fly, so we don't have to include
93 // a large file in the repo. The video needs to have a certain minimum length so
94 // that FFmpeg properly applies bitrate limits.
95 // https://stackoverflow.com/a/15795112
96 return new Promise<string>((res, rej) => {
98 .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
99 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
100 .outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
101 .output(tempFixturePath)
103 .on('end', () => res(tempFixturePath))
108 return tempFixturePath
111 async function generateVideoWithFramerate (fps = 60) {
112 const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true)
114 await ensureDir(dirname(tempFixturePath))
116 const exists = await pathExists(tempFixturePath)
118 return new Promise<string>((res, rej) => {
120 .outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ])
121 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
122 .outputOptions([ `-r ${fps}` ])
123 .output(tempFixturePath)
125 .on('end', () => res(tempFixturePath))
130 return tempFixturePath
133 async function getFileSize (path: string) {
134 const stats = await stat(path)
139 // ---------------------------------------------------------------------------
144 areHttpImportTestsDisabled,
145 buildServerDirectory,
150 buildAbsoluteFixturePath,
152 generateHighBitrateVideo,
153 generateVideoWithFramerate