]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/miscs/miscs.ts
Fix video play promise error on non supported browsers
[github/Chocobozzz/PeerTube.git] / server / tests / utils / miscs / miscs.ts
CommitLineData
9ee83eb9
C
1/* tslint:disable:no-unused-expression */
2
ac81d1a0 3import { isAbsolute, join } from 'path'
9ee83eb9 4import * as request from 'supertest'
0e1dc3e7 5import * as WebTorrent from 'webtorrent'
9ee83eb9 6import { readFileBufferPromise } from '../../../helpers/core-utils'
0e1dc3e7
C
7
8let webtorrent = new WebTorrent()
9
26d21b78
C
10function immutableAssign <T, U> (target: T, source: U) {
11 return Object.assign<{}, T, U>({}, target, source)
12}
13
b1f5b93e
C
14 // Default interval -> 5 minutes
15function dateIsValid (dateString: string, interval = 300000) {
0e1dc3e7
C
16 const dateToCheck = new Date(dateString)
17 const now = new Date()
18
19 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
20}
21
22function wait (milliseconds: number) {
23 return new Promise(resolve => setTimeout(resolve, milliseconds))
24}
25
26function 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
f05a1c30
C
32function root () {
33 // We are in server/tests/utils/miscs
34 return join(__dirname, '..', '..', '..', '..')
35}
36
9ee83eb9
C
37async 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))
ac81d1a0
C
48 const minLength = body.length - ((20 * body.length) / 100)
49 const maxLength = body.length + ((20 * body.length) / 100)
9ee83eb9
C
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
ac81d1a0
C
58function buildAbsoluteFixturePath (path: string) {
59 if (isAbsolute(path)) {
60 return path
61 }
62
63 return join(__dirname, '..', '..', 'api', 'fixtures', path)
64}
65
0e1dc3e7
C
66// ---------------------------------------------------------------------------
67
68export {
0e1dc3e7
C
69 dateIsValid,
70 wait,
26d21b78 71 webtorrentAdd,
f05a1c30 72 immutableAssign,
9ee83eb9 73 testImage,
ac81d1a0 74 buildAbsoluteFixturePath,
f05a1c30 75 root
0e1dc3e7 76}