2020-08-01 03:43:15 +02:00
"use strict" ;
2020-11-30 23:46:45 +01:00
const { strict : assert } = require ( "assert" ) ;
2020-12-01 00:02:16 +01:00
const { zrequire } = require ( "../zjsunit/namespace" ) ;
2020-12-01 00:39:47 +01:00
const { run _test } = require ( "../zjsunit/test" ) ;
2020-11-30 23:46:45 +01:00
2020-02-26 23:53:46 +01:00
const params = {
2020-07-15 01:29:15 +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
2020-08-20 21:24:06 +02:00
const people = zrequire ( "people" ) ;
2021-02-10 04:53:22 +01:00
const alert _words = zrequire ( "alert_words" ) ;
2017-11-08 16:48:11 +01:00
2020-02-26 23:53:46 +01:00
alert _words . initialize ( params ) ;
2020-05-26 22:34:15 +02:00
people . add _active _user ( {
2020-07-15 01:29:15 +02:00
email : "tester@zulip.com" ,
full _name : "Tester von Tester" ,
2017-01-19 23:04:52 +01:00
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 = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
content : "<p>a message</p>" ,
2018-05-07 03:30:13 +02:00
} ;
const own _message = {
2020-07-15 01:29:15 +02:00
sender _email : "tester@zulip.com" ,
content : "<p>hey this message alertone</p>" ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
const other _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
content : "<p>another alertone message</p>" ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
const caps _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
content : "<p>another ALERTtwo message</p>" ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
const alertwordboundary _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
content : "<p>another alertthreemessage</p>" ,
2018-05-07 03:30:13 +02:00
alerted : false ,
} ;
const multialert _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
2021-05-07 13:32:35 +02:00
content : "<p>another emoji alertone and then alerttwo</p>" ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
const unsafe _word _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
content : "<p>gotta al*rt.*s all</p>" ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
const alert _in _url _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
content : "<p>http://www.google.com/alertone/me</p>" ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
const question _word _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
content : "<p>still alertone? me</p>" ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
2021-03-06 20:36:45 +01:00
const typo _word _message = {
sender _email : "another@zulip.com" ,
content : "<p>alertones alerttwo alerttwo alertthreez</p>" ,
alerted : true ,
} ;
2018-05-07 03:30:13 +02:00
const alert _domain _message = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
2020-07-15 00:34:28 +02:00
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>' ,
2018-05-07 03:30:13 +02:00
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 = {
2020-07-15 01:29:15 +02:00
sender _email : "another@zulip.com" ,
2020-07-15 00:34:28 +02:00
content :
'<p>I <img alt=":heart:" class="emoji" src="/static/generated/emoji/images/emoji/unicode/2764.png" title="heart"> emoji!</p>' ,
2018-05-07 03:30:13 +02:00
alerted : true ,
} ;
2014-03-04 22:55:36 +01:00
2020-07-15 01:29:15 +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
2020-07-15 01:29:15 +02:00
run _test ( "munging" , ( ) => {
2021-03-08 15:27:01 +01:00
alert _words . initialize ( params ) ;
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 ) ;
2021-03-08 15:27:01 +01:00
function assert _transform ( message , expected _new _content ) {
const msg = { ... message } ;
alert _words . process _message ( msg ) ;
assert . equal ( msg . content , expected _new _content ) ;
}
assert _transform (
other _message ,
2020-07-15 00:34:28 +02:00
"<p>another <span class='alert-word'>alertone</span> message</p>" ,
) ;
2021-03-08 15:27:01 +01:00
assert _transform (
caps _message ,
2020-07-15 00:34:28 +02:00
"<p>another <span class='alert-word'>ALERTtwo</span> message</p>" ,
) ;
2013-08-29 21:33:26 +02:00
2021-03-08 15:27:01 +01:00
assert _transform (
multialert _message ,
2021-05-07 13:32:35 +02:00
"<p>another <span class='alert-word'>emoji</span> <span class='alert-word'>alertone</span> and then <span class='alert-word'>alerttwo</span></p>" ,
2020-07-15 00:34:28 +02:00
) ;
2013-08-29 21:33:26 +02:00
2021-03-08 15:27:01 +01:00
assert _transform (
unsafe _word _message ,
2020-07-15 00:34:28 +02:00
"<p>gotta <span class='alert-word'>al*rt.*s</span> all</p>" ,
) ;
2013-10-09 20:09:48 +02:00
2021-03-08 15:27:01 +01:00
assert _transform ( alert _in _url _message , "<p>http://www.google.com/alertone/me</p>" ) ;
2013-10-09 20:09:48 +02:00
2021-03-08 15:27:01 +01:00
assert _transform (
question _word _message ,
2020-07-15 00:34:28 +02:00
"<p>still <span class='alert-word'>alertone</span>? me</p>" ,
) ;
2014-03-04 22:55:36 +01:00
2021-03-06 20:36:45 +01:00
assert _transform (
typo _word _message ,
"<p>alertones <span class='alert-word'>alerttwo</span> <span class='alert-word'>alerttwo</span> alertthreez</p>" ,
) ;
2021-03-08 15:27:01 +01:00
assert _transform (
alert _domain _message ,
2020-07-15 00:34:28 +02:00
'<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
2021-03-08 15:27:01 +01:00
assert _transform (
message _with _emoji ,
2020-07-15 00:34:28 +02:00
'<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
2020-07-15 01:29:15 +02:00
run _test ( "basic get/set operations" , ( ) => {
2021-03-08 15:27:01 +01:00
alert _words . initialize ( { alert _words : [ ] } ) ;
2020-07-15 01:29:15 +02:00
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" ) ) ;
2020-02-26 23:53:46 +01:00
} ) ;