]>
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' |
0e1dc3e7 | 8 | |
7b0956ec | 9 | const expect = chai.expect |
0e1dc3e7 C |
10 | let webtorrent = new WebTorrent() |
11 | ||
26d21b78 C |
12 | function immutableAssign <T, U> (target: T, source: U) { |
13 | return Object.assign<{}, T, U>({}, target, source) | |
14 | } | |
15 | ||
b1f5b93e C |
16 | // Default interval -> 5 minutes |
17 | function 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 | ||
24 | function wait (milliseconds: number) { | |
25 | return new Promise(resolve => setTimeout(resolve, milliseconds)) | |
26 | } | |
27 | ||
28 | function 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 |
34 | function root () { |
35 | // We are in server/tests/utils/miscs | |
36 | return join(__dirname, '..', '..', '..', '..') | |
37 | } | |
38 | ||
9ee83eb9 C |
39 | async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { |
40 | // Don't test images if the node env is not set | |
41 | // Because we need a special ffmpeg version for this test | |
42 | if (process.env[ 'NODE_TEST_IMAGE' ]) { | |
43 | const res = await request(url) | |
44 | .get(imagePath) | |
45 | .expect(200) | |
46 | ||
47 | const body = res.body | |
48 | ||
49 | const data = await readFileBufferPromise(join(__dirname, '..', '..', 'api', 'fixtures', imageName + extension)) | |
ac81d1a0 C |
50 | const minLength = body.length - ((20 * body.length) / 100) |
51 | const maxLength = body.length + ((20 * body.length) / 100) | |
9ee83eb9 | 52 | |
7b0956ec C |
53 | expect(data.length).to.be.above(minLength) |
54 | expect(data.length).to.be.below(maxLength) | |
9ee83eb9 C |
55 | } else { |
56 | console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.') | |
57 | return true | |
58 | } | |
59 | } | |
60 | ||
ac81d1a0 C |
61 | function buildAbsoluteFixturePath (path: string) { |
62 | if (isAbsolute(path)) { | |
63 | return path | |
64 | } | |
65 | ||
66 | return join(__dirname, '..', '..', 'api', 'fixtures', path) | |
67 | } | |
68 | ||
0e1dc3e7 C |
69 | // --------------------------------------------------------------------------- |
70 | ||
71 | export { | |
0e1dc3e7 C |
72 | dateIsValid, |
73 | wait, | |
26d21b78 | 74 | webtorrentAdd, |
f05a1c30 | 75 | immutableAssign, |
9ee83eb9 | 76 | testImage, |
ac81d1a0 | 77 | buildAbsoluteFixturePath, |
f05a1c30 | 78 | root |
0e1dc3e7 | 79 | } |