X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fhelpers%2Fcore-utils.ts;h=de6ba4f82f22e545f81864be1a138936d93b6d3a;hb=ff91b644fb1b063d0a8eff7492beb1a9bf7e4ce1;hp=c028b316d9695512273705d92ad3a15afa9721ee;hpb=610d0be13b3d01f653ef269271dd667a57c85ef2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/helpers/core-utils.ts b/server/tests/helpers/core-utils.ts index c028b316d..de6ba4f82 100644 --- a/server/tests/helpers/core-utils.ts +++ b/server/tests/helpers/core-utils.ts @@ -1,52 +1,71 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' -import 'mocha' +import { expect } from 'chai' import { snakeCase } from 'lodash' -import { objectConverter, parseBytes } from '../../helpers/core-utils' import validator from 'validator' - -const expect = chai.expect +import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils' +import { VideoResolution } from '@shared/models' +import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils' describe('Parse Bytes', function () { - it('Should pass when given valid value', async function () { + it('Should pass on valid value', async function () { // just return it - expect(parseBytes(1024)).to.be.eq(1024) - expect(parseBytes(1048576)).to.be.eq(1048576) - expect(parseBytes('1024')).to.be.eq(1024) - expect(parseBytes('1048576')).to.be.eq(1048576) + expect(parseBytes(-1024)).to.equal(-1024) + expect(parseBytes(1024)).to.equal(1024) + expect(parseBytes(1048576)).to.equal(1048576) + expect(parseBytes('1024')).to.equal(1024) + expect(parseBytes('1048576')).to.equal(1048576) // sizes - expect(parseBytes('1B')).to.be.eq(1024) - expect(parseBytes('1MB')).to.be.eq(1048576) - expect(parseBytes('1GB')).to.be.eq(1073741824) - expect(parseBytes('1TB')).to.be.eq(1099511627776) + expect(parseBytes('1B')).to.equal(1024) + expect(parseBytes('1MB')).to.equal(1048576) + expect(parseBytes('1GB')).to.equal(1073741824) + expect(parseBytes('1TB')).to.equal(1099511627776) - expect(parseBytes('5GB')).to.be.eq(5368709120) - expect(parseBytes('5TB')).to.be.eq(5497558138880) + expect(parseBytes('5GB')).to.equal(5368709120) + expect(parseBytes('5TB')).to.equal(5497558138880) - expect(parseBytes('1024B')).to.be.eq(1048576) - expect(parseBytes('1024MB')).to.be.eq(1073741824) - expect(parseBytes('1024GB')).to.be.eq(1099511627776) - expect(parseBytes('1024TB')).to.be.eq(1125899906842624) + expect(parseBytes('1024B')).to.equal(1048576) + expect(parseBytes('1024MB')).to.equal(1073741824) + expect(parseBytes('1024GB')).to.equal(1099511627776) + expect(parseBytes('1024TB')).to.equal(1125899906842624) // with whitespace - expect(parseBytes('1 GB')).to.be.eq(1073741824) - expect(parseBytes('1\tGB')).to.be.eq(1073741824) + expect(parseBytes('1 GB')).to.equal(1073741824) + expect(parseBytes('1\tGB')).to.equal(1073741824) // sum value - expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600) - expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120) - expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880) - expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880) - expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224) + expect(parseBytes('1TB 1024MB')).to.equal(1100585369600) + expect(parseBytes('4GB 1024MB')).to.equal(5368709120) + expect(parseBytes('4TB 1024GB')).to.equal(5497558138880) + expect(parseBytes('4TB 1024GB 0MB')).to.equal(5497558138880) + expect(parseBytes('1024TB 1024GB 1024MB')).to.equal(1127000492212224) }) it('Should be invalid when given invalid value', async function () { - expect(parseBytes('6GB 1GB')).to.be.eq(6) + expect(parseBytes('6GB 1GB')).to.equal(6) + }) +}) + +describe('Parse duration', function () { + + it('Should pass when given valid value', async function () { + expect(parseDurationToMs(35)).to.equal(35) + expect(parseDurationToMs(-35)).to.equal(-35) + expect(parseDurationToMs('35 seconds')).to.equal(35 * 1000) + expect(parseDurationToMs('1 minute')).to.equal(60 * 1000) + expect(parseDurationToMs('1 hour')).to.equal(3600 * 1000) + expect(parseDurationToMs('35 hours')).to.equal(3600 * 35 * 1000) }) + it('Should be invalid when given invalid value', async function () { + expect(parseBytes('35m 5s')).to.equal(35) + }) +}) + +describe('Object', function () { + it('Should convert an object', async function () { function keyConverter (k: string) { return snakeCase(k) @@ -94,3 +113,38 @@ describe('Parse Bytes', function () { expect(obj['my_super_key']).to.be.undefined }) }) + +describe('Bitrate', function () { + + it('Should get appropriate max bitrate', function () { + const tests = [ + { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 200, max: 400 }, + { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 600, max: 800 }, + { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 1200, max: 1600 }, + { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 2000, max: 2300 }, + { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 4000, max: 4400 }, + { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 8000, max: 10000 }, + { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 25000, max: 30000 } + ] + + for (const test of tests) { + expect(getMaxBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000) + } + }) + + it('Should get appropriate average bitrate', function () { + const tests = [ + { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 50, max: 300 }, + { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 350, max: 450 }, + { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 700, max: 900 }, + { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 1100, max: 1300 }, + { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 2300, max: 2500 }, + { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 4700, max: 5000 }, + { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 15000, max: 17000 } + ] + + for (const test of tests) { + expect(getAverageBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000) + } + }) +})