1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
4 import { isAbsolute, join } from 'path'
5 import * as request from 'supertest'
6 import * as WebTorrent from 'webtorrent'
7 import { readFile } from 'fs-extra'
9 const expect = chai.expect
10 let webtorrent = new WebTorrent()
12 function immutableAssign <T, U> (target: T, source: U) {
13 return Object.assign<{}, T, U>({}, target, source)
16 // Default interval -> 5 minutes
17 function dateIsValid (dateString: string, interval = 300000) {
18 const dateToCheck = new Date(dateString)
19 const now = new Date()
21 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
24 function wait (milliseconds: number) {
25 return new Promise(resolve => setTimeout(resolve, milliseconds))
28 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
29 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
31 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
35 // We are in server/tests/utils/miscs
36 return join(__dirname, '..', '..', '..', '..')
39 async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
40 const res = await request(url)
46 const data = await readFile(join(__dirname, '..', '..', 'fixtures', imageName + extension))
47 const minLength = body.length - ((20 * body.length) / 100)
48 const maxLength = body.length + ((20 * body.length) / 100)
50 expect(data.length).to.be.above(minLength)
51 expect(data.length).to.be.below(maxLength)
54 function buildAbsoluteFixturePath (path: string) {
55 if (isAbsolute(path)) {
59 return join(__dirname, '..', '..', 'fixtures', path)
62 // ---------------------------------------------------------------------------
70 buildAbsoluteFixturePath,