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