]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/miscs/miscs.ts
Fix tests
[github/Chocobozzz/PeerTube.git] / server / tests / utils / miscs / miscs.ts
1 /* tslint:disable:no-unused-expression */
2
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'
8
9 let webtorrent = new WebTorrent()
10
11 function immutableAssign <T, U> (target: T, source: U) {
12 return Object.assign<{}, T, U>({}, target, source)
13 }
14
15 // Default interval -> 5 minutes
16 function dateIsValid (dateString: string, interval = 300000) {
17 const dateToCheck = new Date(dateString)
18 const now = new Date()
19
20 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
21 }
22
23 function wait (milliseconds: number) {
24 return new Promise(resolve => setTimeout(resolve, milliseconds))
25 }
26
27 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
28 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
29
30 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
31 }
32
33 function root () {
34 // We are in server/tests/utils/miscs
35 return join(__dirname, '..', '..', '..', '..')
36 }
37
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)
43 .get(imagePath)
44 .expect(200)
45
46 const body = res.body
47
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)
51
52 return data.length > minLength && data.length < maxLength
53 } else {
54 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
55 return true
56 }
57 }
58
59 // ---------------------------------------------------------------------------
60
61 export {
62 dateIsValid,
63 wait,
64 webtorrentAdd,
65 immutableAssign,
66 testImage,
67 root
68 }