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