2022-07-25 08:17:40 +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 { ClientEvent, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
2023-02-16 13:17:43 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-07-25 08:17:40 +00: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 13:17:43 +00:00
|
|
|
import { Member } from "../../src/utils/direct-messages";
|
|
|
|
import { resolveThreePids } from "../../src/utils/threepids";
|
2022-07-25 08:17:40 +00: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 13:17:43 +00:00
|
|
|
jest.mock("../../src/utils/threepids", () => ({
|
|
|
|
resolveThreePids: jest.fn().mockImplementation(async (members: Member[]) => {
|
|
|
|
return members;
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
|
2022-07-25 08:17:40 +00: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);
|
|
|
|
room1.getMyMembership = () => "join";
|
|
|
|
|
|
|
|
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 13:17:43 +00:00
|
|
|
jest.spyOn(logger, "warn");
|
2022-07-25 08:17:40 +00:00
|
|
|
|
2023-02-16 13:17:43 +00:00
|
|
|
jest.useFakeTimers();
|
2022-07-25 08:17:40 +00:00
|
|
|
jest.setSystemTime(new Date(2022, 7, 4, 11, 12, 30, 42));
|
|
|
|
});
|
|
|
|
|
2023-02-16 13:17:43 +00:00
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-07-25 08:17:40 +00: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 13:17:43 +00:00
|
|
|
const members = [member1];
|
|
|
|
const room = await dmModule.startDmOnFirstMessage(mockClient, members);
|
2022-07-25 08:17:40 +00:00
|
|
|
expect(room).toBe(localRoom);
|
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
joining: false,
|
|
|
|
targets: [member1],
|
|
|
|
});
|
2023-02-16 13:17:43 +00: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];
|
|
|
|
const room = await dmModule.startDmOnFirstMessage(mockClient, members);
|
|
|
|
expect(room).toBe(localRoom);
|
|
|
|
|
|
|
|
// 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 08:17:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("if a room exists", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(findDMRoom).mockReturnValue(room1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the room and dispatch a view room event", async () => {
|
|
|
|
const room = await dmModule.startDmOnFirstMessage(mockClient, [member1]);
|
|
|
|
expect(room).toBe(room1);
|
|
|
|
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 11:24:14 +00: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);
|
|
|
|
expect(waitForRoomReadyAndApplyAfterCreateCallbacks).toHaveBeenCalledWith(mockClient, localRoom);
|
|
|
|
});
|
2022-07-25 08:17:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|