22
33import chai , { expect } from 'chai' ;
44import chaiAsPromised from 'chai-as-promised' ;
5- import pino from 'pino' ;
65import { Registry } from 'prom-client' ;
76import sinon from 'sinon' ;
87
@@ -13,8 +12,11 @@ chai.use(chaiAsPromised);
1312
1413describe ( 'LocalLRUCache Test Suite' , async function ( ) {
1514 this . timeout ( 10000 ) ;
16-
17- const logger = pino ( { level : 'silent' } ) ;
15+ const logger = {
16+ child : sinon . stub ( ) . returnsThis ( ) ,
17+ trace : sinon . stub ( ) ,
18+ isLevelEnabled : sinon . stub ( ) . returns ( true ) ,
19+ } ;
1820 const registry = new Registry ( ) ;
1921 const callingMethod = 'localLRUCacheTest' ;
2022
@@ -284,4 +286,98 @@ describe('LocalLRUCache Test Suite', async function () {
284286 expect ( await localLRUCache . get ( 'number' , callingMethod ) ) . to . equal ( 5644 ) ;
285287 } ) ;
286288 } ) ;
289+
290+ describe ( 'incrBy' , function ( ) {
291+ it ( 'increments an existing integer value' , async function ( ) {
292+ const key = 'counter' ;
293+ await localLRUCache . set ( key , 5 , callingMethod ) ;
294+ const newValue = await localLRUCache . incrBy ( key , 3 , callingMethod ) ;
295+ expect ( newValue ) . to . equal ( 8 ) ;
296+
297+ const stored = await localLRUCache . get ( key , callingMethod ) ;
298+ expect ( stored ) . to . equal ( 8 ) ;
299+ } ) ;
300+
301+ it ( 'increments when value is zero' , async function ( ) {
302+ const key = 'counter' ;
303+ await localLRUCache . set ( key , 0 , callingMethod ) ;
304+ const newValue = await localLRUCache . incrBy ( key , 10 , callingMethod ) ;
305+ expect ( newValue ) . to . equal ( 10 ) ;
306+ } ) ;
307+
308+ it ( 'increments when key does not exist' , async function ( ) {
309+ await localLRUCache . clear ( ) ;
310+ const key = 'missing' ;
311+ const newValue = await localLRUCache . incrBy ( key , 5 , callingMethod ) ;
312+ expect ( newValue ) . to . equal ( 5 ) ;
313+ } ) ;
314+ } ) ;
315+
316+ describe ( 'lRange' , function ( ) {
317+ it ( 'returns the correct range for a valid array' , async function ( ) {
318+ const key = 'list' ;
319+ const value = [ 10 , 20 , 30 , 40 ] ;
320+ await localLRUCache . set ( key , value , callingMethod ) ;
321+
322+ const result = await localLRUCache . lRange ( key , 1 , 2 , callingMethod ) ;
323+ expect ( result ) . to . deep . equal ( [ 20 , 30 ] ) ;
324+ } ) ;
325+
326+ it ( 'supports negative end index' , async function ( ) {
327+ const key = 'list' ;
328+ const value = [ 10 , 20 , 30 , 40 ] ;
329+ await localLRUCache . set ( key , value , callingMethod ) ;
330+
331+ const result = await localLRUCache . lRange ( key , 1 , - 1 , callingMethod ) ;
332+ expect ( result ) . to . deep . equal ( [ 20 , 30 , 40 ] ) ;
333+ } ) ;
334+
335+ it ( 'returns empty array when key does not exist' , async function ( ) {
336+ const key = 'missing' ;
337+ const result = await localLRUCache . lRange ( key , 0 , 10 , callingMethod ) ;
338+ expect ( result ) . to . deep . equal ( [ ] ) ;
339+ } ) ;
340+
341+ it ( 'throws when the value is not an array' , async function ( ) {
342+ const key = 'notList' ;
343+ await localLRUCache . set ( key , 123 , callingMethod ) ;
344+
345+ await expect ( localLRUCache . lRange ( key , 0 , 1 , callingMethod ) ) . to . be . rejectedWith (
346+ `Value at key ${ key } is not an array` ,
347+ ) ;
348+ } ) ;
349+ } ) ;
350+
351+ describe ( 'rPush' , function ( ) {
352+ it ( 'pushes value to end of an existing list' , async function ( ) {
353+ const key = 'list' ;
354+ const initial = [ 1 , 2 ] ;
355+ await localLRUCache . set ( key , initial , callingMethod ) ;
356+
357+ const length = await localLRUCache . rPush ( key , 3 , callingMethod ) ;
358+ expect ( length ) . to . equal ( 3 ) ;
359+
360+ const stored = await localLRUCache . get ( key , callingMethod ) ;
361+ expect ( stored ) . to . deep . equal ( [ 1 , 2 , 3 ] ) ;
362+ } ) ;
363+
364+ it ( 'initializes a new list when key does not exist' , async function ( ) {
365+ const key = 'missing' ;
366+
367+ const length = await localLRUCache . rPush ( key , 'a' , callingMethod ) ;
368+ expect ( length ) . to . equal ( 1 ) ;
369+
370+ const stored = await localLRUCache . get ( key , callingMethod ) ;
371+ expect ( stored ) . to . deep . equal ( [ 'a' ] ) ;
372+ } ) ;
373+
374+ it ( 'throws when existing value is not an array' , async function ( ) {
375+ const key = 'wrongType' ;
376+ await localLRUCache . set ( key , 999 , callingMethod ) ;
377+
378+ await expect ( localLRUCache . rPush ( key , 'x' , callingMethod ) ) . to . be . rejectedWith (
379+ `Value at key ${ key } is not an array` ,
380+ ) ;
381+ } ) ;
382+ } ) ;
287383} ) ;
0 commit comments