1 /* tslint:disable:no-unused-expression */
3 import { isAbsolute, join } from 'path'
4 import * as request from 'supertest'
5 import * as WebTorrent from 'webtorrent'
6 import { readFileBufferPromise } from '../../../helpers/core-utils'
8 let webtorrent = new WebTorrent()
10 function immutableAssign <T, U> (target: T, source: U) {
11 return Object.assign<{}, T, U>({}, target, source)
14 // Default interval -> 5 minutes
15 function dateIsValid (dateString: string, interval = 300000) {
16 const dateToCheck = new Date(dateString)
17 const now = new Date()
19 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
22 function wait (milliseconds: number) {
23 return new Promise(resolve => setTimeout(resolve, milliseconds))
26 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
27 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
29 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
33 // We are in server/tests/utils/miscs
34 return join(__dirname, '..', '..', '..', '..')
37 async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
38 // Don't test images if the node env is not set
39 // Because we need a special ffmpeg version for this test
40 if (process.env[ 'NODE_TEST_IMAGE' ]) {
41 const res = await request(url)
47 const data = await readFileBufferPromise(join(__dirname, '..', '..', 'api', 'fixtures', imageName + extension))
48 const minLength = body.length - ((20 * body.length) / 100)
49 const maxLength = body.length + ((20 * body.length) / 100)
51 return data.length > minLength && data.length < maxLength
53 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
58 function buildAbsoluteFixturePath (path: string) {
59 if (isAbsolute(path)) {
63 return join(__dirname, '..', '..', 'api', 'fixtures', path)
66 // ---------------------------------------------------------------------------
74 buildAbsoluteFixturePath,