2022-10-19 16:01:14 +03:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-12-22 13:37:07 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2022-11-24 11:08:41 +03:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-10-19 16:01:14 +03: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 13:37:07 +03:00
|
|
|
const otherDeviceId = "ASD123";
|
2022-10-19 16:01:14 +03:00
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
2022-11-24 11:08:41 +03:00
|
|
|
let expectedEvent: MatrixEvent | null = null;
|
2022-10-19 16:01:14 +03:00
|
|
|
|
2022-12-22 13:37:07 +03: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 11:08:41 +03:00
|
|
|
room.currentState.setStateEvents([infoEvent]);
|
2022-12-22 13:37:07 +03:00
|
|
|
room.relations.aggregateChildEvent(infoEvent);
|
2022-11-24 11:08:41 +03:00
|
|
|
return infoEvent;
|
2022-10-19 16:01:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnTrueTrue = () => {
|
2022-12-22 13:37:07 +03:00
|
|
|
it("should return true/true", async () => {
|
|
|
|
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
|
2022-10-19 16:01:14 +03:00
|
|
|
hasBroadcast: true,
|
2022-11-24 11:08:41 +03:00
|
|
|
infoEvent: expectedEvent,
|
2022-10-19 16:01:14 +03:00
|
|
|
startedByUser: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnTrueFalse = () => {
|
2022-12-22 13:37:07 +03:00
|
|
|
it("should return true/false", async () => {
|
|
|
|
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
|
2022-10-19 16:01:14 +03:00
|
|
|
hasBroadcast: true,
|
2022-11-24 11:08:41 +03:00
|
|
|
infoEvent: expectedEvent,
|
2022-10-19 16:01:14 +03:00
|
|
|
startedByUser: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnFalseFalse = () => {
|
2022-12-22 13:37:07 +03:00
|
|
|
it("should return false/false", async () => {
|
|
|
|
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
|
2022-10-19 16:01:14 +03:00
|
|
|
hasBroadcast: false,
|
2022-11-24 11:08:41 +03:00
|
|
|
infoEvent: null,
|
2022-10-19 16:01:14 +03:00
|
|
|
startedByUser: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-22 13:37:07 +03: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 11:08:41 +03:00
|
|
|
expectedEvent = null;
|
2022-10-19 16:01:14 +03: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 13:37:07 +03:00
|
|
|
user: client.getSafeUserId(),
|
2022-10-19 16:01:14 +03:00
|
|
|
type: VoiceBroadcastInfoEventType,
|
2022-12-22 13:37:07 +03:00
|
|
|
skey: client.getSafeUserId(),
|
2022-10-19 16:01:14 +03:00
|
|
|
content: {},
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live broadcast from the current and another user", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 13:37:07 +03:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId, otherDeviceId);
|
2022-10-19 16:01:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there are only stopped info events", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 13:37:07 +03:00
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, client.getSafeUserId(), client.getDeviceId()!);
|
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, otherUserId, otherDeviceId);
|
2022-10-19 16:01:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
2022-12-22 13:37:07 +03: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 16:01:14 +03:00
|
|
|
beforeEach(() => {
|
2022-12-22 13:37:07 +03:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Resumed,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
expectedEvent,
|
|
|
|
);
|
2022-10-19 16:01:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there was a live broadcast, that has been stopped", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 13:37:07 +03:00
|
|
|
const startedEvent = addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
|
|
|
addVoiceBroadcastInfoEvent(
|
|
|
|
VoiceBroadcastInfoState.Stopped,
|
|
|
|
client.getSafeUserId(),
|
|
|
|
client.getDeviceId()!,
|
|
|
|
startedEvent,
|
|
|
|
);
|
2022-10-19 16:01:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live broadcast from another user", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 13:37:07 +03:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId, otherDeviceId);
|
2022-10-19 16:01:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueFalse();
|
|
|
|
});
|
|
|
|
});
|