]>
Commit | Line | Data |
---|---|---|
9ee83eb9 C |
1 | /* tslint:disable:no-unused-expression */ |
2 | ||
7b0956ec | 3 | import * as chai from 'chai' |
2284f202 | 4 | import { basename, dirname, isAbsolute, join, resolve } from 'path' |
9ee83eb9 | 5 | import * as request from 'supertest' |
0e1dc3e7 | 6 | import * as WebTorrent from 'webtorrent' |
2284f202 | 7 | import { ensureDir, pathExists, readFile } from 'fs-extra' |
74cd011b | 8 | import * as ffmpeg from 'fluent-ffmpeg' |
0e1dc3e7 | 9 | |
7b0956ec | 10 | const expect = chai.expect |
8d2be0ed | 11 | let webtorrent: WebTorrent.Instance |
0e1dc3e7 | 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) { | |
8d2be0ed C |
30 | const WebTorrent = require('webtorrent') |
31 | ||
32 | if (!webtorrent) webtorrent = new WebTorrent() | |
0e1dc3e7 C |
33 | if (refreshWebTorrent === true) webtorrent = new WebTorrent() |
34 | ||
35 | return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res)) | |
36 | } | |
37 | ||
f05a1c30 | 38 | function root () { |
94565d52 | 39 | // We are in /miscs |
1a12f66d C |
40 | let root = join(__dirname, '..', '..', '..') |
41 | ||
42 | if (basename(root) === 'dist') root = resolve(root, '..') | |
43 | ||
44 | return root | |
f05a1c30 C |
45 | } |
46 | ||
9ee83eb9 | 47 | async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { |
288178bf C |
48 | const res = await request(url) |
49 | .get(imagePath) | |
50 | .expect(200) | |
51 | ||
52 | const body = res.body | |
53 | ||
2a8c5d0a | 54 | const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension)) |
288178bf C |
55 | const minLength = body.length - ((20 * body.length) / 100) |
56 | const maxLength = body.length + ((20 * body.length) / 100) | |
57 | ||
58 | expect(data.length).to.be.above(minLength) | |
59 | expect(data.length).to.be.below(maxLength) | |
9ee83eb9 C |
60 | } |
61 | ||
2284f202 | 62 | function buildAbsoluteFixturePath (path: string, customCIPath = false) { |
ac81d1a0 C |
63 | if (isAbsolute(path)) { |
64 | return path | |
65 | } | |
66 | ||
2284f202 | 67 | if (customCIPath && process.env.GITLAB_CI) return join(root(), 'cached-fixtures', path) |
c1c86c15 | 68 | |
2a8c5d0a | 69 | return join(root(), 'server', 'tests', 'fixtures', path) |
ac81d1a0 C |
70 | } |
71 | ||
74cd011b C |
72 | async function generateHighBitrateVideo () { |
73 | const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true) | |
74 | ||
2284f202 C |
75 | await ensureDir(dirname(tempFixturePath)) |
76 | ||
74cd011b C |
77 | const exists = await pathExists(tempFixturePath) |
78 | if (!exists) { | |
79 | ||
80 | // Generate a random, high bitrate video on the fly, so we don't have to include | |
81 | // a large file in the repo. The video needs to have a certain minimum length so | |
82 | // that FFmpeg properly applies bitrate limits. | |
83 | // https://stackoverflow.com/a/15795112 | |
84 | return new Promise<string>(async (res, rej) => { | |
85 | ffmpeg() | |
86 | .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ]) | |
87 | .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ]) | |
88 | .outputOptions([ '-maxrate 10M', '-bufsize 10M' ]) | |
89 | .output(tempFixturePath) | |
90 | .on('error', rej) | |
91 | .on('end', () => res(tempFixturePath)) | |
92 | .run() | |
93 | }) | |
94 | } | |
95 | ||
96 | return tempFixturePath | |
97 | } | |
98 | ||
0e1dc3e7 C |
99 | // --------------------------------------------------------------------------- |
100 | ||
101 | export { | |
0e1dc3e7 C |
102 | dateIsValid, |
103 | wait, | |
26d21b78 | 104 | webtorrentAdd, |
f05a1c30 | 105 | immutableAssign, |
9ee83eb9 | 106 | testImage, |
ac81d1a0 | 107 | buildAbsoluteFixturePath, |
74cd011b C |
108 | root, |
109 | generateHighBitrateVideo | |
0e1dc3e7 | 110 | } |