Skip to content

Commit

Permalink
Improve E2E tests for JS (#860)
Browse files Browse the repository at this point in the history
* improve e2e-js utils exposing createRoom too

* lock updates

* use createRoom when needed and delete room on end tests

* changeset
  • Loading branch information
Edoardo Gallo authored Aug 22, 2023
1 parent 5e1ff11 commit 19f8673
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-fireants-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sw-internal/e2e-js': patch
---

Improve e2e-tests for the browser sDK
7 changes: 5 additions & 2 deletions internal/e2e-js/tests/roomSessionAutomaticStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
createTestRoomSession,
expectRoomJoined,
expectMCUVisible,
createOrUpdateRoom,
createRoom,
createStreamForRoom,
randomizeRoomName,
deleteRoom,
} from '../utils'

test.describe('Room Session Auto Stream', () => {
Expand All @@ -31,7 +32,7 @@ test.describe('Room Session Auto Stream', () => {
const pageOne = await createCustomPage({ name: '[pageOne]' })
pageOne.goto(SERVER_URL)

await createOrUpdateRoom({ name: roomName })
const roomData = await createRoom({ name: roomName })
await createStreamForRoom(roomName, streamingURL)

await createTestRoomSession(pageOne, connectionSettings)
Expand All @@ -47,5 +48,7 @@ test.describe('Room Session Auto Stream', () => {
})

expect(streamsResult.streams).toHaveLength(1)

await deleteRoom(roomData.id)
})
})
11 changes: 7 additions & 4 deletions internal/e2e-js/tests/roomSessionMaxMembers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
createTestRoomSession,
expectRoomJoined,
expectMCUVisible,
createOrUpdateRoom,
createRoom,
randomizeRoomName,
deleteRoom,
} from '../utils'

test.describe('Room Session Max Members', () => {
Expand All @@ -25,7 +26,7 @@ test.describe('Room Session Max Members', () => {

const roomName = randomizeRoomName('max_member_e2e')

await createOrUpdateRoom({
const roomData = await createRoom({
name: roomName,
max_members: 2,
})
Expand All @@ -41,8 +42,8 @@ test.describe('Room Session Max Members', () => {
},
initialEvents: ['stream.started', 'stream.ended'],
expectToJoin: false,
})}
)
})
})
)

await Promise.all([expectRoomJoined(pageOne), expectRoomJoined(pageTwo)])
Expand All @@ -67,5 +68,7 @@ test.describe('Room Session Max Members', () => {
})

await expect(joinRoom).rejects.toBeTruthy()

await deleteRoom(roomData.id)
})
})
7 changes: 7 additions & 0 deletions internal/e2e-js/tests/roomSessionReattachMultiple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
expectRoomJoined,
expectMCUVisible,
expectMCUVisibleForAudience,
deleteRoom,
} from '../utils'

type Test = {
Expand Down Expand Up @@ -47,6 +48,7 @@ test.describe('RoomSessionReattach', () => {
await createTestRoomSession(page, connectionSettings)

const joinParams: any = await expectRoomJoined(page)
const roomId = joinParams.room_session.room_id

expect(joinParams.room).toBeDefined()
expect(joinParams.room_session).toBeDefined()
Expand Down Expand Up @@ -116,6 +118,9 @@ test.describe('RoomSessionReattach', () => {
)
).toBeTruthy()
}
// Make sure the room_id is stable
expect(reattachParams2.room_session.room_id).toBe(roomId)

expect(reattachParams2.room_session.name).toBe(roomName)
expect(reattachParams2.room.name).toBe(roomName)
// Make sure the member_id is stable
Expand All @@ -125,6 +130,8 @@ test.describe('RoomSessionReattach', () => {
expect(reattachParams2.call_id).toBeDefined()
expect(reattachParams2.call_id).toBe(joinParams.call_id)
await row.expectMCU(page)

await deleteRoom(roomId)
})
})
})
4 changes: 2 additions & 2 deletions internal/e2e-js/tests/roomSessionStreamingAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from '../fixtures'
import {
SERVER_URL,
createTestRoomSession,
createOrUpdateRoom,
createRoom,
randomizeRoomName,
createStreamForRoom,
deleteRoom,
Expand Down Expand Up @@ -30,7 +30,7 @@ test.describe('Room Streaming from REST API', () => {
initialEvents: ['stream.started', 'stream.ended'],
}

const roomData = await createOrUpdateRoom({
const roomData = await createRoom({
name: room_name,
})

Expand Down
4 changes: 2 additions & 2 deletions internal/e2e-js/tests/roomSettings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from '../fixtures'
import {
SERVER_URL,
createTestRoomSession,
createOrUpdateRoom,
createRoom,
deleteRoom,
CreateOrUpdateRoomOptions,
randomizeRoomName,
Expand Down Expand Up @@ -46,7 +46,7 @@ test.describe('Room Settings', () => {
const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

const roomData = await createOrUpdateRoom({
const roomData = await createRoom({
name: row.roomName,
...row.roomSettings,
})
Expand Down
59 changes: 36 additions & 23 deletions internal/e2e-js/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const TARGET_ROOT_PATH: Record<
}

export const SERVER_URL = 'http://localhost:1337'
export const BASIC_TOKEN = Buffer.from(
`${process.env.RELAY_PROJECT}:${process.env.RELAY_TOKEN}`
).toString('base64')

export const createTestServer = async (
options: CreateTestServerOptions = { target: 'blank' }
Expand Down Expand Up @@ -75,11 +78,11 @@ export const createTestRoomSession = async (
host: options.RELAY_HOST,
token: options.API_TOKEN,
rootElement: document.getElementById('rootElement'),
// logLevel: options.CI ? 'warn' : 'debug',
logLevel: 'debug',
// logLevel: options.CI ? 'info' : 'debug',
// logLevel: 'debug',
debug: {
// logWsTraffic: !options.CI,
logWsTraffic: true,
logWsTraffic: false,
},
...options.roomSessionOptions,
})
Expand Down Expand Up @@ -193,18 +196,17 @@ interface CreateTestVRTOptions {
media_allowed?: 'audio-only' | 'video-only' | 'all'
join_audio_muted?: boolean
join_video_muted?: boolean
end_room_session_on_leave?: boolean,
end_room_session_on_leave?: boolean
}

export const createTestVRTToken = async (body: CreateTestVRTOptions) => {
const authCreds = `${process.env.RELAY_PROJECT}:${process.env.RELAY_TOKEN}`
const response = await fetch(
`https://${process.env.API_HOST}/api/video/room_tokens`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(authCreds).toString('base64')}`,
Authorization: `Basic ${BASIC_TOKEN}`,
},
body: JSON.stringify(body),
}
Expand All @@ -221,14 +223,13 @@ interface CreateTestCRTOptions {
}

export const createTestCRTToken = async (body: CreateTestCRTOptions) => {
const authCreds = `${process.env.RELAY_PROJECT}:${process.env.RELAY_TOKEN}`
const response = await fetch(
`https://${process.env.API_HOST}/api/chat/tokens`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(authCreds).toString('base64')}`,
Authorization: `Basic ${BASIC_TOKEN}`,
},
body: JSON.stringify(body),
}
Expand Down Expand Up @@ -306,7 +307,7 @@ export const expectRoomJoined = (
return new Promise<any>(async (resolve) => {
// @ts-expect-error
const roomObj: Video.RoomSession = window._roomObj

roomObj.once('room.joined', resolve)

if (invokeJoin) {
Expand Down Expand Up @@ -417,14 +418,13 @@ export const expectTotalAudioEnergyToBeGreaterThan = async (
}

const getRoomByName = async (roomName: string) => {
const authCreds = `${process.env.RELAY_PROJECT}:${process.env.RELAY_TOKEN}`
const response = await fetch(
`https://${process.env.API_HOST}/api/video/rooms/${roomName}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(authCreds).toString('base64')}`,
Authorization: `Basic ${BASIC_TOKEN}`,
},
}
)
Expand All @@ -450,38 +450,52 @@ export interface CreateOrUpdateRoomOptions {

export const createOrUpdateRoom = async (body: CreateOrUpdateRoomOptions) => {
const room = await getRoomByName(body.name)
const authCreds = `${process.env.RELAY_PROJECT}:${process.env.RELAY_TOKEN}`
if (!room) {
return createRoom(body)
}

const response = await fetch(
`https://${process.env.API_HOST}/api/video/rooms${
room ? `/${room.id}` : ''
}`,
`https://${process.env.API_HOST}/api/video/rooms/${room.id}`,
{
method: room ? 'PUT' : 'POST',
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(authCreds).toString('base64')}`,
Authorization: `Basic ${BASIC_TOKEN}`,
},
body: JSON.stringify(body),
}
)
const data = await response.json()
return data
return response.json()
}

export const createRoom = async (body: CreateOrUpdateRoomOptions) => {
const response = await fetch(
`https://${process.env.API_HOST}/api/video/rooms`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${BASIC_TOKEN}`,
},
body: JSON.stringify(body),
}
)
return response.json()
}

export const createStreamForRoom = async (name: string, url: string) => {
const room = await getRoomByName(name)
if (!room) {
throw new Error('Room not found')
}
const authCreds = `${process.env.RELAY_PROJECT}:${process.env.RELAY_TOKEN}`

const response = await fetch(
`https://${process.env.API_HOST}/api/video/rooms/${room.id}/streams`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(authCreds).toString('base64')}`,
Authorization: `Basic ${BASIC_TOKEN}`,
},
body: JSON.stringify({ url }),
}
Expand All @@ -496,12 +510,11 @@ export const createStreamForRoom = async (name: string, url: string) => {
}

export const deleteRoom = async (id: string) => {
const authCreds = `${process.env.RELAY_PROJECT}:${process.env.RELAY_TOKEN}`
return await fetch(`https://${process.env.API_HOST}/api/video/rooms/${id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(authCreds).toString('base64')}`,
Authorization: `Basic ${BASIC_TOKEN}`,
},
})
}
Expand Down
Loading

0 comments on commit 19f8673

Please sign in to comment.