]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/helpers/core-utils.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / helpers / core-utils.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { snakeCase } from 'lodash'
5 import validator from 'validator'
6 import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils'
7 import { VideoResolution } from '@shared/models'
8 import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils'
9
10 const expect = chai.expect
11
12 describe('Parse Bytes', function () {
13
14 it('Should pass on valid value', async function () {
15 // just return it
16 expect(parseBytes(-1024)).to.equal(-1024)
17 expect(parseBytes(1024)).to.equal(1024)
18 expect(parseBytes(1048576)).to.equal(1048576)
19 expect(parseBytes('1024')).to.equal(1024)
20 expect(parseBytes('1048576')).to.equal(1048576)
21
22 // sizes
23 expect(parseBytes('1B')).to.equal(1024)
24 expect(parseBytes('1MB')).to.equal(1048576)
25 expect(parseBytes('1GB')).to.equal(1073741824)
26 expect(parseBytes('1TB')).to.equal(1099511627776)
27
28 expect(parseBytes('5GB')).to.equal(5368709120)
29 expect(parseBytes('5TB')).to.equal(5497558138880)
30
31 expect(parseBytes('1024B')).to.equal(1048576)
32 expect(parseBytes('1024MB')).to.equal(1073741824)
33 expect(parseBytes('1024GB')).to.equal(1099511627776)
34 expect(parseBytes('1024TB')).to.equal(1125899906842624)
35
36 // with whitespace
37 expect(parseBytes('1 GB')).to.equal(1073741824)
38 expect(parseBytes('1\tGB')).to.equal(1073741824)
39
40 // sum value
41 expect(parseBytes('1TB 1024MB')).to.equal(1100585369600)
42 expect(parseBytes('4GB 1024MB')).to.equal(5368709120)
43 expect(parseBytes('4TB 1024GB')).to.equal(5497558138880)
44 expect(parseBytes('4TB 1024GB 0MB')).to.equal(5497558138880)
45 expect(parseBytes('1024TB 1024GB 1024MB')).to.equal(1127000492212224)
46 })
47
48 it('Should be invalid when given invalid value', async function () {
49 expect(parseBytes('6GB 1GB')).to.equal(6)
50 })
51 })
52
53 describe('Parse duration', function () {
54
55 it('Should pass when given valid value', async function () {
56 expect(parseDurationToMs(35)).to.equal(35)
57 expect(parseDurationToMs(-35)).to.equal(-35)
58 expect(parseDurationToMs('35 seconds')).to.equal(35 * 1000)
59 expect(parseDurationToMs('1 minute')).to.equal(60 * 1000)
60 expect(parseDurationToMs('1 hour')).to.equal(3600 * 1000)
61 expect(parseDurationToMs('35 hours')).to.equal(3600 * 35 * 1000)
62 })
63
64 it('Should be invalid when given invalid value', async function () {
65 expect(parseBytes('35m 5s')).to.equal(35)
66 })
67 })
68
69 describe('Object', function () {
70
71 it('Should convert an object', async function () {
72 function keyConverter (k: string) {
73 return snakeCase(k)
74 }
75
76 function valueConverter (v: any) {
77 if (validator.isNumeric(v + '')) return parseInt('' + v, 10)
78
79 return v
80 }
81
82 const obj = {
83 mySuperKey: 'hello',
84 mySuper2Key: '45',
85 mySuper3Key: {
86 mySuperSubKey: '15',
87 mySuperSub2Key: 'hello',
88 mySuperSub3Key: [ '1', 'hello', 2 ],
89 mySuperSub4Key: 4
90 },
91 mySuper4Key: 45,
92 toto: {
93 super_key: '15',
94 superKey2: 'hello'
95 },
96 super_key: {
97 superKey4: 15
98 }
99 }
100
101 const res = objectConverter(obj, keyConverter, valueConverter)
102
103 expect(res.my_super_key).to.equal('hello')
104 expect(res.my_super_2_key).to.equal(45)
105 expect(res.my_super_3_key.my_super_sub_key).to.equal(15)
106 expect(res.my_super_3_key.my_super_sub_2_key).to.equal('hello')
107 expect(res.my_super_3_key.my_super_sub_3_key).to.deep.equal([ 1, 'hello', 2 ])
108 expect(res.my_super_3_key.my_super_sub_4_key).to.equal(4)
109 expect(res.toto.super_key).to.equal(15)
110 expect(res.toto.super_key_2).to.equal('hello')
111 expect(res.super_key.super_key_4).to.equal(15)
112
113 // Immutable
114 expect(res.mySuperKey).to.be.undefined
115 expect(obj['my_super_key']).to.be.undefined
116 })
117 })
118
119 describe('Bitrate', function () {
120
121 it('Should get appropriate max bitrate', function () {
122 const tests = [
123 { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 200, max: 400 },
124 { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 600, max: 800 },
125 { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 1200, max: 1600 },
126 { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 2000, max: 2300 },
127 { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 4000, max: 4400 },
128 { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 8000, max: 10000 },
129 { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 25000, max: 30000 }
130 ]
131
132 for (const test of tests) {
133 expect(getMaxBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000)
134 }
135 })
136
137 it('Should get appropriate average bitrate', function () {
138 const tests = [
139 { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 50, max: 300 },
140 { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 350, max: 450 },
141 { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 700, max: 900 },
142 { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 1100, max: 1300 },
143 { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 2300, max: 2500 },
144 { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 4700, max: 5000 },
145 { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 15000, max: 17000 }
146 ]
147
148 for (const test of tests) {
149 expect(getAverageBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000)
150 }
151 })
152 })