2022-10-17 15:31:03 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-17 15:31:03 +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-10-17 15:31:03 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2022-12-16 21:23:29 +03:00
|
|
|
import { EventType, MatrixClient, MatrixEvent, MatrixEventEvent, RelationType, Room } from "matrix-js-sdk/src/matrix";
|
2022-10-17 15:31:03 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import { RelationsHelper, RelationsHelperEvent } from "../../../src/events/RelationsHelper";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
2022-10-17 15:31:03 +03:00
|
|
|
|
|
|
|
describe("RelationsHelper", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let event: MatrixEvent;
|
|
|
|
let relatedEvent1: MatrixEvent;
|
|
|
|
let relatedEvent2: MatrixEvent;
|
2022-11-07 17:19:49 +03:00
|
|
|
let relatedEvent3: MatrixEvent;
|
2022-10-17 15:31:03 +03:00
|
|
|
let room: Room;
|
|
|
|
let client: MatrixClient;
|
|
|
|
let relationsHelper: RelationsHelper;
|
|
|
|
let onAdd: (event: MatrixEvent) => void;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
2022-11-07 17:19:49 +03:00
|
|
|
mocked(client.relations).mockClear();
|
2022-12-16 21:23:29 +03:00
|
|
|
room = new Room(roomId, client, client.getSafeUserId());
|
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId?: string): Room | null => {
|
|
|
|
if (getRoomId === roomId) return room;
|
2022-11-07 17:19:49 +03:00
|
|
|
return null;
|
2022-10-17 15:31:03 +03:00
|
|
|
});
|
|
|
|
event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
room: roomId,
|
2022-12-16 21:23:29 +03:00
|
|
|
user: client.getSafeUserId(),
|
2022-10-17 15:31:03 +03:00
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
relatedEvent1 = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
room: roomId,
|
2022-12-16 21:23:29 +03:00
|
|
|
user: client.getSafeUserId(),
|
|
|
|
content: {
|
|
|
|
["m.relates_to"]: {
|
|
|
|
rel_type: RelationType.Reference,
|
|
|
|
event_id: event.getId(),
|
|
|
|
},
|
|
|
|
},
|
2022-10-17 15:31:03 +03:00
|
|
|
});
|
|
|
|
relatedEvent2 = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
room: roomId,
|
2022-12-16 21:23:29 +03:00
|
|
|
user: client.getSafeUserId(),
|
|
|
|
content: {
|
|
|
|
["m.relates_to"]: {
|
|
|
|
rel_type: RelationType.Reference,
|
|
|
|
event_id: event.getId(),
|
|
|
|
},
|
|
|
|
},
|
2022-11-07 17:19:49 +03:00
|
|
|
});
|
|
|
|
relatedEvent3 = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
room: roomId,
|
2022-12-16 21:23:29 +03:00
|
|
|
user: client.getSafeUserId(),
|
|
|
|
content: {
|
|
|
|
["m.relates_to"]: {
|
|
|
|
rel_type: RelationType.Reference,
|
|
|
|
event_id: event.getId(),
|
|
|
|
},
|
|
|
|
},
|
2022-10-17 15:31:03 +03:00
|
|
|
});
|
|
|
|
onAdd = jest.fn();
|
|
|
|
});
|
|
|
|
|
2022-11-07 17:19:49 +03:00
|
|
|
afterEach(() => {
|
|
|
|
relationsHelper?.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is an event without ID", () => {
|
|
|
|
it("should raise an error", () => {
|
|
|
|
jest.spyOn(event, "getId").mockReturnValue(undefined);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
new RelationsHelper(event, RelationType.Reference, EventType.RoomMessage, client);
|
2023-03-01 18:23:35 +03:00
|
|
|
}).toThrow("unable to create RelationsHelper: missing event ID");
|
2022-11-07 17:19:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is an event without room ID", () => {
|
|
|
|
it("should raise an error", () => {
|
|
|
|
jest.spyOn(event, "getRoomId").mockReturnValue(undefined);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
new RelationsHelper(event, RelationType.Reference, EventType.RoomMessage, client);
|
2023-03-01 18:23:35 +03:00
|
|
|
}).toThrow("unable to create RelationsHelper: missing room ID");
|
2022-11-07 17:19:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-17 15:31:03 +03:00
|
|
|
describe("when there is an event without relations", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
relationsHelper = new RelationsHelper(event, RelationType.Reference, EventType.RoomMessage, client);
|
|
|
|
relationsHelper.on(RelationsHelperEvent.Add, onAdd);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("emitCurrent", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
relationsHelper.emitCurrent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not emit any event", () => {
|
|
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-16 21:23:29 +03:00
|
|
|
describe("and a new event appears", () => {
|
2022-10-17 15:31:03 +03:00
|
|
|
beforeEach(() => {
|
2022-12-16 21:23:29 +03:00
|
|
|
room.relations.aggregateChildEvent(relatedEvent2);
|
2022-10-17 15:31:03 +03:00
|
|
|
event.emit(MatrixEventEvent.RelationsCreated, RelationType.Reference, EventType.RoomMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit the new event", () => {
|
|
|
|
expect(onAdd).toHaveBeenCalledWith(relatedEvent2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-07 17:19:49 +03:00
|
|
|
describe("when there is an event with two pages server side relations", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.relations)
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
events: [relatedEvent1, relatedEvent2],
|
|
|
|
nextBatch: "next",
|
|
|
|
})
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
events: [relatedEvent3],
|
|
|
|
nextBatch: null,
|
|
|
|
});
|
|
|
|
relationsHelper = new RelationsHelper(event, RelationType.Reference, EventType.RoomMessage, client);
|
|
|
|
relationsHelper.on(RelationsHelperEvent.Add, onAdd);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("emitFetchCurrent", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await relationsHelper.emitFetchCurrent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit the server side events", () => {
|
|
|
|
expect(onAdd).toHaveBeenCalledWith(relatedEvent1);
|
|
|
|
expect(onAdd).toHaveBeenCalledWith(relatedEvent2);
|
|
|
|
expect(onAdd).toHaveBeenCalledWith(relatedEvent3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-17 15:31:03 +03:00
|
|
|
describe("when there is an event with relations", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-16 21:23:29 +03:00
|
|
|
room.relations.aggregateChildEvent(relatedEvent1);
|
2022-10-17 15:31:03 +03:00
|
|
|
relationsHelper = new RelationsHelper(event, RelationType.Reference, EventType.RoomMessage, client);
|
|
|
|
relationsHelper.on(RelationsHelperEvent.Add, onAdd);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("emitCurrent", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
relationsHelper.emitCurrent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit the related event", () => {
|
|
|
|
expect(onAdd).toHaveBeenCalledWith(relatedEvent1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and a new event appears", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-16 21:23:29 +03:00
|
|
|
room.relations.aggregateChildEvent(relatedEvent2);
|
|
|
|
event.emit(MatrixEventEvent.RelationsCreated, RelationType.Reference, EventType.RoomMessage);
|
2022-10-17 15:31:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit the new event", () => {
|
|
|
|
expect(onAdd).toHaveBeenCalledWith(relatedEvent2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|