]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
9ee83eb9 | 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 | |
a1587156 | 13 | function immutableAssign<T, U> (target: T, source: U) { |
26d21b78 C |
14 | return Object.assign<{}, T, U>({}, target, source) |
15 | } | |
16 | ||
a1587156 | 17 | // Default interval -> 5 minutes |
b1f5b93e | 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 | ||
e2600d8b C |
47 | function buildServerDirectory (internalServerNumber: number, directory: string) { |
48 | return join(root(), 'test' + internalServerNumber, directory) | |
49 | } | |
50 | ||
9ee83eb9 | 51 | async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { |
288178bf C |
52 | const res = await request(url) |
53 | .get(imagePath) | |
54 | .expect(200) | |
55 | ||
56 | const body = res.body | |
57 | ||
2a8c5d0a | 58 | const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension)) |
288178bf C |
59 | const minLength = body.length - ((20 * body.length) / 100) |
60 | const maxLength = body.length + ((20 * body.length) / 100) | |
61 | ||
62 | expect(data.length).to.be.above(minLength) | |
63 | expect(data.length).to.be.below(maxLength) | |
9ee83eb9 C |
64 | } |
65 | ||
2284f202 | 66 | function buildAbsoluteFixturePath (path: string, customCIPath = false) { |
ac81d1a0 C |
67 | if (isAbsolute(path)) { |
68 | return path | |
69 | } | |
70 | ||
c928e136 C |
71 | if (customCIPath) { |
72 | if (process.env.GITLAB_CI) return join(root(), 'cached-fixtures', path) | |
73 | ||
74 | if (process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path) | |
75 | } | |
c1c86c15 | 76 | |
2a8c5d0a | 77 | return join(root(), 'server', 'tests', 'fixtures', path) |
ac81d1a0 C |
78 | } |
79 | ||
74cd011b C |
80 | async function generateHighBitrateVideo () { |
81 | const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true) | |
82 | ||
2284f202 C |
83 | await ensureDir(dirname(tempFixturePath)) |
84 | ||
74cd011b C |
85 | const exists = await pathExists(tempFixturePath) |
86 | if (!exists) { | |
87 | ||
88 | // Generate a random, high bitrate video on the fly, so we don't have to include | |
89 | // a large file in the repo. The video needs to have a certain minimum length so | |
90 | // that FFmpeg properly applies bitrate limits. | |
91 | // https://stackoverflow.com/a/15795112 | |
a1587156 | 92 | return new Promise<string>((res, rej) => { |
74cd011b C |
93 | ffmpeg() |
94 | .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ]) | |
95 | .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ]) | |
96 | .outputOptions([ '-maxrate 10M', '-bufsize 10M' ]) | |
97 | .output(tempFixturePath) | |
98 | .on('error', rej) | |
99 | .on('end', () => res(tempFixturePath)) | |
100 | .run() | |
101 | }) | |
102 | } | |
103 | ||
104 | return tempFixturePath | |
105 | } | |
106 | ||
837666fe RK |
107 | async function generateVideoWithFramerate (fps = 60) { |
108 | const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true) | |
109 | ||
110 | await ensureDir(dirname(tempFixturePath)) | |
111 | ||
112 | const exists = await pathExists(tempFixturePath) | |
113 | if (!exists) { | |
a1587156 | 114 | return new Promise<string>((res, rej) => { |
837666fe | 115 | ffmpeg() |
c7f36e4f | 116 | .outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ]) |
837666fe RK |
117 | .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ]) |
118 | .outputOptions([ `-r ${fps}` ]) | |
119 | .output(tempFixturePath) | |
120 | .on('error', rej) | |
121 | .on('end', () => res(tempFixturePath)) | |
122 | .run() | |
123 | }) | |
124 | } | |
125 | ||
126 | return tempFixturePath | |
127 | } | |
128 | ||
0e1dc3e7 C |
129 | // --------------------------------------------------------------------------- |
130 | ||
131 | export { | |
0e1dc3e7 C |
132 | dateIsValid, |
133 | wait, | |
e2600d8b | 134 | buildServerDirectory, |
26d21b78 | 135 | webtorrentAdd, |
f05a1c30 | 136 | immutableAssign, |
9ee83eb9 | 137 | testImage, |
ac81d1a0 | 138 | buildAbsoluteFixturePath, |
74cd011b | 139 | root, |
837666fe RK |
140 | generateHighBitrateVideo, |
141 | generateVideoWithFramerate | |
0e1dc3e7 | 142 | } |