Add more CLI tests
[oweals/peertube.git] / shared / extra-utils / miscs / miscs.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import { basename, isAbsolute, join, resolve } from 'path'
5 import * as request from 'supertest'
6 import * as WebTorrent from 'webtorrent'
7 import { pathExists, readFile } from 'fs-extra'
8 import * as ffmpeg from 'fluent-ffmpeg'
9
10 const expect = chai.expect
11 let webtorrent = new WebTorrent()
12
13 function immutableAssign <T, U> (target: T, source: U) {
14   return Object.assign<{}, T, U>({}, target, source)
15 }
16
17   // Default interval -> 5 minutes
18 function dateIsValid (dateString: string, interval = 300000) {
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
35 function root () {
36   // We are in /miscs
37   let root = join(__dirname, '..', '..', '..')
38
39   if (basename(root) === 'dist') root = resolve(root, '..')
40
41   return root
42 }
43
44 async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
45   const res = await request(url)
46     .get(imagePath)
47     .expect(200)
48
49   const body = res.body
50
51   const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
52   const minLength = body.length - ((20 * body.length) / 100)
53   const maxLength = body.length + ((20 * body.length) / 100)
54
55   expect(data.length).to.be.above(minLength)
56   expect(data.length).to.be.below(maxLength)
57 }
58
59 function buildAbsoluteFixturePath (path: string, customTravisPath = false) {
60   if (isAbsolute(path)) {
61     return path
62   }
63
64   if (customTravisPath && process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path)
65
66   return join(root(), 'server', 'tests', 'fixtures', path)
67 }
68
69 async function generateHighBitrateVideo () {
70   const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
71
72   const exists = await pathExists(tempFixturePath)
73   if (!exists) {
74
75     // Generate a random, high bitrate video on the fly, so we don't have to include
76     // a large file in the repo. The video needs to have a certain minimum length so
77     // that FFmpeg properly applies bitrate limits.
78     // https://stackoverflow.com/a/15795112
79     return new Promise<string>(async (res, rej) => {
80       ffmpeg()
81         .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
82         .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
83         .outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
84         .output(tempFixturePath)
85         .on('error', rej)
86         .on('end', () => res(tempFixturePath))
87         .run()
88     })
89   }
90
91   return tempFixturePath
92 }
93
94 // ---------------------------------------------------------------------------
95
96 export {
97   dateIsValid,
98   wait,
99   webtorrentAdd,
100   immutableAssign,
101   testImage,
102   buildAbsoluteFixturePath,
103   root,
104   generateHighBitrateVideo
105 }