@@ -477,15 +477,59 @@ describe('IBClient', () => {
477477 } ) ;
478478
479479 describe ( 'getOrders' , ( ) => {
480- it ( 'should fetch all orders' , async ( ) => {
480+ it ( 'should fetch orders for all discovered trading accounts' , async ( ) => {
481+ const mockClient = vi . mocked ( axios . create ) . mock . results [ 0 ] . value ;
482+ const firstAccountOrders = [ { orderId : '123' , status : 'Filled' } ] ;
483+ const secondAccountOrders = [ { orderId : '456' , status : 'Submitted' } ] ;
484+
485+ mockClient . get
486+ . mockResolvedValueOnce ( { data : { accounts : [ 'U12345' , { accountId : 'U67890' } ] , selectedAccount : 'U12345' } } )
487+ . mockResolvedValueOnce ( { data : { orders : firstAccountOrders } } )
488+ . mockResolvedValueOnce ( { data : { orders : secondAccountOrders } } ) ;
489+
490+ const result = await client . getOrders ( ) ;
491+
492+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 1 , '/iserver/accounts' ) ;
493+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 2 , '/iserver/account/orders' , { params : { accountId : 'U12345' } } ) ;
494+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 3 , '/iserver/account/orders' , { params : { accountId : 'U67890' } } ) ;
495+ expect ( result . orders ) . toEqual ( [ ...firstAccountOrders , ...secondAccountOrders ] ) ;
496+ expect ( result . accountResults ) . toEqual ( [
497+ { accountId : 'U12345' , data : { orders : firstAccountOrders } } ,
498+ { accountId : 'U67890' , data : { orders : secondAccountOrders } } ,
499+ ] ) ;
500+ } ) ;
501+
502+ it ( 'should fall back to portfolio accounts when iserver account discovery fails' , async ( ) => {
481503 const mockClient = vi . mocked ( axios . create ) . mock . results [ 0 ] . value ;
482504 const mockOrders = [ { orderId : '123' , status : 'Filled' } ] ;
483505
484- mockClient . get . mockResolvedValueOnce ( { data : mockOrders } ) ;
506+ mockClient . get
507+ . mockRejectedValueOnce ( new Error ( 'iserver accounts unavailable' ) )
508+ . mockResolvedValueOnce ( { data : [ { id : 'U12345' } ] } )
509+ . mockResolvedValueOnce ( { data : { orders : mockOrders } } ) ;
510+
511+ const result = await client . getOrders ( ) ;
512+
513+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 1 , '/iserver/accounts' ) ;
514+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 2 , '/portfolio/accounts' ) ;
515+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 3 , '/iserver/account/orders' , { params : { accountId : 'U12345' } } ) ;
516+ expect ( result . orders ) . toEqual ( mockOrders ) ;
517+ } ) ;
518+
519+ it ( 'should fall back to an unscoped orders request when account discovery returns no accounts' , async ( ) => {
520+ const mockClient = vi . mocked ( axios . create ) . mock . results [ 0 ] . value ;
521+ const mockOrders = [ { orderId : '123' , status : 'Filled' } ] ;
522+
523+ mockClient . get
524+ . mockResolvedValueOnce ( { data : { accounts : [ ] } } )
525+ . mockResolvedValueOnce ( { data : [ ] } )
526+ . mockResolvedValueOnce ( { data : mockOrders } ) ;
485527
486528 const result = await client . getOrders ( ) ;
487529
488- expect ( mockClient . get ) . toHaveBeenCalledWith ( '/iserver/account/orders' , { params : { } } ) ;
530+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 1 , '/iserver/accounts' ) ;
531+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 2 , '/portfolio/accounts' ) ;
532+ expect ( mockClient . get ) . toHaveBeenNthCalledWith ( 3 , '/iserver/account/orders' , { params : { } } ) ;
489533 expect ( result ) . toEqual ( mockOrders ) ;
490534 } ) ;
491535
0 commit comments