2022-10-14 04:13:17 +00: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";
|
|
|
|
import {
|
|
|
|
MatrixClient,
|
|
|
|
MatrixEvent,
|
|
|
|
Room,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import {
|
2022-10-24 14:06:58 +00:00
|
|
|
VoiceBroadcastInfoState,
|
2022-10-14 04:13:17 +00:00
|
|
|
VoiceBroadcastPlayback,
|
2022-10-24 14:06:58 +00:00
|
|
|
VoiceBroadcastPlaybackEvent,
|
2022-10-14 04:13:17 +00:00
|
|
|
VoiceBroadcastPlaybacksStore,
|
|
|
|
VoiceBroadcastPlaybacksStoreEvent,
|
2022-10-24 14:06:58 +00:00
|
|
|
VoiceBroadcastPlaybackState,
|
2022-10-14 04:13:17 +00:00
|
|
|
} from "../../../src/voice-broadcast";
|
2022-10-24 14:06:58 +00:00
|
|
|
import { mkStubRoom, stubClient } from "../../test-utils";
|
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
2022-10-14 04:13:17 +00:00
|
|
|
|
|
|
|
describe("VoiceBroadcastPlaybacksStore", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
2022-10-24 14:06:58 +00:00
|
|
|
let infoEvent1: MatrixEvent;
|
|
|
|
let infoEvent2: MatrixEvent;
|
|
|
|
let playback1: VoiceBroadcastPlayback;
|
|
|
|
let playback2: VoiceBroadcastPlayback;
|
2022-10-14 04:13:17 +00:00
|
|
|
let playbacks: VoiceBroadcastPlaybacksStore;
|
|
|
|
let onCurrentChanged: (playback: VoiceBroadcastPlayback) => void;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
room = mkStubRoom(roomId, "test room", client);
|
|
|
|
mocked(client.getRoom).mockImplementation((roomId: string) => {
|
|
|
|
if (roomId === room.roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
});
|
2022-10-24 14:06:58 +00:00
|
|
|
|
|
|
|
infoEvent1 = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getUserId(),
|
|
|
|
client.getDeviceId(),
|
|
|
|
);
|
|
|
|
infoEvent2 = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getUserId(),
|
|
|
|
client.getDeviceId(),
|
|
|
|
);
|
|
|
|
playback1 = new VoiceBroadcastPlayback(infoEvent1, client);
|
|
|
|
jest.spyOn(playback1, "off");
|
|
|
|
playback2 = new VoiceBroadcastPlayback(infoEvent2, client);
|
|
|
|
jest.spyOn(playback2, "off");
|
|
|
|
|
2022-10-14 04:13:17 +00:00
|
|
|
playbacks = new VoiceBroadcastPlaybacksStore();
|
2022-10-24 14:06:58 +00:00
|
|
|
jest.spyOn(playbacks, "removeAllListeners");
|
2022-10-14 04:13:17 +00:00
|
|
|
onCurrentChanged = jest.fn();
|
|
|
|
playbacks.on(VoiceBroadcastPlaybacksStoreEvent.CurrentChanged, onCurrentChanged);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
playbacks.off(VoiceBroadcastPlaybacksStoreEvent.CurrentChanged, onCurrentChanged);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when setting a current Voice Broadcast playback", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 14:06:58 +00:00
|
|
|
playbacks.setCurrent(playback1);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return it as current", () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
expect(playbacks.getCurrent()).toBe(playback1);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return it by id", () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
expect(playbacks.getByInfoEvent(infoEvent1, client)).toBe(playback1);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit a CurrentChanged event", () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
expect(onCurrentChanged).toHaveBeenCalledWith(playback1);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and setting the same again", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(onCurrentChanged).mockClear();
|
2022-10-24 14:06:58 +00:00
|
|
|
playbacks.setCurrent(playback1);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not emit a CurrentChanged event", () => {
|
|
|
|
expect(onCurrentChanged).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-24 14:06:58 +00:00
|
|
|
|
|
|
|
describe("and setting another playback and start both", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playbacks.setCurrent(playback2);
|
|
|
|
playback1.start();
|
|
|
|
playback2.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set playback1 to paused", () => {
|
|
|
|
expect(playback1.getState()).toBe(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set playback2 to buffering", () => {
|
|
|
|
// buffering because there are no chunks, yet
|
|
|
|
expect(playback2.getState()).toBe(VoiceBroadcastPlaybackState.Buffering);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling destroy", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playbacks.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove all listeners", () => {
|
|
|
|
expect(playbacks.removeAllListeners).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should deregister the listeners on the playbacks", () => {
|
|
|
|
expect(playback1.off).toHaveBeenCalledWith(
|
|
|
|
VoiceBroadcastPlaybackEvent.StateChanged,
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
expect(playback2.off).toHaveBeenCalledWith(
|
|
|
|
VoiceBroadcastPlaybackEvent.StateChanged,
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("getByInfoEventId", () => {
|
|
|
|
let returnedPlayback: VoiceBroadcastPlayback;
|
|
|
|
|
|
|
|
describe("when retrieving a known playback", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 14:06:58 +00:00
|
|
|
playbacks.setCurrent(playback1);
|
|
|
|
returnedPlayback = playbacks.getByInfoEvent(infoEvent1, client);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the playback", () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
expect(returnedPlayback).toBe(playback1);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when retrieving an unknown playback", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 14:06:58 +00:00
|
|
|
returnedPlayback = playbacks.getByInfoEvent(infoEvent1, client);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the playback", () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
expect(returnedPlayback.infoEvent).toBe(infoEvent1);
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|