@@ -180,4 +180,50 @@ describe('Redis', () => {
180180 } ) ;
181181 } ) ;
182182 } ) ;
183+
184+ describe ( 'calculateCacheItemSize' , ( ) => {
185+ it ( 'should return byte length for Buffer input' , ( ) => {
186+ const buffer = Buffer . from ( 'test' ) ;
187+ const result = calculateCacheItemSize ( buffer ) ;
188+ expect ( result ) . toBe ( 4 ) ;
189+ } ) ;
190+
191+ it ( 'should return string length for string input' , ( ) => {
192+ const str = 'test' ;
193+ const result = calculateCacheItemSize ( str ) ;
194+ expect ( result ) . toBe ( 4 ) ;
195+ } ) ;
196+
197+ it ( 'should return number length for number input' , ( ) => {
198+ const num = 1234 ;
199+ const result = calculateCacheItemSize ( num ) ;
200+ expect ( result ) . toBe ( 4 ) ;
201+ } ) ;
202+
203+ it ( 'should return 0 for null or undefined input' , ( ) => {
204+ const resultForNull = calculateCacheItemSize ( null ) ;
205+ const resultForUndefined = calculateCacheItemSize ( undefined ) ;
206+ expect ( resultForNull ) . toBe ( 0 ) ;
207+ expect ( resultForUndefined ) . toBe ( 0 ) ;
208+ } ) ;
209+
210+ it ( 'should return total size for array input' , ( ) => {
211+ const arr = [ 'test' , Buffer . from ( 'test' ) , 1234 ] ;
212+ const result = calculateCacheItemSize ( arr ) ;
213+ expect ( result ) . toBe ( 12 ) ;
214+ } ) ;
215+
216+ it ( 'should return JSON string length for object input' , ( ) => {
217+ const obj = { key : 'value' } ;
218+ const result = calculateCacheItemSize ( obj ) ;
219+ expect ( result ) . toBe ( 15 ) ;
220+ } ) ;
221+
222+ it ( 'should return undefined for circular objects' , ( ) => {
223+ const circularObject : { self ?: any } = { } ;
224+ circularObject . self = circularObject ; // This creates a circular reference
225+ const result = calculateCacheItemSize ( circularObject ) ;
226+ expect ( result ) . toBeUndefined ( ) ;
227+ } ) ;
228+ } ) ;
183229} ) ;
0 commit comments