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