Skip to content

Commit

Permalink
Fix issue with Call.connect and inbound calls + e2e voice connect/d…
Browse files Browse the repository at this point in the history
…etectDigit (#531)

* fix issue of missing tag for inbound calls

* add e2e test for connect and detectDigits

* add changeset

* add missing env
  • Loading branch information
framini authored May 6, 2022
1 parent d42824a commit 9e6ad45
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-apes-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/realtime-api': patch
---

Fix issue with `Call.connect` and inbound calls
2 changes: 2 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ steps:
from_secret: VOICE_DIAL_FROM_NUMBER
VOICE_DIAL_TO_NUMBER:
from_secret: VOICE_DIAL_TO_NUMBER
VOICE_DIAL_CONNECT_TO_NUMBER:
from_secret: VOICE_DIAL_CONNECT_TO_NUMBER

trigger:
event: push
59 changes: 56 additions & 3 deletions internal/e2e-realtime-api/src/voice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ const handler = () => {
project: process.env.VOICE_PROJECT as string,
token: process.env.VOICE_TOKEN as string,
contexts: [process.env.VOICE_CONTEXT as string],
// logLevel: "trace",
// debug: {
// logWsTraffic: true,
// },
})

let callsReceived = new Set()
client.on('call.received', async (call) => {
console.log('Got call', call.id, call.from, call.to, call.direction)
callsReceived.add(call.id)
console.log(
`Got call number: ${callsReceived.size}`,
call.id,
call.from,
call.to,
call.direction
)

try {
const resultAnswer = await call.answer()
Expand All @@ -32,6 +41,13 @@ const handler = () => {
'Call answered gets the same instance'
)

if (callsReceived.size === 2) {
await sleep()
console.log(`Sending digits from call: ${call.id}`)
await call.sendDigits('1#')
return
}

const recording = await call.recordAudio()
tap.ok(recording.id, 'Recording started')

Expand Down Expand Up @@ -73,7 +89,44 @@ const handler = () => {
tap.equal(prompt.id, result.id, 'Instances are the same')
tap.equal(result.digits, '123', 'Correct Digits were entered')

console.log('Finishing the call.')
console.log(
`Connecting ${process.env.VOICE_DIAL_FROM_NUMBER} to ${process.env.VOICE_DIAL_CONNECT_TO_NUMBER}`
)
const peer = await call.connect({
devices: new Voice.DeviceBuilder().add(
Voice.DeviceBuilder.Phone({
from: process.env.VOICE_DIAL_FROM_NUMBER!,
to: process.env.VOICE_DIAL_CONNECT_TO_NUMBER!,
timeout: 30,
})
),
ringback: new Voice.Playlist().add(
Voice.Playlist.Ringtone({
name: 'it',
})
),
})

console.log('Peer:', peer.id, peer.type, peer.from, peer.to)
console.log('Main:', call.id, call.type, call.from, call.to)

const detector = await call.detectDigit({
digits: '1',
})

const resultDetector = await detector.waitForResult()

// TODO: update this once the backend can send us
// the actual result
tap.equal(
// @ts-expect-error
resultDetector.detect.params.event,
'finished',
'Detect digit is finished'
)

console.log('Finishing the calls.')
await peer.hangup()
await call.hangup()
} catch (error) {
console.error('Error', error)
Expand Down Expand Up @@ -116,7 +169,7 @@ async function main() {
const runner = createTestRunner({
name: 'Voice E2E',
testHandler: handler,
executionTime: 30_000,
executionTime: 60_000,
})

await runner.run()
Expand Down
20 changes: 18 additions & 2 deletions packages/realtime-api/src/voice/workers/voiceCallConnectWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,32 @@ export const voiceCallConnectWorker: SDKWorker<Call> = function* (
yield sagaEffects.take(swEventChannel, (action: SDKActions) => {
return (
action.type === 'calling.call.connect' &&
action.payload.tag === instance.tag
(action.payload.tag === instance.tag ||
/**
* This branch applies for Inbound calls that
* don't have a `tag` at the payload's root
* level.
*/
action.payload.peer?.tag === instance.tag)
)
})

/**
* Add `tag` to the payload to allow pubSubSaga to match
* it with the Call namespace
*/
const payloadWithTag = {
// @ts-expect-error
tag: instance.tag,
...action.payload,
}

/**
* Dispatch public events for each connect_state
*/
yield sagaEffects.put(pubSubChannel, {
type: `calling.connect.${action.payload.connect_state}`,
payload: action.payload,
payload: payloadWithTag,
})

switch (action.payload.connect_state) {
Expand Down

0 comments on commit 9e6ad45

Please sign in to comment.