]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/helpers/core-utils.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / helpers / core-utils.ts
index fa0a713412b6926469298420152f51f22c235112..e3e5eb45cb878e467b2fea3f09fb33a3ac8f8e01 100644 (file)
@@ -6,47 +6,64 @@ import { snakeCase } from 'lodash'
 import validator from 'validator'
 import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils'
 import { VideoResolution } from '@shared/models'
-import { objectConverter, parseBytes } from '../../helpers/core-utils'
+import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils'
 
 const expect = chai.expect
 
 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.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('6GB 1GB')).to.be.eq(6)
+    expect(parseBytes('35m 5s')).to.equal(35)
   })
 })