11import { getConversationByAddressPair } from "../../src/utils" ;
22import client from '../../src/twilioClient' ;
3- import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation' ;
43
54jest . mock ( '../../src/twilioClient' )
65let mockedClient = jest . mocked ( client , true )
@@ -10,28 +9,88 @@ describe('GetConversationByAddressPair util', () => {
109 jest . resetAllMocks ( )
1110 } )
1211
13- it ( 'it gets Conversation by address pair' , async ( ) => {
12+ it ( 'it gets Conversation by address pair, excluding closed conversations ' , async ( ) => {
1413 const mockParticipants = [ {
1514 conversationState : "closed" ,
16- conversationSid : "myConversationSid" ,
15+ conversationSid : "myClosedConversationSid" ,
16+ participantMessagingBinding : {
17+ proxy_address : "+0000"
18+ }
19+ } , {
20+ conversationState : "open" ,
21+ conversationSid : "myClosedConversationSid" ,
22+ participantMessagingBinding : {
23+ proxy_address : "+0000"
24+ }
25+ } , {
26+ conversationState : "open" ,
27+ conversationSid : "myOpenConversationSid" ,
1728 participantMessagingBinding : {
1829 proxy_address : "+5678"
1930 }
2031 } ]
2132
22- const createSpy = jest . fn ( ( options ) => { return mockParticipants } )
33+ const listSpy = jest . fn ( ( options ) => { return mockParticipants } )
2334 mockedClient [ 'conversations' ] = {
2435 v1 :{
2536 participantConversations : {
26- list : ( options ) => createSpy ( options )
37+ list : ( options ) => listSpy ( options )
2738 }
2839 }
2940 } as any
3041
3142 const result = await getConversationByAddressPair ( "+1234" , "+5678" ) ;
32- expect ( createSpy ) . toBeCalledWith ( { "address" :"+1234" } ) ;
33- expect ( result ) . not . toBeNull ( ) ;
43+ expect ( listSpy ) . toBeCalledWith ( { "address" :"+1234" } ) ;
44+ expect ( result ) . toEqual ( { conversationSid : "myOpenConversationSid" , conversationState : "open" , participantMessagingBinding : { proxy_address : "+5678" } } ) ;
3445 // ToDo: add proper mocked response with ParticipantConversationInstance
3546 } )
47+
48+ it ( "should throw error if Twilio client throws" , async ( ) => {
49+ const createSpy = jest . fn ( ( options ) => { throw new Error ( 'Twilio Problem' ) } )
50+ mockedClient [ 'conversations' ] = {
51+ v1 :{
52+ participantConversations : {
53+ list : ( options ) => createSpy ( options )
54+ }
55+ }
56+ } as any
57+
58+ await expect ( getConversationByAddressPair ( "bad input" , "worse input" ) )
59+ . rejects
60+ . toThrowError ( 'Twilio Proble' )
61+ } )
62+
63+ it ( "should retry if Twilio error is 429" , async ( ) => {
64+
65+ interface TwilioError extends Error {
66+ status : number
67+ }
68+
69+ class TwilioError extends Error {
70+ constructor ( message ) {
71+ super ( message ) ;
72+ this . name = "ConcurrencyLimit" ;
73+ this . status = 429
74+ }
75+ }
76+
77+ mockedClient [ 'conversations' ] = {
78+ v1 :{
79+ participantConversations : {
80+ list : ( options ) => {
81+ throw new TwilioError ( 'Too many requests' )
82+ }
83+ }
84+ }
85+ } as any
86+
87+ const consoleSpy = jest . spyOn ( console , 'log' ) ;
88+
89+ try {
90+ await getConversationByAddressPair ( "+1234" , "+5678" , { retries : 0 , factor : 1 , maxTimeout : 0 , minTimeout : 0 } ) ;
91+ } catch ( e ) {
92+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Re-trying on 429 error' ) ;
93+ }
94+ } )
3695} )
3796
0 commit comments