Fix tests
[oweals/peertube.git] / server / tests / helpers / core-utils.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { snakeCase } from 'lodash'
6 import { objectConverter, parseBytes } from '../../helpers/core-utils'
7 import { isNumeric } from 'validator'
8
9 const expect = chai.expect
10
11 describe('Parse Bytes', function () {
12
13   it('Should pass when given valid value', async function () {
14     // just return it
15     expect(parseBytes(1024)).to.be.eq(1024)
16     expect(parseBytes(1048576)).to.be.eq(1048576)
17     expect(parseBytes('1024')).to.be.eq(1024)
18     expect(parseBytes('1048576')).to.be.eq(1048576)
19
20     // sizes
21     expect(parseBytes('1B')).to.be.eq(1024)
22     expect(parseBytes('1MB')).to.be.eq(1048576)
23     expect(parseBytes('1GB')).to.be.eq(1073741824)
24     expect(parseBytes('1TB')).to.be.eq(1099511627776)
25
26     expect(parseBytes('5GB')).to.be.eq(5368709120)
27     expect(parseBytes('5TB')).to.be.eq(5497558138880)
28
29     expect(parseBytes('1024B')).to.be.eq(1048576)
30     expect(parseBytes('1024MB')).to.be.eq(1073741824)
31     expect(parseBytes('1024GB')).to.be.eq(1099511627776)
32     expect(parseBytes('1024TB')).to.be.eq(1125899906842624)
33
34     // with whitespace
35     expect(parseBytes('1 GB')).to.be.eq(1073741824)
36     expect(parseBytes('1\tGB')).to.be.eq(1073741824)
37
38     // sum value
39     expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600)
40     expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120)
41     expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880)
42     expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880)
43     expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224)
44   })
45
46   it('Should be invalid when given invalid value', async function () {
47     expect(parseBytes('6GB 1GB')).to.be.eq(6)
48   })
49
50   it('Should convert an object', async function () {
51     function keyConverter (k: string) {
52       return snakeCase(k)
53     }
54
55     function valueConverter (v: any) {
56       if (isNumeric(v + '')) return parseInt('' + v, 10)
57
58       return v
59     }
60
61     const obj = {
62       mySuperKey: 'hello',
63       mySuper2Key: '45',
64       mySuper3Key: {
65         mySuperSubKey: '15',
66         mySuperSub2Key: 'hello',
67         mySuperSub3Key: [ '1', 'hello', 2 ],
68         mySuperSub4Key: 4
69       },
70       mySuper4Key: 45,
71       toto: {
72         super_key: '15',
73         superKey2: 'hello'
74       },
75       super_key: {
76         superKey4: 15
77       }
78     }
79
80     const res = objectConverter(obj, keyConverter, valueConverter)
81
82     expect(res.my_super_key).to.equal('hello')
83     expect(res.my_super_2_key).to.equal(45)
84     expect(res.my_super_3_key.my_super_sub_key).to.equal(15)
85     expect(res.my_super_3_key.my_super_sub_2_key).to.equal('hello')
86     expect(res.my_super_3_key.my_super_sub_3_key).to.deep.equal([ 1, 'hello', 2 ])
87     expect(res.my_super_3_key.my_super_sub_4_key).to.equal(4)
88     expect(res.toto.super_key).to.equal(15)
89     expect(res.toto.super_key_2).to.equal('hello')
90     expect(res.super_key.super_key_4).to.equal(15)
91
92     // Immutable
93     expect(res.mySuperKey).to.be.undefined
94     expect(obj['my_super_key']).to.be.undefined
95   })
96 })