2022-09-28 16:22:50 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2023-08-10 16:01:14 +08:00
|
|
|
import { EventType, ISendEventResponse, MatrixClient, MatrixEvent, Room, SyncState } from "matrix-js-sdk/src/matrix";
|
2022-09-28 16:22:50 +08:00
|
|
|
|
2022-10-19 21:01:14 +08:00
|
|
|
import Modal from "../../../src/Modal";
|
2022-09-28 16:22:50 +08:00
|
|
|
import {
|
|
|
|
startNewVoiceBroadcastRecording,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
VoiceBroadcastRecording,
|
2022-11-30 18:16:22 +08:00
|
|
|
VoiceBroadcastPlaybacksStore,
|
|
|
|
VoiceBroadcastPlayback,
|
2022-09-28 16:22:50 +08:00
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
2022-10-19 21:01:14 +08:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
2022-09-28 16:22:50 +08:00
|
|
|
|
|
|
|
jest.mock("../../../src/voice-broadcast/models/VoiceBroadcastRecording", () => ({
|
|
|
|
VoiceBroadcastRecording: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-10-19 21:01:14 +08:00
|
|
|
jest.mock("../../../src/Modal");
|
|
|
|
|
2022-09-28 16:22:50 +08:00
|
|
|
describe("startNewVoiceBroadcastRecording", () => {
|
|
|
|
const roomId = "!room:example.com";
|
2022-10-19 21:01:14 +08:00
|
|
|
const otherUserId = "@other:example.com";
|
2022-09-28 16:22:50 +08:00
|
|
|
let client: MatrixClient;
|
2022-11-30 18:16:22 +08:00
|
|
|
let playbacksStore: VoiceBroadcastPlaybacksStore;
|
2022-09-28 16:22:50 +08:00
|
|
|
let recordingsStore: VoiceBroadcastRecordingsStore;
|
|
|
|
let room: Room;
|
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let otherEvent: MatrixEvent;
|
2022-10-19 21:01:14 +08:00
|
|
|
let result: VoiceBroadcastRecording | null;
|
2022-09-28 16:22:50 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
2022-11-30 18:16:22 +08:00
|
|
|
room = new Room(roomId, client, client.getUserId()!);
|
2022-10-19 21:01:14 +08:00
|
|
|
jest.spyOn(room.currentState, "maySendStateEvent");
|
|
|
|
|
2022-09-28 16:22:50 +08:00
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
|
|
|
|
if (getRoomId === roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
2022-11-30 18:16:22 +08:00
|
|
|
|
|
|
|
return null;
|
2022-09-28 16:22:50 +08:00
|
|
|
});
|
2022-12-12 19:24:14 +08:00
|
|
|
mocked(client.sendStateEvent).mockImplementation(
|
|
|
|
(sendRoomId: string, eventType: string, content: any, stateKey: string): Promise<ISendEventResponse> => {
|
|
|
|
if (sendRoomId === roomId && eventType === VoiceBroadcastInfoEventType) {
|
|
|
|
return Promise.resolve({ event_id: infoEvent.getId()! });
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Unexpected sendStateEvent call");
|
|
|
|
},
|
|
|
|
);
|
2022-09-28 16:22:50 +08:00
|
|
|
|
2022-10-21 15:30:02 +08:00
|
|
|
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
2022-11-30 18:16:22 +08:00
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
2022-10-21 15:30:02 +08:00
|
|
|
);
|
2022-09-28 16:22:50 +08:00
|
|
|
otherEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
content: {},
|
2022-11-30 18:16:22 +08:00
|
|
|
user: client.getUserId()!,
|
2022-09-28 16:22:50 +08:00
|
|
|
room: roomId,
|
2022-10-19 21:01:14 +08:00
|
|
|
skey: "",
|
2022-09-28 16:22:50 +08:00
|
|
|
});
|
|
|
|
|
2023-01-02 20:21:33 +08:00
|
|
|
playbacksStore = new VoiceBroadcastPlaybacksStore(recordingsStore);
|
2022-11-30 18:16:22 +08:00
|
|
|
recordingsStore = {
|
|
|
|
setCurrent: jest.fn(),
|
|
|
|
getCurrent: jest.fn(),
|
|
|
|
} as unknown as VoiceBroadcastRecordingsStore;
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
mocked(VoiceBroadcastRecording).mockImplementation((infoEvent: MatrixEvent, client: MatrixClient): any => {
|
2022-09-28 16:22:50 +08:00
|
|
|
return {
|
|
|
|
infoEvent,
|
|
|
|
client,
|
2022-10-12 06:31:28 +08:00
|
|
|
start: jest.fn(),
|
2022-09-28 16:22:50 +08:00
|
|
|
} as unknown as VoiceBroadcastRecording;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-19 21:01:14 +08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-01-09 23:08:30 +08:00
|
|
|
describe("when trying to start a broadcast if there is no connection", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(client.getSyncState).mockReturnValue(SyncState.Error);
|
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show an info dialog and not start a recording", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-19 21:01:14 +08:00
|
|
|
describe("when the current user is allowed to send voice broadcast info state events", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(room.currentState.maySendStateEvent).mockReturnValue(true);
|
|
|
|
});
|
2022-09-28 16:22:50 +08:00
|
|
|
|
2022-11-30 18:16:22 +08:00
|
|
|
describe("when currently listening to a broadcast and there is no recording", () => {
|
|
|
|
let playback: VoiceBroadcastPlayback;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-01-02 20:21:33 +08:00
|
|
|
playback = new VoiceBroadcastPlayback(infoEvent, client, recordingsStore);
|
2022-11-30 18:16:22 +08:00
|
|
|
jest.spyOn(playback, "pause");
|
|
|
|
playbacksStore.setCurrent(playback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should stop listen to the current broadcast and create a new recording", async () => {
|
2022-12-12 19:24:14 +08:00
|
|
|
mocked(client.sendStateEvent).mockImplementation(
|
|
|
|
async (
|
|
|
|
_roomId: string,
|
|
|
|
_eventType: string,
|
|
|
|
_content: any,
|
|
|
|
_stateKey = "",
|
|
|
|
): Promise<ISendEventResponse> => {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
// emit state events after resolving the promise
|
|
|
|
room.currentState.setStateEvents([otherEvent]);
|
|
|
|
room.currentState.setStateEvents([infoEvent]);
|
|
|
|
}, 0);
|
|
|
|
return { event_id: infoEvent.getId()! };
|
|
|
|
},
|
|
|
|
);
|
2022-11-30 18:16:22 +08:00
|
|
|
const recording = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
|
|
|
expect(recording).not.toBeNull();
|
|
|
|
|
|
|
|
// expect to stop and clear the current playback
|
|
|
|
expect(playback.pause).toHaveBeenCalled();
|
|
|
|
expect(playbacksStore.getCurrent()).toBeNull();
|
2022-10-19 21:01:14 +08:00
|
|
|
|
|
|
|
expect(client.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
{
|
2022-10-20 21:41:10 +08:00
|
|
|
chunk_length: 120,
|
2022-10-19 21:01:14 +08:00
|
|
|
device_id: client.getDeviceId(),
|
|
|
|
state: VoiceBroadcastInfoState.Started,
|
|
|
|
},
|
2023-02-16 17:38:44 +08:00
|
|
|
client.getUserId()!,
|
2022-10-19 21:01:14 +08:00
|
|
|
);
|
2022-11-30 18:16:22 +08:00
|
|
|
expect(recording!.infoEvent).toBe(infoEvent);
|
|
|
|
expect(recording!.start).toHaveBeenCalled();
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
2022-09-28 16:22:50 +08:00
|
|
|
});
|
|
|
|
|
2022-10-19 22:22:07 +08:00
|
|
|
describe("when there is already a current voice broadcast", () => {
|
|
|
|
beforeEach(async () => {
|
2022-12-12 19:24:14 +08:00
|
|
|
mocked(recordingsStore.getCurrent).mockReturnValue(new VoiceBroadcastRecording(infoEvent, client));
|
2022-10-19 22:22:07 +08:00
|
|
|
|
2022-11-30 18:16:22 +08:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 22:22:07 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there already is a live broadcast of the current user in the room", () => {
|
2022-10-19 21:01:14 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
room.currentState.setStateEvents([
|
2022-10-21 15:30:02 +08:00
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
2022-10-26 20:54:44 +08:00
|
|
|
VoiceBroadcastInfoState.Resumed,
|
2022-11-30 18:16:22 +08:00
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
2022-10-21 15:30:02 +08:00
|
|
|
),
|
2022-10-19 21:01:14 +08:00
|
|
|
]);
|
|
|
|
|
2022-11-30 18:16:22 +08:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
2022-09-28 16:22:50 +08:00
|
|
|
|
2022-10-19 21:01:14 +08:00
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there already is a live broadcast of another user", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
room.currentState.setStateEvents([
|
2022-12-12 19:24:14 +08:00
|
|
|
mkVoiceBroadcastInfoStateEvent(roomId, VoiceBroadcastInfoState.Resumed, otherUserId, "ASD123"),
|
2022-10-19 21:01:14 +08:00
|
|
|
]);
|
|
|
|
|
2022-11-30 18:16:22 +08:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the current user is not allowed to send voice broadcast info state events", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(room.currentState.maySendStateEvent).mockReturnValue(false);
|
2022-11-30 18:16:22 +08:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 21:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
2022-09-28 16:22:50 +08:00
|
|
|
|
2022-10-19 21:01:14 +08:00
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
2022-09-28 16:22:50 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|