1+ import { addParticipant } from '../../src/utils/addParticipant.util'
2+ import { mockParticpantInstance } from '../support/testSupport'
3+ import * as retry from '../../src/utils/retryAddParticipant.util'
4+
5+ jest . mock ( '../../src/utils/addParticipant.util' )
6+
7+
8+ describe ( 'retryParticipantAdd' , ( ) => {
9+ it ( 'throws if no proxy addresses are left' , async ( ) => {
10+ await expect ( retry . retryAddParticipant ( 'CH1234' , '+111' , [ ] ) )
11+ . rejects
12+ . toThrowError ( 'No available proxy addresses for +111' )
13+ } )
14+
15+ it ( 'calls itself again when 50416 is thrown to try another address' , async ( ) => {
16+ const mockAddParticipant = jest . mocked ( addParticipant , true )
17+
18+ interface TwilioError extends Error {
19+ code : number
20+ }
21+
22+ class TwilioError extends Error {
23+ constructor ( message ) {
24+ super ( message ) ;
25+ this . name = "ConcurrencyLimit" ;
26+ this . code = 50416
27+ }
28+ }
29+
30+ const retrySpy = jest . spyOn ( retry , 'retryAddParticipant' )
31+
32+ const rejectedValue = new TwilioError ( 'Number already in use' )
33+
34+ mockAddParticipant
35+ . mockRejectedValueOnce ( rejectedValue )
36+ . mockResolvedValueOnce ( mockParticpantInstance as any )
37+ // .mockResolvedValueOnce(mockParticpantInstance as any)
38+
39+ const result = await retry . retryAddParticipant ( 'CH1234' , '+111' , [ '+222' , '+333' ] )
40+ expect ( retrySpy ) . toBeCalledTimes ( 1 )
41+ expect ( mockAddParticipant ) . nthCalledWith ( 1 , 'CH1234' , {
42+ 'messagingBinding.address' : '+111' ,
43+ 'messagingBinding.proxyAddress' : '+222'
44+ } )
45+ expect ( mockAddParticipant ) . nthCalledWith ( 2 , 'CH1234' , {
46+ 'messagingBinding.address' : '+111' ,
47+ 'messagingBinding.proxyAddress' : '+333'
48+ } )
49+ expect ( mockAddParticipant ) . toBeCalledTimes ( 2 )
50+ expect ( result ) . toEqual ( mockParticpantInstance )
51+ } )
52+
53+ it ( 'throws an error if client err code is not 50416' , async ( ) => {
54+ const mockAddParticipant = jest . mocked ( addParticipant , true )
55+
56+ const rejectedValue = new Error ( 'Twilio Problem' )
57+ mockAddParticipant
58+ . mockRejectedValue ( rejectedValue )
59+
60+ debugger
61+ await expect ( retry . retryAddParticipant ( 'CH1234' , '+111' , [ '+222' , '+333' ] ) )
62+ . rejects
63+ . toThrow ( 'Twilio Problem' )
64+ } )
65+ } )
0 commit comments