@@ -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 ;
@@ -2504,12 +2509,7 @@ describe("Room", function () {
2504
2509
} ) ;
2505
2510
2506
2511
it ( "returns the same model when creating a thread twice" , ( ) => {
2507
- const { thread, rootEvent } = mkThread ( {
2508
- room,
2509
- client : new TestClient ( ) . client ,
2510
- authorId : "@bob:example.org" ,
2511
- participantUserIds : [ "@bob:example.org" ] ,
2512
- } ) ;
2512
+ const { thread, rootEvent } = mkThread ( { room } ) ;
2513
2513
2514
2514
expect ( thread ) . toBeInstanceOf ( Thread ) ;
2515
2515
@@ -3534,32 +3534,50 @@ describe("Room", function () {
3534
3534
} ) ;
3535
3535
3536
3536
describe ( "getLastLiveEvent" , ( ) => {
3537
- let lastEventInMainTimeline : MatrixEvent ;
3538
- let lastEventInThread : MatrixEvent ;
3539
-
3540
3537
it ( "when there are no events, it should return undefined" , ( ) => {
3541
3538
expect ( room . getLastLiveEvent ( ) ) . toBeUndefined ( ) ;
3542
3539
} ) ;
3543
3540
3544
3541
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" , ( ) => {
3545
- lastEventInMainTimeline = addRoomMainAndThreadMessages ( room , 23 ) . mainEvent ! ;
3546
- room . addLiveEvents ( [ lastEventInMainTimeline ] ) ;
3542
+ const lastEventInMainTimeline = mkMessageInRoom ( room , 23 ) ;
3547
3543
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3548
3544
} ) ;
3549
3545
3546
+ /**
3547
+ * This should normally not happen. The test exists only for the sake of completeness.
3548
+ * No event is added to the room's live timeline here.
3549
+ */
3550
3550
it ( "when there is no event in the room live timeline but in a thread, it should return the last event from the thread" , ( ) => {
3551
- lastEventInThread = addRoomMainAndThreadMessages ( room , undefined , 42 ) . threadEvent ! ;
3551
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3552
+ const lastEventInThread = mkMessageInThread ( thread , 42 ) ;
3552
3553
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3553
3554
} ) ;
3554
3555
3555
3556
describe ( "when there are events in both, the main timeline and threads" , ( ) => {
3556
3557
it ( "and the last event is in a thread, it should return the last event from the thread" , ( ) => {
3557
- lastEventInThread = addRoomMainAndThreadMessages ( room , 23 , 42 ) . threadEvent ! ;
3558
+ mkMessageInRoom ( room , 23 ) ;
3559
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3560
+ const lastEventInThread = mkMessageInThread ( thread , 42 ) ;
3558
3561
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3559
3562
} ) ;
3560
3563
3561
3564
it ( "and the last event is in the main timeline, it should return the last event from the main timeline" , ( ) => {
3562
- lastEventInMainTimeline = addRoomMainAndThreadMessages ( room , 42 , 23 ) . mainEvent ! ;
3565
+ const lastEventInMainTimeline = mkMessageInRoom ( room , 42 ) ;
3566
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3567
+ mkMessageInThread ( thread , 23 ) ;
3568
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3569
+ } ) ;
3570
+
3571
+ it ( "and both events have the same timestamp, it should return the last event from the thread" , ( ) => {
3572
+ mkMessageInRoom ( room , 23 ) ;
3573
+ const { thread } = mkThread ( { room, length : 0 } ) ;
3574
+ const lastEventInThread = mkMessageInThread ( thread , 23 ) ;
3575
+ expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInThread ) ;
3576
+ } ) ;
3577
+
3578
+ it ( "and there is a thread without any messages, it should return the last event from the main timeline" , ( ) => {
3579
+ const lastEventInMainTimeline = mkMessageInRoom ( room , 23 ) ;
3580
+ mkThread ( { room, length : 0 } ) ;
3563
3581
expect ( room . getLastLiveEvent ( ) ) . toBe ( lastEventInMainTimeline ) ;
3564
3582
} ) ;
3565
3583
} ) ;
0 commit comments