@@ -15,18 +15,18 @@ limitations under the License.
15
15
*/
16
16
17
17
import { mocked , Mocked } from "jest-mock" ;
18
- import { EventType , IContent , MatrixClient } from "matrix-js-sdk/src/matrix" ;
18
+ import { logger } from "matrix-js-sdk/src/logger" ;
19
+ import { ClientEvent , EventType , IContent , MatrixClient , MatrixEvent } from "matrix-js-sdk/src/matrix" ;
19
20
20
21
import DMRoomMap from "../../src/utils/DMRoomMap" ;
21
22
import { mkEvent , stubClient } from "../test-utils" ;
22
-
23
23
describe ( "DMRoomMap" , ( ) => {
24
24
const roomId1 = "!room1:example.com" ;
25
25
const roomId2 = "!room2:example.com" ;
26
26
const roomId3 = "!room3:example.com" ;
27
27
const roomId4 = "!room4:example.com" ;
28
28
29
- const mDirectContent = {
29
+ const validMDirectContent = {
30
30
"[email protected] " :
[ roomId1 , roomId2 ] ,
31
31
"@user:example.com" : [ roomId1 , roomId3 , roomId4 ] ,
32
32
"@user2:example.com" : [ ] as string [ ] ,
@@ -35,20 +35,106 @@ describe("DMRoomMap", () => {
35
35
let client : Mocked < MatrixClient > ;
36
36
let dmRoomMap : DMRoomMap ;
37
37
38
+ const mkMDirectEvent = ( content : any ) : MatrixEvent => {
39
+ return mkEvent ( {
40
+ event : true ,
41
+ type : EventType . Direct ,
42
+ user : client . getSafeUserId ( ) ,
43
+ content : content ,
44
+ } ) ;
45
+ } ;
46
+
38
47
beforeEach ( ( ) => {
39
48
client = mocked ( stubClient ( ) ) ;
49
+ jest . spyOn ( logger , "warn" ) ;
50
+ } ) ;
40
51
41
- const mDirectEvent = mkEvent ( {
42
- event : true ,
52
+ describe ( "when m.direct has valid content" , ( ) => {
53
+ beforeEach ( ( ) => {
54
+ client . getAccountData . mockReturnValue ( mkMDirectEvent ( validMDirectContent ) ) ;
55
+ dmRoomMap = new DMRoomMap ( client ) ;
56
+ dmRoomMap . start ( ) ;
57
+ } ) ;
58
+
59
+ it ( "getRoomIds should return the room Ids" , ( ) => {
60
+ expect ( dmRoomMap . getRoomIds ( ) ) . toEqual ( new Set ( [ roomId1 , roomId2 , roomId3 , roomId4 ] ) ) ;
61
+ } ) ;
62
+
63
+ describe ( "and there is an update with valid data" , ( ) => {
64
+ beforeEach ( ( ) => {
65
+ client . emit (
66
+ ClientEvent . AccountData ,
67
+ mkMDirectEvent ( {
68
+ "@user:example.com" : [ roomId1 , roomId3 ] ,
69
+ } ) ,
70
+ ) ;
71
+ } ) ;
72
+
73
+ it ( "getRoomIds should return the new room Ids" , ( ) => {
74
+ expect ( dmRoomMap . getRoomIds ( ) ) . toEqual ( new Set ( [ roomId1 , roomId3 ] ) ) ;
75
+ } ) ;
76
+ } ) ;
77
+
78
+ describe ( "and there is an update with invalid data" , ( ) => {
79
+ const partiallyInvalidContent = {
80
+ "@user1:example.com" : [ roomId1 , roomId3 ] ,
81
+ "@user2:example.com" : "room2, room3" ,
82
+ } ;
83
+
84
+ beforeEach ( ( ) => {
85
+ client . emit ( ClientEvent . AccountData , mkMDirectEvent ( partiallyInvalidContent ) ) ;
86
+ } ) ;
87
+
88
+ it ( "getRoomIds should return the valid room Ids" , ( ) => {
89
+ expect ( dmRoomMap . getRoomIds ( ) ) . toEqual ( new Set ( [ roomId1 , roomId3 ] ) ) ;
90
+ } ) ;
91
+
92
+ it ( "should log the invalid content" , ( ) => {
93
+ expect ( logger . warn ) . toHaveBeenCalledWith ( "Invalid m.direct content occurred" , partiallyInvalidContent ) ;
94
+ } ) ;
95
+ } ) ;
96
+ } ) ;
97
+
98
+ describe ( "when m.direct content contains the entire event" , ( ) => {
99
+ const mDirectContentContent = {
43
100
type : EventType . Direct ,
44
- user : client . getSafeUserId ( ) ,
45
- content : mDirectContent ,
101
+ content : validMDirectContent ,
102
+ } ;
103
+
104
+ beforeEach ( ( ) => {
105
+ client . getAccountData . mockReturnValue ( mkMDirectEvent ( mDirectContentContent ) ) ;
106
+ dmRoomMap = new DMRoomMap ( client ) ;
107
+ } ) ;
108
+
109
+ it ( "should log the invalid content" , ( ) => {
110
+ expect ( logger . warn ) . toHaveBeenCalledWith ( "Invalid m.direct content occurred" , mDirectContentContent ) ;
111
+ } ) ;
112
+
113
+ it ( "getRoomIds should return an empty list" , ( ) => {
114
+ expect ( dmRoomMap . getRoomIds ( ) ) . toEqual ( new Set ( [ ] ) ) ;
46
115
} ) ;
47
- client . getAccountData . mockReturnValue ( mDirectEvent ) ;
48
- dmRoomMap = new DMRoomMap ( client ) ;
49
116
} ) ;
50
117
51
- it ( "getRoomIds should return the room Ids" , ( ) => {
52
- expect ( dmRoomMap . getRoomIds ( ) ) . toEqual ( new Set ( [ roomId1 , roomId2 , roomId3 , roomId4 ] ) ) ;
118
+ describe ( "when partially crap m.direct content appears" , ( ) => {
119
+ const partiallyCrapContent = {
120
+ "hello" : 23 ,
121
+ "@user1:example.com" : [ ] as string [ ] ,
122
+ "@user2:example.com" : [ roomId1 , roomId2 ] ,
123
+ "@user3:example.com" : "room1, room2, room3" ,
124
+ "@user4:example.com" : [ roomId4 ] ,
125
+ } ;
126
+
127
+ beforeEach ( ( ) => {
128
+ client . getAccountData . mockReturnValue ( mkMDirectEvent ( partiallyCrapContent ) ) ;
129
+ dmRoomMap = new DMRoomMap ( client ) ;
130
+ } ) ;
131
+
132
+ it ( "should log the invalid content" , ( ) => {
133
+ expect ( logger . warn ) . toHaveBeenCalledWith ( "Invalid m.direct content occurred" , partiallyCrapContent ) ;
134
+ } ) ;
135
+
136
+ it ( "getRoomIds should only return the valid items" , ( ) => {
137
+ expect ( dmRoomMap . getRoomIds ( ) ) . toEqual ( new Set ( [ roomId1 , roomId2 , roomId4 ] ) ) ;
138
+ } ) ;
53
139
} ) ;
54
140
} ) ;
0 commit comments