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";
|
2022-11-07 14:19:49 +00:00
|
|
|
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2022-10-14 04:13:17 +00:00
|
|
|
|
2022-10-14 14:48:54 +00:00
|
|
|
import { Playback, PlaybackState } from "../../../src/audio/Playback";
|
|
|
|
import { PlaybackManager } from "../../../src/audio/PlaybackManager";
|
2022-10-17 15:35:13 +00:00
|
|
|
import { RelationsHelperEvent } from "../../../src/events/RelationsHelper";
|
2022-10-14 14:48:54 +00:00
|
|
|
import { MediaEventHelper } from "../../../src/utils/MediaEventHelper";
|
2022-10-14 04:13:17 +00:00
|
|
|
import {
|
2022-10-17 14:31:22 +00:00
|
|
|
VoiceBroadcastInfoState,
|
2022-11-16 15:13:59 +00:00
|
|
|
VoiceBroadcastLiveness,
|
2022-10-14 04:13:17 +00:00
|
|
|
VoiceBroadcastPlayback,
|
|
|
|
VoiceBroadcastPlaybackEvent,
|
|
|
|
VoiceBroadcastPlaybackState,
|
|
|
|
} from "../../../src/voice-broadcast";
|
2022-11-07 14:19:49 +00:00
|
|
|
import { flushPromises, stubClient } from "../../test-utils";
|
2022-10-14 14:48:54 +00:00
|
|
|
import { createTestPlayback } from "../../test-utils/audio";
|
2022-11-07 14:19:49 +00:00
|
|
|
import { mkVoiceBroadcastChunkEvent, mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
2022-10-14 14:48:54 +00:00
|
|
|
|
|
|
|
jest.mock("../../../src/events/getReferenceRelationsForEvent", () => ({
|
|
|
|
getReferenceRelationsForEvent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../../src/utils/MediaEventHelper", () => ({
|
|
|
|
MediaEventHelper: jest.fn(),
|
|
|
|
}));
|
2022-10-14 04:13:17 +00:00
|
|
|
|
|
|
|
describe("VoiceBroadcastPlayback", () => {
|
|
|
|
const userId = "@user:example.com";
|
2022-11-07 14:19:49 +00:00
|
|
|
let deviceId: string;
|
2022-10-14 04:13:17 +00:00
|
|
|
const roomId = "!room:example.com";
|
2022-10-14 14:48:54 +00:00
|
|
|
let client: MatrixClient;
|
2022-10-14 04:13:17 +00:00
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let playback: VoiceBroadcastPlayback;
|
|
|
|
let onStateChanged: (state: VoiceBroadcastPlaybackState) => void;
|
2022-10-14 14:48:54 +00:00
|
|
|
let chunk1Event: MatrixEvent;
|
|
|
|
let chunk2Event: MatrixEvent;
|
2022-11-09 11:17:54 +00:00
|
|
|
let chunk2BEvent: MatrixEvent;
|
2022-10-17 15:35:13 +00:00
|
|
|
let chunk3Event: MatrixEvent;
|
2022-11-04 10:50:19 +00:00
|
|
|
const chunk1Length = 2300;
|
|
|
|
const chunk2Length = 4200;
|
|
|
|
const chunk3Length = 6900;
|
2022-10-14 14:48:54 +00:00
|
|
|
const chunk1Data = new ArrayBuffer(2);
|
|
|
|
const chunk2Data = new ArrayBuffer(3);
|
2022-10-17 15:35:13 +00:00
|
|
|
const chunk3Data = new ArrayBuffer(3);
|
2022-10-14 14:48:54 +00:00
|
|
|
let chunk1Helper: MediaEventHelper;
|
|
|
|
let chunk2Helper: MediaEventHelper;
|
2022-10-17 15:35:13 +00:00
|
|
|
let chunk3Helper: MediaEventHelper;
|
2022-10-14 14:48:54 +00:00
|
|
|
let chunk1Playback: Playback;
|
|
|
|
let chunk2Playback: Playback;
|
2022-10-17 15:35:13 +00:00
|
|
|
let chunk3Playback: Playback;
|
2022-10-14 04:13:17 +00:00
|
|
|
|
|
|
|
const itShouldSetTheStateTo = (state: VoiceBroadcastPlaybackState) => {
|
|
|
|
it(`should set the state to ${state}`, () => {
|
|
|
|
expect(playback.getState()).toBe(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldEmitAStateChangedEvent = (state: VoiceBroadcastPlaybackState) => {
|
|
|
|
it(`should emit a ${state} state changed event`, () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
expect(mocked(onStateChanged)).toHaveBeenCalledWith(state, playback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-16 15:13:59 +00:00
|
|
|
const itShouldHaveLiveness = (liveness: VoiceBroadcastLiveness): void => {
|
|
|
|
it(`should have liveness ${liveness}`, () => {
|
|
|
|
expect(playback.getLiveness()).toBe(liveness);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-24 14:06:58 +00:00
|
|
|
const startPlayback = () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.start();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const pausePlayback = () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.pause();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const stopPlayback = () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.stop();
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-14 14:48:54 +00:00
|
|
|
const mkChunkHelper = (data: ArrayBuffer): MediaEventHelper => {
|
|
|
|
return {
|
|
|
|
sourceBlob: {
|
2022-11-07 14:19:49 +00:00
|
|
|
cachedValue: new Blob(),
|
2022-10-14 14:48:54 +00:00
|
|
|
done: false,
|
|
|
|
value: {
|
|
|
|
// @ts-ignore
|
|
|
|
arrayBuffer: jest.fn().mockResolvedValue(data),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-17 14:31:22 +00:00
|
|
|
const mkInfoEvent = (state: VoiceBroadcastInfoState) => {
|
2022-11-07 14:19:49 +00:00
|
|
|
return mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
state,
|
|
|
|
userId,
|
|
|
|
deviceId,
|
|
|
|
);
|
2022-10-17 14:31:22 +00:00
|
|
|
};
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
const mkPlayback = async () => {
|
2022-10-17 14:31:22 +00:00
|
|
|
const playback = new VoiceBroadcastPlayback(infoEvent, client);
|
|
|
|
jest.spyOn(playback, "removeAllListeners");
|
2022-11-28 17:45:50 +00:00
|
|
|
jest.spyOn(playback, "destroy");
|
2022-10-17 14:31:22 +00:00
|
|
|
playback.on(VoiceBroadcastPlaybackEvent.StateChanged, onStateChanged);
|
2022-11-07 14:19:49 +00:00
|
|
|
await flushPromises();
|
2022-10-17 14:31:22 +00:00
|
|
|
return playback;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setUpChunkEvents = (chunkEvents: MatrixEvent[]) => {
|
2022-11-07 14:19:49 +00:00
|
|
|
mocked(client.relations).mockResolvedValueOnce({
|
|
|
|
events: chunkEvents,
|
|
|
|
});
|
2022-10-17 14:31:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client = stubClient();
|
2022-11-07 14:19:49 +00:00
|
|
|
deviceId = client.getDeviceId() || "";
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-04 10:50:19 +00:00
|
|
|
chunk1Event = mkVoiceBroadcastChunkEvent(userId, roomId, chunk1Length, 1);
|
|
|
|
chunk2Event = mkVoiceBroadcastChunkEvent(userId, roomId, chunk2Length, 2);
|
2022-11-09 11:17:54 +00:00
|
|
|
chunk2Event.setTxnId("tx-id-1");
|
|
|
|
chunk2BEvent = mkVoiceBroadcastChunkEvent(userId, roomId, chunk2Length, 2);
|
|
|
|
chunk2BEvent.setTxnId("tx-id-1");
|
2022-11-04 10:50:19 +00:00
|
|
|
chunk3Event = mkVoiceBroadcastChunkEvent(userId, roomId, chunk3Length, 3);
|
2022-10-14 14:48:54 +00:00
|
|
|
|
|
|
|
chunk1Helper = mkChunkHelper(chunk1Data);
|
|
|
|
chunk2Helper = mkChunkHelper(chunk2Data);
|
2022-10-17 15:35:13 +00:00
|
|
|
chunk3Helper = mkChunkHelper(chunk3Data);
|
2022-10-14 14:48:54 +00:00
|
|
|
|
|
|
|
chunk1Playback = createTestPlayback();
|
|
|
|
chunk2Playback = createTestPlayback();
|
2022-10-17 15:35:13 +00:00
|
|
|
chunk3Playback = createTestPlayback();
|
2022-10-14 14:48:54 +00:00
|
|
|
|
|
|
|
jest.spyOn(PlaybackManager.instance, "createPlaybackInstance").mockImplementation(
|
|
|
|
(buffer: ArrayBuffer, _waveForm?: number[]) => {
|
|
|
|
if (buffer === chunk1Data) return chunk1Playback;
|
|
|
|
if (buffer === chunk2Data) return chunk2Playback;
|
2022-10-17 15:35:13 +00:00
|
|
|
if (buffer === chunk3Data) return chunk3Playback;
|
2022-11-07 14:19:49 +00:00
|
|
|
|
|
|
|
throw new Error("unexpected buffer");
|
2022-10-14 14:48:54 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-11-04 10:48:08 +00:00
|
|
|
mocked(MediaEventHelper).mockImplementation((event: MatrixEvent): any => {
|
2022-10-14 14:48:54 +00:00
|
|
|
if (event === chunk1Event) return chunk1Helper;
|
|
|
|
if (event === chunk2Event) return chunk2Helper;
|
2022-10-17 15:35:13 +00:00
|
|
|
if (event === chunk3Event) return chunk3Helper;
|
2022-10-14 14:48:54 +00:00
|
|
|
});
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-10-17 14:31:22 +00:00
|
|
|
jest.clearAllMocks();
|
2022-10-14 04:13:17 +00:00
|
|
|
onStateChanged = jest.fn();
|
|
|
|
});
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
afterEach(() => {
|
|
|
|
playback.destroy();
|
|
|
|
});
|
|
|
|
|
2022-10-26 12:54:44 +00:00
|
|
|
describe(`when there is a ${VoiceBroadcastInfoState.Resumed} broadcast without chunks yet`, () => {
|
2022-11-07 14:19:49 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
// info relation
|
|
|
|
mocked(client.relations).mockResolvedValueOnce({ events: [] });
|
2022-10-17 15:35:13 +00:00
|
|
|
setUpChunkEvents([]);
|
2022-11-07 14:19:49 +00:00
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed);
|
|
|
|
playback = await mkPlayback();
|
2022-10-17 15:35:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling start", () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
startPlayback();
|
2022-10-17 15:35:13 +00:00
|
|
|
|
2022-11-16 15:13:59 +00:00
|
|
|
itShouldHaveLiveness("grey");
|
|
|
|
|
2022-10-17 15:35:13 +00:00
|
|
|
it("should be in buffering state", () => {
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Buffering);
|
|
|
|
});
|
|
|
|
|
2022-11-04 10:50:19 +00:00
|
|
|
it("should have duration 0", () => {
|
|
|
|
expect(playback.durationSeconds).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be at time 0", () => {
|
|
|
|
expect(playback.timeSeconds).toBe(0);
|
|
|
|
});
|
|
|
|
|
2022-10-24 14:06:58 +00:00
|
|
|
describe("and calling stop", () => {
|
|
|
|
stopPlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
|
|
|
|
describe("and calling pause", () => {
|
|
|
|
pausePlayback();
|
|
|
|
// stopped voice broadcasts cannot be paused
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling pause", () => {
|
|
|
|
pausePlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
});
|
|
|
|
|
2022-10-17 15:35:13 +00:00
|
|
|
describe("and receiving the first chunk", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// TODO Michael W: Use RelationsHelper
|
|
|
|
// @ts-ignore
|
|
|
|
playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk1Event);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-11-16 15:13:59 +00:00
|
|
|
itShouldHaveLiveness("live");
|
2022-10-17 15:35:13 +00:00
|
|
|
|
2022-11-04 10:50:19 +00:00
|
|
|
it("should update the duration", () => {
|
|
|
|
expect(playback.durationSeconds).toBe(2.3);
|
|
|
|
});
|
|
|
|
|
2022-10-17 15:35:13 +00:00
|
|
|
it("should play the first chunk", () => {
|
|
|
|
expect(chunk1Playback.play).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-26 12:54:44 +00:00
|
|
|
describe(`when there is a ${VoiceBroadcastInfoState.Resumed} voice broadcast with some chunks`, () => {
|
2022-11-07 14:19:49 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
// info relation
|
|
|
|
mocked(client.relations).mockResolvedValueOnce({ events: [] });
|
2022-10-31 17:35:02 +00:00
|
|
|
setUpChunkEvents([chunk2Event, chunk1Event]);
|
2022-11-07 14:19:49 +00:00
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed);
|
|
|
|
playback = await mkPlayback();
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
2022-11-09 11:17:54 +00:00
|
|
|
it("durationSeconds should have the length of the known chunks", () => {
|
|
|
|
expect(playback.durationSeconds).toEqual(6.5);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and an event with the same transaction Id occurs", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk2BEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("durationSeconds should not change", () => {
|
|
|
|
expect(playback.durationSeconds).toEqual(6.5);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-17 14:31:22 +00:00
|
|
|
describe("and calling start", () => {
|
2022-10-24 14:06:58 +00:00
|
|
|
startPlayback();
|
2022-10-14 04:13:17 +00:00
|
|
|
|
2022-10-17 14:31:22 +00:00
|
|
|
it("should play the last chunk", () => {
|
2022-11-28 17:45:50 +00:00
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Playing);
|
2022-10-17 15:35:13 +00:00
|
|
|
// assert that the last chunk is played first
|
2022-10-17 14:31:22 +00:00
|
|
|
expect(chunk2Playback.play).toHaveBeenCalled();
|
|
|
|
expect(chunk1Playback.play).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-10-17 15:35:13 +00:00
|
|
|
|
|
|
|
describe("and the playback of the last chunk ended", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunk2Playback.emit(PlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Buffering);
|
|
|
|
|
|
|
|
describe("and the next chunk arrived", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// TODO Michael W: Use RelationsHelper
|
|
|
|
// @ts-ignore
|
|
|
|
playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk3Event);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
|
|
|
|
it("should play the next chunk", () => {
|
|
|
|
expect(chunk3Playback.play).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-11-28 17:45:50 +00:00
|
|
|
|
|
|
|
describe("and the info event is deleted", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
infoEvent.makeRedacted(new MatrixEvent({}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should stop and destroy the playback", () => {
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
expect(playback.destroy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-17 14:31:22 +00:00
|
|
|
describe("when there is a stopped voice broadcast", () => {
|
2022-11-07 14:19:49 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
setUpChunkEvents([chunk2Event, chunk1Event]);
|
2022-10-17 14:31:22 +00:00
|
|
|
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Stopped);
|
2022-11-07 14:19:49 +00:00
|
|
|
playback = await mkPlayback();
|
2022-10-14 14:48:54 +00:00
|
|
|
});
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
it("should expose the info event", () => {
|
|
|
|
expect(playback.infoEvent).toBe(infoEvent);
|
|
|
|
});
|
2022-10-14 04:13:17 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling start", () => {
|
|
|
|
startPlayback();
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
it("should play the chunks beginning with the first one", () => {
|
|
|
|
// assert that the first chunk is being played
|
|
|
|
expect(chunk1Playback.play).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.play).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and the chunk playback progresses", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunk1Playback.clockInfo.liveData.update([11]);
|
2022-11-04 10:50:19 +00:00
|
|
|
});
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
it("should update the time", () => {
|
|
|
|
expect(playback.timeSeconds).toBe(11);
|
|
|
|
});
|
|
|
|
});
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and skipping to the middle of the second chunk", () => {
|
|
|
|
const middleOfSecondChunk = (chunk1Length + (chunk2Length / 2)) / 1000;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.skipTo(middleOfSecondChunk);
|
2022-11-04 10:50:19 +00:00
|
|
|
});
|
2022-10-17 14:31:22 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
it("should play the second chunk", () => {
|
|
|
|
expect(chunk1Playback.stop).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.play).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the time", () => {
|
|
|
|
expect(playback.timeSeconds).toBe(middleOfSecondChunk);
|
|
|
|
});
|
2022-10-17 14:31:22 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and skipping to the start", () => {
|
2022-11-04 10:50:19 +00:00
|
|
|
beforeEach(async () => {
|
2022-11-07 14:19:49 +00:00
|
|
|
await playback.skipTo(0);
|
2022-11-04 10:50:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should play the second chunk", () => {
|
2022-11-07 14:19:49 +00:00
|
|
|
expect(chunk1Playback.play).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.stop).toHaveBeenCalled();
|
2022-11-04 10:50:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the time", () => {
|
2022-11-07 14:19:49 +00:00
|
|
|
expect(playback.timeSeconds).toBe(0);
|
2022-11-04 10:50:19 +00:00
|
|
|
});
|
2022-11-07 14:19:49 +00:00
|
|
|
});
|
|
|
|
});
|
2022-11-04 10:50:19 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and the first chunk ends", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunk1Playback.emit(PlaybackState.Stopped);
|
|
|
|
});
|
2022-11-04 10:50:19 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
it("should play until the end", () => {
|
|
|
|
// assert that the second chunk is being played
|
|
|
|
expect(chunk2Playback.play).toHaveBeenCalled();
|
2022-11-04 10:50:19 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
// simulate end of second chunk
|
|
|
|
chunk2Playback.emit(PlaybackState.Stopped);
|
2022-11-04 10:50:19 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
// assert that the entire playback is now in stopped state
|
|
|
|
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Stopped);
|
|
|
|
});
|
|
|
|
});
|
2022-11-04 10:50:19 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling pause", () => {
|
|
|
|
pausePlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
});
|
2022-11-04 10:50:19 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling stop", () => {
|
|
|
|
stopPlayback();
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
2022-12-05 08:43:47 +00:00
|
|
|
|
|
|
|
describe("and skipping to somewhere in the middle of the first chunk", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(chunk1Playback.play).mockClear();
|
|
|
|
await playback.skipTo(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start the playback", () => {
|
|
|
|
expect(chunk1Playback.play).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-11-07 14:19:49 +00:00
|
|
|
});
|
2022-11-04 10:50:19 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling destroy", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback.destroy();
|
2022-10-14 14:48:54 +00:00
|
|
|
});
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
it("should call removeAllListeners", () => {
|
|
|
|
expect(playback.removeAllListeners).toHaveBeenCalled();
|
2022-10-17 14:31:22 +00:00
|
|
|
});
|
2022-10-24 14:06:58 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
it("should call destroy on the playbacks", () => {
|
|
|
|
expect(chunk1Playback.destroy).toHaveBeenCalled();
|
|
|
|
expect(chunk2Playback.destroy).toHaveBeenCalled();
|
2022-10-24 14:06:58 +00:00
|
|
|
});
|
2022-11-07 14:19:49 +00:00
|
|
|
});
|
|
|
|
});
|
2022-10-31 17:35:02 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling toggle for the first time", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
2022-10-14 14:48:54 +00:00
|
|
|
});
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
|
|
|
|
describe("and calling toggle a second time", () => {
|
2022-10-14 14:48:54 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Paused);
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling toggle a third time", () => {
|
2022-10-14 14:48:54 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
await playback.toggle();
|
|
|
|
});
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 14:48:54 +00:00
|
|
|
});
|
|
|
|
});
|
2022-11-07 14:19:49 +00:00
|
|
|
});
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling stop", () => {
|
|
|
|
stopPlayback();
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Stopped);
|
2022-10-14 14:48:54 +00:00
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
describe("and calling toggle", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(onStateChanged).mockReset();
|
|
|
|
await playback.toggle();
|
2022-10-17 14:31:22 +00:00
|
|
|
});
|
2022-11-07 14:19:49 +00:00
|
|
|
|
|
|
|
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
|
|
|
|
itShouldEmitAStateChangedEvent(VoiceBroadcastPlaybackState.Playing);
|
2022-10-14 14:48:54 +00:00
|
|
|
});
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|