]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/miscs/miscs.ts
Try to cache video_high_bitrate_1080p in travis
[github/Chocobozzz/PeerTube.git] / server / tests / utils / miscs / miscs.ts
CommitLineData
9ee83eb9
C
1/* tslint:disable:no-unused-expression */
2
7b0956ec 3import * as chai from 'chai'
ac81d1a0 4import { isAbsolute, join } from 'path'
9ee83eb9 5import * as request from 'supertest'
0e1dc3e7 6import * as WebTorrent from 'webtorrent'
62689b94 7import { readFile } from 'fs-extra'
0e1dc3e7 8
7b0956ec 9const expect = chai.expect
0e1dc3e7
C
10let webtorrent = new WebTorrent()
11
26d21b78
C
12function immutableAssign <T, U> (target: T, source: U) {
13 return Object.assign<{}, T, U>({}, target, source)
14}
15
b1f5b93e
C
16 // Default interval -> 5 minutes
17function dateIsValid (dateString: string, interval = 300000) {
0e1dc3e7
C
18 const dateToCheck = new Date(dateString)
19 const now = new Date()
20
21 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
22}
23
24function wait (milliseconds: number) {
25 return new Promise(resolve => setTimeout(resolve, milliseconds))
26}
27
28function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
29 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
30
31 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
32}
33
f05a1c30
C
34function root () {
35 // We are in server/tests/utils/miscs
36 return join(__dirname, '..', '..', '..', '..')
37}
38
9ee83eb9 39async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
288178bf
C
40 const res = await request(url)
41 .get(imagePath)
42 .expect(200)
43
44 const body = res.body
45
62689b94 46 const data = await readFile(join(__dirname, '..', '..', 'fixtures', imageName + extension))
288178bf
C
47 const minLength = body.length - ((20 * body.length) / 100)
48 const maxLength = body.length + ((20 * body.length) / 100)
49
50 expect(data.length).to.be.above(minLength)
51 expect(data.length).to.be.below(maxLength)
9ee83eb9
C
52}
53
c1c86c15 54function buildAbsoluteFixturePath (path: string, customTravisPath = false) {
ac81d1a0
C
55 if (isAbsolute(path)) {
56 return path
57 }
58
c1c86c15
C
59 if (customTravisPath && process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path)
60
99d10301 61 return join(__dirname, '..', '..', 'fixtures', path)
ac81d1a0
C
62}
63
0e1dc3e7
C
64// ---------------------------------------------------------------------------
65
66export {
0e1dc3e7
C
67 dateIsValid,
68 wait,
26d21b78 69 webtorrentAdd,
f05a1c30 70 immutableAssign,
9ee83eb9 71 testImage,
ac81d1a0 72 buildAbsoluteFixturePath,
f05a1c30 73 root
0e1dc3e7 74}