@@ -159,6 +159,64 @@ describe("UrbitSSEClient", () => {
159159 } ) ;
160160 } ) ;
161161
162+ describe ( "close" , ( ) => {
163+ it ( "releases shutdown responses without leaking cancellation failures" , async ( ) => {
164+ const unhandledRejections : unknown [ ] = [ ] ;
165+ const onUnhandledRejection = ( reason : unknown ) => {
166+ unhandledRejections . push ( reason ) ;
167+ } ;
168+ const shutdownResponses = [
169+ { method : "PUT" , label : "unsubscribe" } ,
170+ { method : "DELETE" , label : "delete" } ,
171+ ] . map ( ( { method, label } ) => {
172+ const cancel = vi . fn ( ( ) => {
173+ throw new Error ( `${ label } cancel failed` ) ;
174+ } ) ;
175+ const release = vi . fn ( ) . mockResolvedValue ( undefined ) ;
176+ return {
177+ method,
178+ cancel,
179+ release,
180+ result : {
181+ response : new Response ( new ReadableStream < Uint8Array > ( { cancel } ) ) ,
182+ finalUrl : "https://example.com" ,
183+ release,
184+ } ,
185+ } ;
186+ } ) ;
187+ const [ unsubscribeResponse , deleteResponse ] = shutdownResponses ;
188+ if ( ! unsubscribeResponse || ! deleteResponse ) {
189+ throw new Error ( "Expected both shutdown response fixtures" ) ;
190+ }
191+ const mockUrbitFetch = vi . mocked ( urbitFetch ) ;
192+ mockUrbitFetch
193+ . mockResolvedValueOnce ( unsubscribeResponse . result )
194+ . mockResolvedValueOnce ( deleteResponse . result ) ;
195+ const client = new UrbitSSEClient ( "https://example.com" , "urbauth-~zod=123" , {
196+ autoReconnect : false ,
197+ } ) ;
198+ process . on ( "unhandledRejection" , onUnhandledRejection ) ;
199+
200+ try {
201+ await client . close ( ) ;
202+
203+ expect ( mockUrbitFetch ) . toHaveBeenCalledTimes ( 2 ) ;
204+ for ( const [ index , { method, cancel, release } ] of shutdownResponses . entries ( ) ) {
205+ expect ( mockUrbitFetch . mock . calls [ index ] ?. [ 0 ] . init ?. method ) . toBe ( method ) ;
206+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
207+ expect ( release ) . toHaveBeenCalledOnce ( ) ;
208+ }
209+ await new Promise < void > ( ( resolve ) => {
210+ setImmediate ( resolve ) ;
211+ } ) ;
212+ expect ( unhandledRejections ) . toStrictEqual ( [ ] ) ;
213+ } finally {
214+ process . off ( "unhandledRejection" , onUnhandledRejection ) ;
215+ expect ( process . listeners ( "unhandledRejection" ) ) . not . toContain ( onUnhandledRejection ) ;
216+ }
217+ } ) ;
218+ } ) ;
219+
162220 describe ( "reconnection" , ( ) => {
163221 it ( "has autoReconnect enabled by default" , ( ) => {
164222 const client = new UrbitSSEClient ( "https://example.com" , "urbauth-~zod=123" ) ;
0 commit comments