@@ -166,6 +166,49 @@ describe("Chunked Cookie Utils", () => {
166
166
) ;
167
167
} ) ;
168
168
169
+ it ( "should clear existing chunked cookies when setting a single cookie" , ( ) => {
170
+ const name = "testCookie" ;
171
+ const value = "small value" ;
172
+ const options = { path : "/" } as CookieOptions ;
173
+
174
+ const chunk0 = "chunk0 value" ;
175
+ const chunk1 = "chunk1 value" ;
176
+ const chunk2 = "chunk2 value" ;
177
+
178
+ cookieStore . set ( `${ name } __1` , chunk1 ) ;
179
+ cookieStore . set ( `${ name } __0` , chunk0 ) ;
180
+ cookieStore . set ( `${ name } __2` , chunk2 ) ;
181
+
182
+ setChunkedCookie ( name , value , options , reqCookies , resCookies ) ;
183
+
184
+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 1 ) ;
185
+ expect ( resCookies . set ) . toHaveBeenCalledWith ( name , value , options ) ;
186
+ expect ( reqCookies . set ) . toHaveBeenCalledTimes ( 1 ) ;
187
+ expect ( reqCookies . set ) . toHaveBeenCalledWith ( name , value ) ;
188
+ expect ( reqCookies . delete ) . toHaveBeenCalledTimes ( 3 ) ;
189
+ expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } __0` ) ;
190
+ expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } __1` ) ;
191
+ expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } __2` ) ;
192
+ } ) ;
193
+
194
+ it ( "should clear existing single cookies when setting a chunked cookie" , ( ) => {
195
+ const name = "testCookie" ;
196
+ const value = "small value" ;
197
+
198
+ cookieStore . set ( `${ name } ` , value ) ;
199
+
200
+ // Create a large string (8000 bytes)
201
+ const largeValue = "a" . repeat ( 8000 ) ;
202
+ const options = { path : "/" } as CookieOptions ;
203
+
204
+ setChunkedCookie ( name , largeValue , options , reqCookies , resCookies ) ;
205
+
206
+ expect ( reqCookies . delete ) . toHaveBeenCalledTimes ( 1 ) ;
207
+ expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } ` ) ;
208
+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 3 ) ;
209
+ expect ( reqCookies . set ) . toHaveBeenCalledTimes ( 3 ) ;
210
+ } ) ;
211
+
169
212
it ( "should clean up unused chunks when cookie shrinks" , ( ) => {
170
213
const name = "testCookie" ;
171
214
const options = { path : "/" } as CookieOptions ;
0 commit comments