Skip to content

Commit 7750916

Browse files
committed
Add tests
1 parent 22b9bc1 commit 7750916

File tree

4 files changed

+164
-6
lines changed

4 files changed

+164
-6
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { IEvent, MatrixEvent } from "../../../src";
2+
import { RoomStickyEvents } from "../../../src/models/room-sticky-events";
3+
4+
describe.only("RoomStickyEvents", () => {
5+
const stickyEvent: IEvent = {
6+
event_id: "$foo:bar",
7+
room_id: "!roomId",
8+
type: "org.example.any_type",
9+
msc4354_sticky: {
10+
duration_ms: 15000,
11+
},
12+
content: {
13+
msc4354_sticky_key: "foobar",
14+
},
15+
sender: "@alice:example.org",
16+
origin_server_ts: Date.now(),
17+
unsigned: {},
18+
};
19+
20+
describe.only("addStickyEvents", () => {
21+
it("should not allow adding an event without a msc4354_sticky_key", () => {
22+
expect(() => new RoomStickyEvents().unstableAddStickyEvent(new MatrixEvent({ ...stickyEvent, content: {} }))).toThrow(
23+
`${stickyEvent.event_id} is missing msc4354_sticky_key`,
24+
);
25+
});
26+
it("should not allow adding an event without a msc4354_sticky property", () => {
27+
expect(() =>
28+
new RoomStickyEvents().unstableAddStickyEvent(new MatrixEvent({ ...stickyEvent, msc4354_sticky: undefined })),
29+
).toThrow(`${stickyEvent.event_id} is missing msc4354_sticky.duration_ms`);
30+
expect(() =>
31+
new RoomStickyEvents().unstableAddStickyEvent(
32+
new MatrixEvent({ ...stickyEvent, msc4354_sticky: { duration_ms: undefined } as any }),
33+
),
34+
).toThrow(`${stickyEvent.event_id} is missing msc4354_sticky.duration_ms`);
35+
});
36+
it("should not allow adding an event without a sender", () => {
37+
expect(() =>
38+
new RoomStickyEvents().unstableAddStickyEvent(new MatrixEvent({ ...stickyEvent, sender: undefined })),
39+
).toThrow(`${stickyEvent.event_id} is missing a sender`);
40+
});
41+
it("should ignore old events", () => {
42+
expect(
43+
new RoomStickyEvents().unstableAddStickyEvent(
44+
new MatrixEvent({
45+
...stickyEvent,
46+
origin_server_ts: 0,
47+
msc4354_sticky: {
48+
duration_ms: 1,
49+
},
50+
}),
51+
),
52+
).toEqual({ added: false });
53+
});
54+
it("should not replace newer events", () => {
55+
const stickyEvents = new RoomStickyEvents();
56+
expect(
57+
stickyEvents.unstableAddStickyEvent(
58+
new MatrixEvent({
59+
...stickyEvent,
60+
}),
61+
),
62+
).toEqual({ added: true });
63+
expect(
64+
stickyEvents.unstableAddStickyEvent(
65+
new MatrixEvent({
66+
...stickyEvent,
67+
origin_server_ts: 1,
68+
}),
69+
),
70+
).toEqual({ added: false });
71+
});
72+
it("should not replace events on ID tie break", () => {
73+
const stickyEvents = new RoomStickyEvents();
74+
expect(
75+
stickyEvents.unstableAddStickyEvent(
76+
new MatrixEvent({
77+
...stickyEvent,
78+
}),
79+
),
80+
).toEqual({ added: true });
81+
expect(
82+
stickyEvents.unstableAddStickyEvent(
83+
new MatrixEvent({
84+
...stickyEvent,
85+
event_id: "$abc:bar",
86+
}),
87+
),
88+
).toEqual({ added: false });
89+
});
90+
});
91+
92+
describe("getStickyEvents", () => {
93+
it("should have zero sticky events", () => {
94+
expect([...new RoomStickyEvents().unstableGetStickyEvents()]).toHaveLength(0);
95+
});
96+
it("should contain a sticky event", () => {
97+
const stickyEvents = new RoomStickyEvents();
98+
const ev = new MatrixEvent({
99+
...stickyEvent,
100+
});
101+
stickyEvents.unstableAddStickyEvent(
102+
new MatrixEvent({
103+
...stickyEvent,
104+
}),
105+
);
106+
expect([...stickyEvents.unstableGetStickyEvents()]).toEqual([ev]);
107+
});
108+
});
109+
});

spec/unit/models/room.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
1716
import { Direction, type MatrixClient, MatrixEvent, Room } from "../../../src";
1817
import type { MockedObject } from "jest-mock";
1918

spec/unit/sync-accumulator.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
type ILeftRoom,
2727
type IRoomEvent,
2828
type IStateEvent,
29+
IStickyEvent,
2930
type IStrippedState,
3031
type ISyncResponse,
3132
SyncAccumulator,
@@ -1067,6 +1068,55 @@ describe("SyncAccumulator", function () {
10671068
);
10681069
});
10691070
});
1071+
1072+
describe.only("MSC4354 sticky events", () => {
1073+
function stickyEvent(ts = 0): IStickyEvent {
1074+
const msgData = msg("test", "test text");
1075+
return {
1076+
...msgData,
1077+
msc4354_sticky: {
1078+
duration_ms: 1000,
1079+
},
1080+
origin_server_ts: ts,
1081+
};
1082+
}
1083+
1084+
beforeAll(() => {
1085+
jest.useFakeTimers();
1086+
});
1087+
1088+
afterAll(() => {
1089+
jest.useRealTimers();
1090+
});
1091+
1092+
it("should accumulate sticky events", () => {
1093+
jest.setSystemTime(0);
1094+
const ev = stickyEvent();
1095+
sa.accumulate(
1096+
syncSkeleton({
1097+
msc4354_sticky: {
1098+
events: [ev],
1099+
},
1100+
}),
1101+
);
1102+
expect(sa.getJSON().roomsData[Category.Join]["!foo:bar"].msc4354_sticky?.events).toEqual([ev]);
1103+
});
1104+
it("should clear stale sticky events", () => {
1105+
jest.setSystemTime(1000);
1106+
const ev = stickyEvent(1000);
1107+
sa.accumulate(
1108+
syncSkeleton({
1109+
msc4354_sticky: {
1110+
events: [ev, stickyEvent(0)],
1111+
},
1112+
}),
1113+
);
1114+
expect(sa.getJSON().roomsData[Category.Join]["!foo:bar"].msc4354_sticky?.events).toEqual([ev]);
1115+
jest.setSystemTime(2000); // Expire the event
1116+
sa.accumulate(syncSkeleton({}));
1117+
expect(sa.getJSON().roomsData[Category.Join]["!foo:bar"].msc4354_sticky?.events).toHaveLength(0);
1118+
});
1119+
});
10701120
});
10711121

10721122
function syncSkeleton(

src/matrixrtc/MatrixRTCSession.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
298298
* @deprecated Use `MatrixRTCSession.sessionMembershipsForRoom` instead.
299299
*/
300300
public static callMembershipsForRoom(
301-
room: Pick<Room, "getLiveTimeline" | "roomId" | "hasMembershipState" | "getStickyEventsMap">,
301+
room: Pick<Room, "getLiveTimeline" | "roomId" | "hasMembershipState" | "unstableGetStickyEvents">,
302302
): CallMembership[] {
303303
return MatrixRTCSession.sessionMembershipsForRoom(room, {
304304
id: "",
@@ -311,7 +311,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
311311
* oldest first.
312312
*/
313313
public static sessionMembershipsForRoom(
314-
room: Pick<Room, "getLiveTimeline" | "roomId" | "hasMembershipState" | "getStickyEventsMap">,
314+
room: Pick<Room, "getLiveTimeline" | "roomId" | "hasMembershipState" | "unstableGetStickyEvents">,
315315
sessionDescription: SessionDescription,
316316
// default both true this implied we combine sticky and state events for the final call state
317317
// (prefer sticky events in case of a duplicate)
@@ -322,7 +322,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
322322
let callMemberEvents = [] as MatrixEvent[];
323323
if (useStickyEvents) {
324324
// prefill with sticky events
325-
callMemberEvents = Array.from(room.getStickyEventsMap().values()).filter(
325+
callMemberEvents = Array.from(room.unstableGetStickyEvents()).filter(
326326
(e) => e.getType() === EventType.GroupCallMemberPrefix,
327327
);
328328
}
@@ -790,8 +790,8 @@ export class MatrixRTCSession extends TypedEventEmitter<
790790
this.recalculateSessionMembers();
791791
};
792792

793-
private onStickyEventUpdate = (stickyEvents: Map<string, MatrixEvent>, room: Room): void => {
794-
if (Array.from(stickyEvents.values()).some((e) => e.getType() === EventType.GroupCallMemberPrefix)) {
793+
private onStickyEventUpdate = (added: MatrixEvent[], _removed: MatrixEvent[], room: Room): void => {
794+
if ([...added, ..._removed].some((e) => e.getType() === EventType.GroupCallMemberPrefix)) {
795795
this.recalculateSessionMembers();
796796
}
797797
};

0 commit comments

Comments
 (0)