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