Skip to content

Commit 7005961

Browse files
committed
wip
1 parent 2138296 commit 7005961

File tree

5 files changed

+293
-3
lines changed

5 files changed

+293
-3
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import client from "../twilioClient"
22

33
import retry from 'async-retry'
4+
import { retryConfig } from "../config/retry.config";
45

5-
export const deleteConversation = async (conversationSid: string) : Promise<boolean> => {
6+
export const deleteConversation = async (conversationSid: string, retryOptions = retryConfig) : Promise<boolean> => {
67
return retry(async(quit) => {
78
try {
89
await client.conversations.conversations(conversationSid).remove()
910
return true
1011
} catch (err) {
1112
if (err.status !== 429) {
12-
quit(new Error(err));
13+
console.log('Quit without retry')
14+
console.log(err)
15+
quit(new Error('Quit without retry'));
1316
return;
1417
}
1518

1619
console.log('Re-trying on 429 error');
1720
throw new Error(err);
1821
}
19-
})
22+
}, retryOptions)
2023
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { addParticipant } from "../../src/utils";
2+
import client from '../../src/twilioClient';
3+
4+
import { ParticipantInstance, ParticipantListInstanceCreateOptions } from "twilio/lib/rest/conversations/v1/conversation/participant";
5+
6+
7+
jest.mock('../../src/twilioClient')
8+
let mockedClient = jest.mocked(client, true)
9+
10+
const mockParticipant: Partial<ParticipantListInstanceCreateOptions> = {
11+
identity: "+1234",
12+
}
13+
14+
describe('addParticipant util', () => {
15+
beforeEach(() => {
16+
jest.resetAllMocks()
17+
})
18+
19+
20+
it('it adds participant to conversation', async () => {
21+
const createSpy = jest.fn((options) => { return options })
22+
mockedClient['conversations'].conversations('123').participants = {
23+
participants:{
24+
create: (opt) => createSpy(opt)
25+
}
26+
} as any
27+
// mockedClient['conversations'] = {
28+
// conversations: (opt1) => {
29+
// fetch: (options) => {return options},
30+
// participants: {
31+
// create: (options) => jest.fn().mockResolvedValue({...mockParticipant})
32+
// }
33+
// }
34+
// } as any
35+
36+
// mockedClient['conversations'].conversations().
37+
38+
const result = await addParticipant("+1234", mockParticipant );
39+
expect(createSpy).toBeCalledWith("+1234", mockParticipant);
40+
expect(result).not.toBeNull();
41+
42+
})
43+
44+
45+
46+
})
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { createConversation } from "../../src/utils";
2+
import client from '../../src/twilioClient'
3+
4+
jest.mock('../../src/twilioClient')
5+
let mockedClient = jest.mocked(client, true)
6+
7+
describe('createConversation util', () => {
8+
beforeEach(() => {
9+
jest.resetAllMocks()
10+
})
11+
12+
it('it creates conversation with options passed', async () => {
13+
const createSpy = jest.fn((options) => { return options })
14+
mockedClient['conversations'] = {
15+
conversations: {
16+
create: (options) => createSpy(options)
17+
}
18+
} as any
19+
20+
const result = await createConversation({ friendlyName: "my conversation", addresses: ['1', '2'] })
21+
22+
expect(createSpy).toBeCalledWith({ friendlyName: "my conversation", addresses: ['1', '2'] })
23+
expect(result).toEqual({ friendlyName: "my conversation", addresses: ['1', '2'] })
24+
})
25+
26+
it('calls quit if error is not a 429 retry', async () => {
27+
mockedClient['conversations'] = {
28+
conversations: {
29+
create: (options) => {
30+
throw new Error('Twilio Problem')
31+
}
32+
}
33+
} as any
34+
35+
const consoleSpy = jest.spyOn(console, 'log');
36+
37+
try {
38+
await createConversation({ friendlyName: "my conversation", addresses: ['1', '2'] });
39+
} catch (e) {
40+
expect(consoleSpy).toHaveBeenCalledWith('Quit without retry');
41+
}
42+
})
43+
44+
it('throws error to retry on 429 status code', async () => {
45+
46+
interface TwilioError extends Error {
47+
status: number
48+
}
49+
50+
class TwilioError extends Error {
51+
constructor(message) {
52+
super(message);
53+
this.name = "ConcurrencyLimit";
54+
this.status = 429
55+
}
56+
}
57+
58+
mockedClient['conversations'] = {
59+
conversations: {
60+
create: (options) => {
61+
throw new TwilioError('Too many requests')
62+
}
63+
}
64+
} as any
65+
66+
const consoleSpy = jest.spyOn(console, 'log');
67+
68+
try {
69+
await createConversation(
70+
{ friendlyName: "my conversation", addresses: ['1', '2']},
71+
{ retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
72+
} catch (e) {
73+
expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
74+
}
75+
})
76+
})
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { deleteConversation } from "../../src/utils";
2+
import client from '../../src/twilioClient'
3+
4+
jest.mock('../../src/twilioClient')
5+
let mockedClient = jest.mocked(client, true)
6+
7+
describe('deleteConversation util', () => {
8+
beforeEach(() => {
9+
jest.resetAllMocks()
10+
})
11+
12+
it('it deletes conversation', async () => {
13+
const createSpy = jest.fn((options) => { return options })
14+
mockedClient['conversations'] = {
15+
conversations: {
16+
conversations: (options) => {
17+
remove: createSpy(options)
18+
}
19+
}
20+
} as any
21+
22+
23+
const result = await deleteConversation("myConversationSid")
24+
expect(createSpy).toBeCalledWith("myConversationSid")
25+
//expect(result).toEqual(true)
26+
})
27+
28+
// it('calls quit if error is not a 429 retry', async () => {
29+
// mockedClient['conversations'] = {
30+
// conversations: {
31+
// conversation: (options) =>{
32+
// remove: (options) => {
33+
// throw new Error('Twilio Problem')
34+
// }
35+
// }
36+
// }
37+
// } as any
38+
39+
// const consoleSpy = jest.spyOn(console, 'log');
40+
41+
// try {
42+
// await deleteConversation("myConversationSid");
43+
// } catch (e) {
44+
// expect(consoleSpy).toHaveBeenCalledWith('Quit without retry');
45+
// }
46+
// })
47+
48+
49+
50+
// it('throws error to retry on 429 status code', async () => {
51+
52+
// interface TwilioError extends Error {
53+
// status: number
54+
// }
55+
56+
// class TwilioError extends Error {
57+
// constructor(message) {
58+
// super(message);
59+
// this.name = "ConcurrencyLimit";
60+
// this.status = 429
61+
// }
62+
// }
63+
64+
// mockedClient['conversations'] = {
65+
// conversations: {
66+
// delete: (options) => {
67+
// throw new TwilioError('Too many requests')
68+
// }
69+
// }
70+
// } as any
71+
72+
// const consoleSpy = jest.spyOn(console, 'log');
73+
74+
// try {
75+
// await deleteConversation(
76+
// "myConversationSid",
77+
// { retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
78+
// } catch (e) {
79+
// expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
80+
// }
81+
// })
82+
83+
84+
85+
})
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { listParticipantConversations } from "../../src/utils";
2+
import client from '../../src/twilioClient';
3+
import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v1/participantConversation'
4+
5+
jest.mock('../../src/twilioClient')
6+
let mockedClient = jest.mocked(client, true)
7+
8+
describe('ListParticipantConversations util', () => {
9+
beforeEach(() => {
10+
jest.resetAllMocks()
11+
})
12+
13+
it('it lists conversations based on participant number', async () => {
14+
const createSpy = jest.fn((options) => { return options })
15+
mockedClient['conversations'] = {
16+
participantConversations: {
17+
list: (options) => createSpy(options)
18+
}
19+
} as any
20+
21+
const result = await listParticipantConversations("+1234");
22+
expect(createSpy).toBeCalledWith({"address":"+1234"});
23+
expect(result).not.toBeNull();
24+
// ToDo: add proper mocked response with ParticipantConversationInstance
25+
})
26+
27+
28+
it('calls quit if error is not a 429 retry', async () => {
29+
mockedClient['conversations'] = {
30+
participantConversations: {
31+
list: (options) => {
32+
throw new Error('Twilio Problem')
33+
}
34+
}
35+
} as any
36+
37+
const consoleSpy = jest.spyOn(console, 'log');
38+
39+
try {
40+
await listParticipantConversations("+1234");
41+
} catch (e) {
42+
expect(consoleSpy).toHaveBeenCalledWith('Quit without retry');
43+
}
44+
})
45+
46+
47+
it('throws error to retry on 429 status code', async () => {
48+
49+
interface TwilioError extends Error {
50+
status: number
51+
}
52+
53+
class TwilioError extends Error {
54+
constructor(message) {
55+
super(message);
56+
this.name = "ConcurrencyLimit";
57+
this.status = 429
58+
}
59+
}
60+
61+
mockedClient['conversations'] = {
62+
participantConversations: {
63+
list: (options) => {
64+
throw new TwilioError('Too many requests')
65+
}
66+
}
67+
} as any
68+
69+
const consoleSpy = jest.spyOn(console, 'log');
70+
71+
try {
72+
await listParticipantConversations(
73+
"+1234",
74+
{ retries: 0, factor: 1, maxTimeout: 0, minTimeout: 0 });
75+
} catch (e) {
76+
expect(consoleSpy).toHaveBeenCalledWith('Re-trying on 429 error');
77+
}
78+
})
79+
80+
})

0 commit comments

Comments
 (0)