@@ -173,6 +173,69 @@ describe('POST /api/:coin/wallet/:walletId/consolidateunspents', () => {
173173 sinon . assert . calledOnce ( consolidateUnspentsStub ) ;
174174 } ) ;
175175
176+ it ( 'should handle array result from consolidateUnspents and return first element' , async ( ) => {
177+ const walletGetNock = nock ( bitgoApiUrl )
178+ . get ( `/api/v2/${ coin } /wallet/${ walletId } ` )
179+ . matchHeader ( 'authorization' , `Bearer ${ accessToken } ` )
180+ . reply ( 200 , mockWalletData ) ;
181+
182+ const keychainGetNock = nock ( bitgoApiUrl )
183+ . get ( `/api/v2/${ coin } /key/user-key-id` )
184+ . matchHeader ( 'authorization' , `Bearer ${ accessToken } ` )
185+ . reply ( 200 , mockUserKeychain ) ;
186+
187+ const mockArrayResult = [
188+ {
189+ transfer : {
190+ entries : [
191+ { address : 'tb1qu...' , value : - 4000 } ,
192+ { address : 'tb1qle...' , value : - 4000 } ,
193+ { address : 'tb1qtw...' , value : 2714 , isChange : true } ,
194+ ] ,
195+ id : 'first-transfer-id' ,
196+ coin : 'tbtc' ,
197+ wallet : '685abbf19ca95b79f88e0b41d9337109' ,
198+ txid : 'first-tx-id' ,
199+ status : 'signed' ,
200+ } ,
201+ txid : 'first-tx-id' ,
202+ tx : '01000000000102first...' ,
203+ status : 'signed' ,
204+ } ,
205+ ] ;
206+
207+ const consolidateUnspentsStub = sinon
208+ . stub ( Wallet . prototype , 'consolidateUnspents' )
209+ . resolves ( mockArrayResult ) ;
210+
211+ const requestPayload = {
212+ pubkey : mockUserKeychain . pub ,
213+ source : 'user' as const ,
214+ feeRate : 1000 ,
215+ bulk : true ,
216+ } ;
217+
218+ const response = await agent
219+ . post ( `/api/${ coin } /wallet/${ walletId } /consolidateunspents` )
220+ . set ( 'Authorization' , `Bearer ${ accessToken } ` )
221+ . send ( requestPayload ) ;
222+
223+ response . status . should . equal ( 200 ) ;
224+ // Should return only the first element from the array
225+ response . body . should . have . property ( 'transfer' ) ;
226+ response . body . should . have . property ( 'txid' , 'first-tx-id' ) ;
227+ response . body . should . have . property ( 'tx' , '01000000000102first...' ) ;
228+ response . body . should . have . property ( 'status' , 'signed' ) ;
229+ response . body . transfer . should . have . property ( 'id' , 'first-transfer-id' ) ;
230+ response . body . transfer . should . have . property ( 'txid' , 'first-tx-id' ) ;
231+ response . body . transfer . should . have . property ( 'status' , 'signed' ) ;
232+ response . body . transfer . should . have . property ( 'entries' ) . which . is . Array ( ) ;
233+
234+ walletGetNock . done ( ) ;
235+ keychainGetNock . done ( ) ;
236+ sinon . assert . calledOnce ( consolidateUnspentsStub ) ;
237+ } ) ;
238+
176239 it ( 'should succeed in consolidating unspents with all optional parameters' , async ( ) => {
177240 const walletGetNock = nock ( bitgoApiUrl )
178241 . get ( `/api/v2/${ coin } /wallet/${ walletId } ` )
0 commit comments