@@ -189,6 +189,36 @@ describe('AgentClient', () => {
189189 } ) ;
190190
191191 describe ( 'processContainer' , ( ) => {
192+ test ( 'should await emitContainerReport before resolving' , async ( ) => {
193+ let resolveEmit ;
194+ const emitPromise = new Promise < void > ( ( resolve ) => {
195+ resolveEmit = resolve ;
196+ } ) ;
197+ event . emitContainerReport . mockReturnValueOnce ( emitPromise ) ;
198+ storeContainer . getContainer . mockReturnValue ( undefined ) ;
199+ storeContainer . insertContainer . mockReturnValue ( { id : 'c1' , updateAvailable : true } ) ;
200+
201+ let resolved = false ;
202+ const processPromise = client . processContainer ( { id : 'c1' , name : 'test' } ) ;
203+ void processPromise . then ( ( ) => {
204+ resolved = true ;
205+ } ) ;
206+
207+ await Promise . resolve ( ) ;
208+
209+ expect ( event . emitContainerReport ) . toHaveBeenCalledWith (
210+ expect . objectContaining ( {
211+ container : expect . objectContaining ( { id : 'c1' } ) ,
212+ changed : true ,
213+ } ) ,
214+ ) ;
215+ expect ( resolved ) . toBe ( false ) ;
216+
217+ resolveEmit ( ) ;
218+ await processPromise ;
219+ expect ( resolved ) . toBe ( true ) ;
220+ } ) ;
221+
192222 test ( 'should insert new container and emit report with changed=true' , async ( ) => {
193223 storeContainer . getContainer . mockReturnValue ( undefined ) ;
194224 storeContainer . insertContainer . mockReturnValue ( { id : 'c1' , updateAvailable : false } ) ;
@@ -435,6 +465,54 @@ describe('AgentClient', () => {
435465 } ) ;
436466 } ) ;
437467
468+ describe ( 'processAuthoritativeContainers' , ( ) => {
469+ test ( 'should await emitContainerReports before resolving' , async ( ) => {
470+ let resolveEmit ;
471+ const emitPromise = new Promise < void > ( ( resolve ) => {
472+ resolveEmit = resolve ;
473+ } ) ;
474+ event . emitContainerReports . mockReturnValueOnce ( emitPromise ) ;
475+ storeContainer . getContainer . mockReturnValue ( undefined ) ;
476+ storeContainer . insertContainer . mockImplementation ( ( container ) => ( {
477+ ...container ,
478+ updateAvailable : true ,
479+ } ) ) ;
480+
481+ const internal = client as unknown as {
482+ processAuthoritativeContainers : (
483+ containers : Array < Record < string , unknown > > ,
484+ ) => Promise < unknown > ;
485+ } ;
486+
487+ let resolved = false ;
488+ const processPromise = internal . processAuthoritativeContainers ( [ { id : 'c1' , name : 'test' } ] ) ;
489+ void processPromise . then ( ( ) => {
490+ resolved = true ;
491+ } ) ;
492+
493+ await vi . waitFor ( ( ) =>
494+ expect ( event . emitContainerReports ) . toHaveBeenCalledWith ( [
495+ expect . objectContaining ( {
496+ container : expect . objectContaining ( { id : 'c1' } ) ,
497+ changed : true ,
498+ } ) ,
499+ ] ) ,
500+ ) ;
501+
502+ expect ( event . emitContainerReports ) . toHaveBeenCalledWith ( [
503+ expect . objectContaining ( {
504+ container : expect . objectContaining ( { id : 'c1' } ) ,
505+ changed : true ,
506+ } ) ,
507+ ] ) ;
508+ expect ( resolved ) . toBe ( false ) ;
509+
510+ resolveEmit ( ) ;
511+ await processPromise ;
512+ expect ( resolved ) . toBe ( true ) ;
513+ } ) ;
514+ } ) ;
515+
438516 describe ( 'handshake' , ( ) => {
439517 test ( 'should fetch containers, process them, and register components' , async ( ) => {
440518 const containers = [ { id : 'c1' } , { id : 'c2' } ] ;
0 commit comments