@@ -24,7 +24,7 @@ function fixture() {
2424 onTestFinished ( ( ) => bridge . dispose ( ) ) ;
2525 const enabled = new Set < string > ( ) ;
2626 const held : number [ ] = [ ] ;
27- let hold = false ;
27+ let heldMethod : string | undefined ;
2828 let accessAllowed = true ;
2929 let contextOffset = - 10 ;
3030 const extension = wireExtension ( bridge , ( msg ) => {
@@ -51,14 +51,14 @@ function fixture() {
5151 msg . sessionId ,
5252 ) ;
5353 }
54- if ( hold ) {
55- held . push ( msg . seq ) ;
56- return null ;
57- }
5854 }
5955 if ( msg . method === "Runtime.disable" ) {
6056 enabled . delete ( key ) ;
6157 }
58+ if ( msg . method === heldMethod ) {
59+ held . push ( msg . seq ) ;
60+ return null ;
61+ }
6262 }
6363 return { type : "result" , seq : msg . seq , result : { } } ;
6464 } ) ;
@@ -136,11 +136,11 @@ function fixture() {
136136 revokeAccess : ( ) => {
137137 accessAllowed = false ;
138138 } ,
139- hold : ( ) => {
140- hold = true ;
139+ hold : ( method = "Runtime.enable" ) => {
140+ heldMethod = method ;
141141 } ,
142142 release : ( ) => {
143- hold = false ;
143+ heldMethod = undefined ;
144144 for ( const seq of held . splice ( 0 ) ) {
145145 extension . handlers . onMessage ( JSON . stringify ( { type : "result" , seq, result : { } } ) ) ;
146146 }
@@ -739,7 +739,9 @@ describe("relay logical Runtime subscriptions", () => {
739739 const f = fixture ( ) ;
740740 const client = f . client ( ) ;
741741 const root = await client . attach ( ) ;
742- await f . client ( ) . attach ( ) ;
742+ const peer = f . client ( ) ;
743+ const peerRoot = await peer . attach ( ) ;
744+ await peer . request ( "Runtime.enable" , peerRoot ) ;
743745 expect (
744746 ( await client . request ( "Runtime.addBinding" , root , { name : "relayBinding" } ) ) . error ,
745747 ) . toBeUndefined ( ) ;
@@ -754,13 +756,158 @@ describe("relay logical Runtime subscriptions", () => {
754756 method : "Runtime.bindingCalled" ,
755757 params,
756758 } ) ;
759+ expect (
760+ peer . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
761+ ) . toEqual ( [ ] ) ;
757762 await client . request ( "Target.detachFromTarget" , undefined , { sessionId : root } ) ;
758763 const before = client . socket . frames ( ) . length ;
759764 f . event ( "Runtime.bindingCalled" , params ) ;
760765 expect ( client . socket . frames ( ) ) . toHaveLength ( before ) ;
761766 } ,
762767 ) ;
763768
769+ it . each ( [ "Runtime.removeBinding" , "Target.detachFromTarget" , "socket close" ] ) (
770+ "preserves a peer's same-name binding after %s and removes the final native registration" ,
771+ async ( operation ) => {
772+ const f = fixture ( ) ;
773+ const first = f . client ( ) ;
774+ const firstRoot = await first . attach ( ) ;
775+ const second = f . client ( ) ;
776+ const secondRoot = await second . attach ( ) ;
777+ const observer = f . client ( ) ;
778+ const observerRoot = await observer . attach ( ) ;
779+ const name = "sharedBinding" ;
780+ for ( const [ client , root ] of [
781+ [ first , firstRoot ] ,
782+ [ second , secondRoot ] ,
783+ ] as const ) {
784+ expect ( ( await client . request ( "Runtime.addBinding" , root , { name } ) ) . error ) . toBeUndefined ( ) ;
785+ }
786+ await observer . request ( "Runtime.removeBinding" , observerRoot , { name } ) ;
787+ expect ( f . commands ( "Runtime.removeBinding" ) ) . toHaveLength ( 0 ) ;
788+ if ( operation === "socket close" ) {
789+ first . handlers . onClose ( ) ;
790+ await flush ( ) ;
791+ } else if ( operation === "Target.detachFromTarget" ) {
792+ await first . request ( operation , undefined , { sessionId : firstRoot } ) ;
793+ } else {
794+ await first . request ( operation , firstRoot , { name } ) ;
795+ }
796+ expect ( f . commands ( "Runtime.removeBinding" ) ) . toHaveLength ( 0 ) ;
797+ const params = { name, payload : "still-owned" , executionContextId : 1 } ;
798+ f . event ( "Runtime.bindingCalled" , params ) ;
799+ expect (
800+ first . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
801+ ) . toEqual ( [ ] ) ;
802+ expect (
803+ observer . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
804+ ) . toEqual ( [ ] ) ;
805+ expect (
806+ second . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
807+ ) . toEqual ( [ { sessionId : secondRoot , method : "Runtime.bindingCalled" , params } ] ) ;
808+ await second . request ( "Runtime.removeBinding" , secondRoot , { name } ) ;
809+ expect ( f . commands ( "Runtime.removeBinding" ) . map ( ( frame ) => frame . params ) ) . toEqual ( [ { name } ] ) ;
810+ f . event ( "Runtime.bindingCalled" , params ) ;
811+ expect (
812+ second . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
813+ ) . toHaveLength ( 1 ) ;
814+ } ,
815+ ) ;
816+
817+ it . each ( [ false , true ] ) (
818+ "cleans up a native add admitted after logical close (peer owns same name=%s)" ,
819+ async ( sameName ) => {
820+ const f = fixture ( ) ;
821+ const first = f . client ( ) ;
822+ const firstRoot = await first . attach ( ) ;
823+ const second = f . client ( ) ;
824+ const secondRoot = await second . attach ( ) ;
825+ const name = "pendingBinding" ;
826+ if ( sameName ) {
827+ await second . request ( "Runtime.addBinding" , secondRoot , { name } ) ;
828+ }
829+ f . hold ( "Runtime.addBinding" ) ;
830+ const pending = first . send ( "Runtime.addBinding" , firstRoot , { name } ) ;
831+ await flush ( ) ;
832+ first . handlers . onClose ( ) ;
833+ f . release ( ) ;
834+ await flush ( ) ;
835+ expect ( first . socket . frames ( ) . find ( ( frame ) => frame . id === pending ) ) . toBeUndefined ( ) ;
836+ expect ( f . commands ( "Runtime.removeBinding" ) ) . toHaveLength ( sameName ? 0 : 1 ) ;
837+ const params = { name, payload : "after-close" , executionContextId : 1 } ;
838+ f . event ( "Runtime.bindingCalled" , params ) ;
839+ expect (
840+ first . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
841+ ) . toEqual ( [ ] ) ;
842+ expect (
843+ second . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
844+ ) . toHaveLength ( sameName ? 1 : 0 ) ;
845+ } ,
846+ ) ;
847+
848+ it . each ( [ "Runtime.addBinding" , "Runtime.removeBinding" ] ) (
849+ "preserves acknowledged binding ownership when native %s fails" ,
850+ async ( method ) => {
851+ const f = fixture ( ) ;
852+ const client = f . client ( ) ;
853+ const root = await client . attach ( ) ;
854+ const name = "failedBinding" ;
855+ if ( method === "Runtime.removeBinding" ) {
856+ await client . request ( "Runtime.addBinding" , root , { name } ) ;
857+ }
858+ f . hold ( method ) ;
859+ const pending = client . send ( method , root , { name } ) ;
860+ await flush ( ) ;
861+ f . extension . handlers . onMessage (
862+ JSON . stringify ( {
863+ type : "error" ,
864+ seq : f . commands ( method ) . at ( - 1 ) ! . seq ,
865+ message : "native binding command failed" ,
866+ } ) ,
867+ ) ;
868+ await flush ( ) ;
869+ expect ( client . socket . frames ( ) . find ( ( frame ) => frame . id === pending ) ?. error ) . toBeDefined ( ) ;
870+ f . release ( ) ;
871+ f . event ( "Runtime.bindingCalled" , { name, payload : "failure" , executionContextId : 1 } ) ;
872+ expect (
873+ client . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
874+ ) . toHaveLength ( method === "Runtime.removeBinding" ? 1 : 0 ) ;
875+ expect ( ( await client . request ( method , root , { name } ) ) . error ) . toBeUndefined ( ) ;
876+ f . event ( "Runtime.bindingCalled" , { name, payload : "retry" , executionContextId : 1 } ) ;
877+ expect (
878+ client . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
879+ ) . toHaveLength ( 1 ) ;
880+ } ,
881+ ) ;
882+
883+ it ( "keeps a replacement physical session independent of pending retired bindings" , async ( ) => {
884+ const f = fixture ( ) ;
885+ const client = f . client ( ) ;
886+ const root = await client . attach ( ) ;
887+ const name = "retiredBinding" ;
888+ f . hold ( "Runtime.addBinding" ) ;
889+ client . send ( "Runtime.addBinding" , root , { name } ) ;
890+ await flush ( ) ;
891+ f . extension . handlers . onMessage (
892+ JSON . stringify ( { type : "detached" , tabId : 1 , reason : "revoked" } ) ,
893+ ) ;
894+ f . release ( ) ;
895+ await flush ( ) ;
896+ const next = await client . attach ( ) ;
897+ expect ( next ) . not . toBe ( root ) ;
898+ const params = { name, payload : "replacement" , executionContextId : 11 } ;
899+ f . event ( "Runtime.bindingCalled" , params ) ;
900+ expect (
901+ client . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
902+ ) . toEqual ( [ ] ) ;
903+ await client . request ( "Runtime.addBinding" , next , { name } ) ;
904+ f . event ( "Runtime.bindingCalled" , params ) ;
905+ expect (
906+ client . socket . frames ( ) . filter ( ( frame ) => frame . method === "Runtime.bindingCalled" ) ,
907+ ) . toEqual ( [ { sessionId : next , method : "Runtime.bindingCalled" , params } ] ) ;
908+ expect ( f . commands ( "Runtime.removeBinding" ) ) . toHaveLength ( 0 ) ;
909+ } ) ;
910+
764911 it ( "keeps a concurrent admission alive when another enable for the same logical owner fails" , async ( ) => {
765912 const f = fixture ( ) ;
766913 const c = f . client ( ) ;
0 commit comments