@@ -235,9 +235,10 @@ test("reattach reuses an existing about:blank target", async () => {
235235 }
236236} )
237237
238- test ( "reattach creates a blank page instead of switching to another live target " , async ( ) => {
238+ test ( "a missing original target is reported without replaying on another page " , async ( ) => {
239239 let attachCount = 0
240- let createdTarget : unknown
240+ let createCount = 0
241+ let commandCount = 0
241242 const attachedTargets : string [ ] = [ ]
242243 const server = Bun . serve ( {
243244 port : 0 ,
@@ -266,15 +267,16 @@ test("reattach creates a blank page instead of switching to another live target"
266267 return
267268 }
268269 if ( message . method === "Target.createTarget" ) {
269- createdTarget = message . params
270- socket . send ( JSON . stringify ( { id : message . id , result : { targetId : "fresh -page" } } ) )
270+ createCount ++
271+ socket . send ( JSON . stringify ( { id : message . id , result : { targetId : "unexpected -page" } } ) )
271272 return
272273 }
273274 if ( [ "Page.enable" , "DOM.enable" , "Runtime.enable" , "Network.enable" ] . includes ( message . method ) ) {
274275 socket . send ( JSON . stringify ( { id : message . id , result : { } } ) )
275276 return
276277 }
277278 if ( message . method === "Runtime.evaluate" ) {
279+ commandCount ++
278280 if ( message . sessionId === "session-1" ) {
279281 socket . send ( JSON . stringify ( {
280282 id : message . id ,
@@ -293,12 +295,57 @@ test("reattach creates a blank page instead of switching to another live target"
293295 try {
294296 await session . connect ( { wsUrl : wsUrl ( server ) } )
295297 await session . use ( "old-page" )
298+ await expect ( session . domains . Runtime . evaluate ( { expression : "submit()" } ) )
299+ . rejects . toThrow ( "CDP target old-page was closed" )
300+
301+ expect ( commandCount ) . toBe ( 1 )
302+ expect ( createCount ) . toBe ( 0 )
303+ expect ( attachCount ) . toBe ( 1 )
304+ expect ( attachedTargets ) . toEqual ( [ "old-page" ] )
305+ } finally {
306+ session . close ( )
307+ server . stop ( true )
308+ }
309+ } )
310+
311+ test ( "no-argument connect preserves a healthy socket and active target" , async ( ) => {
312+ let connectionCount = 0
313+ const commandSessions : string [ ] = [ ]
314+ const server = Bun . serve ( {
315+ port : 0 ,
316+ fetch ( req , bunServer ) {
317+ if ( ! bunServer . upgrade ( req ) ) return new Response ( "nope" , { status : 400 } )
318+ connectionCount ++
319+ return undefined
320+ } ,
321+ websocket : {
322+ message ( socket , raw ) {
323+ const message = JSON . parse ( String ( raw ) )
324+ if ( message . method === "Target.attachToTarget" ) {
325+ socket . send ( JSON . stringify ( { id : message . id , result : { sessionId : "session-1" } } ) )
326+ return
327+ }
328+ if ( message . method !== "Runtime.evaluate" ) return
329+ commandSessions . push ( message . sessionId )
330+ socket . send ( JSON . stringify ( {
331+ id : message . id ,
332+ result : { result : { type : "boolean" , value : true } } ,
333+ } ) )
334+ } ,
335+ close ( ) { } ,
336+ } ,
337+ } )
338+ const session = new Session ( )
339+
340+ try {
341+ await session . connect ( { wsUrl : wsUrl ( server ) } )
342+ await session . use ( "page-1" )
343+ await session . connect ( )
296344 const result = await session . domains . Runtime . evaluate ( { expression : "true" } )
297345
298346 expect ( result . result . value ) . toBe ( true )
299- expect ( createdTarget ) . toEqual ( { url : "about:blank" } )
300- expect ( attachCount ) . toBe ( 2 )
301- expect ( attachedTargets ) . toEqual ( [ "old-page" , "fresh-page" ] )
347+ expect ( connectionCount ) . toBe ( 1 )
348+ expect ( commandSessions ) . toEqual ( [ "session-1" ] )
302349 } finally {
303350 session . close ( )
304351 server . stop ( true )
0 commit comments