2022-07-25 16:17:40 +08: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 { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
2022-08-02 01:28:33 +08:00
|
|
|
import { createTestClient, makeMembershipEvent } from "../../test-utils";
|
|
|
|
import { LocalRoom } from "../../../src/models/LocalRoom";
|
2022-07-25 16:17:40 +08:00
|
|
|
import { findDMForUser } from "../../../src/utils/dm/findDMForUser";
|
2022-08-02 01:28:33 +08:00
|
|
|
import { getFunctionalMembers } from "../../../src/utils/room/getFunctionalMembers";
|
2022-07-25 16:17:40 +08:00
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
jest.mock("../../../src/utils/room/getFunctionalMembers", () => ({
|
|
|
|
getFunctionalMembers: jest.fn(),
|
2022-07-25 16:17:40 +08:00
|
|
|
}));
|
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
describe("findDMForUser", () => {
|
2022-07-25 16:17:40 +08:00
|
|
|
const userId1 = "@user1:example.com";
|
2022-08-02 01:28:33 +08:00
|
|
|
const userId2 = "@user2:example.com";
|
|
|
|
const botId = "@bot:example.com";
|
2022-07-25 16:17:40 +08:00
|
|
|
let room1: Room;
|
2022-08-02 01:28:33 +08:00
|
|
|
let room2: LocalRoom;
|
|
|
|
let room3: Room;
|
|
|
|
let room4: Room;
|
|
|
|
let room5: Room;
|
2022-07-25 16:17:40 +08:00
|
|
|
let dmRoomMap: DMRoomMap;
|
2022-08-02 01:28:33 +08:00
|
|
|
let mockClient: MatrixClient;
|
2022-07-25 16:17:40 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient = createTestClient();
|
2022-08-02 01:28:33 +08:00
|
|
|
|
|
|
|
// always return the bot user as functional member
|
|
|
|
mocked(getFunctionalMembers).mockReturnValue([botId]);
|
2022-07-25 16:17:40 +08:00
|
|
|
|
|
|
|
room1 = new Room("!room1:example.com", mockClient, userId1);
|
|
|
|
room1.getMyMembership = () => "join";
|
2022-08-02 01:28:33 +08:00
|
|
|
room1.currentState.setStateEvents([
|
|
|
|
makeMembershipEvent(room1.roomId, userId1, "join"),
|
|
|
|
makeMembershipEvent(room1.roomId, userId2, "join"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// this should not be a DM room because it is a local room
|
|
|
|
room2 = new LocalRoom("!room2:example.com", mockClient, userId1);
|
|
|
|
room2.getMyMembership = () => "join";
|
|
|
|
room2.getLastActiveTimestamp = () => 100;
|
|
|
|
|
|
|
|
room3 = new Room("!room3:example.com", mockClient, userId1);
|
|
|
|
room3.getMyMembership = () => "join";
|
|
|
|
room3.currentState.setStateEvents([
|
|
|
|
makeMembershipEvent(room3.roomId, userId1, "join"),
|
|
|
|
makeMembershipEvent(room3.roomId, userId2, "join"),
|
|
|
|
// Adding the bot user here. Should be excluded when determining if the room is a DM.
|
|
|
|
makeMembershipEvent(room3.roomId, botId, "join"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// this should not be a DM room because it has only one joined user
|
|
|
|
room4 = new Room("!room4:example.com", mockClient, userId1);
|
|
|
|
room4.getMyMembership = () => "join";
|
|
|
|
room4.currentState.setStateEvents([
|
|
|
|
makeMembershipEvent(room4.roomId, userId1, "invite"),
|
|
|
|
makeMembershipEvent(room4.roomId, userId2, "join"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// this should not be a DM room because it has no users
|
|
|
|
room5 = new Room("!room5:example.com", mockClient, userId1);
|
|
|
|
room5.getLastActiveTimestamp = () => 100;
|
|
|
|
|
|
|
|
mocked(mockClient.getRoom).mockImplementation((roomId: string) => {
|
|
|
|
return {
|
|
|
|
[room1.roomId]: room1,
|
|
|
|
[room2.roomId]: room2,
|
|
|
|
[room3.roomId]: room3,
|
|
|
|
[room4.roomId]: room4,
|
|
|
|
[room5.roomId]: room5,
|
|
|
|
}[roomId];
|
|
|
|
});
|
2022-07-25 16:17:40 +08:00
|
|
|
|
|
|
|
dmRoomMap = {
|
|
|
|
getDMRoomForIdentifiers: jest.fn(),
|
|
|
|
getDMRoomsForUserId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
2022-08-02 01:28:33 +08:00
|
|
|
mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([
|
|
|
|
room1.roomId,
|
|
|
|
room2.roomId,
|
|
|
|
room3.roomId,
|
|
|
|
room4.roomId,
|
|
|
|
room5.roomId,
|
|
|
|
]);
|
2022-07-25 16:17:40 +08:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
describe("for an empty DM room list", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([]);
|
|
|
|
});
|
2022-07-25 16:17:40 +08:00
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
it("should return undefined", () => {
|
|
|
|
expect(findDMForUser(mockClient, userId1)).toBeUndefined();
|
|
|
|
});
|
2022-07-25 16:17:40 +08:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
it("should find a room ordered by last activity 1", () => {
|
|
|
|
room1.getLastActiveTimestamp = () => 2;
|
|
|
|
room3.getLastActiveTimestamp = () => 1;
|
|
|
|
|
|
|
|
expect(findDMForUser(mockClient, userId1)).toBe(room1);
|
2022-07-25 16:17:40 +08:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
it("should find a room ordered by last activity 2", () => {
|
|
|
|
room1.getLastActiveTimestamp = () => 1;
|
|
|
|
room3.getLastActiveTimestamp = () => 2;
|
|
|
|
|
|
|
|
expect(findDMForUser(mockClient, userId1)).toBe(room3);
|
2022-07-25 16:17:40 +08:00
|
|
|
});
|
|
|
|
});
|