2022-09-28 11:22:50 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-09-28 11:22:50 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03: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-09-28 11:22:50 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2022-10-12 01:31:28 +03:00
|
|
|
import {
|
2023-01-17 10:57:59 +03:00
|
|
|
ClientEvent,
|
2022-10-12 01:31:28 +03:00
|
|
|
EventTimelineSet,
|
|
|
|
EventType,
|
2023-05-11 15:15:34 +03:00
|
|
|
LOCAL_NOTIFICATION_SETTINGS_PREFIX,
|
2022-10-12 01:31:28 +03:00
|
|
|
MatrixClient,
|
|
|
|
MatrixEvent,
|
2022-10-19 19:02:48 +03:00
|
|
|
MatrixEventEvent,
|
2022-10-12 01:31:28 +03:00
|
|
|
MsgType,
|
|
|
|
RelationType,
|
|
|
|
Room,
|
2023-08-09 18:10:54 +03:00
|
|
|
Relations,
|
2023-08-10 11:01:14 +03:00
|
|
|
SyncState,
|
2022-10-12 01:31:28 +03:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-11 12:30:00 +03:00
|
|
|
import { EncryptedFile } from "matrix-js-sdk/src/types";
|
2024-07-04 21:08:06 +03:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2022-09-28 11:22:50 +03:00
|
|
|
|
2022-10-12 01:31:28 +03:00
|
|
|
import { uploadFile } from "../../../src/ContentMessages";
|
|
|
|
import { createVoiceMessageContent } from "../../../src/utils/createVoiceMessageContent";
|
2022-09-28 11:22:50 +03:00
|
|
|
import {
|
2022-10-12 01:31:28 +03:00
|
|
|
createVoiceBroadcastRecorder,
|
2022-11-10 13:53:49 +03:00
|
|
|
getChunkLength,
|
|
|
|
getMaxBroadcastLength,
|
2022-09-28 11:22:50 +03:00
|
|
|
VoiceBroadcastInfoEventContent,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
2022-10-12 01:31:28 +03:00
|
|
|
VoiceBroadcastRecorder,
|
|
|
|
VoiceBroadcastRecorderEvent,
|
2022-09-28 11:22:50 +03:00
|
|
|
VoiceBroadcastRecording,
|
|
|
|
VoiceBroadcastRecordingEvent,
|
2023-01-17 10:57:59 +03:00
|
|
|
VoiceBroadcastRecordingState,
|
2022-09-28 11:22:50 +03:00
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkEvent, mkStubRoom, stubClient } from "../../test-utils";
|
2022-10-16 14:35:25 +03:00
|
|
|
import dis from "../../../src/dispatcher/dispatcher";
|
2022-11-10 13:53:49 +03:00
|
|
|
import { VoiceRecording } from "../../../src/audio/VoiceRecording";
|
2024-07-04 21:08:06 +03:00
|
|
|
import { createAudioContext } from "../../../src/audio/compat";
|
2022-09-28 11:22:50 +03:00
|
|
|
|
2022-10-12 01:31:28 +03:00
|
|
|
jest.mock("../../../src/voice-broadcast/audio/VoiceBroadcastRecorder", () => ({
|
|
|
|
...(jest.requireActual("../../../src/voice-broadcast/audio/VoiceBroadcastRecorder") as object),
|
|
|
|
createVoiceBroadcastRecorder: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-11-10 13:53:49 +03:00
|
|
|
// mock VoiceRecording because it contains all the audio APIs
|
|
|
|
jest.mock("../../../src/audio/VoiceRecording", () => ({
|
|
|
|
VoiceRecording: jest.fn().mockReturnValue({
|
|
|
|
disableMaxLength: jest.fn(),
|
|
|
|
liveData: {
|
|
|
|
onUpdate: jest.fn(),
|
|
|
|
},
|
|
|
|
off: jest.fn(),
|
|
|
|
on: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
stop: jest.fn(),
|
|
|
|
destroy: jest.fn(),
|
|
|
|
contentType: "audio/ogg",
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
|
2022-10-12 01:31:28 +03:00
|
|
|
jest.mock("../../../src/ContentMessages", () => ({
|
|
|
|
uploadFile: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../../src/utils/createVoiceMessageContent", () => ({
|
|
|
|
createVoiceMessageContent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2024-07-04 21:08:06 +03:00
|
|
|
jest.mock("../../../src/audio/compat", () => ({
|
|
|
|
...jest.requireActual("../../../src/audio/compat"),
|
|
|
|
createAudioContext: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-09-28 11:22:50 +03:00
|
|
|
describe("VoiceBroadcastRecording", () => {
|
|
|
|
const roomId = "!room:example.com";
|
2022-10-12 01:31:28 +03:00
|
|
|
const uploadedUrl = "mxc://example.com/vb";
|
2023-07-17 15:07:58 +03:00
|
|
|
const uploadedFile = { file: true } as unknown as EncryptedFile;
|
2022-11-10 13:53:49 +03:00
|
|
|
const maxLength = getMaxBroadcastLength();
|
2022-09-28 11:22:50 +03:00
|
|
|
let room: Room;
|
|
|
|
let client: MatrixClient;
|
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let voiceBroadcastRecording: VoiceBroadcastRecording;
|
2023-01-17 10:57:59 +03:00
|
|
|
let onStateChanged: (state: VoiceBroadcastRecordingState) => void;
|
2022-10-12 01:31:28 +03:00
|
|
|
let voiceBroadcastRecorder: VoiceBroadcastRecorder;
|
2023-05-11 15:15:34 +03:00
|
|
|
let audioElement: HTMLAudioElement;
|
2022-09-28 11:22:50 +03:00
|
|
|
|
|
|
|
const mkVoiceBroadcastInfoEvent = (content: VoiceBroadcastInfoEventContent) => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: VoiceBroadcastInfoEventType,
|
2023-01-17 10:57:59 +03:00
|
|
|
user: client.getSafeUserId(),
|
2022-09-28 11:22:50 +03:00
|
|
|
room: roomId,
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const setUpVoiceBroadcastRecording = () => {
|
|
|
|
voiceBroadcastRecording = new VoiceBroadcastRecording(infoEvent, client);
|
|
|
|
voiceBroadcastRecording.on(VoiceBroadcastRecordingEvent.StateChanged, onStateChanged);
|
2022-10-19 19:02:48 +03:00
|
|
|
jest.spyOn(voiceBroadcastRecording, "destroy");
|
2022-11-10 13:53:49 +03:00
|
|
|
jest.spyOn(voiceBroadcastRecording, "emit");
|
2022-10-12 01:31:28 +03:00
|
|
|
jest.spyOn(voiceBroadcastRecording, "removeAllListeners");
|
2022-09-28 11:22:50 +03:00
|
|
|
};
|
|
|
|
|
2023-01-17 10:57:59 +03:00
|
|
|
const itShouldBeInState = (state: VoiceBroadcastRecordingState) => {
|
2022-10-16 14:35:25 +03:00
|
|
|
it(`should be in state stopped ${state}`, () => {
|
|
|
|
expect(voiceBroadcastRecording.getState()).toBe(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-17 10:57:59 +03:00
|
|
|
const emitFirsChunkRecorded = () => {
|
|
|
|
voiceBroadcastRecorder.emit(VoiceBroadcastRecorderEvent.ChunkRecorded, {
|
|
|
|
buffer: new Uint8Array([1, 2, 3]),
|
|
|
|
length: 23,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-27 17:24:03 +03:00
|
|
|
const itShouldSendAnInfoEvent = (state: VoiceBroadcastInfoState, lastChunkSequence: number) => {
|
2022-10-20 15:44:41 +03:00
|
|
|
it(`should send a ${state} info event`, () => {
|
|
|
|
expect(client.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
{
|
|
|
|
device_id: client.getDeviceId(),
|
|
|
|
state,
|
2022-10-27 17:24:03 +03:00
|
|
|
last_chunk_sequence: lastChunkSequence,
|
2022-10-20 15:44:41 +03:00
|
|
|
["m.relates_to"]: {
|
|
|
|
rel_type: RelationType.Reference,
|
|
|
|
event_id: infoEvent.getId(),
|
|
|
|
},
|
2022-10-27 17:24:03 +03:00
|
|
|
} as VoiceBroadcastInfoEventContent,
|
2023-02-16 12:38:44 +03:00
|
|
|
client.getUserId()!,
|
2022-10-20 15:44:41 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-10 13:53:49 +03:00
|
|
|
const itShouldSendAVoiceMessage = (data: number[], size: number, duration: number, sequence: number) => {
|
|
|
|
// events contain milliseconds
|
|
|
|
duration *= 1000;
|
|
|
|
|
|
|
|
it("should send a voice message", () => {
|
|
|
|
expect(uploadFile).toHaveBeenCalledWith(
|
|
|
|
client,
|
|
|
|
roomId,
|
|
|
|
new Blob([new Uint8Array(data)], { type: voiceBroadcastRecorder.contentType }),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(mocked(client.sendMessage)).toHaveBeenCalledWith(roomId, {
|
|
|
|
body: "Voice message",
|
2022-12-12 14:24:14 +03:00
|
|
|
file: {
|
2022-11-10 13:53:49 +03:00
|
|
|
file: true,
|
2022-12-12 14:24:14 +03:00
|
|
|
},
|
|
|
|
info: {
|
2022-11-10 13:53:49 +03:00
|
|
|
duration,
|
|
|
|
mimetype: "audio/ogg",
|
2022-12-12 14:24:14 +03:00
|
|
|
size,
|
|
|
|
},
|
2022-11-10 13:53:49 +03:00
|
|
|
["m.relates_to"]: {
|
|
|
|
event_id: infoEvent.getId(),
|
|
|
|
rel_type: "m.reference",
|
2022-12-12 14:24:14 +03:00
|
|
|
},
|
2022-11-10 13:53:49 +03:00
|
|
|
msgtype: "m.audio",
|
|
|
|
["org.matrix.msc1767.audio"]: {
|
|
|
|
duration,
|
|
|
|
waveform: undefined,
|
2022-12-12 14:24:14 +03:00
|
|
|
},
|
2022-11-10 13:53:49 +03:00
|
|
|
["org.matrix.msc1767.file"]: {
|
|
|
|
file: {
|
|
|
|
file: true,
|
|
|
|
},
|
|
|
|
mimetype: "audio/ogg",
|
|
|
|
name: "Voice message.ogg",
|
|
|
|
size,
|
|
|
|
url: "mxc://example.com/vb",
|
|
|
|
},
|
|
|
|
["org.matrix.msc1767.text"]: "Voice message",
|
|
|
|
["org.matrix.msc3245.voice"]: {},
|
|
|
|
url: "mxc://example.com/vb",
|
|
|
|
["io.element.voice_broadcast_chunk"]: {
|
|
|
|
sequence,
|
2022-12-12 14:24:14 +03:00
|
|
|
},
|
2022-11-10 13:53:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-17 10:57:59 +03:00
|
|
|
const setUpUploadFileMock = () => {
|
|
|
|
mocked(uploadFile).mockResolvedValue({
|
|
|
|
url: uploadedUrl,
|
|
|
|
file: uploadedFile,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-07-04 21:08:06 +03:00
|
|
|
const mockAudioBufferSourceNode = {
|
|
|
|
addEventListener: jest.fn(),
|
|
|
|
connect: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
};
|
|
|
|
const mockAudioContext = {
|
|
|
|
decodeAudioData: jest.fn(),
|
|
|
|
suspend: jest.fn(),
|
|
|
|
resume: jest.fn(),
|
|
|
|
createBufferSource: jest.fn().mockReturnValue(mockAudioBufferSourceNode),
|
|
|
|
currentTime: 1337,
|
|
|
|
};
|
|
|
|
|
2022-09-28 11:22:50 +03:00
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
room = mkStubRoom(roomId, "Test Room", client);
|
2023-01-17 10:57:59 +03:00
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId: string | undefined): Room | null => {
|
2022-09-28 11:22:50 +03:00
|
|
|
if (getRoomId === roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
2023-01-17 10:57:59 +03:00
|
|
|
|
|
|
|
return null;
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
onStateChanged = jest.fn();
|
2022-11-10 13:53:49 +03:00
|
|
|
voiceBroadcastRecorder = new VoiceBroadcastRecorder(new VoiceRecording(), getChunkLength());
|
|
|
|
jest.spyOn(voiceBroadcastRecorder, "start");
|
|
|
|
jest.spyOn(voiceBroadcastRecorder, "stop");
|
|
|
|
jest.spyOn(voiceBroadcastRecorder, "destroy");
|
2022-10-12 01:31:28 +03:00
|
|
|
mocked(createVoiceBroadcastRecorder).mockReturnValue(voiceBroadcastRecorder);
|
|
|
|
|
2023-01-17 10:57:59 +03:00
|
|
|
setUpUploadFileMock();
|
2022-10-12 01:31:28 +03:00
|
|
|
|
|
|
|
mocked(createVoiceMessageContent).mockImplementation(
|
2022-12-12 14:24:14 +03:00
|
|
|
(
|
2023-01-17 10:57:59 +03:00
|
|
|
mxc: string | undefined,
|
2022-10-12 01:31:28 +03:00
|
|
|
mimetype: string,
|
|
|
|
duration: number,
|
|
|
|
size: number,
|
2023-07-17 15:07:58 +03:00
|
|
|
file?: EncryptedFile,
|
2022-10-12 01:31:28 +03:00
|
|
|
waveform?: number[],
|
|
|
|
) => {
|
|
|
|
return {
|
|
|
|
body: "Voice message",
|
|
|
|
msgtype: MsgType.Audio,
|
|
|
|
url: mxc,
|
|
|
|
file,
|
2022-12-12 14:24:14 +03:00
|
|
|
info: {
|
2022-10-12 01:31:28 +03:00
|
|
|
duration,
|
|
|
|
mimetype,
|
2022-12-12 14:24:14 +03:00
|
|
|
size,
|
|
|
|
},
|
2022-10-12 01:31:28 +03:00
|
|
|
["org.matrix.msc1767.text"]: "Voice message",
|
|
|
|
["org.matrix.msc1767.file"]: {
|
|
|
|
url: mxc,
|
2022-12-12 14:24:14 +03:00
|
|
|
file,
|
2022-10-12 01:31:28 +03:00
|
|
|
name: "Voice message.ogg",
|
|
|
|
mimetype,
|
|
|
|
size,
|
|
|
|
},
|
|
|
|
["org.matrix.msc1767.audio"]: {
|
|
|
|
duration,
|
|
|
|
// https://github.com/matrix-org/matrix-doc/pull/3246
|
|
|
|
waveform,
|
|
|
|
},
|
|
|
|
["org.matrix.msc3245.voice"]: {}, // No content, this is a rendering hint
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
2023-05-11 15:15:34 +03:00
|
|
|
|
|
|
|
audioElement = {
|
|
|
|
play: jest.fn(),
|
|
|
|
} as any as HTMLAudioElement;
|
|
|
|
|
|
|
|
jest.spyOn(document, "querySelector").mockImplementation((selector: string) => {
|
|
|
|
if (selector === "audio#errorAudio") {
|
|
|
|
return audioElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
2024-07-04 21:08:06 +03:00
|
|
|
|
|
|
|
mocked(createAudioContext).mockReturnValue(mockAudioContext as unknown as AudioContext);
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2023-01-17 10:57:59 +03:00
|
|
|
voiceBroadcastRecording?.off(VoiceBroadcastRecordingEvent.StateChanged, onStateChanged);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is an info event without id", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
infoEvent = mkVoiceBroadcastInfoEvent({
|
|
|
|
device_id: client.getDeviceId()!,
|
|
|
|
state: VoiceBroadcastInfoState.Started,
|
|
|
|
});
|
|
|
|
jest.spyOn(infoEvent, "getId").mockReturnValue(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should raise an error when creating a broadcast", () => {
|
|
|
|
expect(() => {
|
|
|
|
setUpVoiceBroadcastRecording();
|
2023-03-01 18:23:35 +03:00
|
|
|
}).toThrow("Cannot create broadcast for info event without Id.");
|
2023-01-17 10:57:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is an info event without room", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
infoEvent = mkVoiceBroadcastInfoEvent({
|
|
|
|
device_id: client.getDeviceId()!,
|
|
|
|
state: VoiceBroadcastInfoState.Started,
|
|
|
|
});
|
|
|
|
jest.spyOn(infoEvent, "getRoomId").mockReturnValue(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should raise an error when creating a broadcast", () => {
|
|
|
|
expect(() => {
|
|
|
|
setUpVoiceBroadcastRecording();
|
2023-03-01 18:23:35 +03:00
|
|
|
}).toThrow(`Cannot create broadcast for unknown room (info event ${infoEvent.getId()})`);
|
2023-01-17 10:57:59 +03:00
|
|
|
});
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when created for a Voice Broadcast Info without relations", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
infoEvent = mkVoiceBroadcastInfoEvent({
|
2023-01-17 10:57:59 +03:00
|
|
|
device_id: client.getDeviceId()!,
|
2022-09-28 11:22:50 +03:00
|
|
|
state: VoiceBroadcastInfoState.Started,
|
|
|
|
});
|
|
|
|
setUpVoiceBroadcastRecording();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be in Started state", () => {
|
2022-10-12 01:31:28 +03:00
|
|
|
expect(voiceBroadcastRecording.getState()).toBe(VoiceBroadcastInfoState.Started);
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
|
2023-01-09 20:18:46 +03:00
|
|
|
describe("and calling stop", () => {
|
2022-09-28 11:22:50 +03:00
|
|
|
beforeEach(() => {
|
|
|
|
voiceBroadcastRecording.stop();
|
|
|
|
});
|
|
|
|
|
2023-01-09 20:18:46 +03:00
|
|
|
itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Stopped, 0);
|
2022-10-16 14:35:25 +03:00
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Stopped);
|
2022-09-28 11:22:50 +03:00
|
|
|
|
|
|
|
it("should emit a stopped state changed event", () => {
|
|
|
|
expect(onStateChanged).toHaveBeenCalledWith(VoiceBroadcastInfoState.Stopped);
|
|
|
|
});
|
|
|
|
});
|
2022-10-12 01:31:28 +03:00
|
|
|
|
|
|
|
describe("and calling start", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await voiceBroadcastRecording.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should start the recorder", () => {
|
|
|
|
expect(voiceBroadcastRecorder.start).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-10-19 19:02:48 +03:00
|
|
|
describe("and the info event is redacted", () => {
|
|
|
|
beforeEach(() => {
|
2023-01-17 10:57:59 +03:00
|
|
|
infoEvent.emit(
|
|
|
|
MatrixEventEvent.BeforeRedaction,
|
|
|
|
infoEvent,
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomRedaction,
|
|
|
|
user: client.getSafeUserId(),
|
|
|
|
content: {},
|
|
|
|
}),
|
|
|
|
);
|
2022-10-19 19:02:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Stopped);
|
|
|
|
|
|
|
|
it("should destroy the recording", () => {
|
|
|
|
expect(voiceBroadcastRecording.destroy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-16 14:35:25 +03:00
|
|
|
describe("and receiving a call action", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
dis.dispatch(
|
|
|
|
{
|
|
|
|
action: "call_state",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-28 14:43:59 +03:00
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Paused);
|
2022-10-16 14:35:25 +03:00
|
|
|
});
|
|
|
|
|
2022-11-10 13:53:49 +03:00
|
|
|
describe("and a chunk time update occurs", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
voiceBroadcastRecorder.emit(VoiceBroadcastRecorderEvent.CurrentChunkLengthUpdated, 10);
|
2022-10-12 01:31:28 +03:00
|
|
|
});
|
|
|
|
|
2022-11-10 13:53:49 +03:00
|
|
|
it("should update time left", () => {
|
|
|
|
expect(voiceBroadcastRecording.getTimeLeft()).toBe(maxLength - 10);
|
|
|
|
expect(voiceBroadcastRecording.emit).toHaveBeenCalledWith(
|
|
|
|
VoiceBroadcastRecordingEvent.TimeLeftChanged,
|
|
|
|
maxLength - 10,
|
2022-10-12 01:31:28 +03:00
|
|
|
);
|
2022-11-10 13:53:49 +03:00
|
|
|
});
|
2022-10-12 01:31:28 +03:00
|
|
|
|
2022-11-10 13:53:49 +03:00
|
|
|
describe("and a chunk time update occurs, that would increase time left", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(voiceBroadcastRecording.emit).mockClear();
|
|
|
|
voiceBroadcastRecorder.emit(VoiceBroadcastRecorderEvent.CurrentChunkLengthUpdated, 5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not change time left", () => {
|
|
|
|
expect(voiceBroadcastRecording.getTimeLeft()).toBe(maxLength - 10);
|
|
|
|
expect(voiceBroadcastRecording.emit).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and a chunk has been recorded", () => {
|
|
|
|
beforeEach(async () => {
|
2023-01-17 10:57:59 +03:00
|
|
|
emitFirsChunkRecorded();
|
2022-10-12 01:31:28 +03:00
|
|
|
});
|
2022-11-10 13:53:49 +03:00
|
|
|
|
|
|
|
itShouldSendAVoiceMessage([1, 2, 3], 3, 23, 1);
|
|
|
|
|
|
|
|
describe("and another chunk has been recorded, that exceeds the max time", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(voiceBroadcastRecorder.stop).mockResolvedValue({
|
|
|
|
buffer: new Uint8Array([23, 24, 25]),
|
|
|
|
length: getMaxBroadcastLength(),
|
|
|
|
});
|
|
|
|
voiceBroadcastRecorder.emit(
|
|
|
|
VoiceBroadcastRecorderEvent.CurrentChunkLengthUpdated,
|
|
|
|
getMaxBroadcastLength(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Stopped);
|
|
|
|
itShouldSendAVoiceMessage([23, 24, 25], 3, getMaxBroadcastLength(), 2);
|
2023-01-09 20:18:46 +03:00
|
|
|
itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Stopped, 2);
|
2022-11-10 13:53:49 +03:00
|
|
|
});
|
2022-10-12 01:31:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling stop", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(voiceBroadcastRecorder.stop).mockResolvedValue({
|
|
|
|
buffer: new Uint8Array([4, 5, 6]),
|
|
|
|
length: 42,
|
|
|
|
});
|
|
|
|
await voiceBroadcastRecording.stop();
|
|
|
|
});
|
|
|
|
|
2022-11-10 13:53:49 +03:00
|
|
|
itShouldSendAVoiceMessage([4, 5, 6], 3, 42, 1);
|
2023-01-09 20:18:46 +03:00
|
|
|
itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Stopped, 1);
|
2022-10-12 01:31:28 +03:00
|
|
|
});
|
|
|
|
|
2022-10-20 15:44:41 +03:00
|
|
|
describe.each([
|
|
|
|
["pause", async () => voiceBroadcastRecording.pause()],
|
|
|
|
["toggle", async () => voiceBroadcastRecording.toggle()],
|
|
|
|
])("and calling %s", (_case: string, action: Function) => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await action();
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Paused);
|
2023-01-09 20:18:46 +03:00
|
|
|
itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Paused, 0);
|
2022-10-20 15:44:41 +03:00
|
|
|
|
|
|
|
it("should stop the recorder", () => {
|
|
|
|
expect(mocked(voiceBroadcastRecorder.stop)).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit a paused state changed event", () => {
|
|
|
|
expect(onStateChanged).toHaveBeenCalledWith(VoiceBroadcastInfoState.Paused);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-17 10:57:59 +03:00
|
|
|
describe("and there is no connection", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.sendStateEvent).mockImplementation(() => {
|
|
|
|
throw new Error();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each([
|
|
|
|
["pause", async () => voiceBroadcastRecording.pause()],
|
|
|
|
["toggle", async () => voiceBroadcastRecording.toggle()],
|
|
|
|
])("and calling %s", (_case: string, action: Function) => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await action();
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState("connection_error");
|
|
|
|
|
|
|
|
describe("and the connection is back", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.sendStateEvent).mockResolvedValue({ event_id: "e1" });
|
|
|
|
client.emit(ClientEvent.Sync, SyncState.Catchup, SyncState.Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Paused);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-12 01:31:28 +03:00
|
|
|
describe("and calling destroy", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
voiceBroadcastRecording.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should stop the recorder and remove all listeners", () => {
|
|
|
|
expect(mocked(voiceBroadcastRecorder.stop)).toHaveBeenCalled();
|
2022-11-10 13:53:49 +03:00
|
|
|
expect(mocked(voiceBroadcastRecorder.destroy)).toHaveBeenCalled();
|
2022-10-12 01:31:28 +03:00
|
|
|
expect(mocked(voiceBroadcastRecording.removeAllListeners)).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2023-01-17 10:57:59 +03:00
|
|
|
|
|
|
|
describe("and a chunk has been recorded and the upload fails", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(uploadFile).mockRejectedValue("Error");
|
|
|
|
emitFirsChunkRecorded();
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState("connection_error");
|
|
|
|
|
|
|
|
describe("and the connection is back", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setUpUploadFileMock();
|
|
|
|
client.emit(ClientEvent.Sync, SyncState.Catchup, SyncState.Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Paused);
|
|
|
|
itShouldSendAVoiceMessage([1, 2, 3], 3, 23, 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-11 15:15:34 +03:00
|
|
|
describe("and audible notifications are disabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const notificationSettings = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${client.getDeviceId()}`,
|
|
|
|
user: client.getSafeUserId(),
|
|
|
|
content: {
|
|
|
|
is_silenced: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
mocked(client.getAccountData).mockReturnValue(notificationSettings);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and a chunk has been recorded and sending the voice message fails", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.sendMessage).mockRejectedValue("Error");
|
|
|
|
emitFirsChunkRecorded();
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState("connection_error");
|
|
|
|
|
|
|
|
it("should not play a notification", () => {
|
|
|
|
expect(audioElement.play).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-17 10:57:59 +03:00
|
|
|
describe("and a chunk has been recorded and sending the voice message fails", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.sendMessage).mockRejectedValue("Error");
|
|
|
|
emitFirsChunkRecorded();
|
2024-07-04 21:08:06 +03:00
|
|
|
fetchMock.get("media/error.mp3", 200);
|
2023-01-17 10:57:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState("connection_error");
|
|
|
|
|
2023-05-11 15:15:34 +03:00
|
|
|
it("should play a notification", () => {
|
2024-07-04 21:08:06 +03:00
|
|
|
expect(mockAudioBufferSourceNode.start).toHaveBeenCalled();
|
2023-05-11 15:15:34 +03:00
|
|
|
});
|
|
|
|
|
2023-01-17 10:57:59 +03:00
|
|
|
describe("and the connection is back", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.sendMessage).mockClear();
|
|
|
|
mocked(client.sendMessage).mockResolvedValue({ event_id: "e23" });
|
|
|
|
client.emit(ClientEvent.Sync, SyncState.Catchup, SyncState.Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Paused);
|
|
|
|
itShouldSendAVoiceMessage([1, 2, 3], 3, 23, 1);
|
|
|
|
});
|
|
|
|
});
|
2022-10-12 01:31:28 +03:00
|
|
|
});
|
2022-10-20 15:44:41 +03:00
|
|
|
|
|
|
|
describe("and it is in paused state", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await voiceBroadcastRecording.pause();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each([
|
|
|
|
["resume", async () => voiceBroadcastRecording.resume()],
|
|
|
|
["toggle", async () => voiceBroadcastRecording.toggle()],
|
|
|
|
])("and calling %s", (_case: string, action: Function) => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await action();
|
|
|
|
});
|
|
|
|
|
2022-10-26 15:54:44 +03:00
|
|
|
itShouldBeInState(VoiceBroadcastInfoState.Resumed);
|
2023-01-09 20:18:46 +03:00
|
|
|
itShouldSendAnInfoEvent(VoiceBroadcastInfoState.Resumed, 0);
|
2022-10-20 15:44:41 +03:00
|
|
|
|
|
|
|
it("should start the recorder", () => {
|
|
|
|
expect(mocked(voiceBroadcastRecorder.start)).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-10-26 15:54:44 +03:00
|
|
|
it(`should emit a ${VoiceBroadcastInfoState.Resumed} state changed event`, () => {
|
|
|
|
expect(onStateChanged).toHaveBeenCalledWith(VoiceBroadcastInfoState.Resumed);
|
2022-10-20 15:44:41 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when created for a Voice Broadcast Info with a Stopped relation", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
infoEvent = mkVoiceBroadcastInfoEvent({
|
2023-01-17 10:57:59 +03:00
|
|
|
device_id: client.getDeviceId()!,
|
2022-09-28 11:22:50 +03:00
|
|
|
state: VoiceBroadcastInfoState.Started,
|
2022-10-20 16:41:10 +03:00
|
|
|
chunk_length: 120,
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const relationsContainer = {
|
|
|
|
getRelations: jest.fn(),
|
|
|
|
} as unknown as Relations;
|
|
|
|
mocked(relationsContainer.getRelations).mockReturnValue([
|
|
|
|
mkVoiceBroadcastInfoEvent({
|
2023-01-17 10:57:59 +03:00
|
|
|
device_id: client.getDeviceId()!,
|
2022-09-28 11:22:50 +03:00
|
|
|
state: VoiceBroadcastInfoState.Stopped,
|
|
|
|
["m.relates_to"]: {
|
|
|
|
rel_type: RelationType.Reference,
|
2023-01-17 10:57:59 +03:00
|
|
|
event_id: infoEvent.getId()!,
|
2022-09-28 11:22:50 +03:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const timelineSet = {
|
|
|
|
relations: {
|
|
|
|
getChildEventsForEvent: jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(
|
|
|
|
(eventId: string, relationType: RelationType | string, eventType: EventType | string) => {
|
|
|
|
if (
|
|
|
|
eventId === infoEvent.getId() &&
|
|
|
|
relationType === RelationType.Reference &&
|
|
|
|
eventType === VoiceBroadcastInfoEventType
|
|
|
|
) {
|
|
|
|
return relationsContainer;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
} as unknown as EventTimelineSet;
|
|
|
|
mocked(room.getUnfilteredTimelineSet).mockReturnValue(timelineSet);
|
|
|
|
|
|
|
|
setUpVoiceBroadcastRecording();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be in Stopped state", () => {
|
2022-10-12 01:31:28 +03:00
|
|
|
expect(voiceBroadcastRecording.getState()).toBe(VoiceBroadcastInfoState.Stopped);
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|