"use strict"; const assert = require("node:assert/strict"); const {set_global, zrequire} = require("./lib/namespace.cjs"); const {run_test} = require("./lib/test.cjs"); set_global("page_params", { is_spectator: false, }); const params = { alert_words: [ "alertone", "alerttwo", "alertthree", "al*rt.*s", ".+", "emoji", "FD&C", "<3", ">8", "5'11\"", ], }; const people = zrequire("people"); const alert_words = zrequire("alert_words"); alert_words.initialize(params); people.add_active_user({ email: "tester@zulip.com", full_name: "Tester von Tester", user_id: 42, }); people.initialize_current_user(42); const regular_message = { sender_email: "another@zulip.com", content: "
a message
", }; const own_message = { sender_email: "tester@zulip.com", content: "hey this message alertone
", alerted: true, }; const other_message = { sender_email: "another@zulip.com", content: "another alertone message
", alerted: true, }; const caps_message = { sender_email: "another@zulip.com", content: "another ALERTtwo message
", alerted: true, }; const alertwordboundary_message = { sender_email: "another@zulip.com", content: "another alertthreemessage
", alerted: false, }; const multialert_message = { sender_email: "another@zulip.com", content: "another emoji alertone and then alerttwo
", alerted: true, }; const unsafe_word_message = { sender_email: "another@zulip.com", content: "gotta al*rt.*s all
", alerted: true, }; const alert_in_url_message = { sender_email: "another@zulip.com", content: "http://www.google.com/alertone/me
", alerted: true, }; const question_word_message = { sender_email: "another@zulip.com", content: "still alertone? me
", alerted: true, }; const typo_word_message = { sender_email: "another@zulip.com", content: "alertones alerttwo alerttwo alertthreez
", alerted: true, }; const alert_domain_message = { sender_email: "another@zulip.com", content: 'now with link www.alerttwo.us/foo/bar
', alerted: true, }; // This test ensure we are not mucking up rendered HTML content. const message_with_emoji = { sender_email: "another@zulip.com", content: 'I emoji!
', alerted: true, }; run_test("notifications", () => { assert.ok(!alert_words.notifies(regular_message)); assert.ok(!alert_words.notifies(own_message)); assert.ok(alert_words.notifies(other_message)); assert.ok(alert_words.notifies(caps_message)); assert.ok(!alert_words.notifies(alertwordboundary_message)); assert.ok(alert_words.notifies(multialert_message)); assert.ok(alert_words.notifies(unsafe_word_message)); assert.ok(alert_words.notifies(alert_domain_message)); assert.ok(alert_words.notifies(message_with_emoji)); }); run_test("munging", () => { alert_words.initialize(params); assert.deepEqual(alert_words.get_word_list(), [ {word: "alertthree"}, {word: "alertone"}, {word: "alerttwo"}, {word: "al*rt.*s"}, {word: "emoji"}, {word: `5'11"`}, {word: "FD&C"}, {word: ".+"}, {word: "<3"}, {word: ">8"}, ]); let saved_content = regular_message.content; 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); 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, "another alertone message
", ); assert_transform( caps_message, "another ALERTtwo message
", ); assert_transform( multialert_message, "another emoji alertone and then alerttwo
", ); assert_transform( unsafe_word_message, "gotta al*rt.*s all
", ); assert_transform(alert_in_url_message, "http://www.google.com/alertone/me
"); assert_transform( question_word_message, "still alertone? me
", ); assert_transform( typo_word_message, "alertones alerttwo alerttwo alertthreez
", ); assert_transform( alert_domain_message, 'now with link www.alerttwo.us/foo/bar
', ); assert_transform( message_with_emoji, 'I emoji!
', ); assert_transform( { sender_email: "another@zulip.com", content: `FD&C <3 >8 5'11" 5'11"
`, alerted: true, }, `FD&C <3 >8 5'11" 5'11"
`, ); }); run_test("basic get/set operations", () => { alert_words.initialize({alert_words: []}); assert.ok(!alert_words.has_alert_word("breakfast")); assert.ok(!alert_words.has_alert_word("lunch")); alert_words.set_words(["breakfast", "lunch"]); assert.ok(alert_words.has_alert_word("breakfast")); assert.ok(alert_words.has_alert_word("lunch")); assert.ok(!alert_words.has_alert_word("dinner")); });