2020-01-31 16:56:52 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2018-01-24 11:03:13 +01:00
|
|
|
|
2018-02-14 18:21:14 +01:00
|
|
|
import * as chai from 'chai'
|
2020-11-24 15:22:56 +01:00
|
|
|
import * as ffmpeg from 'fluent-ffmpeg'
|
2021-07-01 17:47:21 +02:00
|
|
|
import { ensureDir, pathExists, readFile, stat } from 'fs-extra'
|
2019-07-29 11:59:29 +02:00
|
|
|
import { basename, dirname, isAbsolute, join, resolve } from 'path'
|
2018-01-24 11:03:13 +01:00
|
|
|
import * as request from 'supertest'
|
2017-09-04 21:21:47 +02:00
|
|
|
import * as WebTorrent from 'webtorrent'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2017-09-04 21:21:47 +02:00
|
|
|
|
2018-02-14 18:21:14 +01:00
|
|
|
const expect = chai.expect
|
2019-07-11 17:23:24 +02:00
|
|
|
let webtorrent: WebTorrent.Instance
|
2017-09-04 21:21:47 +02:00
|
|
|
|
2020-01-31 16:56:52 +01:00
|
|
|
function immutableAssign<T, U> (target: T, source: U) {
|
2017-12-28 15:25:31 +01:00
|
|
|
return Object.assign<{}, T, U>({}, target, source)
|
|
|
|
}
|
|
|
|
|
2020-01-31 16:56:52 +01:00
|
|
|
// Default interval -> 5 minutes
|
2017-12-29 11:51:55 +01:00
|
|
|
function dateIsValid (dateString: string, interval = 300000) {
|
2017-09-04 21:21:47 +02:00
|
|
|
const dateToCheck = new Date(dateString)
|
|
|
|
const now = new Date()
|
|
|
|
|
|
|
|
return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait (milliseconds: number) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, milliseconds))
|
|
|
|
}
|
|
|
|
|
|
|
|
function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
|
2019-07-11 17:23:24 +02:00
|
|
|
const WebTorrent = require('webtorrent')
|
|
|
|
|
|
|
|
if (!webtorrent) webtorrent = new WebTorrent()
|
2017-09-04 21:21:47 +02:00
|
|
|
if (refreshWebTorrent === true) webtorrent = new WebTorrent()
|
|
|
|
|
|
|
|
return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
|
|
|
|
}
|
|
|
|
|
2018-01-18 10:53:54 +01:00
|
|
|
function root () {
|
2019-04-15 15:26:15 +02:00
|
|
|
// We are in /miscs
|
2019-06-13 11:09:38 +02:00
|
|
|
let root = join(__dirname, '..', '..', '..')
|
|
|
|
|
|
|
|
if (basename(root) === 'dist') root = resolve(root, '..')
|
|
|
|
|
|
|
|
return root
|
2018-01-18 10:53:54 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 15:22:56 +01:00
|
|
|
function buildServerDirectory (server: { internalServerNumber: number }, directory: string) {
|
|
|
|
return join(root(), 'test' + server.internalServerNumber, directory)
|
2019-08-09 15:04:36 +02:00
|
|
|
}
|
|
|
|
|
2018-01-24 11:03:13 +01:00
|
|
|
async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
|
2018-08-10 17:49:12 +02:00
|
|
|
const res = await request(url)
|
|
|
|
.get(imagePath)
|
2020-12-07 14:32:36 +01:00
|
|
|
.expect(HttpStatusCode.OK_200)
|
2018-08-10 17:49:12 +02:00
|
|
|
|
|
|
|
const body = res.body
|
|
|
|
|
2018-12-07 16:09:57 +01:00
|
|
|
const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
|
2020-05-31 20:03:28 +02:00
|
|
|
const minLength = body.length - ((30 * body.length) / 100)
|
|
|
|
const maxLength = body.length + ((30 * body.length) / 100)
|
2018-08-10 17:49:12 +02:00
|
|
|
|
2021-07-01 16:47:14 +02:00
|
|
|
expect(data.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
|
|
|
|
expect(data.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testFileExistsOrNot (server: { internalServerNumber: number }, directory: string, filePath: string, exist: boolean) {
|
|
|
|
const base = buildServerDirectory(server, directory)
|
|
|
|
|
|
|
|
expect(await pathExists(join(base, filePath))).to.equal(exist)
|
2018-01-24 11:03:13 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 11:24:17 +01:00
|
|
|
function isGithubCI () {
|
|
|
|
return !!process.env.GITHUB_WORKSPACE
|
|
|
|
}
|
|
|
|
|
2019-07-29 11:59:29 +02:00
|
|
|
function buildAbsoluteFixturePath (path: string, customCIPath = false) {
|
2020-07-30 09:43:12 +02:00
|
|
|
if (isAbsolute(path)) return path
|
2018-02-13 18:17:05 +01:00
|
|
|
|
2020-08-24 16:37:47 +02:00
|
|
|
if (customCIPath && process.env.GITHUB_WORKSPACE) {
|
|
|
|
return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
|
2019-07-29 14:58:41 +02:00
|
|
|
}
|
2018-10-08 17:47:19 +02:00
|
|
|
|
2018-12-07 16:09:57 +01:00
|
|
|
return join(root(), 'server', 'tests', 'fixtures', path)
|
2018-02-13 18:17:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:43:12 +02:00
|
|
|
function areHttpImportTestsDisabled () {
|
|
|
|
const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true'
|
|
|
|
|
|
|
|
if (disabled) console.log('Import tests are disabled')
|
|
|
|
|
|
|
|
return disabled
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:53:52 +02:00
|
|
|
async function generateHighBitrateVideo () {
|
|
|
|
const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
|
|
|
|
|
2019-07-29 11:59:29 +02:00
|
|
|
await ensureDir(dirname(tempFixturePath))
|
|
|
|
|
2018-10-18 16:53:52 +02:00
|
|
|
const exists = await pathExists(tempFixturePath)
|
|
|
|
if (!exists) {
|
2020-12-01 09:31:45 +01:00
|
|
|
console.log('Generating high bitrate video.')
|
2018-10-18 16:53:52 +02:00
|
|
|
|
|
|
|
// Generate a random, high bitrate video on the fly, so we don't have to include
|
|
|
|
// a large file in the repo. The video needs to have a certain minimum length so
|
|
|
|
// that FFmpeg properly applies bitrate limits.
|
|
|
|
// https://stackoverflow.com/a/15795112
|
2020-01-31 16:56:52 +01:00
|
|
|
return new Promise<string>((res, rej) => {
|
2018-10-18 16:53:52 +02:00
|
|
|
ffmpeg()
|
|
|
|
.outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
|
|
|
|
.outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
|
|
|
|
.outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
|
|
|
|
.output(tempFixturePath)
|
|
|
|
.on('error', rej)
|
|
|
|
.on('end', () => res(tempFixturePath))
|
|
|
|
.run()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return tempFixturePath
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:40:30 +01:00
|
|
|
async function generateVideoWithFramerate (fps = 60) {
|
|
|
|
const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true)
|
|
|
|
|
|
|
|
await ensureDir(dirname(tempFixturePath))
|
|
|
|
|
|
|
|
const exists = await pathExists(tempFixturePath)
|
|
|
|
if (!exists) {
|
2020-12-01 09:31:45 +01:00
|
|
|
console.log('Generating video with framerate %d.', fps)
|
|
|
|
|
2020-01-31 16:56:52 +01:00
|
|
|
return new Promise<string>((res, rej) => {
|
2020-01-20 20:40:30 +01:00
|
|
|
ffmpeg()
|
2020-01-29 16:54:03 +01:00
|
|
|
.outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ])
|
2020-01-20 20:40:30 +01:00
|
|
|
.outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
|
|
|
|
.outputOptions([ `-r ${fps}` ])
|
|
|
|
.output(tempFixturePath)
|
|
|
|
.on('error', rej)
|
|
|
|
.on('end', () => res(tempFixturePath))
|
|
|
|
.run()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return tempFixturePath
|
|
|
|
}
|
|
|
|
|
2020-11-20 15:06:56 +01:00
|
|
|
async function getFileSize (path: string) {
|
|
|
|
const stats = await stat(path)
|
|
|
|
|
|
|
|
return stats.size
|
|
|
|
}
|
|
|
|
|
2017-09-04 21:21:47 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
dateIsValid,
|
|
|
|
wait,
|
2020-07-30 09:43:12 +02:00
|
|
|
areHttpImportTestsDisabled,
|
2019-08-09 15:04:36 +02:00
|
|
|
buildServerDirectory,
|
2017-12-28 15:25:31 +01:00
|
|
|
webtorrentAdd,
|
2020-11-20 15:06:56 +01:00
|
|
|
getFileSize,
|
2018-01-18 10:53:54 +01:00
|
|
|
immutableAssign,
|
2018-01-24 11:03:13 +01:00
|
|
|
testImage,
|
2020-12-10 11:24:17 +01:00
|
|
|
isGithubCI,
|
2018-02-13 18:17:05 +01:00
|
|
|
buildAbsoluteFixturePath,
|
2021-07-01 16:47:14 +02:00
|
|
|
testFileExistsOrNot,
|
2018-10-18 16:53:52 +02:00
|
|
|
root,
|
2020-01-20 20:40:30 +01:00
|
|
|
generateHighBitrateVideo,
|
|
|
|
generateVideoWithFramerate
|
2017-09-04 21:21:47 +02:00
|
|
|
}
|