"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const lodash_1 = require("lodash");
const validator_1 = tslib_1.__importDefault(require("validator"));
const core_utils_1 = require("../../../shared/core-utils");
const core_utils_2 = require("../../helpers/core-utils");
describe('Parse Bytes', function () {
    it('Should pass on valid value', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            (0, chai_1.expect)((0, core_utils_2.parseBytes)(-1024)).to.equal(-1024);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)(1024)).to.equal(1024);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)(1048576)).to.equal(1048576);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1024')).to.equal(1024);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1048576')).to.equal(1048576);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1B')).to.equal(1024);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1MB')).to.equal(1048576);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1GB')).to.equal(1073741824);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1TB')).to.equal(1099511627776);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('5GB')).to.equal(5368709120);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('5TB')).to.equal(5497558138880);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1024B')).to.equal(1048576);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1024MB')).to.equal(1073741824);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1024GB')).to.equal(1099511627776);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1024TB')).to.equal(1125899906842624);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1 GB')).to.equal(1073741824);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1\tGB')).to.equal(1073741824);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1TB 1024MB')).to.equal(1100585369600);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('4GB 1024MB')).to.equal(5368709120);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('4TB 1024GB')).to.equal(5497558138880);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('4TB 1024GB 0MB')).to.equal(5497558138880);
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('1024TB 1024GB 1024MB')).to.equal(1127000492212224);
        });
    });
    it('Should be invalid when given invalid value', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('6GB 1GB')).to.equal(6);
        });
    });
});
describe('Parse duration', function () {
    it('Should pass when given valid value', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            (0, chai_1.expect)((0, core_utils_2.parseDurationToMs)(35)).to.equal(35);
            (0, chai_1.expect)((0, core_utils_2.parseDurationToMs)(-35)).to.equal(-35);
            (0, chai_1.expect)((0, core_utils_2.parseDurationToMs)('35 seconds')).to.equal(35 * 1000);
            (0, chai_1.expect)((0, core_utils_2.parseDurationToMs)('1 minute')).to.equal(60 * 1000);
            (0, chai_1.expect)((0, core_utils_2.parseDurationToMs)('1 hour')).to.equal(3600 * 1000);
            (0, chai_1.expect)((0, core_utils_2.parseDurationToMs)('35 hours')).to.equal(3600 * 35 * 1000);
        });
    });
    it('Should be invalid when given invalid value', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            (0, chai_1.expect)((0, core_utils_2.parseBytes)('35m 5s')).to.equal(35);
        });
    });
});
describe('Object', function () {
    it('Should convert an object', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            function keyConverter(k) {
                return (0, lodash_1.snakeCase)(k);
            }
            function valueConverter(v) {
                if (validator_1.default.isNumeric(v + ''))
                    return parseInt('' + v, 10);
                return v;
            }
            const obj = {
                mySuperKey: 'hello',
                mySuper2Key: '45',
                mySuper3Key: {
                    mySuperSubKey: '15',
                    mySuperSub2Key: 'hello',
                    mySuperSub3Key: ['1', 'hello', 2],
                    mySuperSub4Key: 4
                },
                mySuper4Key: 45,
                toto: {
                    super_key: '15',
                    superKey2: 'hello'
                },
                super_key: {
                    superKey4: 15
                }
            };
            const res = (0, core_utils_2.objectConverter)(obj, keyConverter, valueConverter);
            (0, chai_1.expect)(res.my_super_key).to.equal('hello');
            (0, chai_1.expect)(res.my_super_2_key).to.equal(45);
            (0, chai_1.expect)(res.my_super_3_key.my_super_sub_key).to.equal(15);
            (0, chai_1.expect)(res.my_super_3_key.my_super_sub_2_key).to.equal('hello');
            (0, chai_1.expect)(res.my_super_3_key.my_super_sub_3_key).to.deep.equal([1, 'hello', 2]);
            (0, chai_1.expect)(res.my_super_3_key.my_super_sub_4_key).to.equal(4);
            (0, chai_1.expect)(res.toto.super_key).to.equal(15);
            (0, chai_1.expect)(res.toto.super_key_2).to.equal('hello');
            (0, chai_1.expect)(res.super_key.super_key_4).to.equal(15);
            (0, chai_1.expect)(res.mySuperKey).to.be.undefined;
            (0, chai_1.expect)(obj['my_super_key']).to.be.undefined;
        });
    });
});
describe('Bitrate', function () {
    it('Should get appropriate max bitrate', function () {
        const tests = [
            { resolution: 144, ratio: 16 / 9, fps: 24, min: 200, max: 400 },
            { resolution: 240, ratio: 16 / 9, fps: 24, min: 600, max: 800 },
            { resolution: 360, ratio: 16 / 9, fps: 24, min: 1200, max: 1600 },
            { resolution: 480, ratio: 16 / 9, fps: 24, min: 2000, max: 2300 },
            { resolution: 720, ratio: 16 / 9, fps: 24, min: 4000, max: 4400 },
            { resolution: 1080, ratio: 16 / 9, fps: 24, min: 8000, max: 10000 },
            { resolution: 2160, ratio: 16 / 9, fps: 24, min: 25000, max: 30000 }
        ];
        for (const test of tests) {
            (0, chai_1.expect)((0, core_utils_1.getMaxBitrate)(test)).to.be.above(test.min * 1000).and.below(test.max * 1000);
        }
    });
    it('Should get appropriate average bitrate', function () {
        const tests = [
            { resolution: 144, ratio: 16 / 9, fps: 24, min: 50, max: 300 },
            { resolution: 240, ratio: 16 / 9, fps: 24, min: 350, max: 450 },
            { resolution: 360, ratio: 16 / 9, fps: 24, min: 700, max: 900 },
            { resolution: 480, ratio: 16 / 9, fps: 24, min: 1100, max: 1300 },
            { resolution: 720, ratio: 16 / 9, fps: 24, min: 2300, max: 2500 },
            { resolution: 1080, ratio: 16 / 9, fps: 24, min: 4700, max: 5000 },
            { resolution: 2160, ratio: 16 / 9, fps: 24, min: 15000, max: 17000 }
        ];
        for (const test of tests) {
            (0, chai_1.expect)((0, core_utils_1.getAverageBitrate)(test)).to.be.above(test.min * 1000).and.below(test.max * 1000);
        }
    });
});
//# sourceMappingURL=core-utils.js.map