@@ -60,6 +60,7 @@ import { IOlmDevice } from "../../src/crypto/algorithms/megolm";
60
60
import { QueryDict } from "../../src/utils" ;
61
61
import { SyncState } from "../../src/sync" ;
62
62
import * as featureUtils from "../../src/feature" ;
63
+ import { StubStore } from "../../src/store/stub" ;
63
64
64
65
jest . useFakeTimers ( ) ;
65
66
@@ -1967,4 +1968,82 @@ describe("MatrixClient", function () {
1967
1968
expect ( requestSpy ) . toHaveBeenCalledWith ( Method . Put , path , undefined , { } ) ;
1968
1969
} ) ;
1969
1970
} ) ;
1971
+
1972
+ describe ( "getVisibleRooms" , ( ) => {
1973
+ function roomCreateEvent ( newRoomId : string , predecessorRoomId : string ) : MatrixEvent {
1974
+ return new MatrixEvent ( {
1975
+ content : {
1976
+ "creator" : "@daryl:alexandria.example.com" ,
1977
+ "m.federate" : true ,
1978
+ "predecessor" : {
1979
+ event_id : "spec_is_not_clear_what_id_this_is" ,
1980
+ room_id : predecessorRoomId ,
1981
+ } ,
1982
+ "room_version" : "9" ,
1983
+ } ,
1984
+ event_id : `create_event_id_pred_${ predecessorRoomId } ` ,
1985
+ origin_server_ts : 1432735824653 ,
1986
+ room_id : newRoomId ,
1987
+ sender : "@daryl:alexandria.example.com" ,
1988
+ state_key : "" ,
1989
+ type : "m.room.create" ,
1990
+ } ) ;
1991
+ }
1992
+
1993
+ function tombstoneEvent ( newRoomId : string , predecessorRoomId : string ) : MatrixEvent {
1994
+ return new MatrixEvent ( {
1995
+ content : {
1996
+ body : "This room has been replaced" ,
1997
+ replacement_room : newRoomId ,
1998
+ } ,
1999
+ event_id : `tombstone_event_id_pred_${ predecessorRoomId } ` ,
2000
+ origin_server_ts : 1432735824653 ,
2001
+ room_id : predecessorRoomId ,
2002
+ sender : "@daryl:alexandria.example.com" ,
2003
+ state_key : "" ,
2004
+ type : "m.room.tombstone" ,
2005
+ } ) ;
2006
+ }
2007
+
2008
+ it ( "Returns an empty list if there are no rooms" , ( ) => {
2009
+ client . store = new StubStore ( ) ;
2010
+ client . store . getRooms = ( ) => [ ] ;
2011
+ const rooms = client . getVisibleRooms ( ) ;
2012
+ expect ( rooms ) . toHaveLength ( 0 ) ;
2013
+ } ) ;
2014
+
2015
+ it ( "Returns all non-replaced rooms" , ( ) => {
2016
+ const room1 = new Room ( "room1" , client , "@carol:alexandria.example.com" ) ;
2017
+ const room2 = new Room ( "room2" , client , "@daryl:alexandria.example.com" ) ;
2018
+ client . store = new StubStore ( ) ;
2019
+ client . store . getRooms = ( ) => [ room1 , room2 ] ;
2020
+ const rooms = client . getVisibleRooms ( ) ;
2021
+ expect ( rooms ) . toContain ( room1 ) ;
2022
+ expect ( rooms ) . toContain ( room2 ) ;
2023
+ expect ( rooms ) . toHaveLength ( 2 ) ;
2024
+ } ) ;
2025
+
2026
+ it ( "Does not return replaced rooms" , ( ) => {
2027
+ // Given 4 rooms, 2 of which have been replaced
2028
+ const room1 = new Room ( "room1" , client , "@carol:alexandria.example.com" ) ;
2029
+ const replacedRoom1 = new Room ( "replacedRoom1" , client , "@carol:alexandria.example.com" ) ;
2030
+ const replacedRoom2 = new Room ( "replacedRoom2" , client , "@carol:alexandria.example.com" ) ;
2031
+ const room2 = new Room ( "room2" , client , "@daryl:alexandria.example.com" ) ;
2032
+ client . store = new StubStore ( ) ;
2033
+ client . store . getRooms = ( ) => [ room1 , replacedRoom1 , replacedRoom2 , room2 ] ;
2034
+ room1 . addLiveEvents ( [ roomCreateEvent ( room1 . roomId , replacedRoom1 . roomId ) ] , { } ) ;
2035
+ room2 . addLiveEvents ( [ roomCreateEvent ( room2 . roomId , replacedRoom2 . roomId ) ] , { } ) ;
2036
+ replacedRoom1 . addLiveEvents ( [ tombstoneEvent ( room1 . roomId , replacedRoom1 . roomId ) ] , { } ) ;
2037
+ replacedRoom2 . addLiveEvents ( [ tombstoneEvent ( room2 . roomId , replacedRoom2 . roomId ) ] , { } ) ;
2038
+
2039
+ // When we ask for the visible rooms
2040
+ const rooms = client . getVisibleRooms ( ) ;
2041
+
2042
+ // Then we only get the ones that have not been replaced
2043
+ expect ( rooms ) . not . toContain ( replacedRoom1 ) ;
2044
+ expect ( rooms ) . not . toContain ( replacedRoom2 ) ;
2045
+ expect ( rooms ) . toContain ( room1 ) ;
2046
+ expect ( rooms ) . toContain ( room2 ) ;
2047
+ } ) ;
2048
+ } ) ;
1970
2049
} ) ;
0 commit comments