2022-07-25 11:17:40 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-07-25 11:17:40 +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-07-25 11:17:40 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2024-03-18 17:40:52 +03:00
|
|
|
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2022-07-25 11:17:40 +03:00
|
|
|
|
|
|
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
2023-03-02 11:44:12 +03:00
|
|
|
import { createTestClient, makeMembershipEvent, mkThirdPartyInviteEvent } from "../../test-utils";
|
2022-08-01 20:28:33 +03:00
|
|
|
import { LocalRoom } from "../../../src/models/LocalRoom";
|
2022-07-25 11:17:40 +03:00
|
|
|
import { findDMForUser } from "../../../src/utils/dm/findDMForUser";
|
2022-08-01 20:28:33 +03:00
|
|
|
import { getFunctionalMembers } from "../../../src/utils/room/getFunctionalMembers";
|
2022-07-25 11:17:40 +03:00
|
|
|
|
2022-08-01 20:28:33 +03:00
|
|
|
jest.mock("../../../src/utils/room/getFunctionalMembers", () => ({
|
|
|
|
getFunctionalMembers: jest.fn(),
|
2022-07-25 11:17:40 +03:00
|
|
|
}));
|
|
|
|
|
2022-08-01 20:28:33 +03:00
|
|
|
describe("findDMForUser", () => {
|
2022-07-25 11:17:40 +03:00
|
|
|
const userId1 = "@user1:example.com";
|
2022-08-01 20:28:33 +03:00
|
|
|
const userId2 = "@user2:example.com";
|
2023-02-13 10:46:53 +03:00
|
|
|
const userId3 = "@user3:example.com";
|
2022-08-01 20:28:33 +03:00
|
|
|
const botId = "@bot:example.com";
|
2023-03-02 11:44:12 +03:00
|
|
|
const thirdPartyId = "party@example.com";
|
2022-07-25 11:17:40 +03:00
|
|
|
let room1: Room;
|
2022-08-01 20:28:33 +03:00
|
|
|
let room2: LocalRoom;
|
|
|
|
let room3: Room;
|
|
|
|
let room4: Room;
|
|
|
|
let room5: Room;
|
2023-02-13 10:46:53 +03:00
|
|
|
let room6: Room;
|
2023-03-02 11:44:12 +03:00
|
|
|
let room7: Room;
|
|
|
|
const unknownRoomId = "!unknown:example.com";
|
2022-07-25 11:17:40 +03:00
|
|
|
let dmRoomMap: DMRoomMap;
|
2022-08-01 20:28:33 +03:00
|
|
|
let mockClient: MatrixClient;
|
2022-07-25 11:17:40 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient = createTestClient();
|
2022-08-01 20:28:33 +03:00
|
|
|
|
|
|
|
// always return the bot user as functional member
|
|
|
|
mocked(getFunctionalMembers).mockReturnValue([botId]);
|
2022-07-25 11:17:40 +03:00
|
|
|
|
|
|
|
room1 = new Room("!room1:example.com", mockClient, userId1);
|
2024-03-12 17:52:54 +03:00
|
|
|
room1.getMyMembership = () => KnownMembership.Join;
|
2022-08-01 20:28:33 +03:00
|
|
|
room1.currentState.setStateEvents([
|
2024-03-12 17:52:54 +03:00
|
|
|
makeMembershipEvent(room1.roomId, userId1, KnownMembership.Join),
|
|
|
|
makeMembershipEvent(room1.roomId, userId2, KnownMembership.Join),
|
2022-08-01 20:28:33 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
// this should not be a DM room because it is a local room
|
|
|
|
room2 = new LocalRoom("!room2:example.com", mockClient, userId1);
|
2024-03-12 17:52:54 +03:00
|
|
|
room2.getMyMembership = () => KnownMembership.Join;
|
2022-08-01 20:28:33 +03:00
|
|
|
room2.getLastActiveTimestamp = () => 100;
|
|
|
|
|
|
|
|
room3 = new Room("!room3:example.com", mockClient, userId1);
|
2024-03-12 17:52:54 +03:00
|
|
|
room3.getMyMembership = () => KnownMembership.Join;
|
2022-08-01 20:28:33 +03:00
|
|
|
room3.currentState.setStateEvents([
|
2024-03-12 17:52:54 +03:00
|
|
|
makeMembershipEvent(room3.roomId, userId1, KnownMembership.Join),
|
|
|
|
makeMembershipEvent(room3.roomId, userId2, KnownMembership.Join),
|
2022-08-01 20:28:33 +03:00
|
|
|
// Adding the bot user here. Should be excluded when determining if the room is a DM.
|
2024-03-12 17:52:54 +03:00
|
|
|
makeMembershipEvent(room3.roomId, botId, KnownMembership.Join),
|
2022-08-01 20:28:33 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
// this should not be a DM room because it has only one joined user
|
|
|
|
room4 = new Room("!room4:example.com", mockClient, userId1);
|
2024-03-12 17:52:54 +03:00
|
|
|
room4.getMyMembership = () => KnownMembership.Join;
|
2022-08-01 20:28:33 +03:00
|
|
|
room4.currentState.setStateEvents([
|
2024-03-12 17:52:54 +03:00
|
|
|
makeMembershipEvent(room4.roomId, userId1, KnownMembership.Invite),
|
|
|
|
makeMembershipEvent(room4.roomId, userId2, KnownMembership.Join),
|
2022-08-01 20:28:33 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
// this should not be a DM room because it has no users
|
|
|
|
room5 = new Room("!room5:example.com", mockClient, userId1);
|
|
|
|
room5.getLastActiveTimestamp = () => 100;
|
|
|
|
|
2023-02-13 10:46:53 +03:00
|
|
|
// room not correctly stored in userId → room map; should be found by the "all rooms" fallback
|
|
|
|
room6 = new Room("!room6:example.com", mockClient, userId1);
|
2024-03-12 17:52:54 +03:00
|
|
|
room6.getMyMembership = () => KnownMembership.Join;
|
2023-02-13 10:46:53 +03:00
|
|
|
room6.currentState.setStateEvents([
|
2024-03-12 17:52:54 +03:00
|
|
|
makeMembershipEvent(room6.roomId, userId1, KnownMembership.Join),
|
|
|
|
makeMembershipEvent(room6.roomId, userId3, KnownMembership.Join),
|
2023-02-13 10:46:53 +03:00
|
|
|
]);
|
|
|
|
|
2023-03-02 11:44:12 +03:00
|
|
|
// room with pending third-party invite
|
|
|
|
room7 = new Room("!room7:example.com", mockClient, userId1);
|
2024-03-12 17:52:54 +03:00
|
|
|
room7.getMyMembership = () => KnownMembership.Join;
|
2023-03-02 11:44:12 +03:00
|
|
|
room7.currentState.setStateEvents([
|
2024-03-12 17:52:54 +03:00
|
|
|
makeMembershipEvent(room7.roomId, userId1, KnownMembership.Join),
|
2023-03-02 11:44:12 +03:00
|
|
|
mkThirdPartyInviteEvent(thirdPartyId, "third-party", room7.roomId),
|
|
|
|
]);
|
|
|
|
|
2022-08-01 20:28:33 +03:00
|
|
|
mocked(mockClient.getRoom).mockImplementation((roomId: string) => {
|
2023-02-13 10:46:53 +03:00
|
|
|
return (
|
|
|
|
{
|
|
|
|
[room1.roomId]: room1,
|
|
|
|
[room2.roomId]: room2,
|
|
|
|
[room3.roomId]: room3,
|
|
|
|
[room4.roomId]: room4,
|
|
|
|
[room5.roomId]: room5,
|
|
|
|
[room6.roomId]: room6,
|
2023-03-02 11:44:12 +03:00
|
|
|
[room7.roomId]: room7,
|
2023-02-13 10:46:53 +03:00
|
|
|
}[roomId] || null
|
|
|
|
);
|
2022-08-01 20:28:33 +03:00
|
|
|
});
|
2022-07-25 11:17:40 +03:00
|
|
|
|
|
|
|
dmRoomMap = {
|
|
|
|
getDMRoomForIdentifiers: jest.fn(),
|
|
|
|
getDMRoomsForUserId: jest.fn(),
|
2023-02-13 10:46:53 +03:00
|
|
|
getRoomIds: jest.fn().mockReturnValue(
|
|
|
|
new Set([
|
|
|
|
room1.roomId,
|
|
|
|
room2.roomId,
|
|
|
|
room3.roomId,
|
|
|
|
room4.roomId,
|
|
|
|
room5.roomId,
|
|
|
|
room6.roomId,
|
2023-03-02 11:44:12 +03:00
|
|
|
room7.roomId,
|
|
|
|
unknownRoomId, // this room does not exist in client
|
2023-02-13 10:46:53 +03:00
|
|
|
]),
|
|
|
|
),
|
2022-07-25 11:17:40 +03:00
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
2023-02-13 10:46:53 +03:00
|
|
|
mocked(dmRoomMap.getDMRoomsForUserId).mockImplementation((userId: string) => {
|
|
|
|
if (userId === userId1) {
|
2023-03-02 11:44:12 +03:00
|
|
|
return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, unknownRoomId];
|
2023-02-13 10:46:53 +03:00
|
|
|
}
|
|
|
|
|
2023-03-10 17:39:53 +03:00
|
|
|
if (userId === thirdPartyId) {
|
|
|
|
return [room7.roomId];
|
|
|
|
}
|
|
|
|
|
2023-02-13 10:46:53 +03:00
|
|
|
return [];
|
|
|
|
});
|
2022-07-25 11:17:40 +03:00
|
|
|
});
|
|
|
|
|
2022-08-01 20:28:33 +03:00
|
|
|
describe("for an empty DM room list", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([]);
|
2023-02-13 10:46:53 +03:00
|
|
|
mocked(dmRoomMap.getRoomIds).mockReturnValue(new Set());
|
2022-08-01 20:28:33 +03:00
|
|
|
});
|
2022-07-25 11:17:40 +03:00
|
|
|
|
2022-08-01 20:28:33 +03:00
|
|
|
it("should return undefined", () => {
|
|
|
|
expect(findDMForUser(mockClient, userId1)).toBeUndefined();
|
|
|
|
});
|
2022-07-25 11:17:40 +03:00
|
|
|
});
|
|
|
|
|
2022-08-01 20:28:33 +03: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 11:17:40 +03:00
|
|
|
});
|
|
|
|
|
2022-08-01 20:28:33 +03: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 11:17:40 +03:00
|
|
|
});
|
2023-02-13 10:46:53 +03:00
|
|
|
|
|
|
|
it("should find a room by the 'all rooms' fallback", () => {
|
|
|
|
room1.getLastActiveTimestamp = () => 1;
|
|
|
|
room6.getLastActiveTimestamp = () => 2;
|
|
|
|
|
|
|
|
expect(findDMForUser(mockClient, userId3)).toBe(room6);
|
|
|
|
});
|
2023-03-02 11:44:12 +03:00
|
|
|
|
|
|
|
it("should find a room with a pending third-party invite", () => {
|
|
|
|
expect(findDMForUser(mockClient, thirdPartyId)).toBe(room7);
|
|
|
|
});
|
2023-03-10 17:39:53 +03:00
|
|
|
|
|
|
|
it("should not find a room for an unknown Id", () => {
|
|
|
|
expect(findDMForUser(mockClient, "@unknown:example.com")).toBe(undefined);
|
|
|
|
});
|
2022-07-25 11:17:40 +03:00
|
|
|
});
|