]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
0e5ff97f | 2 | |
86347717 | 3 | import { expect } from 'chai' |
464687bb | 4 | import { snakeCase } from 'lodash' |
7cde3b9c | 5 | import validator from 'validator' |
e7d8e2b2 | 6 | import { getAverageTheoreticalBitrate, getMaxTheoreticalBitrate } from '@shared/core-utils' |
679c12e6 | 7 | import { VideoResolution } from '@shared/models' |
c371016d | 8 | import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils' |
0e5ff97f | 9 | |
0e5ff97f | 10 | describe('Parse Bytes', function () { |
a4101923 | 11 | |
c371016d | 12 | it('Should pass on valid value', async function () { |
0e5ff97f | 13 | // just return it |
c371016d C |
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) | |
0e5ff97f BY |
19 | |
20 | // sizes | |
c371016d C |
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) | |
0e5ff97f | 25 | |
c371016d C |
26 | expect(parseBytes('5GB')).to.equal(5368709120) |
27 | expect(parseBytes('5TB')).to.equal(5497558138880) | |
0e5ff97f | 28 | |
c371016d C |
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) | |
0e5ff97f BY |
33 | |
34 | // with whitespace | |
c371016d C |
35 | expect(parseBytes('1 GB')).to.equal(1073741824) |
36 | expect(parseBytes('1\tGB')).to.equal(1073741824) | |
0e5ff97f BY |
37 | |
38 | // sum value | |
c371016d C |
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) | |
0e5ff97f BY |
60 | }) |
61 | ||
62 | it('Should be invalid when given invalid value', async function () { | |
c371016d | 63 | expect(parseBytes('35m 5s')).to.equal(35) |
0e5ff97f | 64 | }) |
679c12e6 C |
65 | }) |
66 | ||
67 | describe('Object', function () { | |
a4101923 C |
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) { | |
7cde3b9c | 75 | if (validator.isNumeric(v + '')) return parseInt('' + v, 10) |
a4101923 C |
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 | }) | |
0e5ff97f | 115 | }) |
679c12e6 C |
116 | |
117 | describe('Bitrate', function () { | |
118 | ||
119 | it('Should get appropriate max bitrate', function () { | |
120 | const tests = [ | |
8dd754c7 | 121 | { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 200, max: 400 }, |
679c12e6 C |
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) { | |
e7d8e2b2 | 131 | expect(getMaxTheoreticalBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000) |
679c12e6 C |
132 | } |
133 | }) | |
134 | ||
135 | it('Should get appropriate average bitrate', function () { | |
136 | const tests = [ | |
8dd754c7 | 137 | { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 50, max: 300 }, |
679c12e6 C |
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) { | |
e7d8e2b2 | 147 | expect(getAverageTheoreticalBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000) |
679c12e6 C |
148 | } |
149 | }) | |
150 | }) |