File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -500,6 +500,34 @@ describe('TTLCache', () => {
500500 cache . destroy ( ) ;
501501 } ) ;
502502
503+ it ( 'verify TTLCache behavior for empty string keys (Variation 2)' , ( ) => {
504+ const cache = new TTLCache < string > ( ) ;
505+
506+ // Assert that setting a value with empty string key throws error
507+ expect ( ( ) => {
508+ cache . set ( '' , 'test-value' , 60_000 ) ;
509+ } ) . toThrow ( Error ) ;
510+
511+ // Verify the error message is correct
512+ expect ( ( ) => {
513+ cache . set ( '' , 'test-value' , 60_000 ) ;
514+ } ) . toThrow ( 'Cache key cannot be empty' ) ;
515+
516+ // Verify that cache remains empty (no entry for empty key)
517+ expect ( cache . has ( '' ) ) . toBe ( false ) ;
518+ expect ( cache . get ( '' ) ) . toBeNull ( ) ;
519+
520+ // Verify cache size is still 0
521+ expect ( cache . size ( ) ) . toBe ( 0 ) ;
522+
523+ // Verify that normal operations still work after failed attempt
524+ cache . set ( 'valid-key' , 'value' , 60_000 ) ;
525+ expect ( cache . get ( 'valid-key' ) ) . toBe ( 'value' ) ;
526+ expect ( cache . size ( ) ) . toBe ( 1 ) ;
527+
528+ cache . destroy ( ) ;
529+ } ) ;
530+
503531 it ( 'handles rapid get/set/delete cycles' , ( ) => {
504532 const cache = new TTLCache < number > ( ) ;
505533 for ( let i = 0 ; i < 100 ; i ++ ) {
You can’t perform that action at this time.
0 commit comments