core-utils.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import snakeCase from 'lodash-es/snakeCase.js'
  4. import validator from 'validator'
  5. import { getAverageTheoreticalBitrate, getMaxTheoreticalBitrate, parseChapters } from '@peertube/peertube-core-utils'
  6. import { VideoResolution } from '@peertube/peertube-models'
  7. import { objectConverter, parseBytes, parseDurationToMs, parseSemVersion } from '@peertube/peertube-server/core/helpers/core-utils.js'
  8. describe('Parse Bytes', function () {
  9. it('Should pass on valid value', async function () {
  10. // just return it
  11. expect(parseBytes(-1024)).to.equal(-1024)
  12. expect(parseBytes(1024)).to.equal(1024)
  13. expect(parseBytes(1048576)).to.equal(1048576)
  14. expect(parseBytes('1024')).to.equal(1024)
  15. expect(parseBytes('1048576')).to.equal(1048576)
  16. // sizes
  17. expect(parseBytes('1B')).to.equal(1024)
  18. expect(parseBytes('1MB')).to.equal(1048576)
  19. expect(parseBytes('1GB')).to.equal(1073741824)
  20. expect(parseBytes('1TB')).to.equal(1099511627776)
  21. expect(parseBytes('5GB')).to.equal(5368709120)
  22. expect(parseBytes('5TB')).to.equal(5497558138880)
  23. expect(parseBytes('1024B')).to.equal(1048576)
  24. expect(parseBytes('1024MB')).to.equal(1073741824)
  25. expect(parseBytes('1024GB')).to.equal(1099511627776)
  26. expect(parseBytes('1024TB')).to.equal(1125899906842624)
  27. // with whitespace
  28. expect(parseBytes('1 GB')).to.equal(1073741824)
  29. expect(parseBytes('1\tGB')).to.equal(1073741824)
  30. // sum value
  31. expect(parseBytes('1TB 1024MB')).to.equal(1100585369600)
  32. expect(parseBytes('4GB 1024MB')).to.equal(5368709120)
  33. expect(parseBytes('4TB 1024GB')).to.equal(5497558138880)
  34. expect(parseBytes('4TB 1024GB 0MB')).to.equal(5497558138880)
  35. expect(parseBytes('1024TB 1024GB 1024MB')).to.equal(1127000492212224)
  36. })
  37. it('Should be invalid when given invalid value', async function () {
  38. expect(parseBytes('6GB 1GB')).to.equal(6)
  39. })
  40. })
  41. describe('Parse duration', function () {
  42. it('Should pass when given valid value', async function () {
  43. expect(parseDurationToMs(35)).to.equal(35)
  44. expect(parseDurationToMs(-35)).to.equal(-35)
  45. expect(parseDurationToMs('35 seconds')).to.equal(35 * 1000)
  46. expect(parseDurationToMs('1 minute')).to.equal(60 * 1000)
  47. expect(parseDurationToMs('1 hour')).to.equal(3600 * 1000)
  48. expect(parseDurationToMs('35 hours')).to.equal(3600 * 35 * 1000)
  49. })
  50. it('Should be invalid when given invalid value', async function () {
  51. expect(parseBytes('35m 5s')).to.equal(35)
  52. })
  53. })
  54. describe('Object', function () {
  55. it('Should convert an object', async function () {
  56. function keyConverter (k: string) {
  57. return snakeCase(k)
  58. }
  59. function valueConverter (v: any) {
  60. if (validator.default.isNumeric(v + '')) return parseInt('' + v, 10)
  61. return v
  62. }
  63. const obj = {
  64. mySuperKey: 'hello',
  65. mySuper2Key: '45',
  66. mySuper3Key: {
  67. mySuperSubKey: '15',
  68. mySuperSub2Key: 'hello',
  69. mySuperSub3Key: [ '1', 'hello', 2 ],
  70. mySuperSub4Key: 4
  71. },
  72. mySuper4Key: 45,
  73. toto: {
  74. super_key: '15',
  75. superKey2: 'hello'
  76. },
  77. super_key: {
  78. superKey4: 15
  79. }
  80. }
  81. const res = objectConverter(obj, keyConverter, valueConverter)
  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. // Immutable
  92. expect(res.mySuperKey).to.be.undefined
  93. expect(obj['my_super_key']).to.be.undefined
  94. })
  95. })
  96. describe('Bitrate', function () {
  97. it('Should get appropriate max bitrate', function () {
  98. const tests = [
  99. { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 200, max: 400 },
  100. { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 600, max: 800 },
  101. { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 1200, max: 1600 },
  102. { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 2000, max: 2300 },
  103. { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 4000, max: 4400 },
  104. { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 8000, max: 10000 },
  105. { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 25000, max: 30000 }
  106. ]
  107. for (const test of tests) {
  108. expect(getMaxTheoreticalBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000)
  109. }
  110. })
  111. it('Should get appropriate average bitrate', function () {
  112. const tests = [
  113. { resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 50, max: 300 },
  114. { resolution: VideoResolution.H_240P, ratio: 16 / 9, fps: 24, min: 350, max: 450 },
  115. { resolution: VideoResolution.H_360P, ratio: 16 / 9, fps: 24, min: 700, max: 900 },
  116. { resolution: VideoResolution.H_480P, ratio: 16 / 9, fps: 24, min: 1100, max: 1300 },
  117. { resolution: VideoResolution.H_720P, ratio: 16 / 9, fps: 24, min: 2300, max: 2500 },
  118. { resolution: VideoResolution.H_1080P, ratio: 16 / 9, fps: 24, min: 4700, max: 5000 },
  119. { resolution: VideoResolution.H_4K, ratio: 16 / 9, fps: 24, min: 15000, max: 17000 }
  120. ]
  121. for (const test of tests) {
  122. expect(getAverageTheoreticalBitrate(test)).to.be.above(test.min * 1000).and.below(test.max * 1000)
  123. }
  124. })
  125. })
  126. describe('Parse semantic version string', function () {
  127. it('Should parse Node.js version string', function () {
  128. const actual = parseSemVersion('v18.16.0')
  129. expect(actual.major).to.equal(18)
  130. expect(actual.minor).to.equal(16)
  131. expect(actual.patch).to.equal(0)
  132. })
  133. it('Should parse FFmpeg version string from Debian 12 repo', function () {
  134. const actual = parseSemVersion('5.1.3-1')
  135. expect(actual.major).to.equal(5)
  136. expect(actual.minor).to.equal(1)
  137. expect(actual.patch).to.equal(3)
  138. })
  139. it('Should parse FFmpeg version string from Arch repo', function () {
  140. const actual = parseSemVersion('n6.0')
  141. expect(actual.major).to.equal(6)
  142. expect(actual.minor).to.equal(0)
  143. expect(actual.patch).to.equal(0)
  144. })
  145. it('Should parse FFmpeg version from GitHub release', function () {
  146. const actual = parseSemVersion('5.1.3')
  147. expect(actual.major).to.equal(5)
  148. expect(actual.minor).to.equal(1)
  149. expect(actual.patch).to.equal(3)
  150. })
  151. it('Should parse FFmpeg version from GitHub dev release', function () {
  152. const actual = parseSemVersion('5.1.git')
  153. expect(actual.major).to.equal(5)
  154. expect(actual.minor).to.equal(1)
  155. expect(actual.patch).to.equal(0)
  156. })
  157. it('Should parse FFmpeg version string with missing patch segment', function () {
  158. const actual = parseSemVersion('4.4')
  159. expect(actual.major).to.equal(4)
  160. expect(actual.minor).to.equal(4)
  161. expect(actual.patch).to.equal(0)
  162. })
  163. })
  164. describe('Extract chapters', function () {
  165. it('Should not extract chapters', function () {
  166. expect(parseChapters('my super description\nno?')).to.deep.equal([])
  167. expect(parseChapters('m00:00 super description\nno?')).to.deep.equal([])
  168. expect(parseChapters('00:00super description\nno?')).to.deep.equal([])
  169. expect(parseChapters('my super description\n'.repeat(10) + ' * list1\n * list 2\n * list 3')).to.deep.equal([])
  170. })
  171. it('Should extract chapters', function () {
  172. expect(parseChapters('00:00 coucou')).to.deep.equal([ { timecode: 0, title: 'coucou' } ])
  173. expect(parseChapters('my super description\n\n00:01:30 chapter 1\n00:01:35 chapter 2')).to.deep.equal([
  174. { timecode: 90, title: 'chapter 1' },
  175. { timecode: 95, title: 'chapter 2' }
  176. ])
  177. expect(parseChapters('hi\n\n00:01:30 chapter 1\n00:01:35 chapter 2\nhi')).to.deep.equal([
  178. { timecode: 90, title: 'chapter 1' },
  179. { timecode: 95, title: 'chapter 2' }
  180. ])
  181. expect(parseChapters('hi\n\n00:01:30 chapter 1\n00:01:35 chapter 2\nhi\n00:01:40 chapter 3')).to.deep.equal([
  182. { timecode: 90, title: 'chapter 1' },
  183. { timecode: 95, title: 'chapter 2' }
  184. ])
  185. })
  186. })