@@ -54,6 +54,7 @@ import { Crypto } from "../../src/crypto";
54
54
import { mkThread } from "../test-utils/thread" ;
55
55
import { getMockClientWithEventEmitter , mockClientMethodsUser } from "../test-utils/client" ;
56
56
import { logger } from "../../src/logger" ;
57
+ import { IMessageOpts } from "../test-utils/test-utils" ;
57
58
58
59
describe ( "Room" , function ( ) {
59
60
const roomId = "!foo:bar" ;
@@ -63,9 +64,10 @@ describe("Room", function () {
63
64
const userD = "@dorothy:bar" ;
64
65
let room : Room ;
65
66
66
- const mkMessage = ( ) =>
67
+ const mkMessage = ( opts ?: Partial < IMessageOpts > ) =>
67
68
utils . mkMessage (
68
69
{
70
+ ...opts ,
69
71
event : true ,
70
72
user : userA ,
71
73
room : roomId ,
@@ -113,9 +115,10 @@ describe("Room", function () {
113
115
room . client ,
114
116
) ;
115
117
116
- const mkThreadResponse = ( root : MatrixEvent ) =>
118
+ const mkThreadResponse = ( root : MatrixEvent , opts ?: Partial < IMessageOpts > ) =>
117
119
utils . mkEvent (
118
120
{
121
+ ...opts ,
119
122
event : true ,
120
123
type : EventType . RoomMessage ,
121
124
user : userA ,
@@ -165,6 +168,32 @@ describe("Room", function () {
165
168
room . client ,
166
169
) ;
167
170
171
+ const addRoomMainAndThreadMessages = (
172
+ room : Room ,
173
+ tsMain ?: number ,
174
+ tsThread ?: number ,
175
+ ) : { mainEvent ?: MatrixEvent ; threadEvent ?: MatrixEvent } => {
176
+ const result : { mainEvent ?: MatrixEvent ; threadEvent ?: MatrixEvent } = { } ;
177
+
178
+ if ( tsMain ) {
179
+ result . mainEvent = mkMessage ( { ts : tsMain } ) ;
180
+ room . addLiveEvents ( [ result . mainEvent ] ) ;
181
+ }
182
+
183
+ if ( tsThread ) {
184
+ const { rootEvent, thread } = mkThread ( {
185
+ room,
186
+ client : new TestClient ( ) . client ,
187
+ authorId : "@bob:example.org" ,
188
+ participantUserIds : [ "@bob:example.org" ] ,
189
+ } ) ;
190
+ result . threadEvent = mkThreadResponse ( rootEvent , { ts : tsThread } ) ;
191
+ thread . liveTimeline . addEvent ( result . threadEvent , true ) ;
192
+ }
193
+
194
+ return result ;
195
+ } ;
196
+
168
197
beforeEach ( function ( ) {
169
198
room = new Room ( roomId , new TestClient ( userA , "device" ) . client , userA ) ;
170
199
// mock RoomStates
@@ -3475,4 +3504,56 @@ describe("Room", function () {
3475
3504
expect ( room . findPredecessor ( ) ) . toBeNull ( ) ;
3476
3505
} ) ;
3477
3506
} ) ;
3507
+
3508
+ describe ( "getLastLiveEvent" , ( ) => {
3509
+ let lastEventInMainTimeline : MatrixEvent ;
3510
+ let lastEventInThread : MatrixEvent ;
3511
+
3512
+ it ( "when there are no events, it should return undefined" , ( ) => {
3513
+ expect ( room . getLastLiveEvent ( ) ) . toBeUndefined ( ) ;
3514
+ } ) ;
3515
+
3516
+ describe ( "when there is only an event in the main timeline and there are no threads" , ( ) => {
3517
+ beforeEach ( ( ) => {
3518
+ lastEventInMainTimeline = addRoomMainAndThreadMessages ( room , 23 ) . mainEvent ! ;
3519
+ room . addLiveEvents ( [ lastEventInMainTimeline ] ) ;
3520
+ } ) ;
3521
+
3522
+ it ( "should return the last event from the main timeline" , ( ) => {
3523
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3524
+ } ) ;
3525
+ } ) ;
3526
+
3527
+ describe ( "when there is no event in the room live timeline, but in a thread" , ( ) => {
3528
+ beforeEach ( ( ) => {
3529
+ lastEventInThread = addRoomMainAndThreadMessages ( room , undefined , 42 ) . threadEvent ! ;
3530
+ } ) ;
3531
+
3532
+ it ( "should return the last event from the thread" , ( ) => {
3533
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3534
+ } ) ;
3535
+ } ) ;
3536
+
3537
+ describe ( "when there are events in both, the main timeline and threads" , ( ) => {
3538
+ describe ( "and the last event is in a thread" , ( ) => {
3539
+ beforeEach ( ( ) => {
3540
+ lastEventInThread = addRoomMainAndThreadMessages ( room , 23 , 42 ) . threadEvent ! ;
3541
+ } ) ;
3542
+
3543
+ it ( "should return the last event from the thread" , ( ) => {
3544
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3545
+ } ) ;
3546
+ } ) ;
3547
+
3548
+ describe ( "and the last event is in the main timeline" , ( ) => {
3549
+ beforeEach ( ( ) => {
3550
+ lastEventInMainTimeline = addRoomMainAndThreadMessages ( room , 42 , 23 ) . mainEvent ! ;
3551
+ } ) ;
3552
+
3553
+ it ( "should return the last event from the main timeline" , ( ) => {
3554
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3555
+ } ) ;
3556
+ } ) ;
3557
+ } ) ;
3558
+ } ) ;
3478
3559
} ) ;
0 commit comments