2022-10-19 21:01:14 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-19 21:01:14 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-10-19 21:01:14 +08:00
|
|
|
*/
|
|
|
|
|
2022-12-22 18:37:07 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2022-11-24 16:08:41 +08:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-10-19 21:01:14 +08:00
|
|
|
|
|
|
|
import {
|
|
|
|
hasRoomLiveVoiceBroadcast,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
|
|
|
|
|
|
|
describe("hasRoomLiveVoiceBroadcast", () => {
|
|
|
|
const otherUserId = "@other:example.com";
|
2022-12-22 18:37:07 +08:00
|
|
|
const otherDeviceId = "ASD123";
|
2022-10-19 21:01:14 +08:00
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
2022-11-24 16:08:41 +08:00
|
|
|
let expectedEvent: MatrixEvent | null = null;
|
2022-10-19 21:01:14 +08:00
|
|
|
|
2022-12-22 18:37:07 +08:00
|
|
|
const addVoiceBroadcastInfoEvent = (
|
|
|
|
state: VoiceBroadcastInfoState,
|
|
|
|
userId: string,
|
|
|
|
deviceId: string,
|
|
|
|
startedEvent?: MatrixEvent,
|
|
|
|
): MatrixEvent => {
|
|
|
|
const infoEvent = mkVoiceBroadcastInfoStateEvent(room.roomId, state, userId, deviceId, startedEvent);
|
|
|
|
room.addLiveEvents([infoEvent]);
|
2022-11-24 16:08:41 +08:00
|
|
|
room.currentState.setStateEvents([infoEvent]);
|
2022-12-22 18:37:07 +08:00
|
|
|
room.relations.aggregateChildEvent(infoEvent);
|
2022-11-24 16:08:41 +08:00
|
|
|
return infoEvent;
|
2022-10-19 21:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnTrueTrue = () => {
|
2022-12-22 18:37:07 +08:00
|
|
|
it("should return true/true", async () => {
|
|
|
|
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
|
2022-10-19 21:01:14 +08:00
|
|
|
hasBroadcast: true,
|
2022-11-24 16:08:41 +08:00
|
|
|
infoEvent: expectedEvent,
|
2022-10-19 21:01:14 +08:00
|
|
|
startedByUser: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnTrueFalse = () => {
|
2022-12-22 18:37:07 +08:00
|
|
|
it("should return true/false", async () => {
|
|
|
|
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
|
2022-10-19 21:01:14 +08:00
|
|
|
hasBroadcast: true,
|
2022-11-24 16:08:41 +08:00
|
|
|
infoEvent: expectedEvent,
|
2022-10-19 21:01:14 +08:00
|
|
|
startedByUser: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnFalseFalse = () => {
|
2022-12-22 18:37:07 +08:00
|
|
|
it("should return false/false", async () => {
|
|
|
|
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
|
2022-10-19 21:01:14 +08:00
|
|
|
hasBroadcast: false,
|
2022-11-24 16:08:41 +08:00
|
|
|
infoEvent: null,
|
2022-10-19 21:01:14 +08:00
|
|
|
startedByUser: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-22 18:37:07 +08:00
|
|
|
client = stubClient();
|
|
|
|
room = new Room(roomId, client, client.getSafeUserId());
|
|
|
|
mocked(client.getRoom).mockImplementation((roomId: string): Room | null => {
|
|
|
|
return roomId === room.roomId ? room : null;
|
|
|
|
});
|
2022-11-24 16:08:41 +08:00
|
|
|
expectedEvent = null;
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is no voice broadcast info at all", () => {
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the »state« prop is missing", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
room: room.roomId,
|
2022-12-22 18:37:07 +08:00
|
|
|
user: client.getSafeUserId(),
|
2022-10-19 21:01:14 +08:00
|
|
|
type: VoiceBroadcastInfoEventType,
|
2022-12-22 18:37:07 +08:00
|
|
|
skey: client.getSafeUserId(),
|
2022-10-19 21:01:14 +08:00
|
|
|
content: {},
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live broadcast from the current and another user", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 18:37:07 +08:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId, otherDeviceId);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there are only stopped info events", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 18:37:07 +08:00
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, client.getSafeUserId(), client.getDeviceId()!);
|
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, otherUserId, otherDeviceId);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
2022-12-22 18:37:07 +08:00
|
|
|
describe("when there is a live, started broadcast from the current user", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live, paused broadcast from the current user", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Paused,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
expectedEvent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live, resumed broadcast from the current user", () => {
|
2022-10-19 21:01:14 +08:00
|
|
|
beforeEach(() => {
|
2022-12-22 18:37:07 +08:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Resumed,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
expectedEvent,
|
|
|
|
);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there was a live broadcast, that has been stopped", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 18:37:07 +08:00
|
|
|
const startedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Stopped,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
startedEvent,
|
|
|
|
);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live broadcast from another user", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 18:37:07 +08:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId, otherDeviceId);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueFalse();
|
|
|
|
});
|
|
|
|
});
|