@@ -62,6 +62,7 @@ import { IOlmDevice } from "../../src/crypto/algorithms/megolm";
62
62
import { QueryDict } from "../../src/utils" ;
63
63
import { SyncState } from "../../src/sync" ;
64
64
import * as featureUtils from "../../src/feature" ;
65
+ import { StubStore } from "../../src/store/stub" ;
65
66
66
67
jest . useFakeTimers ( ) ;
67
68
@@ -2196,4 +2197,82 @@ describe("MatrixClient", function () {
2196
2197
expect ( requestSpy ) . toHaveBeenCalledWith ( Method . Put , path , undefined , { } ) ;
2197
2198
} ) ;
2198
2199
} ) ;
2200
+
2201
+ describe ( "getVisibleRooms" , ( ) => {
2202
+ function roomCreateEvent ( newRoomId : string , predecessorRoomId : string ) : MatrixEvent {
2203
+ return new MatrixEvent ( {
2204
+ content : {
2205
+ "creator" : "@daryl:alexandria.example.com" ,
2206
+ "m.federate" : true ,
2207
+ "predecessor" : {
2208
+ event_id : "spec_is_not_clear_what_id_this_is" ,
2209
+ room_id : predecessorRoomId ,
2210
+ } ,
2211
+ "room_version" : "9" ,
2212
+ } ,
2213
+ event_id : `create_event_id_pred_${ predecessorRoomId } ` ,
2214
+ origin_server_ts : 1432735824653 ,
2215
+ room_id : newRoomId ,
2216
+ sender : "@daryl:alexandria.example.com" ,
2217
+ state_key : "" ,
2218
+ type : "m.room.create" ,
2219
+ } ) ;
2220
+ }
2221
+
2222
+ function tombstoneEvent ( newRoomId : string , predecessorRoomId : string ) : MatrixEvent {
2223
+ return new MatrixEvent ( {
2224
+ content : {
2225
+ body : "This room has been replaced" ,
2226
+ replacement_room : newRoomId ,
2227
+ } ,
2228
+ event_id : `tombstone_event_id_pred_${ predecessorRoomId } ` ,
2229
+ origin_server_ts : 1432735824653 ,
2230
+ room_id : predecessorRoomId ,
2231
+ sender : "@daryl:alexandria.example.com" ,
2232
+ state_key : "" ,
2233
+ type : "m.room.tombstone" ,
2234
+ } ) ;
2235
+ }
2236
+
2237
+ it ( "Returns an empty list if there are no rooms" , ( ) => {
2238
+ client . store = new StubStore ( ) ;
2239
+ client . store . getRooms = ( ) => [ ] ;
2240
+ const rooms = client . getVisibleRooms ( ) ;
2241
+ expect ( rooms ) . toHaveLength ( 0 ) ;
2242
+ } ) ;
2243
+
2244
+ it ( "Returns all non-replaced rooms" , ( ) => {
2245
+ const room1 = new Room ( "room1" , client , "@carol:alexandria.example.com" ) ;
2246
+ const room2 = new Room ( "room2" , client , "@daryl:alexandria.example.com" ) ;
2247
+ client . store = new StubStore ( ) ;
2248
+ client . store . getRooms = ( ) => [ room1 , room2 ] ;
2249
+ const rooms = client . getVisibleRooms ( ) ;
2250
+ expect ( rooms ) . toContain ( room1 ) ;
2251
+ expect ( rooms ) . toContain ( room2 ) ;
2252
+ expect ( rooms ) . toHaveLength ( 2 ) ;
2253
+ } ) ;
2254
+
2255
+ it ( "Does not return replaced rooms" , ( ) => {
2256
+ // Given 4 rooms, 2 of which have been replaced
2257
+ const room1 = new Room ( "room1" , client , "@carol:alexandria.example.com" ) ;
2258
+ const replacedRoom1 = new Room ( "replacedRoom1" , client , "@carol:alexandria.example.com" ) ;
2259
+ const replacedRoom2 = new Room ( "replacedRoom2" , client , "@carol:alexandria.example.com" ) ;
2260
+ const room2 = new Room ( "room2" , client , "@daryl:alexandria.example.com" ) ;
2261
+ client . store = new StubStore ( ) ;
2262
+ client . store . getRooms = ( ) => [ room1 , replacedRoom1 , replacedRoom2 , room2 ] ;
2263
+ room1 . addLiveEvents ( [ roomCreateEvent ( room1 . roomId , replacedRoom1 . roomId ) ] , { } ) ;
2264
+ room2 . addLiveEvents ( [ roomCreateEvent ( room2 . roomId , replacedRoom2 . roomId ) ] , { } ) ;
2265
+ replacedRoom1 . addLiveEvents ( [ tombstoneEvent ( room1 . roomId , replacedRoom1 . roomId ) ] , { } ) ;
2266
+ replacedRoom2 . addLiveEvents ( [ tombstoneEvent ( room2 . roomId , replacedRoom2 . roomId ) ] , { } ) ;
2267
+
2268
+ // When we ask for the visible rooms
2269
+ const rooms = client . getVisibleRooms ( ) ;
2270
+
2271
+ // Then we only get the ones that have not been replaced
2272
+ expect ( rooms ) . not . toContain ( replacedRoom1 ) ;
2273
+ expect ( rooms ) . not . toContain ( replacedRoom2 ) ;
2274
+ expect ( rooms ) . toContain ( room1 ) ;
2275
+ expect ( rooms ) . toContain ( room2 ) ;
2276
+ } ) ;
2277
+ } ) ;
2199
2278
} ) ;
0 commit comments