@@ -51,7 +51,7 @@ import { TestClient } from "../TestClient";
51
51
import { ReceiptType , WrappedReceipt } from "../../src/@types/read_receipts" ;
52
52
import { FeatureSupport , Thread , THREAD_RELATION_TYPE , ThreadEvent } from "../../src/models/thread" ;
53
53
import { Crypto } from "../../src/crypto" ;
54
- import { mkThread } from "../test-utils/thread" ;
54
+ import * as threadUtils from "../test-utils/thread" ;
55
55
import { getMockClientWithEventEmitter , mockClientMethodsUser } from "../test-utils/client" ;
56
56
import { logger } from "../../src/logger" ;
57
57
import { IMessageOpts } from "../test-utils/test-utils" ;
@@ -168,30 +168,45 @@ describe("Room", function () {
168
168
room . client ,
169
169
) ;
170
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
- }
171
+ /**
172
+ * @see threadUtils.mkThread
173
+ */
174
+ const mkThread = (
175
+ opts : Partial < Parameters < typeof threadUtils . mkThread > [ 0 ] > ,
176
+ ) : ReturnType < typeof threadUtils . mkThread > => {
177
+ return threadUtils . mkThread ( {
178
+ room,
179
+ client : new TestClient ( ) . client ,
180
+ authorId : "@bob:example.org" ,
181
+ participantUserIds : [ "@bob:example.org" ] ,
182
+ ...opts ,
183
+ } ) ;
184
+ } ;
182
185
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 , { toStartOfTimeline : true } ) ;
192
- }
186
+ /**
187
+ * Creates a message and adds it to the end of the main live timeline.
188
+ *
189
+ * @param room - Room to add the message to
190
+ * @param timestamp - Timestamp of the message
191
+ * @return The message event
192
+ */
193
+ const mkMessageInRoom = ( room : Room , timestamp : number ) => {
194
+ const message = mkMessage ( { ts : timestamp } ) ;
195
+ room . addLiveEvents ( [ message ] ) ;
196
+ return message ;
197
+ } ;
193
198
194
- return result ;
199
+ /**
200
+ * Creates a message in a thread and adds it to the end of the thread live timeline.
201
+ *
202
+ * @param thread - Thread to add the message to
203
+ * @param timestamp - Timestamp of the message
204
+ * @returns The thread message event
205
+ */
206
+ const mkMessageInThread = ( thread : Thread , timestamp : number ) => {
207
+ const message = mkThreadResponse ( thread . rootEvent ! , { ts : timestamp } ) ;
208
+ thread . liveTimeline . addEvent ( message , { toStartOfTimeline : false } ) ;
209
+ return message ;
195
210
} ;
196
211
197
212
const addRoomThreads = (
@@ -202,24 +217,14 @@ describe("Room", function () {
202
217
const result : { thread1 ?: Thread ; thread2 ?: Thread } = { } ;
203
218
204
219
if ( thread1EventTs !== null ) {
205
- const { rootEvent : thread1RootEvent , thread : thread1 } = mkThread ( {
206
- room,
207
- client : new TestClient ( ) . client ,
208
- authorId : "@bob:example.org" ,
209
- participantUserIds : [ "@bob:example.org" ] ,
210
- } ) ;
220
+ const { rootEvent : thread1RootEvent , thread : thread1 } = mkThread ( { room } ) ;
211
221
const thread1Event = mkThreadResponse ( thread1RootEvent , { ts : thread1EventTs } ) ;
212
222
thread1 . liveTimeline . addEvent ( thread1Event , { toStartOfTimeline : true } ) ;
213
223
result . thread1 = thread1 ;
214
224
}
215
225
216
226
if ( thread2EventTs !== null ) {
217
- const { rootEvent : thread2RootEvent , thread : thread2 } = mkThread ( {
218
- room,
219
- client : new TestClient ( ) . client ,
220
- authorId : "@bob:example.org" ,
221
- participantUserIds : [ "@bob:example.org" ] ,
222
- } ) ;
227
+ const { rootEvent : thread2RootEvent , thread : thread2 } = mkThread ( { room } ) ;
223
228
const thread2Event = mkThreadResponse ( thread2RootEvent , { ts : thread2EventTs } ) ;
224
229
thread2 . liveTimeline . addEvent ( thread2Event , { toStartOfTimeline : true } ) ;
225
230
result . thread2 = thread2 ;
@@ -2510,12 +2515,7 @@ describe("Room", function () {
2510
2515
} ) ;
2511
2516
2512
2517
it ( "returns the same model when creating a thread twice" , ( ) => {
2513
- const { thread, rootEvent } = mkThread ( {
2514
- room,
2515
- client : new TestClient ( ) . client ,
2516
- authorId : "@bob:example.org" ,
2517
- participantUserIds : [ "@bob:example.org" ] ,
2518
- } ) ;
2518
+ const { thread, rootEvent } = mkThread ( { room } ) ;
2519
2519
2520
2520
expect ( thread ) . toBeInstanceOf ( Thread ) ;
2521
2521
@@ -3540,32 +3540,50 @@ describe("Room", function () {
3540
3540
} ) ;
3541
3541
3542
3542
describe ( "getLastLiveEvent" , ( ) => {
3543
- let lastEventInMainTimeline : MatrixEvent ;
3544
- let lastEventInThread : MatrixEvent ;
3545
-
3546
3543
it ( "when there are no events, it should return undefined" , ( ) => {
3547
3544
expect ( room . getLastLiveEvent ( ) ) . toBeUndefined ( ) ;
3548
3545
} ) ;
3549
3546
3550
3547
it ( "when there is only an event in the main timeline and there are no threads, it should return the last event from the main timeline" , ( ) => {
3551
- lastEventInMainTimeline = addRoomMainAndThreadMessages ( room , 23 ) . mainEvent ! ;
3552
- room . addLiveEvents ( [ lastEventInMainTimeline ] ) ;
3548
+ const lastEventInMainTimeline = mkMessageInRoom ( room , 23 ) ;
3553
3549
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3554
3550
} ) ;
3555
3551
3552
+ /**
3553
+ * This should normally not happen. The test exists only for the sake of completeness.
3554
+ * No event is added to the room's live timeline here.
3555
+ */
3556
3556
it ( "when there is no event in the room live timeline but in a thread, it should return the last event from the thread" , ( ) => {
3557
- lastEventInThread = addRoomMainAndThreadMessages ( room , undefined , 42 ) . threadEvent ! ;
3557
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3558
+ const lastEventInThread = mkMessageInThread ( thread , 42 ) ;
3558
3559
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3559
3560
} ) ;
3560
3561
3561
3562
describe ( "when there are events in both, the main timeline and threads" , ( ) => {
3562
3563
it ( "and the last event is in a thread, it should return the last event from the thread" , ( ) => {
3563
- lastEventInThread = addRoomMainAndThreadMessages ( room , 23 , 42 ) . threadEvent ! ;
3564
+ mkMessageInRoom ( room , 23 ) ;
3565
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3566
+ const lastEventInThread = mkMessageInThread ( thread , 42 ) ;
3564
3567
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3565
3568
} ) ;
3566
3569
3567
3570
it ( "and the last event is in the main timeline, it should return the last event from the main timeline" , ( ) => {
3568
- lastEventInMainTimeline = addRoomMainAndThreadMessages ( room , 42 , 23 ) . mainEvent ! ;
3571
+ const lastEventInMainTimeline = mkMessageInRoom ( room , 42 ) ;
3572
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3573
+ mkMessageInThread ( thread , 23 ) ;
3574
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3575
+ } ) ;
3576
+
3577
+ it ( "and both events have the same timestamp, it should return the last event from the thread" , ( ) => {
3578
+ mkMessageInRoom ( room , 23 ) ;
3579
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3580
+ const lastEventInThread = mkMessageInThread ( thread , 23 ) ;
3581
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3582
+ } ) ;
3583
+
3584
+ it ( "and there is a thread without any messages, it should return the last event from the main timeline" , ( ) => {
3585
+ const lastEventInMainTimeline = mkMessageInRoom ( room , 23 ) ;
3586
+ mkThread ( { room, length : 0 } ) ;
3569
3587
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3570
3588
} ) ;
3571
3589
} ) ;
0 commit comments