Resume voice broadcast on load (#9475)

This commit is contained in:
Michael Weimann 2022-10-21 09:30:02 +02:00 committed by GitHub
parent 493ee425b7
commit 5f59ce182e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 554 additions and 22 deletions

View file

@ -0,0 +1,111 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { mocked } from "jest-mock";
import { ClientEvent, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import {
findRoomLiveVoiceBroadcastFromUserAndDevice,
resumeVoiceBroadcastInRoom,
VoiceBroadcastInfoState,
VoiceBroadcastResumer,
} from "../../../src/voice-broadcast";
import { stubClient } from "../../test-utils";
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
jest.mock("../../../src/voice-broadcast/utils/findRoomLiveVoiceBroadcastFromUserAndDevice");
jest.mock("../../../src/voice-broadcast/utils/resumeVoiceBroadcastInRoom");
describe("VoiceBroadcastResumer", () => {
const roomId = "!room:example.com";
let client: MatrixClient;
let room: Room;
let resumer: VoiceBroadcastResumer;
let infoEvent: MatrixEvent;
beforeEach(() => {
client = stubClient();
jest.spyOn(client, "off");
room = new Room(roomId, client, client.getUserId());
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
if (getRoomId === roomId) return room;
});
resumer = new VoiceBroadcastResumer(client);
infoEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
VoiceBroadcastInfoState.Started,
client.getUserId(),
client.getDeviceId(),
);
});
afterEach(() => {
jest.clearAllMocks();
});
describe("when there is no info event", () => {
beforeEach(() => {
client.emit(ClientEvent.Room, room);
});
it("should not resume a broadcast", () => {
expect(resumeVoiceBroadcastInRoom).not.toHaveBeenCalled();
});
});
describe("when there is an info event", () => {
beforeEach(() => {
mocked(findRoomLiveVoiceBroadcastFromUserAndDevice).mockImplementation((
findRoom: Room,
userId: string,
deviceId: string,
) => {
if (findRoom === room && userId === client.getUserId() && deviceId === client.getDeviceId()) {
return infoEvent;
}
});
client.emit(ClientEvent.Room, room);
});
it("should resume a broadcast", () => {
expect(resumeVoiceBroadcastInRoom).toHaveBeenCalledWith(
infoEvent,
room,
client,
);
});
describe("and emitting a room event again", () => {
beforeEach(() => {
client.emit(ClientEvent.Room, room);
});
it("should not resume the broadcast again", () => {
expect(resumeVoiceBroadcastInRoom).toHaveBeenCalledTimes(1);
});
});
});
describe("when calling destroy", () => {
beforeEach(() => {
resumer.destroy();
});
it("should deregister from the client", () => {
expect(client.off).toHaveBeenCalledWith(ClientEvent.Room, expect.any(Function));
});
});
});