1
0
Fork 0
peertube/server/tests/utils/miscs/miscs.ts

102 lines
3 KiB
TypeScript
Raw Normal View History

2018-01-24 05:03:13 -05:00
/* tslint:disable:no-unused-expression */
2018-02-14 12:21:14 -05:00
import * as chai from 'chai'
import { isAbsolute, join } from 'path'
2018-01-24 05:03:13 -05:00
import * as request from 'supertest'
2017-09-04 15:21:47 -04:00
import * as WebTorrent from 'webtorrent'
2018-10-18 10:53:52 -04:00
import { pathExists, readFile } from 'fs-extra'
import * as ffmpeg from 'fluent-ffmpeg'
2017-09-04 15:21:47 -04:00
2018-02-14 12:21:14 -05:00
const expect = chai.expect
2017-09-04 15:21:47 -04:00
let webtorrent = new WebTorrent()
2017-12-28 09:25:31 -05:00
function immutableAssign <T, U> (target: T, source: U) {
return Object.assign<{}, T, U>({}, target, source)
}
2017-12-29 05:51:55 -05:00
// Default interval -> 5 minutes
function dateIsValid (dateString: string, interval = 300000) {
2017-09-04 15:21:47 -04: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) {
if (refreshWebTorrent === true) webtorrent = new WebTorrent()
return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
}
function root () {
// We are in server/tests/utils/miscs
return join(__dirname, '..', '..', '..', '..')
}
2018-01-24 05:03:13 -05:00
async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
2018-08-10 11:49:12 -04:00
const res = await request(url)
.get(imagePath)
.expect(200)
const body = res.body
2018-08-27 10:23:34 -04:00
const data = await readFile(join(__dirname, '..', '..', 'fixtures', imageName + extension))
2018-08-10 11:49:12 -04: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 05:03:13 -05:00
}
function buildAbsoluteFixturePath (path: string, customTravisPath = false) {
if (isAbsolute(path)) {
return path
}
if (customTravisPath && process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path)
2018-06-07 03:48:10 -04:00
return join(__dirname, '..', '..', 'fixtures', path)
}
2018-10-18 10:53:52 -04:00
async function generateHighBitrateVideo () {
const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
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
return new Promise<string>(async (res, rej) => {
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
}
2017-09-04 15:21:47 -04:00
// ---------------------------------------------------------------------------
export {
dateIsValid,
wait,
2017-12-28 09:25:31 -05:00
webtorrentAdd,
immutableAssign,
2018-01-24 05:03:13 -05:00
testImage,
buildAbsoluteFixturePath,
2018-10-18 10:53:52 -04:00
root,
generateHighBitrateVideo
2017-09-04 15:21:47 -04:00
}