From d469eb0b6f381f8326d5683537702fef42b4ab6e Mon Sep 17 00:00:00 2001 From: Timo Date: Tue, 18 Jun 2024 12:21:03 +0200 Subject: [PATCH] test skipping insertion --- spec/unit/room-state.spec.ts | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/spec/unit/room-state.spec.ts b/spec/unit/room-state.spec.ts index 9e0c29f35e8..20bc60b8bec 100644 --- a/spec/unit/room-state.spec.ts +++ b/spec/unit/room-state.spec.ts @@ -1122,4 +1122,54 @@ describe("RoomState", function () { ).toBeFalsy(); }); }); + describe("skipWrongOrderRoomStateInserts", () => { + const idNow = "now"; + const idBefore = "before"; + let onRoomStateEvent: (event: MatrixEvent, state: RoomState, lastStateEvent: MatrixEvent | null) => void; + const evNow = new MatrixEvent({ + type: "m.call.member", + room_id: roomId, + state_key: "@user:example.org", + content: {}, + }); + evNow.event.unsigned = { replaces_state: idBefore }; + evNow.event.event_id = idNow; + + const evBefore = new MatrixEvent({ + type: "m.call.member", + room_id: roomId, + state_key: "@user:example.org", + content: {}, + }); + + const updatedStateWithBefore = jest.fn(); + const updatedStateWithNow = jest.fn(); + + beforeEach(() => { + evBefore.event.event_id = idBefore; + onRoomStateEvent = (event, _state, _lastState) => { + if (event.event.event_id === idNow) { + updatedStateWithNow(); + } else if (event.event.event_id === idBefore) { + updatedStateWithBefore(); + } + }; + state.on(RoomStateEvent.Events, onRoomStateEvent); + }); + afterEach(() => { + state.off(RoomStateEvent.Events, onRoomStateEvent); + updatedStateWithNow.mockReset(); + updatedStateWithBefore.mockReset(); + }); + it("should skip inserting state to the end of the timeline if the current endState events replaces_state id is the same as the inserted events id", () => { + state.setStateEvents([evNow, evBefore], { toStartOfTimeline: false }); + expect(updatedStateWithBefore).not.toHaveBeenCalled(); + expect(updatedStateWithNow).toHaveBeenCalled(); + }); + it("should skip inserting state at the beginning of the timeline if the inserted events replaces_state is the event id of the current startState", () => { + state.setStateEvents([evBefore, evNow], { toStartOfTimeline: true }); + expect(updatedStateWithBefore).toHaveBeenCalled(); + expect(updatedStateWithNow).not.toHaveBeenCalled(); + }); + }); });