2020-01-31 09:56:52 -06:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2018-01-24 04:03:13 -06:00
|
|
|
|
2018-02-14 11:21:14 -06:00
|
|
|
import * as chai from 'chai'
|
2019-07-29 04:59:29 -05:00
|
|
|
import { basename, dirname, isAbsolute, join, resolve } from 'path'
|
2018-01-24 04:03:13 -06:00
|
|
|
import * as request from 'supertest'
|
2017-09-04 14:21:47 -05:00
|
|
|
import * as WebTorrent from 'webtorrent'
|
2019-07-29 04:59:29 -05:00
|
|
|
import { ensureDir, pathExists, readFile } from 'fs-extra'
|
2018-10-18 09:53:52 -05:00
|
|
|
import * as ffmpeg from 'fluent-ffmpeg'
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2018-02-14 11:21:14 -06:00
|
|
|
const expect = chai.expect
|
2019-07-11 10:23:24 -05:00
|
|
|
let webtorrent: WebTorrent.Instance
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
function immutableAssign<T, U> (target: T, source: U) {
|
2017-12-28 08:25:31 -06:00
|
|
|
return Object.assign<{}, T, U>({}, target, source)
|
|
|
|
}
|
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
// Default interval -> 5 minutes
|
2017-12-29 04:51:55 -06:00
|
|
|
function dateIsValid (dateString: string, interval = 300000) {
|
2017-09-04 14:21:47 -05: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 10:23:24 -05:00
|
|
|
const WebTorrent = require('webtorrent')
|
|
|
|
|
|
|
|
if (!webtorrent) webtorrent = new WebTorrent()
|
2017-09-04 14:21:47 -05:00
|
|
|
if (refreshWebTorrent === true) webtorrent = new WebTorrent()
|
|
|
|
|
|
|
|
return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
|
|
|
|
}
|
|
|
|
|
2018-01-18 03:53:54 -06:00
|
|
|
function root () {
|
2019-04-15 08:26:15 -05:00
|
|
|
// We are in /miscs
|
2019-06-13 04:09:38 -05:00
|
|
|
let root = join(__dirname, '..', '..', '..')
|
|
|
|
|
|
|
|
if (basename(root) === 'dist') root = resolve(root, '..')
|
|
|
|
|
|
|
|
return root
|
2018-01-18 03:53:54 -06:00
|
|
|
}
|
|
|
|
|
2019-08-09 08:04:36 -05:00
|
|
|
function buildServerDirectory (internalServerNumber: number, directory: string) {
|
|
|
|
return join(root(), 'test' + internalServerNumber, directory)
|
|
|
|
}
|
|
|
|
|
2018-01-24 04:03:13 -06:00
|
|
|
async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
|
2018-08-10 10:49:12 -05:00
|
|
|
const res = await request(url)
|
|
|
|
.get(imagePath)
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
const body = res.body
|
|
|
|
|
2018-12-07 09:09:57 -06:00
|
|
|
const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
|
2018-08-10 10:49:12 -05:00
|
|
|
const minLength = body.length - ((20 * body.length) / 100)
|
|
|
|
const maxLength = body.length + ((20 * body.length) / 100)
|
|
|
|
|
|
|
|
expect(data.length).to.be.above(minLength)
|
|
|
|
expect(data.length).to.be.below(maxLength)
|
2018-01-24 04:03:13 -06:00
|
|
|
}
|
|
|
|
|
2019-07-29 04:59:29 -05:00
|
|
|
function buildAbsoluteFixturePath (path: string, customCIPath = false) {
|
2018-02-13 11:17:05 -06:00
|
|
|
if (isAbsolute(path)) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2019-07-29 07:58:41 -05:00
|
|
|
if (customCIPath) {
|
|
|
|
if (process.env.GITLAB_CI) return join(root(), 'cached-fixtures', path)
|
|
|
|
|
|
|
|
if (process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path)
|
|
|
|
}
|
2018-10-08 10:47:19 -05:00
|
|
|
|
2018-12-07 09:09:57 -06:00
|
|
|
return join(root(), 'server', 'tests', 'fixtures', path)
|
2018-02-13 11:17:05 -06:00
|
|
|
}
|
|
|
|
|
2018-10-18 09:53:52 -05:00
|
|
|
async function generateHighBitrateVideo () {
|
|
|
|
const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
|
|
|
|
|
2019-07-29 04:59:29 -05:00
|
|
|
await ensureDir(dirname(tempFixturePath))
|
|
|
|
|
2018-10-18 09:53:52 -05:00
|
|
|
const exists = await pathExists(tempFixturePath)
|
|
|
|
if (!exists) {
|
|
|
|
|
|
|
|
// 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 09:56:52 -06:00
|
|
|
return new Promise<string>((res, rej) => {
|
2018-10-18 09:53:52 -05: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 13:40:30 -06: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-01-31 09:56:52 -06:00
|
|
|
return new Promise<string>((res, rej) => {
|
2020-01-20 13:40:30 -06:00
|
|
|
ffmpeg()
|
2020-01-29 09:54:03 -06:00
|
|
|
.outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ])
|
2020-01-20 13:40:30 -06: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
|
|
|
|
}
|
|
|
|
|
2017-09-04 14:21:47 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
dateIsValid,
|
|
|
|
wait,
|
2019-08-09 08:04:36 -05:00
|
|
|
buildServerDirectory,
|
2017-12-28 08:25:31 -06:00
|
|
|
webtorrentAdd,
|
2018-01-18 03:53:54 -06:00
|
|
|
immutableAssign,
|
2018-01-24 04:03:13 -06:00
|
|
|
testImage,
|
2018-02-13 11:17:05 -06:00
|
|
|
buildAbsoluteFixturePath,
|
2018-10-18 09:53:52 -05:00
|
|
|
root,
|
2020-01-20 13:40:30 -06:00
|
|
|
generateHighBitrateVideo,
|
|
|
|
generateVideoWithFramerate
|
2017-09-04 14:21:47 -05:00
|
|
|
}
|