]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/helpers/core-utils.ts
Merge branch 'release/4.2.0' into develop
[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'
c371016d 9import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils'
0e5ff97f
BY
10
11const expect = chai.expect
12
13describe('Parse Bytes', function () {
a4101923 14
c371016d 15 it('Should pass on valid value', async function () {
0e5ff97f 16 // just return it
c371016d
C
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)
0e5ff97f
BY
22
23 // sizes
c371016d
C
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)
0e5ff97f 28
c371016d
C
29 expect(parseBytes('5GB')).to.equal(5368709120)
30 expect(parseBytes('5TB')).to.equal(5497558138880)
0e5ff97f 31
c371016d
C
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)
0e5ff97f
BY
36
37 // with whitespace
c371016d
C
38 expect(parseBytes('1 GB')).to.equal(1073741824)
39 expect(parseBytes('1\tGB')).to.equal(1073741824)
0e5ff97f
BY
40
41 // sum value
c371016d
C
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
54describe('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)
0e5ff97f
BY
63 })
64
65 it('Should be invalid when given invalid value', async function () {
c371016d 66 expect(parseBytes('35m 5s')).to.equal(35)
0e5ff97f 67 })
679c12e6
C
68})
69
70describe('Object', function () {
a4101923
C
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) {
7cde3b9c 78 if (validator.isNumeric(v + '')) return parseInt('' + v, 10)
a4101923
C
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 })
0e5ff97f 118})
679c12e6
C
119
120describe('Bitrate', function () {
121
122 it('Should get appropriate max bitrate', function () {
123 const tests = [
8dd754c7 124 { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 200, max: 400 },
679c12e6
C
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 = [
8dd754c7 140 { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 50, max: 300 },
679c12e6
C
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})