Skip to content

Commit

Permalink
E2E test cases for Call Fabric (#902)
Browse files Browse the repository at this point in the history
* E2E test cases for Call Fabric

* call fabric client update

* relay application via call fabric

* expect the hangup on the relay app

* connect relay app via domain app

* expect a silence through relay app
  • Loading branch information
iAmmar7 authored Nov 7, 2023
1 parent 6063d4b commit cfa44a1
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 13 deletions.
219 changes: 219 additions & 0 deletions internal/e2e-js/tests/callfabric/relayApp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import { Voice } from '@signalwire/realtime-api'
import {
createCFClient,
expectPageReceiveAudio,
getAudioStats,
SERVER_URL,
} from '../../utils'
import { test, expect } from '../../fixtures'

test.describe('CallFabric Relay Application', () => {
test('should connect to the relay app and expect an audio playback', async ({
createCustomPage,
}) => {
const client = new Voice.Client({
host: process.env.RELAY_HOST,
project: process.env.CF_RELAY_PROJECT as string,
token: process.env.CF_RELAY_TOKEN as string,
topics: ['cf-e2e-test-relay'],
debug: {
logWsTraffic: true,
},
})

client.on('call.received', async (call) => {
try {
console.log('Call received', call.id)

await call.answer()
console.log('Inbound call answered')

const playback = await call.playAudio({
url: 'https://cdn.signalwire.com/default-music/welcome.mp3',
})
await playback.setVolume(10)
} catch (error) {
console.error('Inbound call error', error)
}
})

try {
const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

await createCFClient(page)

const resourceName = 'cf-e2e-test-relay'

await page.evaluate(
async (options) => {
// @ts-expect-error
const client = window._client

const call = await client.dial({
to: `/public/${options.resourceName}`,
nodeId: undefined,
})

// @ts-expect-error
window._roomObj = call

await call.start()
},
{
resourceName,
}
)

await expectPageReceiveAudio(page)

// Hangup the call
await page.evaluate(async () => {
// @ts-expect-error
const call = window._roomObj

await call.hangup()
})

client.disconnect()
} catch (error) {
console.error('CreateRoomSession Error', error)
}
})

test('should connect to the relay app and expect a silence', async ({
createCustomPage,
}) => {
const client = new Voice.Client({
host: process.env.RELAY_HOST,
project: process.env.CF_RELAY_PROJECT as string,
token: process.env.CF_RELAY_TOKEN as string,
topics: ['cf-e2e-test-relay'],
debug: {
logWsTraffic: true,
},
})

client.on('call.received', async (call) => {
try {
console.log('Call received', call.id)

await call.answer()
console.log('Inbound call answered')

const playback = await call.playSilence({ duration: 60 })
await playback.setVolume(10)
} catch (error) {
console.error('Inbound call error', error)
}
})

try {
const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

await createCFClient(page)

const resourceName = 'cf-e2e-test-relay'

await page.evaluate(
async (options) => {
// @ts-expect-error
const client = window._client

const call = await client.dial({
to: `/public/${options.resourceName}`,
nodeId: undefined,
})

// @ts-expect-error
window._roomObj = call

await call.start()
},
{
resourceName,
}
)

const audioStats = await getAudioStats(page)

expect(audioStats['inbound-rtp']['totalAudioEnergy']).toBeCloseTo(0.1, 0)

// Hangup the call
await page.evaluate(async () => {
// @ts-expect-error
const call = window._roomObj

await call.hangup()
})

client.disconnect()
} catch (error) {
console.error('CreateRoomSession Error', error)
}
})

test('should connect to the relay app and expect a hangup', async ({
createCustomPage,
}) => {
const client = new Voice.Client({
host: process.env.RELAY_HOST,
project: process.env.CF_RELAY_PROJECT as string,
token: process.env.CF_RELAY_TOKEN as string,
topics: ['cf-e2e-test-relay'],
debug: {
logWsTraffic: true,
},
})

client.on('call.received', async (call) => {
try {
console.log('Call received', call.id)

await call.answer()
console.log('Inbound call answered')

await call.hangup()
} catch (error) {
console.error('Inbound call error', error)
}
})

const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

const resourceName = 'cf-e2e-test-relay'

await createCFClient(page)

// Dial an address and listen a TTS
await page.evaluate(
async ({ resourceName }) => {
// @ts-expect-error
const client = window._client

const call = await client.dial({
to: `/public/${resourceName}`,
nodeId: undefined,
})

// @ts-expect-error
window._roomObj = call

await call.start()
},
{ resourceName }
)

await page.waitForTimeout(5000)

const roomSession = await page.evaluate(() => {
// @ts-expect-error
const roomObj = window._roomObj
return roomObj
})

expect(roomSession.state).toBe('destroy')
})
})
50 changes: 47 additions & 3 deletions internal/e2e-js/tests/callfabric/swml.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from '../../fixtures'
import { SERVER_URL, createSWClient, expectPageReceiveAudio } from '../../utils'
import { test, expect } from '../../fixtures'
import { SERVER_URL, createCFClient, expectPageReceiveAudio } from '../../utils'

test.describe('CallFabric SWML', () => {
test('should dial an address and expect a TTS audio', async ({
Expand All @@ -10,7 +10,7 @@ test.describe('CallFabric SWML', () => {

const resourceName = 'cf-e2e-test-tts'

await createSWClient(page)
await createCFClient(page)

// Dial an address and listen a TTS
await page.evaluate(
Expand Down Expand Up @@ -47,4 +47,48 @@ test.describe('CallFabric SWML', () => {
await call.hangup()
})
})

test('should dial an address and expect a hangup', async ({
createCustomPage,
}) => {
const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

const resourceName = 'cf-e2e-test-hangup'

await createCFClient(page)

// Dial an address and listen a TTS
await page.evaluate(
async ({ resourceName }) => {
return new Promise<any>(async (resolve, _reject) => {
// @ts-expect-error
const client = window._client

const call = await client.dial({
to: `/public/${resourceName}`,
nodeId: undefined,
})

// @ts-expect-error
window._roomObj = call

await call.start()

resolve(call)
})
},
{ resourceName }
)

await page.waitForTimeout(1000)

const roomSession = await page.evaluate(() => {
// @ts-expect-error
const roomObj = window._roomObj
return roomObj
})

expect(roomSession.state).toBe('destroy')
})
})
37 changes: 33 additions & 4 deletions internal/e2e-js/tests/callfabric/videoRoom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Video } from '@signalwire/js'
import { test, expect } from '../../fixtures'
import {
SERVER_URL,
createSWClient,
createCFClient,
expectLayoutChanged,
expectMCUVisible,
setLayoutOnPage,
Expand All @@ -17,7 +17,7 @@ test.describe('CallFabric VideoRoom', () => {

const roomName = 'cf-e2e-test-room'

await createSWClient(page)
await createCFClient(page)

// Dial an address and join a video room
const roomSession = await page.evaluate(
Expand All @@ -28,8 +28,6 @@ test.describe('CallFabric VideoRoom', () => {

const call = await client.dial({
to: `/public/${roomName}`,
logLevel: 'debug',
debug: { logWsTraffic: true },
nodeId: undefined,
})

Expand Down Expand Up @@ -199,4 +197,35 @@ test.describe('CallFabric VideoRoom', () => {
await setLayoutOnPage(page, layoutName)
expect(await layoutChangedPromise).toBe(true)
})

test('should fail on invalid address', async ({ createCustomPage }) => {
const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

await createCFClient(page)

// Dial an address and join a video room
const roomSession = await page.evaluate(async () => {
try {
// @ts-expect-error
const client = window._client

const call = await client.dial({
to: `/public/invalid-address`,
nodeId: undefined,
})

// @ts-expect-error
window._roomObj = call

await call.start()

return { success: true }
} catch (error) {
return { success: false, error }
}
})

expect(roomSession.success).toBe(false)
})
})
18 changes: 12 additions & 6 deletions internal/e2e-js/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const createTestRoomSessionWithJWT = async (
)
}

export const createSWClient = async (page: Page) => {
export const createCFClient = async (page: Page) => {
const sat = await createTestSATToken()
if (!sat) {
console.error('Invalid SAT. Exiting..')
Expand All @@ -204,7 +204,7 @@ export const createSWClient = async (page: Page) => {
return client
},
{
RELAY_HOST: process.env.CF_RELAY_HOST,
RELAY_HOST: process.env.RELAY_HOST,
API_TOKEN: sat,
}
)
Expand Down Expand Up @@ -436,10 +436,7 @@ export const expectMediaEvent = (page: Page, event: MediaEvent) => {
)
}

export const expectTotalAudioEnergyToBeGreaterThan = async (
page: Page,
value: number
) => {
export const getAudioStats = async (page: Page) => {
const audioStats = await page.evaluate(async () => {
// @ts-expect-error
const roomObj: Video.RoomSession = window._roomObj
Expand Down Expand Up @@ -488,6 +485,15 @@ export const expectTotalAudioEnergyToBeGreaterThan = async (
})
console.log('audioStats', audioStats)

return audioStats
}

export const expectTotalAudioEnergyToBeGreaterThan = async (
page: Page,
value: number
) => {
const audioStats = await getAudioStats(page)

expect(audioStats['inbound-rtp']['totalAudioEnergy']).toBeGreaterThan(value)
}

Expand Down

0 comments on commit cfa44a1

Please sign in to comment.