2022-07-25 10:17:40 +02: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";
|
2024-03-18 15:40:52 +01:00
|
|
|
import { ClientEvent, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2023-02-16 14:17:43 +01:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-07-25 10:17:40 +02:00
|
|
|
|
|
|
|
import DMRoomMap from "../../src/utils/DMRoomMap";
|
|
|
|
import { createTestClient } from "../test-utils";
|
|
|
|
import { LocalRoom, LocalRoomState, LOCAL_ROOM_ID_PREFIX } from "../../src/models/LocalRoom";
|
|
|
|
import * as dmModule from "../../src/utils/direct-messages";
|
|
|
|
import dis from "../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../src/dispatcher/actions";
|
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
|
|
|
import { waitForRoomReadyAndApplyAfterCreateCallbacks } from "../../src/utils/local-room";
|
|
|
|
import { findDMRoom } from "../../src/utils/dm/findDMRoom";
|
|
|
|
import { createDmLocalRoom } from "../../src/utils/dm/createDmLocalRoom";
|
|
|
|
import { startDm } from "../../src/utils/dm/startDm";
|
2023-02-16 14:17:43 +01:00
|
|
|
import { Member } from "../../src/utils/direct-messages";
|
|
|
|
import { resolveThreePids } from "../../src/utils/threepids";
|
2022-07-25 10:17:40 +02:00
|
|
|
|
|
|
|
jest.mock("../../src/utils/rooms", () => ({
|
|
|
|
...(jest.requireActual("../../src/utils/rooms") as object),
|
|
|
|
privateShouldBeEncrypted: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../src/createRoom", () => ({
|
|
|
|
...(jest.requireActual("../../src/createRoom") as object),
|
|
|
|
canEncryptToAllUsers: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../src/utils/local-room", () => ({
|
|
|
|
waitForRoomReadyAndApplyAfterCreateCallbacks: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../src/utils/dm/findDMForUser", () => ({
|
|
|
|
findDMForUser: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../src/utils/dm/findDMRoom", () => ({
|
|
|
|
findDMRoom: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../src/utils/dm/createDmLocalRoom", () => ({
|
|
|
|
createDmLocalRoom: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../src/utils/dm/startDm", () => ({
|
|
|
|
startDm: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2023-02-16 14:17:43 +01:00
|
|
|
jest.mock("../../src/utils/threepids", () => ({
|
|
|
|
resolveThreePids: jest.fn().mockImplementation(async (members: Member[]) => {
|
|
|
|
return members;
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
|
2022-07-25 10:17:40 +02:00
|
|
|
describe("direct-messages", () => {
|
|
|
|
const userId1 = "@user1:example.com";
|
|
|
|
const member1 = new dmModule.DirectoryMember({ user_id: userId1 });
|
|
|
|
let room1: Room;
|
|
|
|
let localRoom: LocalRoom;
|
|
|
|
let dmRoomMap: DMRoomMap;
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
let roomEvents: Room[];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient = createTestClient();
|
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
|
|
|
roomEvents = [];
|
|
|
|
mockClient.on(ClientEvent.Room, (room: Room) => {
|
|
|
|
roomEvents.push(room);
|
|
|
|
});
|
|
|
|
|
|
|
|
room1 = new Room("!room1:example.com", mockClient, userId1);
|
2024-03-12 15:52:54 +01:00
|
|
|
room1.getMyMembership = () => KnownMembership.Join;
|
2022-07-25 10:17:40 +02:00
|
|
|
|
|
|
|
localRoom = new LocalRoom(LOCAL_ROOM_ID_PREFIX + "test", mockClient, userId1);
|
|
|
|
|
|
|
|
dmRoomMap = {
|
|
|
|
getDMRoomForIdentifiers: jest.fn(),
|
|
|
|
getDMRoomsForUserId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
|
|
|
jest.spyOn(dis, "dispatch");
|
2023-02-16 14:17:43 +01:00
|
|
|
jest.spyOn(logger, "warn");
|
2022-07-25 10:17:40 +02:00
|
|
|
|
2023-02-16 14:17:43 +01:00
|
|
|
jest.useFakeTimers();
|
2022-07-25 10:17:40 +02:00
|
|
|
jest.setSystemTime(new Date(2022, 7, 4, 11, 12, 30, 42));
|
|
|
|
});
|
|
|
|
|
2023-02-16 14:17:43 +01:00
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-07-25 10:17:40 +02:00
|
|
|
describe("startDmOnFirstMessage", () => {
|
|
|
|
describe("if no room exists", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(findDMRoom).mockReturnValue(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create a local room and dispatch a view room event", async () => {
|
|
|
|
mocked(createDmLocalRoom).mockResolvedValue(localRoom);
|
2023-02-16 14:17:43 +01:00
|
|
|
const members = [member1];
|
2023-03-06 12:08:04 +01:00
|
|
|
const roomId = await dmModule.startDmOnFirstMessage(mockClient, members);
|
|
|
|
expect(roomId).toBe(localRoom.roomId);
|
2022-07-25 10:17:40 +02:00
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
2023-03-06 12:08:04 +01:00
|
|
|
room_id: roomId,
|
2022-07-25 10:17:40 +02:00
|
|
|
joining: false,
|
|
|
|
targets: [member1],
|
|
|
|
});
|
2023-02-16 14:17:43 +01:00
|
|
|
|
|
|
|
// assert, that startDmOnFirstMessage tries to resolve 3rd-party IDs
|
|
|
|
expect(resolveThreePids).toHaveBeenCalledWith(members, mockClient);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work when resolveThreePids raises an error", async () => {
|
|
|
|
const error = new Error("error 4711");
|
|
|
|
mocked(resolveThreePids).mockRejectedValue(error);
|
|
|
|
|
|
|
|
mocked(createDmLocalRoom).mockResolvedValue(localRoom);
|
|
|
|
const members = [member1];
|
2023-03-06 12:08:04 +01:00
|
|
|
const roomId = await dmModule.startDmOnFirstMessage(mockClient, members);
|
|
|
|
expect(roomId).toBe(localRoom.roomId);
|
2023-02-16 14:17:43 +01:00
|
|
|
|
|
|
|
// ensure that startDmOnFirstMessage tries to resolve 3rd-party IDs
|
|
|
|
expect(resolveThreePids).toHaveBeenCalledWith(members, mockClient);
|
|
|
|
|
|
|
|
// ensure that the error is logged
|
|
|
|
expect(logger.warn).toHaveBeenCalledWith("Error resolving 3rd-party members", error);
|
2022-07-25 10:17:40 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("if a room exists", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(findDMRoom).mockReturnValue(room1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the room and dispatch a view room event", async () => {
|
2023-03-06 12:08:04 +01:00
|
|
|
const roomId = await dmModule.startDmOnFirstMessage(mockClient, [member1]);
|
|
|
|
expect(roomId).toBe(room1.roomId);
|
2022-07-25 10:17:40 +02:00
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room1.roomId,
|
|
|
|
should_peek: false,
|
|
|
|
joining: false,
|
|
|
|
metricsTrigger: "MessageUser",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("createRoomFromLocalRoom", () => {
|
|
|
|
[LocalRoomState.CREATING, LocalRoomState.CREATED, LocalRoomState.ERROR].forEach((state: LocalRoomState) => {
|
|
|
|
it(`should do nothing for room in state ${state}`, async () => {
|
|
|
|
localRoom.state = state;
|
|
|
|
await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
|
|
|
|
expect(localRoom.state).toBe(state);
|
|
|
|
expect(startDm).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("on startDm error", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(startDm).mockRejectedValue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set the room state to error", async () => {
|
|
|
|
await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
|
|
|
|
expect(localRoom.state).toBe(LocalRoomState.ERROR);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("on startDm success", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(waitForRoomReadyAndApplyAfterCreateCallbacks).mockResolvedValue(room1.roomId);
|
|
|
|
mocked(startDm).mockResolvedValue(room1.roomId);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should set the room into creating state and call waitForRoomReadyAndApplyAfterCreateCallbacks", async () => {
|
|
|
|
const result = await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
|
|
|
|
expect(result).toBe(room1.roomId);
|
|
|
|
expect(localRoom.state).toBe(LocalRoomState.CREATING);
|
2023-06-01 13:55:26 +02:00
|
|
|
expect(waitForRoomReadyAndApplyAfterCreateCallbacks).toHaveBeenCalledWith(
|
|
|
|
mockClient,
|
|
|
|
localRoom,
|
|
|
|
room1.roomId,
|
|
|
|
);
|
2022-12-12 12:24:14 +01:00
|
|
|
});
|
2022-07-25 10:17:40 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|