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