1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-31 13:44:28 +03:00

Stop voice broadcast from save device on resume (#9509)

This commit is contained in:
Michael Weimann
2022-10-26 18:59:47 +02:00
committed by GitHub
parent ee61994c05
commit e6a645dfae
6 changed files with 196 additions and 220 deletions

View File

@ -15,40 +15,78 @@ limitations under the License.
*/
import { mocked } from "jest-mock";
import { ClientEvent, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { ClientEvent, MatrixClient, MatrixEvent, RelationType, Room } from "matrix-js-sdk/src/matrix";
import { SyncState } from "matrix-js-sdk/src/sync";
import {
findRoomLiveVoiceBroadcastFromUserAndDevice,
resumeVoiceBroadcastInRoom,
VoiceBroadcastInfoEventContent,
VoiceBroadcastInfoEventType,
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;
let startedInfoEvent: MatrixEvent;
let pausedInfoEvent: MatrixEvent;
const itShouldNotSendAStateEvent = (): void => {
it("should not send a state event", () => {
expect(client.sendStateEvent).not.toHaveBeenCalled();
});
};
const itShouldSendAStoppedStateEvent = (): void => {
it("should send a stopped state event", () => {
expect(client.sendStateEvent).toHaveBeenCalledWith(
startedInfoEvent.getRoomId(),
VoiceBroadcastInfoEventType,
{
"device_id": client.getDeviceId(),
"state": VoiceBroadcastInfoState.Stopped,
"m.relates_to": {
rel_type: RelationType.Reference,
event_id: startedInfoEvent.getId(),
},
} as VoiceBroadcastInfoEventContent,
client.getUserId(),
);
});
};
const itShouldDeregisterFromTheClient = () => {
it("should deregister from the client", () => {
expect(client.off).toHaveBeenCalledWith(ClientEvent.Sync, expect.any(Function));
});
};
beforeEach(() => {
client = stubClient();
jest.spyOn(client, "off");
room = new Room(roomId, client, client.getUserId());
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
room = new Room(roomId, client, client.getUserId()!);
mocked(client.getRoom).mockImplementation((getRoomId: string | undefined) => {
if (getRoomId === roomId) return room;
return null;
});
resumer = new VoiceBroadcastResumer(client);
infoEvent = mkVoiceBroadcastInfoStateEvent(
mocked(client.getRooms).mockReturnValue([room]);
startedInfoEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
VoiceBroadcastInfoState.Started,
client.getUserId(),
client.getDeviceId(),
client.getUserId()!,
client.getDeviceId()!,
);
pausedInfoEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
VoiceBroadcastInfoState.Paused,
client.getUserId()!,
client.getDeviceId()!,
startedInfoEvent,
);
});
@ -56,56 +94,95 @@ describe("VoiceBroadcastResumer", () => {
jest.clearAllMocks();
});
describe("when there is no info event", () => {
describe("when the initial sync is completed", () => {
beforeEach(() => {
client.emit(ClientEvent.Room, room);
mocked(client.isInitialSyncComplete).mockReturnValue(true);
});
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", () => {
describe("and there is no info event", () => {
beforeEach(() => {
client.emit(ClientEvent.Room, room);
resumer = new VoiceBroadcastResumer(client);
});
it("should not resume the broadcast again", () => {
expect(resumeVoiceBroadcastInRoom).toHaveBeenCalledTimes(1);
itShouldNotSendAStateEvent();
describe("and calling destroy", () => {
beforeEach(() => {
resumer.destroy();
});
itShouldDeregisterFromTheClient();
});
});
describe("and there is a started info event", () => {
beforeEach(() => {
room.currentState.setStateEvents([startedInfoEvent]);
});
describe("and the client knows about the user and device", () => {
beforeEach(() => {
resumer = new VoiceBroadcastResumer(client);
});
itShouldSendAStoppedStateEvent();
});
describe("and the client doesn't know about the user", () => {
beforeEach(() => {
mocked(client.getUserId).mockReturnValue(null);
resumer = new VoiceBroadcastResumer(client);
});
itShouldNotSendAStateEvent();
});
describe("and the client doesn't know about the device", () => {
beforeEach(() => {
mocked(client.getDeviceId).mockReturnValue(null);
resumer = new VoiceBroadcastResumer(client);
});
itShouldNotSendAStateEvent();
});
});
describe("and there is a paused info event", () => {
beforeEach(() => {
room.currentState.setStateEvents([pausedInfoEvent]);
resumer = new VoiceBroadcastResumer(client);
});
itShouldSendAStoppedStateEvent();
});
});
describe("when calling destroy", () => {
describe("when the initial sync is not completed", () => {
beforeEach(() => {
resumer.destroy();
room.currentState.setStateEvents([pausedInfoEvent]);
mocked(client.isInitialSyncComplete).mockReturnValue(false);
mocked(client.getSyncState).mockReturnValue(SyncState.Prepared);
resumer = new VoiceBroadcastResumer(client);
});
it("should deregister from the client", () => {
expect(client.off).toHaveBeenCalledWith(ClientEvent.Room, expect.any(Function));
itShouldNotSendAStateEvent();
describe("and a sync event appears", () => {
beforeEach(() => {
client.emit(ClientEvent.Sync, SyncState.Prepared, SyncState.Stopped);
});
itShouldNotSendAStateEvent();
describe("and the initial sync completed and a sync event appears", () => {
beforeEach(() => {
mocked(client.getSyncState).mockReturnValue(SyncState.Syncing);
client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Prepared);
});
itShouldSendAStoppedStateEvent();
itShouldDeregisterFromTheClient();
});
});
});
});