diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index 8b451b48847..05bd12200e6 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -675,6 +675,69 @@ describe("Thread", () => { }); }); }); + + describe("addEvent", () => { + describe("Given server support for threads", () => { + let previousThreadHasServerSideSupport: FeatureSupport; + + beforeAll(() => { + previousThreadHasServerSideSupport = Thread.hasServerSideSupport; + Thread.hasServerSideSupport = FeatureSupport.Stable; + }); + + afterAll(() => { + Thread.hasServerSideSupport = previousThreadHasServerSideSupport; + }); + + it("Adds events even if they appear out of order", async () => { + // Given a thread exists + const client = createClient(); + const user = "@alice:matrix.org"; + const room = "!room:z"; + const thread = await createThread(client, user, room); + const prevNumEvents = thread.timeline.length; + + // When two messages come in but the later one has an older timestamp + const message1 = createThreadMessage(thread.id, user, room, "message1"); + const message2 = createThreadMessage(thread.id, user, room, "message2"); + message2.localTimestamp -= 10000; + + await thread.addEvent(message1, false); + await thread.addEvent(message2, false); + + // Then both events end up in the timeline + expect(thread.timeline.length - prevNumEvents).toEqual(2); + const lastEvent = thread.timeline.at(-1)!; + const secondLastEvent = thread.timeline.at(-2)!; + expect(lastEvent).toBe(message2); + expect(secondLastEvent).toBe(message1); + }); + + it("Adds events to start even if they appear out of order", async () => { + // Given a thread exists + const client = createClient(); + const user = "@alice:matrix.org"; + const room = "!room:z"; + const thread = await createThread(client, user, room); + const prevNumEvents = thread.timeline.length; + + // When two messages come in but the later one has an older timestamp + const message1 = createThreadMessage(thread.id, user, room, "message1"); + const message2 = createThreadMessage(thread.id, user, room, "message2"); + message2.localTimestamp -= 10000; + + await thread.addEvent(message1, false); + await thread.addEvent(message2, true); + + // Then both events end up in the timeline + expect(thread.timeline.length - prevNumEvents).toEqual(2); + const lastEvent = thread.timeline.at(-1)!; + const firstEvent = thread.timeline.at(0)!; + expect(lastEvent).toBe(message1); + expect(firstEvent).toBe(message2); + }); + }); + }); }); /** diff --git a/src/models/thread.ts b/src/models/thread.ts index 4003f810f0f..1da3fca558b 100644 --- a/src/models/thread.ts +++ b/src/models/thread.ts @@ -430,22 +430,19 @@ export class Thread extends ReadReceipt