@@ -72,21 +72,55 @@ test('capture reuses one attachment and submits only the fixed read-only express
7272 const calls :any [ ] = [ ] ;
7373 const session = { send :async ( ...args :any [ ] ) => {
7474 calls . push ( args ) ;
75- if ( args [ 0 ] === 'Target.getTargets' ) return { result :{ targetInfos :[ { targetId :'game-worker' , type :'worker' , url :'http://localhost:5174/src/worker/emulator.worker.ts?worker_file&type=module' } ] } } ;
75+ if ( args [ 0 ] === 'Target.getTargetInfo' ) return { result :{ targetInfo :{ targetId :'game-page' , type :'page' , url :'http://localhost:5174/?game=dev' } } } ;
76+ if ( args [ 0 ] === 'Target.getTargets' ) return { result :{ targetInfos :[ { targetId :'game-worker' , parentId :'game-page' , type :'worker' , url :'http://localhost:5174/src/worker/emulator.worker.ts?worker_file&type=module' } ] } } ;
7677 if ( args [ 0 ] === 'Target.attachToTarget' ) return { result :{ sessionId :'worker-session' } } ;
7778 return { result :{ result :{ value :fixture ( ) } } } ;
7879 } } ;
7980 const capture = new CpuCapture ( ) ;
8081 await capture . capture ( session ) ; await capture . capture ( session ) ;
8182 expect ( calls . filter ( call => call [ 0 ] === 'Target.attachToTarget' ) ) . toHaveLength ( 1 ) ;
83+ expect ( calls . filter ( call => call [ 0 ] === 'Target.setAutoAttach' ) ) . toEqual ( [
84+ [ 'Target.setAutoAttach' , { autoAttach :true , waitForDebuggerOnStart :false , flatten :true } ] ] ) ;
8285 expect ( calls . filter ( call => call [ 0 ] === 'Target.setDiscoverTargets' ) ) . toHaveLength ( 1 ) ;
8386 expect ( calls . filter ( call => call [ 0 ] === 'Runtime.enable' ) ) . toEqual ( [ [ 'Runtime.enable' , { } , 'worker-session' ] ] ) ;
84- expect ( calls . slice ( 0 , 4 ) . map ( call => call [ 0 ] ) ) . toEqual ( [
85- 'Target.setDiscoverTargets' , 'Target.getTargets' , 'Target.attachToTarget' , 'Runtime.enable' ] ) ;
87+ expect ( calls . slice ( 0 , 6 ) . map ( call => call [ 0 ] ) ) . toEqual ( [
88+ 'Target.setAutoAttach' , 'Target. setDiscoverTargets' , 'Target.getTargetInfo ', 'Target.getTargets' , 'Target.attachToTarget' , 'Runtime.enable' ] ) ;
8689 const evaluations = calls . filter ( call => call [ 0 ] === 'Runtime.evaluate' ) ;
8790 expect ( evaluations ) . toHaveLength ( 2 ) ;
8891 for ( const call of evaluations ) {
8992 expect ( call [ 1 ] ) . toEqual ( { expression :CPU_FRAME_EXPRESSION , returnByValue :true , awaitPromise :false } ) ;
9093 expect ( call [ 2 ] ) . toBe ( 'worker-session' ) ;
9194 }
9295} ) ;
96+
97+ test ( 'fresh blank-URL worker attaches only through its current page parent and reset rediscovers' , async ( ) => {
98+ const attached :string [ ] = [ ] ;
99+ let generation = 1 ;
100+ const session = { send :async ( command :string , args :any ) => {
101+ if ( command === 'Target.getTargetInfo' ) return { result :{ targetInfo :{ targetId :'game-page' , type :'page' , url :'http://localhost:5174/?game=dev' } } } ;
102+ if ( command === 'Target.getTargets' ) return { result :{ targetInfos :[
103+ { targetId :`emulator-${ generation } ` , parentId :'game-page' , type :'worker' , url :'' } ,
104+ { targetId :'nested' , parentId :`emulator-${ generation } ` , type :'worker' , url :'' } ,
105+ { targetId :'other-tab-worker' , parentId :'other-page' , type :'worker' , url :'http://localhost:5174/src/worker/emulator.worker.ts?worker_file' } ,
106+ ] } } ;
107+ if ( command === 'Target.attachToTarget' ) { attached . push ( args . targetId ) ; return { result :{ sessionId :`session-${ generation } ` } } ; }
108+ return { result :{ result :{ value :fixture ( ) } } } ;
109+ } } ;
110+ const capture = new CpuCapture ( ) ;
111+ await capture . capture ( session ) ; generation = 2 ; capture . reset ( ) ; await capture . capture ( session ) ;
112+ expect ( attached ) . toEqual ( [ 'emulator-1' , 'emulator-2' ] ) ;
113+ } ) ;
114+
115+ test ( 'capture refuses ambiguous direct workers and non-BottleShip parent pages' , async ( ) => {
116+ let pageUrl = 'http://localhost:5174/?game=dev' ;
117+ const session = { send :async ( command :string ) => {
118+ if ( command === 'Target.getTargetInfo' ) return { result :{ targetInfo :{ targetId :'game-page' , type :'page' , url :pageUrl } } } ;
119+ if ( command === 'Target.getTargets' ) return { result :{ targetInfos :[ 1 , 2 ] . map ( i => ( { targetId :`w${ i } ` , parentId :'game-page' , type :'worker' , url :'' } ) ) } } ;
120+ if ( command === 'Target.attachToTarget' ) throw Error ( 'Must not attach to an ambiguous or unrelated worker' ) ;
121+ return { } ;
122+ } } ;
123+ await expect ( new CpuCapture ( ) . capture ( session ) ) . rejects . toThrow ( 'ambiguous' ) ;
124+ pageUrl = 'https://example.com/' ;
125+ await expect ( new CpuCapture ( ) . capture ( session ) ) . rejects . toThrow ( 'local BottleShip page' ) ;
126+ } ) ;
0 commit comments