|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import Device from '../../../lib/twilio/device'; |
| 4 | +import type Call from '../../../lib/twilio/call'; |
| 5 | +import { generateAccessToken } from '../../lib/token'; |
| 6 | +import { expectEvent } from '../../lib/util'; |
| 7 | +const env = require('../../env'); |
| 8 | + |
| 9 | +function waitFor(n: number, reject?: boolean) { |
| 10 | + return new Promise((res, rej) => setTimeout(reject ? rej : res, n)); |
| 11 | +} |
| 12 | + |
| 13 | +describe('callProgressEvent', function() { |
| 14 | + let teardown: () => void; |
| 15 | + |
| 16 | + this.timeout(1000 * 60 * 10); // 10 minute timeout for the whole suite |
| 17 | + |
| 18 | + const setup = async (aliceOptions: any, bobOptions: any, tokenTtl = 180) => { |
| 19 | + const aliceId = `client-id-call-message-tests-alice-${Date.now()}`; |
| 20 | + const aliceToken = generateAccessToken(aliceId, tokenTtl, env.appSid); |
| 21 | + const aliceDevice = new Device(aliceToken, aliceOptions); |
| 22 | + |
| 23 | + const bobId = `client-id-call-message-tests-bob-${Date.now()}`; |
| 24 | + const bobToken = generateAccessToken(bobId, tokenTtl, env.appSid); |
| 25 | + const bobDevice = new Device(bobToken, bobOptions); |
| 26 | + |
| 27 | + teardown = () => { |
| 28 | + aliceDevice.destroy(); |
| 29 | + bobDevice.destroy(); |
| 30 | + }; |
| 31 | + |
| 32 | + await bobDevice.register(); |
| 33 | + |
| 34 | + const bobCallPromise: Promise<Call> = expectEvent( |
| 35 | + Device.EventName.Incoming, |
| 36 | + bobDevice, |
| 37 | + ); |
| 38 | + |
| 39 | + const aliceCall = await aliceDevice.connect({ params: { To: bobId } }); |
| 40 | + const bobCall = await bobCallPromise; |
| 41 | + |
| 42 | + const aliceMessageReceivedSpy = sinon.spy(); |
| 43 | + const bobMessageReceivedSpy = sinon.spy(); |
| 44 | + |
| 45 | + aliceCall.on('messageReceived', aliceMessageReceivedSpy); |
| 46 | + bobCall.on('messageReceived', bobMessageReceivedSpy); |
| 47 | + |
| 48 | + const aliceCallAcceptPromise = expectEvent('accept', aliceCall); |
| 49 | + const bobCallAcceptPromise = expectEvent('accept', bobCall); |
| 50 | + |
| 51 | + bobCall.accept(); |
| 52 | + |
| 53 | + await aliceCallAcceptPromise; |
| 54 | + await bobCallAcceptPromise; |
| 55 | + |
| 56 | + await waitFor(5000); |
| 57 | + |
| 58 | + return { |
| 59 | + aliceDevice, |
| 60 | + bobDevice, |
| 61 | + aliceCall, |
| 62 | + bobCall, |
| 63 | + aliceMessageReceivedSpy, |
| 64 | + bobMessageReceivedSpy, |
| 65 | + }; |
| 66 | + }; |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + teardown = () => {}; |
| 70 | + }); |
| 71 | + |
| 72 | + afterEach(() => { |
| 73 | + teardown?.(); |
| 74 | + }); |
| 75 | + |
| 76 | + // NOTE(mhuynh): Once backend changes are done to facilitate call message |
| 77 | + // event type filtering, we can re-enable this test. |
| 78 | + // See VBLOCKS-3332 |
| 79 | + it.skip( |
| 80 | + 'does not receive call progress events', |
| 81 | + async function() { |
| 82 | + const callMessageEvents = []; |
| 83 | + const deviceOptions = { callMessageEvents }; |
| 84 | + |
| 85 | + const { aliceMessageReceivedSpy, bobMessageReceivedSpy } = await setup( |
| 86 | + deviceOptions, |
| 87 | + deviceOptions, |
| 88 | + ); |
| 89 | + |
| 90 | + sinon.assert.notCalled(aliceMessageReceivedSpy); |
| 91 | + sinon.assert.notCalled(bobMessageReceivedSpy); |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + it( |
| 96 | + 'receives call progress events', |
| 97 | + async function() { |
| 98 | + const callMessageEvents = ['call-progress-event']; |
| 99 | + const deviceOptions = { callMessageEvents }; |
| 100 | + |
| 101 | + const { aliceCall, aliceMessageReceivedSpy, bobMessageReceivedSpy } = |
| 102 | + await setup(deviceOptions, deviceOptions); |
| 103 | + |
| 104 | + aliceCall.disconnect(); |
| 105 | + |
| 106 | + await waitFor(5000); |
| 107 | + |
| 108 | + const expectedCallProgressEvents = |
| 109 | + ['ringing', 'initiated', 'in-progress']; |
| 110 | + |
| 111 | + const actualCallProgressEvents: any[] = []; |
| 112 | + |
| 113 | + for (const arg of aliceMessageReceivedSpy.args) { |
| 114 | + assert.strictEqual(arg.length, 1); |
| 115 | + |
| 116 | + const { |
| 117 | + content: { |
| 118 | + ParentCallSid, |
| 119 | + CallType, |
| 120 | + CallStatus, |
| 121 | + CallSid, |
| 122 | + }, |
| 123 | + contentType, |
| 124 | + messageType, |
| 125 | + voiceEventSid |
| 126 | + } = arg[0]; |
| 127 | + |
| 128 | + assert.deepStrictEqual(CallType, 'CLIENT'); |
| 129 | + assert.deepStrictEqual(contentType, 'application/json'); |
| 130 | + assert.deepStrictEqual(messageType, 'call-progress-event'); |
| 131 | + |
| 132 | + actualCallProgressEvents.push(CallStatus); |
| 133 | + } |
| 134 | + |
| 135 | + assert.deepStrictEqual( |
| 136 | + actualCallProgressEvents, |
| 137 | + expectedCallProgressEvents, |
| 138 | + ); |
| 139 | + }, |
| 140 | + ); |
| 141 | +}); |
0 commit comments