mirror of
https://github.com/vector-im/element-call.git
synced 2026-08-29 21:15:19 +00:00
finish up tests
This commit is contained in:
@@ -6,27 +6,29 @@ Please see LICENSE in the repository root for full details.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { render } from "@testing-library/react";
|
import { render } from "@testing-library/react";
|
||||||
import { afterAll, beforeEach, expect, test } from "vitest";
|
import { beforeEach, expect, test } from "vitest";
|
||||||
|
|
||||||
|
|
||||||
import {
|
|
||||||
playReactionsSound,
|
|
||||||
soundEffectVolumeSetting,
|
|
||||||
} from "../settings/settings";
|
|
||||||
import { MockLivekitRoom, mockLivekitRoom, mockLocalParticipant, mockMatrixRoom, mockMatrixRoomMember, mockMediaPlay, mockRemoteParticipant } from "../utils/test";
|
|
||||||
import { MatrixClient } from "matrix-js-sdk/src/client";
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
import { E2eeType } from "../e2ee/e2eeType";
|
import { ConnectionState, RemoteParticipant, Room } from "livekit-client";
|
||||||
import { CallViewModel } from "../state/CallViewModel";
|
|
||||||
import { ConnectionState, Room } from "livekit-client";
|
|
||||||
import { of } from "rxjs";
|
import { of } from "rxjs";
|
||||||
import { CallEventAudioRenderer } from "./CallEventAudioRenderer";
|
|
||||||
|
|
||||||
import enterCallSoundOgg from "../sound/join_call.ogg";
|
|
||||||
import leftCallSoundOgg from "../sound/left_call.ogg";
|
|
||||||
import { afterEach } from "node:test";
|
import { afterEach } from "node:test";
|
||||||
import { act } from "react";
|
import { act } from "react";
|
||||||
import { LazyEventEmitter } from "../LazyEventEmitter";
|
|
||||||
import EventEmitter from "events";
|
import { soundEffectVolumeSetting } from "../settings/settings";
|
||||||
|
import {
|
||||||
|
EmittableMockLivekitRoom,
|
||||||
|
mockLivekitRoom,
|
||||||
|
mockLocalParticipant,
|
||||||
|
mockMatrixRoom,
|
||||||
|
mockMatrixRoomMember,
|
||||||
|
mockMediaPlay,
|
||||||
|
mockRemoteParticipant,
|
||||||
|
} from "../utils/test";
|
||||||
|
import { E2eeType } from "../e2ee/e2eeType";
|
||||||
|
import { CallViewModel } from "../state/CallViewModel";
|
||||||
|
import {
|
||||||
|
CallEventAudioRenderer,
|
||||||
|
MAX_PARTICIPANT_COUNT_FOR_SOUND,
|
||||||
|
} from "./CallEventAudioRenderer";
|
||||||
|
|
||||||
const alice = mockMatrixRoomMember({ userId: "@alice:example.org" });
|
const alice = mockMatrixRoomMember({ userId: "@alice:example.org" });
|
||||||
const bob = mockMatrixRoomMember({ userId: "@bob:example.org" });
|
const bob = mockMatrixRoomMember({ userId: "@bob:example.org" });
|
||||||
@@ -38,6 +40,9 @@ const bobParticipant = mockRemoteParticipant({ identity: bobId });
|
|||||||
|
|
||||||
const originalPlayFn = window.HTMLMediaElement.prototype.play;
|
const originalPlayFn = window.HTMLMediaElement.prototype.play;
|
||||||
|
|
||||||
|
const enterSound = "http://localhost:3000/src/sound/join_call.ogg";
|
||||||
|
const leaveSound = "http://localhost:3000/src/sound/left_call.ogg";
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
soundEffectVolumeSetting.setValue(soundEffectVolumeSetting.defaultValue);
|
soundEffectVolumeSetting.setValue(soundEffectVolumeSetting.defaultValue);
|
||||||
});
|
});
|
||||||
@@ -49,7 +54,7 @@ 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 members = new Map([alice, bob].map((p) => [p.userId, p]));
|
||||||
const remoteParticipants = of([aliceParticipant, bobParticipant]);
|
const remoteParticipants = of([aliceParticipant]);
|
||||||
const liveKitRoom = mockLivekitRoom(
|
const liveKitRoom = mockLivekitRoom(
|
||||||
{ localParticipant },
|
{ localParticipant },
|
||||||
{ remoteParticipants },
|
{ remoteParticipants },
|
||||||
@@ -70,8 +75,10 @@ test("plays a sound when entering a call", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
render(<CallEventAudioRenderer vm={vm} />);
|
render(<CallEventAudioRenderer vm={vm} />);
|
||||||
// Play a sound when joining a call.
|
expect(audioIsPlaying).toEqual([
|
||||||
expect(audioIsPlaying.includes(enterCallSoundOgg));
|
// Joining the call
|
||||||
|
enterSound,
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("plays no sound when muted", () => {
|
test("plays no sound when muted", () => {
|
||||||
@@ -106,8 +113,13 @@ 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 members = new Map([alice].map((p) => [p.userId, p]));
|
||||||
const remoteParticipants = new Map([aliceParticipant].map((p) => [p.identity, p]));
|
const remoteParticipants = new Map(
|
||||||
const liveKitRoom = new MockLivekitRoom({localParticipant, remoteParticipants});
|
[aliceParticipant].map((p) => [p.identity, p]),
|
||||||
|
);
|
||||||
|
const liveKitRoom = new EmittableMockLivekitRoom({
|
||||||
|
localParticipant,
|
||||||
|
remoteParticipants,
|
||||||
|
});
|
||||||
|
|
||||||
const vm = new CallViewModel(
|
const vm = new CallViewModel(
|
||||||
mockMatrixRoom({
|
mockMatrixRoom({
|
||||||
@@ -128,16 +140,24 @@ test("plays a sound when a user joins", () => {
|
|||||||
liveKitRoom.addParticipant(bobParticipant);
|
liveKitRoom.addParticipant(bobParticipant);
|
||||||
});
|
});
|
||||||
// Play a sound when joining a call.
|
// Play a sound when joining a call.
|
||||||
expect(audioIsPlaying).toHaveLength(2);
|
expect(audioIsPlaying).toEqual([
|
||||||
|
// Joining the call
|
||||||
|
enterSound,
|
||||||
|
// Bob leaves
|
||||||
|
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 members = new Map([alice].map((p) => [p.userId, p]));
|
||||||
const remoteParticipants = new Map([aliceParticipant].map((p) => [p.identity, p]));
|
const remoteParticipants = new Map(
|
||||||
const liveKitRoom = new MockLivekitRoom({localParticipant, remoteParticipants});
|
[aliceParticipant].map((p) => [p.identity, p]),
|
||||||
|
);
|
||||||
|
const liveKitRoom = new EmittableMockLivekitRoom({
|
||||||
|
localParticipant,
|
||||||
|
remoteParticipants,
|
||||||
|
});
|
||||||
|
|
||||||
const vm = new CallViewModel(
|
const vm = new CallViewModel(
|
||||||
mockMatrixRoom({
|
mockMatrixRoom({
|
||||||
@@ -157,9 +177,49 @@ test("plays a sound when a user leaves", () => {
|
|||||||
act(() => {
|
act(() => {
|
||||||
liveKitRoom.removeParticipant(aliceParticipant);
|
liveKitRoom.removeParticipant(aliceParticipant);
|
||||||
});
|
});
|
||||||
// Play a join sound and a leave sound.
|
|
||||||
expect(audioIsPlaying).toEqual([
|
expect(audioIsPlaying).toEqual([
|
||||||
'http://localhost:3000/src/sound/join_call.ogg',
|
// Joining the call
|
||||||
'http://localhost:3000/src/sound/left_call.ogg'
|
enterSound,
|
||||||
|
// Alice leaves
|
||||||
|
leaveSound,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("plays no sound when the participant list", () => {
|
||||||
|
const audioIsPlaying: string[] = mockMediaPlay();
|
||||||
|
const members = new Map([alice].map((p) => [p.userId, p]));
|
||||||
|
const remoteParticipants = new Map<string, RemoteParticipant>([
|
||||||
|
[aliceParticipant.identity, aliceParticipant],
|
||||||
|
...Array.from({ length: MAX_PARTICIPANT_COUNT_FOR_SOUND - 1 }).map<
|
||||||
|
[string, RemoteParticipant]
|
||||||
|
>((_, index) => {
|
||||||
|
const p = mockRemoteParticipant({ identity: `user${index}` });
|
||||||
|
return [p.identity, p];
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
const liveKitRoom = new EmittableMockLivekitRoom({
|
||||||
|
localParticipant,
|
||||||
|
remoteParticipants,
|
||||||
|
});
|
||||||
|
|
||||||
|
const vm = new CallViewModel(
|
||||||
|
mockMatrixRoom({
|
||||||
|
client: {
|
||||||
|
getUserId: () => "@carol:example.org",
|
||||||
|
} as Partial<MatrixClient> as MatrixClient,
|
||||||
|
getMember: (userId) => members.get(userId) ?? null,
|
||||||
|
}),
|
||||||
|
liveKitRoom as unknown as Room,
|
||||||
|
{
|
||||||
|
kind: E2eeType.PER_PARTICIPANT,
|
||||||
|
},
|
||||||
|
of(ConnectionState.Connected),
|
||||||
|
);
|
||||||
|
render(<CallEventAudioRenderer vm={vm} />);
|
||||||
|
expect(audioIsPlaying).toEqual([]);
|
||||||
|
// When the count drops
|
||||||
|
act(() => {
|
||||||
|
liveKitRoom.removeParticipant(aliceParticipant);
|
||||||
|
});
|
||||||
|
expect(audioIsPlaying).toEqual([leaveSound]);
|
||||||
|
});
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import { ReactNode, useEffect, useRef } from "react";
|
|||||||
import { filter } from "rxjs";
|
import { filter } from "rxjs";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
playReactionsSound,
|
|
||||||
soundEffectVolumeSetting as effectSoundVolumeSetting,
|
soundEffectVolumeSetting as effectSoundVolumeSetting,
|
||||||
useSetting,
|
useSetting,
|
||||||
} from "../settings/settings";
|
} from "../settings/settings";
|
||||||
@@ -35,7 +34,7 @@ export function CallEventAudioRenderer({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (effectSoundVolume === 0) {
|
if (effectSoundVolume === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const joinSub = vm.memberChanges
|
const joinSub = vm.memberChanges
|
||||||
.pipe(
|
.pipe(
|
||||||
filter(
|
filter(
|
||||||
@@ -44,7 +43,6 @@ export function CallEventAudioRenderer({
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.subscribe(({ joined }) => {
|
.subscribe(({ joined }) => {
|
||||||
console.log("Join", joined);
|
|
||||||
if (callEntered.current?.paused) {
|
if (callEntered.current?.paused) {
|
||||||
void callEntered.current.play();
|
void callEntered.current.play();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
Room as LivekitRoom,
|
Room as LivekitRoom,
|
||||||
RoomEvent,
|
RoomEvent,
|
||||||
} from "livekit-client";
|
} from "livekit-client";
|
||||||
|
import { EventEmitter } from "stream";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
LocalUserMediaViewModel,
|
LocalUserMediaViewModel,
|
||||||
@@ -24,7 +25,6 @@ import {
|
|||||||
import { E2eeType } from "../e2ee/e2eeType";
|
import { E2eeType } from "../e2ee/e2eeType";
|
||||||
import { DEFAULT_CONFIG, ResolvedConfigOptions } from "../config/ConfigOptions";
|
import { DEFAULT_CONFIG, ResolvedConfigOptions } from "../config/ConfigOptions";
|
||||||
import { Config } from "../config/Config";
|
import { Config } from "../config/Config";
|
||||||
import { EventEmitter } from "stream";
|
|
||||||
|
|
||||||
export function withFakeTimers(continuation: () => void): void {
|
export function withFakeTimers(continuation: () => void): void {
|
||||||
vi.useFakeTimers();
|
vi.useFakeTimers();
|
||||||
@@ -111,22 +111,28 @@ export function mockMatrixRoom(room: Partial<MatrixRoom>): MatrixRoom {
|
|||||||
return { ...mockEmitter(), ...room } as Partial<MatrixRoom> as MatrixRoom;
|
return { ...mockEmitter(), ...room } as Partial<MatrixRoom> as MatrixRoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MockLivekitRoom extends EventEmitter {
|
/**
|
||||||
|
* A mock of a Livekit Room that can emit events.
|
||||||
|
*/
|
||||||
|
export class EmittableMockLivekitRoom extends EventEmitter {
|
||||||
public localParticipant?: LocalParticipant;
|
public localParticipant?: LocalParticipant;
|
||||||
public remoteParticipants: Map<string, RemoteParticipant>;
|
public remoteParticipants: Map<string, RemoteParticipant>;
|
||||||
|
|
||||||
constructor(room: {localParticipant?: LocalParticipant, remoteParticipants: Map<string, RemoteParticipant>}) {
|
public constructor(room: {
|
||||||
|
localParticipant?: LocalParticipant;
|
||||||
|
remoteParticipants: Map<string, RemoteParticipant>;
|
||||||
|
}) {
|
||||||
super();
|
super();
|
||||||
this.localParticipant = room.localParticipant;
|
this.localParticipant = room.localParticipant;
|
||||||
this.remoteParticipants = room.remoteParticipants ?? new Map();
|
this.remoteParticipants = room.remoteParticipants ?? new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
public addParticipant(remoteParticipant: RemoteParticipant) {
|
public addParticipant(remoteParticipant: RemoteParticipant): void {
|
||||||
this.remoteParticipants.set(remoteParticipant.identity, remoteParticipant);
|
this.remoteParticipants.set(remoteParticipant.identity, remoteParticipant);
|
||||||
this.emit(RoomEvent.ParticipantConnected, remoteParticipant);
|
this.emit(RoomEvent.ParticipantConnected, remoteParticipant);
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeParticipant(remoteParticipant: RemoteParticipant) {
|
public removeParticipant(remoteParticipant: RemoteParticipant): void {
|
||||||
this.remoteParticipants.delete(remoteParticipant.identity);
|
this.remoteParticipants.delete(remoteParticipant.identity);
|
||||||
this.emit(RoomEvent.ParticipantDisconnected, remoteParticipant);
|
this.emit(RoomEvent.ParticipantDisconnected, remoteParticipant);
|
||||||
}
|
}
|
||||||
@@ -230,11 +236,11 @@ export function mockConfig(config: Partial<ResolvedConfigOptions> = {}): void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mockMediaPlay() {
|
export function mockMediaPlay(): string[] {
|
||||||
const audioIsPlaying: string[] = [];
|
const audioIsPlaying: string[] = [];
|
||||||
window.HTMLMediaElement.prototype.play = async function (): Promise<void> {
|
window.HTMLMediaElement.prototype.play = async function (): Promise<void> {
|
||||||
audioIsPlaying.push((this.children[0] as HTMLSourceElement).src);
|
audioIsPlaying.push((this.children[0] as HTMLSourceElement).src);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
};
|
};
|
||||||
return audioIsPlaying;
|
return audioIsPlaying;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user