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