@@ -166,6 +166,30 @@ describe("Chunked Cookie Utils", () => {
166166 ) ;
167167 } ) ;
168168
169+ it ( "should clean up unused chunks when cookie shrinks" , ( ) => {
170+ const name = "testCookie" ;
171+ const options = { path : "/" } as CookieOptions ;
172+
173+ const chunk0 = "chunk0 value" ;
174+ const chunk1 = "chunk1 value" ;
175+ const chunk2 = "chunk2 value" ;
176+ const chunk3 = "chunk3 value" ;
177+ const chunk4 = "chunk4 value" ;
178+
179+ cookieStore . set ( `${ name } __1` , chunk1 ) ;
180+ cookieStore . set ( `${ name } __0` , chunk0 ) ;
181+ cookieStore . set ( `${ name } __2` , chunk2 ) ;
182+ cookieStore . set ( `${ name } __3` , chunk3 ) ;
183+ cookieStore . set ( `${ name } __4` , chunk4 ) ;
184+
185+ const largeValue = "a" . repeat ( 8000 ) ;
186+ setChunkedCookie ( name , largeValue , options , reqCookies , resCookies ) ;
187+
188+ expect ( reqCookies . delete ) . toHaveBeenCalledTimes ( 2 ) ;
189+ expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } __3` ) ;
190+ expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } __4` ) ;
191+ } ) ;
192+
169193 it ( "should log a warning when cookie size exceeds warning threshold" , ( ) => {
170194 const name = "warningCookie" ;
171195 const options = { path : "/" } as CookieOptions ;
0 commit comments