StorageSettings.utils.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { describe, expect, test } from 'vitest'
  2. import { StorageSizeUnits } from '@/components/interfaces/Storage/StorageSettings/StorageSettings.constants'
  3. import {
  4. BUCKET_LIMIT_ERROR_PREFIX,
  5. convertFromBytes,
  6. convertToBytes,
  7. decodeBucketLimitErrorMessage,
  8. encodeBucketLimitErrorMessage,
  9. isBucketLimitErrorMessage,
  10. } from '@/components/interfaces/Storage/StorageSettings/StorageSettings.utils'
  11. describe('StorageSettings.utils: convertFromBytes', () => {
  12. test('should convert 1024 to 1KB', () => {
  13. const mockInput = 1024
  14. const output = convertFromBytes(mockInput)
  15. expect(output).toStrictEqual({ value: 1, unit: StorageSizeUnits.KB })
  16. })
  17. test('should convert 5242880 to 50MB', () => {
  18. const mockInput = 52428800
  19. const output = convertFromBytes(mockInput)
  20. expect(output).toStrictEqual({ value: 50, unit: StorageSizeUnits.MB })
  21. })
  22. test('should convert 100 to 100 bytes', () => {
  23. const mockInput = 100
  24. const output = convertFromBytes(mockInput)
  25. expect(output).toStrictEqual({ value: 100, unit: StorageSizeUnits.BYTES })
  26. })
  27. test('should convert 5712306503.68 to 5.32GB', () => {
  28. const mockInput = 5712306503.68
  29. const output = convertFromBytes(mockInput)
  30. expect(output).toStrictEqual({ value: 5.32, unit: StorageSizeUnits.GB })
  31. })
  32. test('should convert 9123162431 to 8.496607123874128GB', () => {
  33. const mockInput = 9123162431
  34. const output = convertFromBytes(mockInput)
  35. expect(output).toStrictEqual({ value: 8.496607123874128, unit: StorageSizeUnits.GB })
  36. })
  37. test('should convert negative inputs to just 0', () => {
  38. const mockInput = -1000
  39. const output = convertFromBytes(mockInput)
  40. expect(output).toStrictEqual({ value: 0, unit: StorageSizeUnits.BYTES })
  41. })
  42. test('should convert up to GB', () => {
  43. const mockInput = 10737418240000
  44. const output = convertFromBytes(mockInput)
  45. expect(output).toStrictEqual({ value: 10000, unit: StorageSizeUnits.GB })
  46. })
  47. test('should be able to convert given input into specific output unit', () => {
  48. const mockInput1 = 5368709120
  49. const output1 = convertFromBytes(mockInput1, StorageSizeUnits.GB)
  50. expect(output1).toStrictEqual({ value: 5, unit: StorageSizeUnits.GB })
  51. const mockInput2 = 5368709120
  52. const output2 = convertFromBytes(mockInput2, StorageSizeUnits.MB)
  53. expect(output2).toStrictEqual({ value: 5120, unit: StorageSizeUnits.MB })
  54. const mockInput3 = 5368709120
  55. const output3 = convertFromBytes(mockInput3, StorageSizeUnits.KB)
  56. expect(output3).toStrictEqual({ value: 5242880, unit: StorageSizeUnits.KB })
  57. })
  58. })
  59. describe('StorageSettings.utils: convertToBytes', () => {
  60. test('should be able to convert to bytes', () => {
  61. const mockInput = 100
  62. const output = convertToBytes(mockInput)
  63. expect(output).toStrictEqual(100)
  64. })
  65. test('should be able to convert to KB', () => {
  66. const output = convertToBytes(10, StorageSizeUnits.KB)
  67. expect(output).toStrictEqual(10240)
  68. })
  69. test('should be able to convert to MB', () => {
  70. const output = convertToBytes(51.2, StorageSizeUnits.MB)
  71. expect(output).toStrictEqual(53687091.2)
  72. })
  73. test('should be able to convert to GB', () => {
  74. const output = convertToBytes(10.21, StorageSizeUnits.GB)
  75. expect(output).toStrictEqual(10962904023.04)
  76. })
  77. test('should be able to handle negative inputs', () => {
  78. const output = convertToBytes(-12312, StorageSizeUnits.KB)
  79. expect(output).toStrictEqual(0)
  80. })
  81. })
  82. describe('StorageSettings.utils: bucket limit error encoding', () => {
  83. const bucketLists = [
  84. {
  85. description: 'multiple buckets with standard limits',
  86. buckets: [
  87. { name: 'images', limit: 1024 },
  88. { name: 'avatars', limit: 2048 },
  89. ],
  90. },
  91. {
  92. description: 'single bucket with zero limit',
  93. buckets: [{ name: 'avatars', limit: 0 }],
  94. },
  95. {
  96. description: 'bucket names with spaces and punctuation and large limits',
  97. buckets: [
  98. { name: 'prod-images', limit: Number.MAX_SAFE_INTEGER },
  99. { name: 'logs archive', limit: 987654321 },
  100. ],
  101. },
  102. {
  103. description: 'empty bucket list',
  104. buckets: [],
  105. },
  106. {
  107. description: 'bucket name with pipe/comma character (edge case)',
  108. buckets: [
  109. { name: 'data|backup', limit: 4096 },
  110. { name: 'reports,2023', limit: 8192 },
  111. ],
  112. },
  113. ]
  114. bucketLists.forEach(({ description, buckets }) => {
  115. test(`should round trip encode/decode for ${description}`, () => {
  116. const encoded = encodeBucketLimitErrorMessage(buckets)
  117. expect(encoded.startsWith(BUCKET_LIMIT_ERROR_PREFIX)).toBe(true)
  118. expect(isBucketLimitErrorMessage(encoded)).toBe(true)
  119. const decoded = decodeBucketLimitErrorMessage(encoded)
  120. expect(decoded).toStrictEqual(buckets)
  121. })
  122. })
  123. test('should return empty results for non bucket limit errors', () => {
  124. expect(isBucketLimitErrorMessage('other:error')).toBe(false)
  125. expect(decodeBucketLimitErrorMessage('other:error')).toStrictEqual([])
  126. })
  127. })