@@ -17,56 +17,112 @@ limitations under the License.
1717import { mocked } from "jest-mock" ;
1818import { MatrixClient , Room } from "matrix-js-sdk/src/matrix" ;
1919
20- import { DirectoryMember , ThreepidMember } from "../../../src/utils/direct-messages" ;
21- import { MatrixClientPeg } from "../../../src/MatrixClientPeg" ;
2220import DMRoomMap from "../../../src/utils/DMRoomMap" ;
23- import { createTestClient } from "../../test-utils" ;
24- import { findDMRoom } from "../../../src/utils/dm/findDMRoom " ;
21+ import { createTestClient , makeMembershipEvent } from "../../test-utils" ;
22+ import { LocalRoom } from "../../../src/models/LocalRoom " ;
2523import { findDMForUser } from "../../../src/utils/dm/findDMForUser" ;
24+ import { getFunctionalMembers } from "../../../src/utils/room/getFunctionalMembers" ;
2625
27- jest . mock ( "../../../src/utils/dm/findDMForUser " , ( ) => ( {
28- findDMForUser : jest . fn ( ) ,
26+ jest . mock ( "../../../src/utils/room/getFunctionalMembers " , ( ) => ( {
27+ getFunctionalMembers : jest . fn ( ) ,
2928} ) ) ;
3029
31- describe ( "findDMRoom " , ( ) => {
30+ describe ( "findDMForUser " , ( ) => {
3231 const userId1 = "@user1:example.com" ;
33- const member1 = new DirectoryMember ( { user_id : userId1 } ) ;
34- const member2 = new ThreepidMember ( "user2" ) ;
35- let mockClient : MatrixClient ;
32+ const userId2 = "@user2:example.com" ;
33+ const botId = "@bot:example.com" ;
3634 let room1 : Room ;
35+ let room2 : LocalRoom ;
36+ let room3 : Room ;
37+ let room4 : Room ;
38+ let room5 : Room ;
3739 let dmRoomMap : DMRoomMap ;
40+ let mockClient : MatrixClient ;
3841
3942 beforeEach ( ( ) => {
4043 mockClient = createTestClient ( ) ;
41- jest . spyOn ( MatrixClientPeg , "get" ) . mockReturnValue ( mockClient ) ;
44+
45+ // always return the bot user as functional member
46+ mocked ( getFunctionalMembers ) . mockReturnValue ( [ botId ] ) ;
4247
4348 room1 = new Room ( "!room1:example.com" , mockClient , userId1 ) ;
4449 room1 . getMyMembership = ( ) => "join" ;
50+ room1 . currentState . setStateEvents ( [
51+ makeMembershipEvent ( room1 . roomId , userId1 , "join" ) ,
52+ makeMembershipEvent ( room1 . roomId , userId2 , "join" ) ,
53+ ] ) ;
54+
55+ // this should not be a DM room because it is a local room
56+ room2 = new LocalRoom ( "!room2:example.com" , mockClient , userId1 ) ;
57+ room2 . getMyMembership = ( ) => "join" ;
58+ room2 . getLastActiveTimestamp = ( ) => 100 ;
59+
60+ room3 = new Room ( "!room3:example.com" , mockClient , userId1 ) ;
61+ room3 . getMyMembership = ( ) => "join" ;
62+ room3 . currentState . setStateEvents ( [
63+ makeMembershipEvent ( room3 . roomId , userId1 , "join" ) ,
64+ makeMembershipEvent ( room3 . roomId , userId2 , "join" ) ,
65+ // Adding the bot user here. Should be excluded when determining if the room is a DM.
66+ makeMembershipEvent ( room3 . roomId , botId , "join" ) ,
67+ ] ) ;
68+
69+ // this should not be a DM room because it has only one joined user
70+ room4 = new Room ( "!room4:example.com" , mockClient , userId1 ) ;
71+ room4 . getMyMembership = ( ) => "join" ;
72+ room4 . currentState . setStateEvents ( [
73+ makeMembershipEvent ( room4 . roomId , userId1 , "invite" ) ,
74+ makeMembershipEvent ( room4 . roomId , userId2 , "join" ) ,
75+ ] ) ;
76+
77+ // this should not be a DM room because it has no users
78+ room5 = new Room ( "!room5:example.com" , mockClient , userId1 ) ;
79+ room5 . getLastActiveTimestamp = ( ) => 100 ;
80+
81+ mocked ( mockClient . getRoom ) . mockImplementation ( ( roomId : string ) => {
82+ return {
83+ [ room1 . roomId ] : room1 ,
84+ [ room2 . roomId ] : room2 ,
85+ [ room3 . roomId ] : room3 ,
86+ [ room4 . roomId ] : room4 ,
87+ [ room5 . roomId ] : room5 ,
88+ } [ roomId ] ;
89+ } ) ;
4590
4691 dmRoomMap = {
4792 getDMRoomForIdentifiers : jest . fn ( ) ,
4893 getDMRoomsForUserId : jest . fn ( ) ,
4994 } as unknown as DMRoomMap ;
5095 jest . spyOn ( DMRoomMap , "shared" ) . mockReturnValue ( dmRoomMap ) ;
96+ mocked ( dmRoomMap . getDMRoomsForUserId ) . mockReturnValue ( [
97+ room1 . roomId ,
98+ room2 . roomId ,
99+ room3 . roomId ,
100+ room4 . roomId ,
101+ room5 . roomId ,
102+ ] ) ;
51103 } ) ;
52104
53- it ( "should return the room for a single target with a room", ( ) => {
54- mocked ( findDMForUser ) . mockReturnValue ( room1 ) ;
55- expect ( findDMRoom ( mockClient , [ member1 ] ) ) . toBe ( room1 ) ;
56- } ) ;
105+ describe ( " for an empty DM room list ", ( ) => {
106+ beforeEach ( ( ) => {
107+ mocked ( dmRoomMap . getDMRoomsForUserId ) . mockReturnValue ( [ ] ) ;
108+ } ) ;
57109
58- it ( "should return null for a single target without a room " , ( ) => {
59- mocked ( findDMForUser ) . mockReturnValue ( null ) ;
60- expect ( findDMRoom ( mockClient , [ member1 ] ) ) . toBeNull ( ) ;
110+ it ( "should return undefined " , ( ) => {
111+ expect ( findDMForUser ( mockClient , userId1 ) ) . toBeUndefined ( ) ;
112+ } ) ;
61113 } ) ;
62114
63- it ( "should return the room for 2 targets with a room" , ( ) => {
64- mocked ( dmRoomMap . getDMRoomForIdentifiers ) . mockReturnValue ( room1 ) ;
65- expect ( findDMRoom ( mockClient , [ member1 , member2 ] ) ) . toBe ( room1 ) ;
115+ it ( "should find a room ordered by last activity 1" , ( ) => {
116+ room1 . getLastActiveTimestamp = ( ) => 2 ;
117+ room3 . getLastActiveTimestamp = ( ) => 1 ;
118+
119+ expect ( findDMForUser ( mockClient , userId1 ) ) . toBe ( room1 ) ;
66120 } ) ;
67121
68- it ( "should return null for 2 targets without a room" , ( ) => {
69- mocked ( dmRoomMap . getDMRoomForIdentifiers ) . mockReturnValue ( null ) ;
70- expect ( findDMRoom ( mockClient , [ member1 , member2 ] ) ) . toBeNull ( ) ;
122+ it ( "should find a room ordered by last activity 2" , ( ) => {
123+ room1 . getLastActiveTimestamp = ( ) => 1 ;
124+ room3 . getLastActiveTimestamp = ( ) => 2 ;
125+
126+ expect ( findDMForUser ( mockClient , userId1 ) ) . toBe ( room3 ) ;
71127 } ) ;
72128} ) ;
0 commit comments