2023-02-13 15:46:53 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-02-13 15:46:53 +08:00
|
|
|
Copyright 2023 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.
|
2023-02-13 15:46:53 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked, Mocked } from "jest-mock";
|
2023-03-27 14:37:35 +08:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-04-03 16:26:55 +08:00
|
|
|
import { ClientEvent, EventType, IContent, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2023-02-13 15:46:53 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
2023-02-13 15:46:53 +08:00
|
|
|
describe("DMRoomMap", () => {
|
|
|
|
const roomId1 = "!room1:example.com";
|
|
|
|
const roomId2 = "!room2:example.com";
|
|
|
|
const roomId3 = "!room3:example.com";
|
|
|
|
const roomId4 = "!room4:example.com";
|
|
|
|
|
2023-03-27 14:37:35 +08:00
|
|
|
const validMDirectContent = {
|
2023-02-13 15:46:53 +08:00
|
|
|
"user@example.com": [roomId1, roomId2],
|
|
|
|
"@user:example.com": [roomId1, roomId3, roomId4],
|
|
|
|
"@user2:example.com": [] as string[],
|
2023-02-28 16:58:23 +08:00
|
|
|
} as IContent;
|
2023-02-13 15:46:53 +08:00
|
|
|
|
|
|
|
let client: Mocked<MatrixClient>;
|
|
|
|
let dmRoomMap: DMRoomMap;
|
|
|
|
|
2023-03-27 14:37:35 +08:00
|
|
|
const mkMDirectEvent = (content: any): MatrixEvent => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.Direct,
|
|
|
|
user: client.getSafeUserId(),
|
|
|
|
content: content,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-13 15:46:53 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
client = mocked(stubClient());
|
2023-03-27 14:37:35 +08:00
|
|
|
jest.spyOn(logger, "warn");
|
|
|
|
});
|
2023-02-13 15:46:53 +08:00
|
|
|
|
2023-03-27 14:37:35 +08:00
|
|
|
describe("when m.direct has valid content", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
client.getAccountData.mockReturnValue(mkMDirectEvent(validMDirectContent));
|
|
|
|
dmRoomMap = new DMRoomMap(client);
|
|
|
|
dmRoomMap.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomIds should return the room Ids", () => {
|
|
|
|
expect(dmRoomMap.getRoomIds()).toEqual(new Set([roomId1, roomId2, roomId3, roomId4]));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is an update with valid data", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
client.emit(
|
|
|
|
ClientEvent.AccountData,
|
|
|
|
mkMDirectEvent({
|
|
|
|
"@user:example.com": [roomId1, roomId3],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomIds should return the new room Ids", () => {
|
|
|
|
expect(dmRoomMap.getRoomIds()).toEqual(new Set([roomId1, roomId3]));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is an update with invalid data", () => {
|
|
|
|
const partiallyInvalidContent = {
|
|
|
|
"@user1:example.com": [roomId1, roomId3],
|
|
|
|
"@user2:example.com": "room2, room3",
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client.emit(ClientEvent.AccountData, mkMDirectEvent(partiallyInvalidContent));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomIds should return the valid room Ids", () => {
|
|
|
|
expect(dmRoomMap.getRoomIds()).toEqual(new Set([roomId1, roomId3]));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should log the invalid content", () => {
|
|
|
|
expect(logger.warn).toHaveBeenCalledWith("Invalid m.direct content occurred", partiallyInvalidContent);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when m.direct content contains the entire event", () => {
|
|
|
|
const mDirectContentContent = {
|
2023-02-13 15:46:53 +08:00
|
|
|
type: EventType.Direct,
|
2023-03-27 14:37:35 +08:00
|
|
|
content: validMDirectContent,
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client.getAccountData.mockReturnValue(mkMDirectEvent(mDirectContentContent));
|
|
|
|
dmRoomMap = new DMRoomMap(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should log the invalid content", () => {
|
|
|
|
expect(logger.warn).toHaveBeenCalledWith("Invalid m.direct content occurred", mDirectContentContent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomIds should return an empty list", () => {
|
|
|
|
expect(dmRoomMap.getRoomIds()).toEqual(new Set([]));
|
2023-02-13 15:46:53 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-27 14:37:35 +08:00
|
|
|
describe("when partially crap m.direct content appears", () => {
|
|
|
|
const partiallyCrapContent = {
|
|
|
|
"hello": 23,
|
|
|
|
"@user1:example.com": [] as string[],
|
|
|
|
"@user2:example.com": [roomId1, roomId2],
|
|
|
|
"@user3:example.com": "room1, room2, room3",
|
|
|
|
"@user4:example.com": [roomId4],
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client.getAccountData.mockReturnValue(mkMDirectEvent(partiallyCrapContent));
|
|
|
|
dmRoomMap = new DMRoomMap(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should log the invalid content", () => {
|
|
|
|
expect(logger.warn).toHaveBeenCalledWith("Invalid m.direct content occurred", partiallyCrapContent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomIds should only return the valid items", () => {
|
|
|
|
expect(dmRoomMap.getRoomIds()).toEqual(new Set([roomId1, roomId2, roomId4]));
|
|
|
|
});
|
2023-02-13 15:46:53 +08:00
|
|
|
});
|
2023-04-03 16:26:55 +08:00
|
|
|
|
|
|
|
describe("getUniqueRoomsWithIndividuals()", () => {
|
|
|
|
const bigRoom = {
|
|
|
|
roomId: "!bigRoom:server.org",
|
|
|
|
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(5000),
|
|
|
|
} as unknown as Room;
|
|
|
|
const dmWithBob = {
|
|
|
|
roomId: "!dmWithBob:server.org",
|
|
|
|
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(2),
|
|
|
|
} as unknown as Room;
|
|
|
|
const dmWithCharlie = {
|
|
|
|
roomId: "!dmWithCharlie:server.org",
|
|
|
|
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(2),
|
|
|
|
} as unknown as Room;
|
|
|
|
const smallRoom = {
|
|
|
|
roomId: "!smallRoom:server.org",
|
|
|
|
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(3),
|
|
|
|
} as unknown as Room;
|
|
|
|
|
|
|
|
const mDirectContent = {
|
|
|
|
"@bob:server.org": [bigRoom.roomId, dmWithBob.roomId, smallRoom.roomId],
|
|
|
|
"@charlie:server.org": [dmWithCharlie.roomId, smallRoom.roomId],
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client.getAccountData.mockReturnValue(mkMDirectEvent(mDirectContent));
|
2023-04-04 18:41:46 +08:00
|
|
|
client.getRoom.mockImplementation(
|
|
|
|
(roomId: string) =>
|
|
|
|
[bigRoom, smallRoom, dmWithCharlie, dmWithBob].find((room) => room.roomId === roomId) ?? null,
|
2023-04-03 16:26:55 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns an empty object when room map has not been populated", () => {
|
|
|
|
const instance = new DMRoomMap(client);
|
|
|
|
expect(instance.getUniqueRoomsWithIndividuals()).toEqual({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns map of users to rooms with 2 members", () => {
|
|
|
|
const dmRoomMap = new DMRoomMap(client);
|
|
|
|
dmRoomMap.start();
|
|
|
|
expect(dmRoomMap.getUniqueRoomsWithIndividuals()).toEqual({
|
|
|
|
"@bob:server.org": dmWithBob,
|
|
|
|
"@charlie:server.org": dmWithCharlie,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("excludes rooms that are not found by matrixClient", () => {
|
2023-04-04 18:41:46 +08:00
|
|
|
client.getRoom.mockReset().mockReturnValue(null);
|
2023-04-03 16:26:55 +08:00
|
|
|
const dmRoomMap = new DMRoomMap(client);
|
|
|
|
dmRoomMap.start();
|
|
|
|
expect(dmRoomMap.getUniqueRoomsWithIndividuals()).toEqual({});
|
|
|
|
});
|
|
|
|
});
|
2023-02-13 15:46:53 +08:00
|
|
|
});
|