mirror of
https://github.com/vector-im/element-call.git
synced 2026-08-05 19:59:20 +00:00
Fix tests for CallEventAudioRenderer
This commit is contained in:
@@ -8,10 +8,14 @@ Please see LICENSE in the repository root for full details.
|
|||||||
import { render } from "@testing-library/react";
|
import { render } from "@testing-library/react";
|
||||||
import { beforeEach, expect, test } from "vitest";
|
import { beforeEach, expect, test } from "vitest";
|
||||||
import { MatrixClient } from "matrix-js-sdk/src/client";
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
import { ConnectionState, RemoteParticipant, Room } from "livekit-client";
|
import { ConnectionState, Room } from "livekit-client";
|
||||||
import { of } from "rxjs";
|
import { BehaviorSubject, of } from "rxjs";
|
||||||
import { afterEach } from "node:test";
|
import { afterEach } from "node:test";
|
||||||
import { act } from "react";
|
import { act } from "react";
|
||||||
|
import {
|
||||||
|
CallMembership,
|
||||||
|
type MatrixRTCSession,
|
||||||
|
} from "matrix-js-sdk/src/matrixrtc";
|
||||||
|
|
||||||
import { soundEffectVolumeSetting } from "../settings/settings";
|
import { soundEffectVolumeSetting } from "../settings/settings";
|
||||||
import {
|
import {
|
||||||
@@ -22,6 +26,8 @@ import {
|
|||||||
mockMatrixRoomMember,
|
mockMatrixRoomMember,
|
||||||
mockMediaPlay,
|
mockMediaPlay,
|
||||||
mockRemoteParticipant,
|
mockRemoteParticipant,
|
||||||
|
mockRtcMembership,
|
||||||
|
MockRTCSession,
|
||||||
} from "../utils/test";
|
} from "../utils/test";
|
||||||
import { E2eeType } from "../e2ee/e2eeType";
|
import { E2eeType } from "../e2ee/e2eeType";
|
||||||
import { CallViewModel } from "../state/CallViewModel";
|
import { CallViewModel } from "../state/CallViewModel";
|
||||||
@@ -30,11 +36,15 @@ import {
|
|||||||
MAX_PARTICIPANT_COUNT_FOR_SOUND,
|
MAX_PARTICIPANT_COUNT_FOR_SOUND,
|
||||||
} from "./CallEventAudioRenderer";
|
} from "./CallEventAudioRenderer";
|
||||||
|
|
||||||
const alice = mockMatrixRoomMember({ userId: "@alice:example.org" });
|
const localRtcMember = mockRtcMembership("@carol:example.org", "CCCC");
|
||||||
const bob = mockMatrixRoomMember({ userId: "@bob:example.org" });
|
const local = mockMatrixRoomMember(localRtcMember);
|
||||||
const aliceId = `${alice.userId}:AAAA`;
|
const aliceRtcMember = mockRtcMembership("@alice:example.org", "AAAA");
|
||||||
const bobId = `${bob.userId}:BBBB`;
|
const alice = mockMatrixRoomMember(aliceRtcMember);
|
||||||
|
const bobRtcMember = mockRtcMembership("@bob:example.org", "BBBB");
|
||||||
|
const bob = mockMatrixRoomMember(bobRtcMember);
|
||||||
const localParticipant = mockLocalParticipant({ identity: "" });
|
const localParticipant = mockLocalParticipant({ identity: "" });
|
||||||
|
const aliceId = `${alice.userId}:${aliceRtcMember.deviceId}`;
|
||||||
|
const bobId = `${bob.userId}:${bobRtcMember.deviceId}`;
|
||||||
const aliceParticipant = mockRemoteParticipant({ identity: aliceId });
|
const aliceParticipant = mockRemoteParticipant({ identity: aliceId });
|
||||||
const bobParticipant = mockRemoteParticipant({ identity: bobId });
|
const bobParticipant = mockRemoteParticipant({ identity: bobId });
|
||||||
|
|
||||||
@@ -53,20 +63,28 @@ afterEach(() => {
|
|||||||
|
|
||||||
test("plays a sound when entering a call", () => {
|
test("plays a sound when entering a call", () => {
|
||||||
const audioIsPlaying: string[] = mockMediaPlay();
|
const audioIsPlaying: string[] = mockMediaPlay();
|
||||||
const members = new Map([alice, bob].map((p) => [p.userId, p]));
|
const matrixRoomMembers = new Map(
|
||||||
|
[local, alice, bob].map((p) => [p.userId, p]),
|
||||||
|
);
|
||||||
const remoteParticipants = of([aliceParticipant]);
|
const remoteParticipants = of([aliceParticipant]);
|
||||||
const liveKitRoom = mockLivekitRoom(
|
const liveKitRoom = mockLivekitRoom(
|
||||||
{ localParticipant },
|
{ localParticipant },
|
||||||
{ remoteParticipants },
|
{ remoteParticipants },
|
||||||
);
|
);
|
||||||
|
const matrixRoom = mockMatrixRoom({
|
||||||
|
client: {
|
||||||
|
getUserId: () => localRtcMember.sender,
|
||||||
|
getDeviceId: () => localRtcMember.deviceId,
|
||||||
|
} as Partial<MatrixClient> as MatrixClient,
|
||||||
|
getMember: (userId) => matrixRoomMembers.get(userId) ?? null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const session = new MockRTCSession(matrixRoom, localRtcMember, [
|
||||||
|
aliceRtcMember,
|
||||||
|
]) as unknown as MatrixRTCSession;
|
||||||
|
|
||||||
const vm = new CallViewModel(
|
const vm = new CallViewModel(
|
||||||
mockMatrixRoom({
|
session,
|
||||||
client: {
|
|
||||||
getUserId: () => "@carol:example.org",
|
|
||||||
} as Partial<MatrixClient> as MatrixClient,
|
|
||||||
getMember: (userId) => members.get(userId) ?? null,
|
|
||||||
}),
|
|
||||||
liveKitRoom,
|
liveKitRoom,
|
||||||
{
|
{
|
||||||
kind: E2eeType.PER_PARTICIPANT,
|
kind: E2eeType.PER_PARTICIPANT,
|
||||||
@@ -84,20 +102,29 @@ test("plays a sound when entering a call", () => {
|
|||||||
test("plays no sound when muted", () => {
|
test("plays no sound when muted", () => {
|
||||||
soundEffectVolumeSetting.setValue(0);
|
soundEffectVolumeSetting.setValue(0);
|
||||||
const audioIsPlaying: string[] = mockMediaPlay();
|
const audioIsPlaying: string[] = mockMediaPlay();
|
||||||
const members = new Map([alice, bob].map((p) => [p.userId, p]));
|
const matrixRoomMembers = new Map(
|
||||||
|
[local, alice, bob].map((p) => [p.userId, p]),
|
||||||
|
);
|
||||||
const remoteParticipants = of([aliceParticipant, bobParticipant]);
|
const remoteParticipants = of([aliceParticipant, bobParticipant]);
|
||||||
const liveKitRoom = mockLivekitRoom(
|
const liveKitRoom = mockLivekitRoom(
|
||||||
{ localParticipant },
|
{ localParticipant },
|
||||||
{ remoteParticipants },
|
{ remoteParticipants },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const matrixRoom = mockMatrixRoom({
|
||||||
|
client: {
|
||||||
|
getUserId: () => localRtcMember.sender,
|
||||||
|
getDeviceId: () => localRtcMember.deviceId,
|
||||||
|
} as Partial<MatrixClient> as MatrixClient,
|
||||||
|
getMember: (userId) => matrixRoomMembers.get(userId) ?? null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const session = new MockRTCSession(matrixRoom, localRtcMember, [
|
||||||
|
aliceRtcMember,
|
||||||
|
]) as unknown as MatrixRTCSession;
|
||||||
|
|
||||||
const vm = new CallViewModel(
|
const vm = new CallViewModel(
|
||||||
mockMatrixRoom({
|
session,
|
||||||
client: {
|
|
||||||
getUserId: () => "@carol:example.org",
|
|
||||||
} as Partial<MatrixClient> as MatrixClient,
|
|
||||||
getMember: (userId) => members.get(userId) ?? null,
|
|
||||||
}),
|
|
||||||
liveKitRoom,
|
liveKitRoom,
|
||||||
{
|
{
|
||||||
kind: E2eeType.PER_PARTICIPANT,
|
kind: E2eeType.PER_PARTICIPANT,
|
||||||
@@ -112,7 +139,7 @@ test("plays no sound when muted", () => {
|
|||||||
|
|
||||||
test("plays a sound when a user joins", () => {
|
test("plays a sound when a user joins", () => {
|
||||||
const audioIsPlaying: string[] = mockMediaPlay();
|
const audioIsPlaying: string[] = mockMediaPlay();
|
||||||
const members = new Map([alice].map((p) => [p.userId, p]));
|
const matrixRoomMembers = new Map([local, alice].map((p) => [p.userId, p]));
|
||||||
const remoteParticipants = new Map(
|
const remoteParticipants = new Map(
|
||||||
[aliceParticipant].map((p) => [p.identity, p]),
|
[aliceParticipant].map((p) => [p.identity, p]),
|
||||||
);
|
);
|
||||||
@@ -121,13 +148,27 @@ test("plays a sound when a user joins", () => {
|
|||||||
remoteParticipants,
|
remoteParticipants,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const matrixRoom = mockMatrixRoom({
|
||||||
|
client: {
|
||||||
|
getUserId: () => localRtcMember.sender,
|
||||||
|
getDeviceId: () => localRtcMember.deviceId,
|
||||||
|
} as Partial<MatrixClient> as MatrixClient,
|
||||||
|
getMember: (userId) => matrixRoomMembers.get(userId) ?? null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const remoteRtcMemberships = new BehaviorSubject<CallMembership[]>([
|
||||||
|
aliceRtcMember,
|
||||||
|
]);
|
||||||
|
// we give Bob an RTC session now, but no participant yet
|
||||||
|
const session = new MockRTCSession(
|
||||||
|
matrixRoom,
|
||||||
|
localRtcMember,
|
||||||
|
).withMemberships(
|
||||||
|
remoteRtcMemberships.asObservable(),
|
||||||
|
) as unknown as MatrixRTCSession;
|
||||||
|
|
||||||
const vm = new CallViewModel(
|
const vm = new CallViewModel(
|
||||||
mockMatrixRoom({
|
session,
|
||||||
client: {
|
|
||||||
getUserId: () => "@carol:example.org",
|
|
||||||
} as Partial<MatrixClient> as MatrixClient,
|
|
||||||
getMember: (userId) => members.get(userId) ?? null,
|
|
||||||
}),
|
|
||||||
liveKitRoom as unknown as Room,
|
liveKitRoom as unknown as Room,
|
||||||
{
|
{
|
||||||
kind: E2eeType.PER_PARTICIPANT,
|
kind: E2eeType.PER_PARTICIPANT,
|
||||||
@@ -137,20 +178,20 @@ test("plays a sound when a user joins", () => {
|
|||||||
render(<CallEventAudioRenderer vm={vm} />);
|
render(<CallEventAudioRenderer vm={vm} />);
|
||||||
|
|
||||||
act(() => {
|
act(() => {
|
||||||
liveKitRoom.addParticipant(bobParticipant);
|
remoteRtcMemberships.next([aliceRtcMember, bobRtcMember]);
|
||||||
});
|
});
|
||||||
// Play a sound when joining a call.
|
// Play a sound when joining a call.
|
||||||
expect(audioIsPlaying).toEqual([
|
expect(audioIsPlaying).toEqual([
|
||||||
// Joining the call
|
// Joining the call
|
||||||
enterSound,
|
enterSound,
|
||||||
// Bob leaves
|
// Bob joins
|
||||||
enterSound,
|
enterSound,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("plays a sound when a user leaves", () => {
|
test("plays a sound when a user leaves", () => {
|
||||||
const audioIsPlaying: string[] = mockMediaPlay();
|
const audioIsPlaying: string[] = mockMediaPlay();
|
||||||
const members = new Map([alice].map((p) => [p.userId, p]));
|
const matrixRoomMembers = new Map([local, alice].map((p) => [p.userId, p]));
|
||||||
const remoteParticipants = new Map(
|
const remoteParticipants = new Map(
|
||||||
[aliceParticipant].map((p) => [p.identity, p]),
|
[aliceParticipant].map((p) => [p.identity, p]),
|
||||||
);
|
);
|
||||||
@@ -159,13 +200,25 @@ test("plays a sound when a user leaves", () => {
|
|||||||
remoteParticipants,
|
remoteParticipants,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const matrixRoom = mockMatrixRoom({
|
||||||
|
client: {
|
||||||
|
getUserId: () => localRtcMember.sender,
|
||||||
|
getDeviceId: () => localRtcMember.deviceId,
|
||||||
|
} as Partial<MatrixClient> as MatrixClient,
|
||||||
|
getMember: (userId) => matrixRoomMembers.get(userId) ?? null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const remoteRtcMemberships = new BehaviorSubject<CallMembership[]>([
|
||||||
|
aliceRtcMember,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const session = new MockRTCSession(
|
||||||
|
matrixRoom,
|
||||||
|
localRtcMember,
|
||||||
|
).withMemberships(remoteRtcMemberships) as unknown as MatrixRTCSession;
|
||||||
|
|
||||||
const vm = new CallViewModel(
|
const vm = new CallViewModel(
|
||||||
mockMatrixRoom({
|
session,
|
||||||
client: {
|
|
||||||
getUserId: () => "@carol:example.org",
|
|
||||||
} as Partial<MatrixClient> as MatrixClient,
|
|
||||||
getMember: (userId) => members.get(userId) ?? null,
|
|
||||||
}),
|
|
||||||
liveKitRoom as unknown as Room,
|
liveKitRoom as unknown as Room,
|
||||||
{
|
{
|
||||||
kind: E2eeType.PER_PARTICIPANT,
|
kind: E2eeType.PER_PARTICIPANT,
|
||||||
@@ -175,7 +228,7 @@ test("plays a sound when a user leaves", () => {
|
|||||||
render(<CallEventAudioRenderer vm={vm} />);
|
render(<CallEventAudioRenderer vm={vm} />);
|
||||||
|
|
||||||
act(() => {
|
act(() => {
|
||||||
liveKitRoom.removeParticipant(aliceParticipant);
|
remoteRtcMemberships.next([]);
|
||||||
});
|
});
|
||||||
expect(audioIsPlaying).toEqual([
|
expect(audioIsPlaying).toEqual([
|
||||||
// Joining the call
|
// Joining the call
|
||||||
@@ -185,30 +238,45 @@ test("plays a sound when a user leaves", () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("plays no sound when the participant list", () => {
|
test("plays no sound when the session member count is larger than the max, until decreased", () => {
|
||||||
const audioIsPlaying: string[] = mockMediaPlay();
|
const audioIsPlaying: string[] = mockMediaPlay();
|
||||||
const members = new Map([alice].map((p) => [p.userId, p]));
|
const matrixRoomMembers = new Map([local, alice].map((p) => [p.userId, p]));
|
||||||
const remoteParticipants = new Map<string, RemoteParticipant>([
|
const remoteParticipants = new Map(
|
||||||
[aliceParticipant.identity, aliceParticipant],
|
[aliceParticipant].map((p) => [p.identity, p]),
|
||||||
...Array.from({ length: MAX_PARTICIPANT_COUNT_FOR_SOUND - 1 }).map<
|
);
|
||||||
[string, RemoteParticipant]
|
|
||||||
>((_, index) => {
|
const mockRtcMemberships: CallMembership[] = [];
|
||||||
const p = mockRemoteParticipant({ identity: `user${index}` });
|
|
||||||
return [p.identity, p];
|
for (let i = 0; i < MAX_PARTICIPANT_COUNT_FOR_SOUND; i++) {
|
||||||
}),
|
mockRtcMemberships.push(
|
||||||
]);
|
mockRtcMembership(`@user${i}:example.org`, `DEVICE${i}`),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const remoteRtcMemberships = new BehaviorSubject<CallMembership[]>(
|
||||||
|
mockRtcMemberships,
|
||||||
|
);
|
||||||
|
|
||||||
const liveKitRoom = new EmittableMockLivekitRoom({
|
const liveKitRoom = new EmittableMockLivekitRoom({
|
||||||
localParticipant,
|
localParticipant,
|
||||||
remoteParticipants,
|
remoteParticipants,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const matrixRoom = mockMatrixRoom({
|
||||||
|
client: {
|
||||||
|
getUserId: () => localRtcMember.sender,
|
||||||
|
getDeviceId: () => localRtcMember.deviceId,
|
||||||
|
} as Partial<MatrixClient> as MatrixClient,
|
||||||
|
getMember: (userId) => matrixRoomMembers.get(userId) ?? null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const session = new MockRTCSession(
|
||||||
|
matrixRoom,
|
||||||
|
localRtcMember,
|
||||||
|
).withMemberships(remoteRtcMemberships) as unknown as MatrixRTCSession;
|
||||||
|
|
||||||
const vm = new CallViewModel(
|
const vm = new CallViewModel(
|
||||||
mockMatrixRoom({
|
session,
|
||||||
client: {
|
|
||||||
getUserId: () => "@carol:example.org",
|
|
||||||
} as Partial<MatrixClient> as MatrixClient,
|
|
||||||
getMember: (userId) => members.get(userId) ?? null,
|
|
||||||
}),
|
|
||||||
liveKitRoom as unknown as Room,
|
liveKitRoom as unknown as Room,
|
||||||
{
|
{
|
||||||
kind: E2eeType.PER_PARTICIPANT,
|
kind: E2eeType.PER_PARTICIPANT,
|
||||||
@@ -217,9 +285,11 @@ test("plays no sound when the participant list", () => {
|
|||||||
);
|
);
|
||||||
render(<CallEventAudioRenderer vm={vm} />);
|
render(<CallEventAudioRenderer vm={vm} />);
|
||||||
expect(audioIsPlaying).toEqual([]);
|
expect(audioIsPlaying).toEqual([]);
|
||||||
// When the count drops
|
// When the count drops to the max we should play the leave sound
|
||||||
act(() => {
|
act(() => {
|
||||||
liveKitRoom.removeParticipant(aliceParticipant);
|
remoteRtcMemberships.next(
|
||||||
|
mockRtcMemberships.slice(0, MAX_PARTICIPANT_COUNT_FOR_SOUND - 1),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
expect(audioIsPlaying).toEqual([leaveSound]);
|
expect(audioIsPlaying).toEqual([leaveSound]);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user