2022-07-13 13:56:36 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-07-13 13:56:36 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08: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-07-13 13:56:36 +08:00
|
|
|
*/
|
|
|
|
|
2023-02-28 16:58:23 +08:00
|
|
|
import React, { createRef, RefObject } from "react";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { render } from "jest-matrix-react";
|
2022-11-04 18:48:08 +08:00
|
|
|
import { MatrixClient, MsgType, Room } from "matrix-js-sdk/src/matrix";
|
2022-07-13 13:56:36 +08:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import VoiceRecordComposerTile from "../../../../../src/components/views/rooms/VoiceRecordComposerTile";
|
|
|
|
import { doMaybeLocalRoomAction } from "../../../../../src/utils/local-room";
|
|
|
|
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
|
|
|
|
import { IUpload, VoiceMessageRecording } from "../../../../../src/audio/VoiceMessageRecording";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import { VoiceRecordingStore } from "../../../../../src/stores/VoiceRecordingStore";
|
|
|
|
import { PlaybackClock } from "../../../../../src/audio/PlaybackClock";
|
|
|
|
import { mkEvent } from "../../../../test-utils";
|
2022-07-13 13:56:36 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../../../../src/utils/local-room", () => ({
|
2022-07-13 13:56:36 +08:00
|
|
|
doMaybeLocalRoomAction: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../../../../src/stores/VoiceRecordingStore", () => ({
|
2023-02-28 16:58:23 +08:00
|
|
|
VoiceRecordingStore: {
|
|
|
|
getVoiceRecordingId: jest.fn().mockReturnValue("voice-recording-id"),
|
|
|
|
instance: {
|
|
|
|
getActiveRecording: jest.fn(),
|
|
|
|
disposeRecording: jest.fn(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2022-07-13 13:56:36 +08:00
|
|
|
describe("<VoiceRecordComposerTile/>", () => {
|
2023-02-28 16:58:23 +08:00
|
|
|
let voiceRecordComposerTile: RefObject<VoiceRecordComposerTile>;
|
|
|
|
let mockRecorder: VoiceMessageRecording;
|
2022-07-13 13:56:36 +08:00
|
|
|
let mockUpload: IUpload;
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient = {
|
2023-03-23 19:47:40 +08:00
|
|
|
getSafeUserId: jest.fn().mockReturnValue("@alice:example.com"),
|
2022-07-13 13:56:36 +08:00
|
|
|
sendMessage: jest.fn(),
|
|
|
|
} as unknown as MatrixClient;
|
|
|
|
MatrixClientPeg.get = () => mockClient;
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet = () => mockClient;
|
2022-07-13 13:56:36 +08:00
|
|
|
|
2023-02-24 23:28:40 +08:00
|
|
|
const room = {
|
|
|
|
roomId,
|
|
|
|
} as unknown as Room;
|
2023-02-28 16:58:23 +08:00
|
|
|
|
|
|
|
voiceRecordComposerTile = createRef();
|
2022-07-13 13:56:36 +08:00
|
|
|
const props = {
|
2023-02-24 23:28:40 +08:00
|
|
|
room,
|
2023-02-28 16:58:23 +08:00
|
|
|
ref: voiceRecordComposerTile,
|
2023-02-24 23:28:40 +08:00
|
|
|
permalinkCreator: new RoomPermalinkCreator(room),
|
2022-07-13 13:56:36 +08:00
|
|
|
};
|
|
|
|
mockUpload = {
|
|
|
|
mxc: "mxc://example.com/voice",
|
|
|
|
};
|
|
|
|
mockRecorder = {
|
2023-02-28 16:58:23 +08:00
|
|
|
on: jest.fn(),
|
|
|
|
off: jest.fn(),
|
2022-07-13 13:56:36 +08:00
|
|
|
stop: jest.fn(),
|
|
|
|
upload: () => Promise.resolve(mockUpload),
|
|
|
|
durationSeconds: 1337,
|
|
|
|
contentType: "audio/ogg",
|
|
|
|
getPlayback: () => ({
|
2023-02-28 16:58:23 +08:00
|
|
|
on: jest.fn(),
|
|
|
|
off: jest.fn(),
|
|
|
|
prepare: jest.fn().mockResolvedValue(void 0),
|
|
|
|
clockInfo: {
|
|
|
|
timeSeconds: 0,
|
|
|
|
liveData: {
|
|
|
|
onUpdate: jest.fn(),
|
|
|
|
},
|
|
|
|
} as unknown as PlaybackClock,
|
|
|
|
waveform: [1.4, 2.5, 3.6],
|
|
|
|
waveformData: {
|
|
|
|
onUpdate: jest.fn(),
|
|
|
|
},
|
2022-09-30 03:06:49 +08:00
|
|
|
thumbnailWaveform: [1.4, 2.5, 3.6],
|
2022-07-13 13:56:36 +08:00
|
|
|
}),
|
2023-02-28 16:58:23 +08:00
|
|
|
} as unknown as VoiceMessageRecording;
|
|
|
|
mocked(VoiceRecordingStore.instance.getActiveRecording).mockReturnValue(mockRecorder);
|
|
|
|
render(<VoiceRecordComposerTile {...props} />);
|
2022-07-13 13:56:36 +08:00
|
|
|
|
2022-11-04 18:48:08 +08:00
|
|
|
mocked(doMaybeLocalRoomAction).mockImplementation(
|
2023-02-24 23:28:40 +08:00
|
|
|
<T,>(roomId: string, fn: (actualRoomId: string) => Promise<T>, _client?: MatrixClient) => {
|
2022-07-13 13:56:36 +08:00
|
|
|
return fn(roomId);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("send", () => {
|
|
|
|
it("should send the voice recording", async () => {
|
2023-02-28 16:58:23 +08:00
|
|
|
await voiceRecordComposerTile.current!.send();
|
2022-07-13 13:56:36 +08:00
|
|
|
expect(mockClient.sendMessage).toHaveBeenCalledWith(roomId, {
|
|
|
|
"body": "Voice message",
|
|
|
|
"file": undefined,
|
|
|
|
"info": {
|
|
|
|
duration: 1337000,
|
|
|
|
mimetype: "audio/ogg",
|
|
|
|
size: undefined,
|
|
|
|
},
|
|
|
|
"msgtype": MsgType.Audio,
|
|
|
|
"org.matrix.msc1767.audio": {
|
|
|
|
duration: 1337000,
|
2022-09-30 03:06:49 +08:00
|
|
|
waveform: [1434, 2560, 3686],
|
2022-07-13 13:56:36 +08:00
|
|
|
},
|
|
|
|
"org.matrix.msc1767.file": {
|
|
|
|
file: undefined,
|
|
|
|
mimetype: "audio/ogg",
|
|
|
|
name: "Voice message.ogg",
|
|
|
|
size: undefined,
|
|
|
|
url: "mxc://example.com/voice",
|
|
|
|
},
|
|
|
|
"org.matrix.msc1767.text": "Voice message",
|
|
|
|
"org.matrix.msc3245.voice": {},
|
|
|
|
"url": "mxc://example.com/voice",
|
2023-07-12 06:29:54 +08:00
|
|
|
"m.mentions": {},
|
2023-03-23 19:47:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("reply with voice recording", async () => {
|
|
|
|
const room = {
|
|
|
|
roomId,
|
|
|
|
} as unknown as Room;
|
|
|
|
|
|
|
|
const replyToEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@bob:test",
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const props = {
|
|
|
|
room,
|
|
|
|
ref: voiceRecordComposerTile,
|
|
|
|
permalinkCreator: new RoomPermalinkCreator(room),
|
|
|
|
replyToEvent,
|
|
|
|
};
|
|
|
|
render(<VoiceRecordComposerTile {...props} />);
|
|
|
|
|
|
|
|
await voiceRecordComposerTile.current!.send();
|
|
|
|
expect(mockClient.sendMessage).toHaveBeenCalledWith(roomId, {
|
|
|
|
"body": "Voice message",
|
|
|
|
"file": undefined,
|
|
|
|
"info": {
|
|
|
|
duration: 1337000,
|
|
|
|
mimetype: "audio/ogg",
|
|
|
|
size: undefined,
|
|
|
|
},
|
|
|
|
"msgtype": MsgType.Audio,
|
|
|
|
"org.matrix.msc1767.audio": {
|
|
|
|
duration: 1337000,
|
|
|
|
waveform: [1434, 2560, 3686],
|
|
|
|
},
|
|
|
|
"org.matrix.msc1767.file": {
|
|
|
|
file: undefined,
|
|
|
|
mimetype: "audio/ogg",
|
|
|
|
name: "Voice message.ogg",
|
|
|
|
size: undefined,
|
|
|
|
url: "mxc://example.com/voice",
|
|
|
|
},
|
|
|
|
"org.matrix.msc1767.text": "Voice message",
|
|
|
|
"org.matrix.msc3245.voice": {},
|
|
|
|
"url": "mxc://example.com/voice",
|
|
|
|
"m.relates_to": {
|
|
|
|
"m.in_reply_to": {
|
|
|
|
event_id: replyToEvent.getId(),
|
|
|
|
},
|
|
|
|
},
|
2023-07-12 06:29:54 +08:00
|
|
|
"m.mentions": { user_ids: ["@bob:test"] },
|
2022-07-13 13:56:36 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|