2020-02-26 23:53:46 +01:00
const params = {
2017-07-29 21:17:12 +02:00
alert _words : [ 'alertone' , 'alerttwo' , 'alertthree' , 'al*rt.*s' , '.+' , 'emoji' ] ,
2018-08-01 21:17:03 +02:00
} ;
2013-08-29 21:33:26 +02:00
2017-11-08 16:48:11 +01:00
zrequire ( 'people' ) ;
zrequire ( 'alert_words' ) ;
2020-02-26 23:53:46 +01:00
alert _words . initialize ( params ) ;
2020-03-21 21:54:16 +01:00
people . add ( {
2017-01-19 23:04:52 +01:00
email : 'tester@zulip.com' ,
full _name : 'Tester von Tester' ,
user _id : 42 ,
} ) ;
2020-03-21 21:54:16 +01:00
people . initialize _current _user ( 42 ) ;
2017-01-19 23:04:52 +01:00
2018-05-07 03:30:13 +02:00
const regular _message = {
sender _email : 'another@zulip.com' ,
content : '<p>a message</p>' ,
} ;
const own _message = {
sender _email : 'tester@zulip.com' ,
content : '<p>hey this message alertone</p>' ,
alerted : true ,
} ;
const other _message = {
sender _email : 'another@zulip.com' ,
content : '<p>another alertone message</p>' ,
alerted : true ,
} ;
const caps _message = {
sender _email : 'another@zulip.com' ,
content : '<p>another ALERTtwo message</p>' ,
alerted : true ,
} ;
const alertwordboundary _message = {
sender _email : 'another@zulip.com' ,
content : '<p>another alertthreemessage</p>' ,
alerted : false ,
} ;
const multialert _message = {
sender _email : 'another@zulip.com' ,
content : '<p>another alertthreemessage alertone and then alerttwo</p>' ,
alerted : true ,
} ;
const unsafe _word _message = {
sender _email : 'another@zulip.com' ,
content : '<p>gotta al*rt.*s all</p>' ,
alerted : true ,
} ;
const alert _in _url _message = {
sender _email : 'another@zulip.com' ,
content : '<p>http://www.google.com/alertone/me</p>' ,
alerted : true ,
} ;
const question _word _message = {
sender _email : 'another@zulip.com' ,
content : '<p>still alertone? me</p>' ,
alerted : true ,
} ;
const alert _domain _message = {
sender _email : 'another@zulip.com' ,
content : '<p>now with link <a href="http://www.alerttwo.us/foo/bar" target="_blank" title="http://www.alerttwo.us/foo/bar">www.alerttwo.us/foo/bar</a></p>' ,
alerted : true ,
} ;
2017-07-29 21:17:12 +02:00
// This test ensure we are not mucking up rendered HTML content.
2018-05-07 03:30:13 +02:00
const message _with _emoji = {
sender _email : 'another@zulip.com' ,
content : '<p>I <img alt=":heart:" class="emoji" src="/static/generated/emoji/images/emoji/unicode/2764.png" title="heart"> emoji!</p>' ,
alerted : true ,
} ;
2014-03-04 22:55:36 +01:00
2018-05-15 12:40:07 +02:00
run _test ( 'notifications' , ( ) => {
2014-01-08 00:04:52 +01:00
assert ( ! alert _words . notifies ( regular _message ) ) ;
assert ( ! alert _words . notifies ( own _message ) ) ;
assert ( alert _words . notifies ( other _message ) ) ;
assert ( alert _words . notifies ( caps _message ) ) ;
assert ( ! alert _words . notifies ( alertwordboundary _message ) ) ;
assert ( alert _words . notifies ( multialert _message ) ) ;
assert ( alert _words . notifies ( unsafe _word _message ) ) ;
2014-03-04 22:55:36 +01:00
assert ( alert _words . notifies ( alert _domain _message ) ) ;
2017-07-29 21:17:12 +02:00
assert ( alert _words . notifies ( message _with _emoji ) ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2013-08-29 21:33:26 +02:00
2018-05-15 12:40:07 +02:00
run _test ( 'munging' , ( ) => {
2018-04-18 22:49:57 +02:00
let saved _content = regular _message . content ;
2013-08-29 21:33:26 +02:00
alert _words . process _message ( regular _message ) ;
assert . equal ( saved _content , regular _message . content ) ;
saved _content = alertwordboundary _message . content ;
alert _words . process _message ( alertwordboundary _message ) ;
assert . equal ( alertwordboundary _message . content , saved _content ) ;
alert _words . process _message ( other _message ) ;
assert . equal ( other _message . content , "<p>another <span class='alert-word'>alertone</span> message</p>" ) ;
alert _words . process _message ( caps _message ) ;
assert . equal ( caps _message . content , "<p>another <span class='alert-word'>ALERTtwo</span> message</p>" ) ;
alert _words . process _message ( multialert _message ) ;
assert . equal ( multialert _message . content , "<p>another alertthreemessage <span class='alert-word'>alertone</span> and then <span class='alert-word'>alerttwo</span></p>" ) ;
alert _words . process _message ( unsafe _word _message ) ;
assert . equal ( unsafe _word _message . content , "<p>gotta <span class='alert-word'>al*rt.*s</span> all</p>" ) ;
2013-10-09 20:09:48 +02:00
alert _words . process _message ( alert _in _url _message ) ;
assert . equal ( alert _in _url _message . content , "<p>http://www.google.com/alertone/me</p>" ) ;
alert _words . process _message ( question _word _message ) ;
assert . equal ( question _word _message . content , "<p>still <span class='alert-word'>alertone</span>? me</p>" ) ;
2014-03-04 22:55:36 +01:00
alert _words . process _message ( alert _domain _message ) ;
2018-04-18 22:49:57 +02:00
assert . equal ( alert _domain _message . content , ` <p>now with link <a href="http://www.alerttwo.us/foo/bar" target="_blank" title="http://www.alerttwo.us/foo/bar">www.<span class='alert-word'>alerttwo</span>.us/foo/bar</a></p> ` ) ;
2017-07-29 21:17:12 +02:00
alert _words . process _message ( message _with _emoji ) ;
2018-04-18 22:49:57 +02:00
assert . equal ( message _with _emoji . content , ` <p>I <img alt=":heart:" class="emoji" src="/static/generated/emoji/images/emoji/unicode/2764.png" title="heart"> <span class='alert-word'>emoji</span>!</p> ` ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2020-02-26 23:53:46 +01:00
run _test ( 'basic get/set operations' , ( ) => {
assert ( ! alert _words . has _alert _word ( 'breakfast' ) ) ;
assert ( ! alert _words . has _alert _word ( 'lunch' ) ) ;
alert _words . set _words ( [ 'breakfast' , 'lunch' ] ) ;
assert ( alert _words . has _alert _word ( 'breakfast' ) ) ;
assert ( alert _words . has _alert _word ( 'lunch' ) ) ;
assert ( ! alert _words . has _alert _word ( 'dinner' ) ) ;
} ) ;