UNPKG

11.3 kBJavaScriptView Raw
1"use strict";
2import 'source-map-support/register';
3const PushProcessor = require("../../lib/pushprocessor");
4const utils = require("../test-utils");
5
6import expect from 'expect';
7
8describe('NotificationService', function() {
9 const testUserId = "@ali:matrix.org";
10 const testDisplayName = "Alice M";
11 const testRoomId = "!fl1bb13:localhost";
12
13 let testEvent;
14
15 let pushProcessor;
16
17 // These would be better if individual rules were configured in the tests themselves.
18 const matrixClient = {
19 getRoom: function() {
20 return {
21 currentState: {
22 getMember: function() {
23 return {
24 name: testDisplayName,
25 };
26 },
27 getJoinedMemberCount: function() {
28 return 0;
29 },
30 members: {},
31 },
32 };
33 },
34 credentials: {
35 userId: testUserId,
36 },
37 pushRules: {
38 "device": {},
39 "global": {
40 "content": [
41 {
42 "actions": [
43 "notify",
44 {
45 "set_tweak": "sound",
46 "value": "default",
47 },
48 {
49 "set_tweak": "highlight",
50 },
51 ],
52 "enabled": true,
53 "pattern": "ali",
54 "rule_id": ".m.rule.contains_user_name",
55 },
56 {
57 "actions": [
58 "notify",
59 {
60 "set_tweak": "sound",
61 "value": "default",
62 },
63 {
64 "set_tweak": "highlight",
65 },
66 ],
67 "enabled": true,
68 "pattern": "coffee",
69 "rule_id": "coffee",
70 },
71 {
72 "actions": [
73 "notify",
74 {
75 "set_tweak": "sound",
76 "value": "default",
77 },
78 {
79 "set_tweak": "highlight",
80 },
81 ],
82 "enabled": true,
83 "pattern": "foo*bar",
84 "rule_id": "foobar",
85 },
86 {
87 "actions": [
88 "notify",
89 {
90 "set_tweak": "sound",
91 "value": "default",
92 },
93 {
94 "set_tweak": "highlight",
95 },
96 ],
97 "enabled": true,
98 "pattern": "p[io]ng",
99 "rule_id": "pingpong",
100 },
101 {
102 "actions": [
103 "notify",
104 {
105 "set_tweak": "sound",
106 "value": "default",
107 },
108 {
109 "set_tweak": "highlight",
110 },
111 ],
112 "enabled": true,
113 "pattern": "I ate [0-9] pies",
114 "rule_id": "pies",
115 },
116 {
117 "actions": [
118 "notify",
119 {
120 "set_tweak": "sound",
121 "value": "default",
122 },
123 {
124 "set_tweak": "highlight",
125 },
126 ],
127 "enabled": true,
128 "pattern": "b[!ai]ke",
129 "rule_id": "bakebike",
130 },
131 ],
132 "override": [
133 {
134 "actions": [
135 "notify",
136 {
137 "set_tweak": "sound",
138 "value": "default",
139 },
140 {
141 "set_tweak": "highlight",
142 },
143 ],
144 "conditions": [
145 {
146 "kind": "contains_display_name",
147 },
148 ],
149 "enabled": true,
150 "rule_id": ".m.rule.contains_display_name",
151 },
152 {
153 "actions": [
154 "notify",
155 {
156 "set_tweak": "sound",
157 "value": "default",
158 },
159 ],
160 "conditions": [
161 {
162 "is": "2",
163 "kind": "room_member_count",
164 },
165 ],
166 "enabled": true,
167 "rule_id": ".m.rule.room_one_to_one",
168 },
169 ],
170 "room": [],
171 "sender": [],
172 "underride": [
173 {
174 "actions": [
175 "dont-notify",
176 ],
177 "conditions": [
178 {
179 "key": "content.msgtype",
180 "kind": "event_match",
181 "pattern": "m.notice",
182 },
183 ],
184 "enabled": true,
185 "rule_id": ".m.rule.suppress_notices",
186 },
187 {
188 "actions": [
189 "notify",
190 {
191 "set_tweak": "highlight",
192 "value": false,
193 },
194 ],
195 "conditions": [],
196 "enabled": true,
197 "rule_id": ".m.rule.fallback",
198 },
199 ],
200 },
201 },
202 };
203
204 beforeEach(function() {
205 testEvent = utils.mkEvent({
206 type: "m.room.message",
207 room: testRoomId,
208 user: "@alfred:localhost",
209 event: true,
210 content: {
211 body: "",
212 msgtype: "m.text",
213 },
214 });
215 pushProcessor = new PushProcessor(matrixClient);
216 });
217
218 // User IDs
219
220 it('should bing on a user ID.', function() {
221 testEvent.event.content.body = "Hello @ali:matrix.org, how are you?";
222 const actions = pushProcessor.actionsForEvent(testEvent);
223 expect(actions.tweaks.highlight).toEqual(true);
224 });
225
226 it('should bing on a partial user ID with an @.', function() {
227 testEvent.event.content.body = "Hello @ali, how are you?";
228 const actions = pushProcessor.actionsForEvent(testEvent);
229 expect(actions.tweaks.highlight).toEqual(true);
230 });
231
232 it('should bing on a partial user ID without @.', function() {
233 testEvent.event.content.body = "Hello ali, how are you?";
234 const actions = pushProcessor.actionsForEvent(testEvent);
235 expect(actions.tweaks.highlight).toEqual(true);
236 });
237
238 it('should bing on a case-insensitive user ID.', function() {
239 testEvent.event.content.body = "Hello @AlI:matrix.org, how are you?";
240 const actions = pushProcessor.actionsForEvent(testEvent);
241 expect(actions.tweaks.highlight).toEqual(true);
242 });
243
244 // Display names
245
246 it('should bing on a display name.', function() {
247 testEvent.event.content.body = "Hello Alice M, how are you?";
248 const actions = pushProcessor.actionsForEvent(testEvent);
249 expect(actions.tweaks.highlight).toEqual(true);
250 });
251
252 it('should bing on a case-insensitive display name.', function() {
253 testEvent.event.content.body = "Hello ALICE M, how are you?";
254 const actions = pushProcessor.actionsForEvent(testEvent);
255 expect(actions.tweaks.highlight).toEqual(true);
256 });
257
258 // Bing words
259
260 it('should bing on a bing word.', function() {
261 testEvent.event.content.body = "I really like coffee";
262 const actions = pushProcessor.actionsForEvent(testEvent);
263 expect(actions.tweaks.highlight).toEqual(true);
264 });
265
266 it('should bing on case-insensitive bing words.', function() {
267 testEvent.event.content.body = "Coffee is great";
268 const actions = pushProcessor.actionsForEvent(testEvent);
269 expect(actions.tweaks.highlight).toEqual(true);
270 });
271
272 it('should bing on wildcard (.*) bing words.', function() {
273 testEvent.event.content.body = "It was foomahbar I think.";
274 const actions = pushProcessor.actionsForEvent(testEvent);
275 expect(actions.tweaks.highlight).toEqual(true);
276 });
277
278 it('should bing on character group ([abc]) bing words.', function() {
279 testEvent.event.content.body = "Ping!";
280 let actions = pushProcessor.actionsForEvent(testEvent);
281 expect(actions.tweaks.highlight).toEqual(true);
282 testEvent.event.content.body = "Pong!";
283 actions = pushProcessor.actionsForEvent(testEvent);
284 expect(actions.tweaks.highlight).toEqual(true);
285 });
286
287 it('should bing on character range ([a-z]) bing words.', function() {
288 testEvent.event.content.body = "I ate 6 pies";
289 const actions = pushProcessor.actionsForEvent(testEvent);
290 expect(actions.tweaks.highlight).toEqual(true);
291 });
292
293 it('should bing on character negation ([!a]) bing words.', function() {
294 testEvent.event.content.body = "boke";
295 let actions = pushProcessor.actionsForEvent(testEvent);
296 expect(actions.tweaks.highlight).toEqual(true);
297 testEvent.event.content.body = "bake";
298 actions = pushProcessor.actionsForEvent(testEvent);
299 expect(actions.tweaks.highlight).toEqual(false);
300 });
301
302 // invalid
303
304 it('should gracefully handle bad input.', function() {
305 testEvent.event.content.body = { "foo": "bar" };
306 const actions = pushProcessor.actionsForEvent(testEvent);
307 expect(actions.tweaks.highlight).toEqual(false);
308 });
309});