]>
Commit | Line | Data |
---|---|---|
9ee83eb9 C |
1 | /* tslint:disable:no-unused-expression */ |
2 | ||
7b0956ec | 3 | import * as chai from 'chai' |
ac81d1a0 | 4 | import { isAbsolute, join } from 'path' |
9ee83eb9 | 5 | import * as request from 'supertest' |
0e1dc3e7 | 6 | import * as WebTorrent from 'webtorrent' |
9ee83eb9 | 7 | import { readFileBufferPromise } from '../../../helpers/core-utils' |
3cd0734f | 8 | import { ServerInfo } from '..' |
0e1dc3e7 | 9 | |
7b0956ec | 10 | const expect = chai.expect |
0e1dc3e7 C |
11 | let webtorrent = new WebTorrent() |
12 | ||
26d21b78 C |
13 | function immutableAssign <T, U> (target: T, source: U) { |
14 | return Object.assign<{}, T, U>({}, target, source) | |
15 | } | |
16 | ||
b1f5b93e C |
17 | // Default interval -> 5 minutes |
18 | function dateIsValid (dateString: string, interval = 300000) { | |
0e1dc3e7 C |
19 | const dateToCheck = new Date(dateString) |
20 | const now = new Date() | |
21 | ||
22 | return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval | |
23 | } | |
24 | ||
25 | function wait (milliseconds: number) { | |
26 | return new Promise(resolve => setTimeout(resolve, milliseconds)) | |
27 | } | |
28 | ||
29 | function webtorrentAdd (torrent: string, refreshWebTorrent = false) { | |
30 | if (refreshWebTorrent === true) webtorrent = new WebTorrent() | |
31 | ||
32 | return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res)) | |
33 | } | |
34 | ||
f05a1c30 C |
35 | function root () { |
36 | // We are in server/tests/utils/miscs | |
37 | return join(__dirname, '..', '..', '..', '..') | |
38 | } | |
39 | ||
9ee83eb9 C |
40 | async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { |
41 | // Don't test images if the node env is not set | |
42 | // Because we need a special ffmpeg version for this test | |
43 | if (process.env[ 'NODE_TEST_IMAGE' ]) { | |
44 | const res = await request(url) | |
45 | .get(imagePath) | |
46 | .expect(200) | |
47 | ||
48 | const body = res.body | |
49 | ||
99d10301 | 50 | const data = await readFileBufferPromise(join(__dirname, '..', '..', 'fixtures', imageName + extension)) |
ac81d1a0 C |
51 | const minLength = body.length - ((20 * body.length) / 100) |
52 | const maxLength = body.length + ((20 * body.length) / 100) | |
9ee83eb9 | 53 | |
7b0956ec C |
54 | expect(data.length).to.be.above(minLength) |
55 | expect(data.length).to.be.below(maxLength) | |
9ee83eb9 C |
56 | } else { |
57 | console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.') | |
58 | return true | |
59 | } | |
60 | } | |
61 | ||
ac81d1a0 C |
62 | function buildAbsoluteFixturePath (path: string) { |
63 | if (isAbsolute(path)) { | |
64 | return path | |
65 | } | |
66 | ||
99d10301 | 67 | return join(__dirname, '..', '..', 'fixtures', path) |
ac81d1a0 C |
68 | } |
69 | ||
0e1dc3e7 C |
70 | // --------------------------------------------------------------------------- |
71 | ||
72 | export { | |
0e1dc3e7 C |
73 | dateIsValid, |
74 | wait, | |
26d21b78 | 75 | webtorrentAdd, |
f05a1c30 | 76 | immutableAssign, |
9ee83eb9 | 77 | testImage, |
ac81d1a0 | 78 | buildAbsoluteFixturePath, |
f05a1c30 | 79 | root |
0e1dc3e7 | 80 | } |