2020-02-28 23:59:07 +01:00
|
|
|
const { JSDOM } = require("jsdom");
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("$", global.make_zjquery());
|
|
|
|
set_global("DOMParser", new JSDOM().window.DOMParser);
|
|
|
|
set_global("document", {});
|
|
|
|
const util = zrequire("util");
|
2016-10-05 03:38:12 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("CachedValue", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let x = 5;
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const cv = new util.CachedValue({
|
2013-08-08 22:04:41 +02:00
|
|
|
compute_value: function () {
|
|
|
|
return x * 2;
|
2016-12-03 23:17:57 +01:00
|
|
|
},
|
2013-08-08 22:04:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(cv.get(), 10);
|
|
|
|
|
|
|
|
x = 6;
|
|
|
|
assert.equal(cv.get(), 10);
|
|
|
|
cv.reset();
|
|
|
|
assert.equal(cv.get(), 12);
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_reload_topic", () => {
|
|
|
|
assert.equal(util.get_reload_topic({subject: "foo"}), "foo");
|
|
|
|
assert.equal(util.get_reload_topic({topic: "bar"}), "bar");
|
2018-12-16 18:02:16 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("extract_pm_recipients", () => {
|
|
|
|
assert.equal(util.extract_pm_recipients("bob@foo.com, alice@foo.com").length, 2);
|
|
|
|
assert.equal(util.extract_pm_recipients("bob@foo.com, ").length, 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-11-15 06:40:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("is_pm_recipient", () => {
|
|
|
|
const message = { to_user_ids: "31,32,33" };
|
2020-06-05 19:49:16 +02:00
|
|
|
assert(util.is_pm_recipient(31, message));
|
|
|
|
assert(util.is_pm_recipient(32, message));
|
|
|
|
assert(!util.is_pm_recipient(34, message));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("lower_bound", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let arr = [10, 20, 30, 40, 50];
|
2013-08-08 22:04:41 +02:00
|
|
|
assert.equal(util.lower_bound(arr, 5), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 10), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 15), 1);
|
|
|
|
assert.equal(util.lower_bound(arr, 50), 4);
|
|
|
|
assert.equal(util.lower_bound(arr, 55), 5);
|
|
|
|
assert.equal(util.lower_bound(arr, 2, 4, 31), 3);
|
|
|
|
|
2018-12-18 19:34:45 +01:00
|
|
|
arr = [{x: 10}, {x: 20}, {x: 30}];
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function compare(a, b) {
|
2013-08-08 22:04:41 +02:00
|
|
|
return a.x < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(util.lower_bound(arr, 5, compare), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 10, compare), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 15, compare), 1);
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("same_recipient", () => {
|
2017-02-24 01:44:38 +01:00
|
|
|
assert(util.same_recipient(
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "stream", stream_id: 101, topic: "Bar"},
|
|
|
|
{type: "stream", stream_id: 101, topic: "bar"}));
|
2017-02-24 01:44:38 +01:00
|
|
|
|
|
|
|
assert(!util.same_recipient(
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "stream", stream_id: 101, topic: "Bar"},
|
|
|
|
{type: "stream", stream_id: 102, topic: "whatever"}));
|
2017-02-24 01:44:38 +01:00
|
|
|
|
|
|
|
assert(util.same_recipient(
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "private", to_user_ids: "101,102"},
|
|
|
|
{type: "private", to_user_ids: "101,102"}));
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2017-02-25 00:57:09 +01:00
|
|
|
assert(!util.same_recipient(
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "private", to_user_ids: "101,102"},
|
|
|
|
{type: "private", to_user_ids: "103"}));
|
2013-08-08 22:04:41 +02:00
|
|
|
|
|
|
|
assert(!util.same_recipient(
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "stream", stream_id: 101, topic: "Bar"},
|
|
|
|
{type: "private"}));
|
2014-03-13 16:39:29 +01:00
|
|
|
|
2017-06-03 20:54:43 +02:00
|
|
|
assert(!util.same_recipient(
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "private", to_user_ids: undefined},
|
|
|
|
{type: "private"}));
|
2017-06-03 20:54:43 +02:00
|
|
|
|
|
|
|
assert(!util.same_recipient(
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "unknown type"},
|
|
|
|
{type: "unknown type"}));
|
2017-06-03 20:54:43 +02:00
|
|
|
|
|
|
|
assert(!util.same_recipient(
|
|
|
|
undefined,
|
2020-07-15 01:29:15 +02:00
|
|
|
{type: "private"}));
|
2017-06-03 20:54:43 +02:00
|
|
|
|
|
|
|
assert(!util.same_recipient(undefined, undefined));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("robust_uri_decode", () => {
|
|
|
|
assert.equal(util.robust_uri_decode("xxx%3Ayyy"), "xxx:yyy");
|
|
|
|
assert.equal(util.robust_uri_decode("xxx%3"), "xxx");
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("decodeURIComponent", () => { throw "foo"; });
|
2017-06-03 20:54:43 +02:00
|
|
|
try {
|
2020-07-15 01:29:15 +02:00
|
|
|
util.robust_uri_decode("%E0%A4%A");
|
2017-06-03 20:54:43 +02:00
|
|
|
} catch (e) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(e, "foo");
|
2017-06-03 20:54:43 +02:00
|
|
|
}
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("dumb_strcmp", () => {
|
2017-06-03 23:55:52 +02:00
|
|
|
Intl.Collator = undefined;
|
2019-11-02 00:06:25 +01:00
|
|
|
const strcmp = util.make_strcmp();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(strcmp("a", "b"), -1);
|
|
|
|
assert.equal(strcmp("c", "c"), 0);
|
|
|
|
assert.equal(strcmp("z", "y"), 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-03 23:55:52 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_edit_event_orig_topic", () => {
|
|
|
|
assert.equal(util.get_edit_event_orig_topic({orig_subject: "lunch"}), "lunch");
|
2018-12-22 17:39:37 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_edit_event_prev_topic", () => {
|
|
|
|
assert.equal(util.get_edit_event_prev_topic({prev_subject: "dinner"}), "dinner");
|
2019-02-22 15:08:16 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("is_mobile", () => {
|
2017-06-03 20:54:43 +02:00
|
|
|
global.window.navigator = { userAgent: "Android" };
|
|
|
|
assert(util.is_mobile());
|
|
|
|
|
|
|
|
global.window.navigator = { userAgent: "Not mobile" };
|
|
|
|
assert(!util.is_mobile());
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("array_compare", () => {
|
2013-08-08 22:04:41 +02:00
|
|
|
assert(util.array_compare([], []));
|
2018-12-07 22:14:28 +01:00
|
|
|
assert(util.array_compare([1, 2, 3], [1, 2, 3]));
|
|
|
|
assert(!util.array_compare([1, 2], [1, 2, 3]));
|
|
|
|
assert(!util.array_compare([1, 2, 3], [1, 2]));
|
|
|
|
assert(!util.array_compare([1, 2, 3, 4], [1, 2, 3, 5]));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("normalize_recipients", () => {
|
2016-11-19 00:33:32 +01:00
|
|
|
assert.equal(
|
2020-07-15 01:29:15 +02:00
|
|
|
util.normalize_recipients("ZOE@foo.com, bob@foo.com, alice@foo.com, AARON@foo.com "),
|
|
|
|
"aaron@foo.com,alice@foo.com,bob@foo.com,zoe@foo.com");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-03-13 16:55:22 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("random_int", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const min = 0;
|
|
|
|
const max = 100;
|
2014-03-13 17:28:50 +01:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
_.times(500, () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const val = util.random_int(min, max);
|
2014-03-13 17:28:50 +01:00
|
|
|
assert(min <= val);
|
|
|
|
assert(val <= max);
|
|
|
|
assert.equal(val, Math.floor(val));
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-03-13 17:28:50 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("all_and_everyone_mentions_regexp", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const messages_with_all_mentions = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"@**all**",
|
|
|
|
"some text before @**all** some text after",
|
|
|
|
"@**all** some text after only",
|
|
|
|
"some text before only @**all**",
|
2016-08-15 22:24:40 +02:00
|
|
|
];
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const messages_with_everyone_mentions = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"@**everyone**",
|
|
|
|
"some text before @**everyone** some text after",
|
|
|
|
"@**everyone** some text after only",
|
|
|
|
"some text before only @**everyone**",
|
2016-08-15 22:24:40 +02:00
|
|
|
];
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const messages_with_stream_mentions = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"@**stream**",
|
|
|
|
"some text before @**stream** some text after",
|
|
|
|
"@**stream** some text after only",
|
|
|
|
"some text before only @**stream**",
|
2018-04-03 17:55:57 +02:00
|
|
|
];
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const messages_without_all_mentions = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"@all",
|
|
|
|
"some text before @all some text after",
|
|
|
|
"`@everyone`",
|
|
|
|
"some_email@everyone.com",
|
|
|
|
"`@**everyone**`",
|
|
|
|
"some_email@**everyone**.com",
|
2016-08-15 22:24:40 +02:00
|
|
|
];
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const messages_without_everyone_mentions = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"some text before @everyone some text after",
|
|
|
|
"@everyone",
|
|
|
|
"`@everyone`",
|
|
|
|
"some_email@everyone.com",
|
|
|
|
"`@**everyone**`",
|
|
|
|
"some_email@**everyone**.com",
|
2016-08-15 22:24:40 +02:00
|
|
|
];
|
2018-04-03 17:55:57 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const messages_without_stream_mentions = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"some text before @stream some text after",
|
|
|
|
"@stream",
|
|
|
|
"`@stream`",
|
|
|
|
"some_email@stream.com",
|
|
|
|
"`@**stream**`",
|
|
|
|
"some_email@**stream**.com",
|
2018-04-03 17:55:57 +02:00
|
|
|
];
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 0; i < messages_with_all_mentions.length; i += 1) {
|
2020-01-23 07:22:26 +01:00
|
|
|
assert(util.find_wildcard_mentions(messages_with_all_mentions[i]));
|
2016-08-15 22:24:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 0; i < messages_with_everyone_mentions.length; i += 1) {
|
2020-01-23 07:22:26 +01:00
|
|
|
assert(util.find_wildcard_mentions(messages_with_everyone_mentions[i]));
|
2016-08-15 22:24:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 0; i < messages_with_stream_mentions.length; i += 1) {
|
2020-01-23 07:22:26 +01:00
|
|
|
assert(util.find_wildcard_mentions(messages_with_stream_mentions[i]));
|
2018-04-03 17:55:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 0; i < messages_without_all_mentions.length; i += 1) {
|
2020-01-23 07:22:26 +01:00
|
|
|
assert(!util.find_wildcard_mentions(messages_without_everyone_mentions[i]));
|
2016-08-15 22:24:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 0; i < messages_without_everyone_mentions.length; i += 1) {
|
2020-01-23 07:22:26 +01:00
|
|
|
assert(!util.find_wildcard_mentions(messages_without_everyone_mentions[i]));
|
2016-08-15 22:24:40 +02:00
|
|
|
}
|
2018-04-03 17:55:57 +02:00
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 0; i < messages_without_stream_mentions.length; i += 1) {
|
2020-01-23 07:22:26 +01:00
|
|
|
assert(!util.find_wildcard_mentions(messages_without_stream_mentions[i]));
|
2018-04-03 17:55:57 +02:00
|
|
|
}
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-12-13 20:03:23 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("move_array_elements_to_front", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const strings = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"string1",
|
|
|
|
"string3",
|
|
|
|
"string2",
|
|
|
|
"string4",
|
2016-12-13 20:03:23 +01:00
|
|
|
];
|
2019-11-02 00:06:25 +01:00
|
|
|
const strings_selection = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"string4",
|
|
|
|
"string1",
|
2016-12-13 20:03:23 +01:00
|
|
|
];
|
2019-11-02 00:06:25 +01:00
|
|
|
const strings_expected = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"string1",
|
|
|
|
"string4",
|
|
|
|
"string3",
|
|
|
|
"string2",
|
2016-12-13 20:03:23 +01:00
|
|
|
];
|
2019-11-02 00:06:25 +01:00
|
|
|
const strings_no_selection = util.move_array_elements_to_front(strings, []);
|
|
|
|
const strings_no_array = util.move_array_elements_to_front([], strings_selection);
|
|
|
|
const strings_actual = util.move_array_elements_to_front(strings, strings_selection);
|
|
|
|
const emails = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"test@zulip.com",
|
|
|
|
"test@test.com",
|
|
|
|
"test@localhost",
|
|
|
|
"test@invalid@email",
|
|
|
|
"something@zulip.com",
|
2016-12-13 20:03:23 +01:00
|
|
|
];
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails_selection = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"test@test.com",
|
|
|
|
"test@localhost",
|
|
|
|
"test@invalid@email",
|
2016-12-13 20:03:23 +01:00
|
|
|
];
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails_expected = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"test@test.com",
|
|
|
|
"test@localhost",
|
|
|
|
"test@invalid@email",
|
|
|
|
"test@zulip.com",
|
|
|
|
"something@zulip.com",
|
2016-12-13 20:03:23 +01:00
|
|
|
];
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails_actual = util.move_array_elements_to_front(emails, emails_selection);
|
|
|
|
let i;
|
2016-12-13 20:03:23 +01:00
|
|
|
assert(strings_no_selection.length === strings.length);
|
|
|
|
for (i = 0; i < strings_no_selection.length; i += 1) {
|
|
|
|
assert(strings_no_selection[i] === strings[i]);
|
|
|
|
}
|
|
|
|
assert(strings_no_array.length === 0);
|
|
|
|
assert(strings_actual.length === strings_expected.length);
|
|
|
|
for (i = 0; i < strings_actual.length; i += 1) {
|
|
|
|
assert(strings_actual[i] === strings_expected[i]);
|
|
|
|
}
|
|
|
|
assert(emails_actual.length === emails_expected.length);
|
|
|
|
for (i = 0; i < emails_actual.length; i += 1) {
|
|
|
|
assert(emails_actual[i] === emails_expected[i]);
|
|
|
|
}
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2020-02-28 23:59:07 +01:00
|
|
|
|
|
|
|
run_test("clean_user_content_links", () => {
|
|
|
|
window.location.href = "http://zulip.zulipdev.com/";
|
2020-05-09 01:52:39 +02:00
|
|
|
window.location.origin = "http://zulip.zulipdev.com";
|
2020-02-28 23:59:07 +01:00
|
|
|
assert.equal(
|
|
|
|
util.clean_user_content_links(
|
|
|
|
'<a href="http://example.com">good</a> ' +
|
2020-05-09 01:52:39 +02:00
|
|
|
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/file.png">upload</a> ' +
|
2020-02-28 23:59:07 +01:00
|
|
|
'<a href="http://localhost:NNNN">invalid</a> ' +
|
2020-03-24 03:53:15 +01:00
|
|
|
'<a href="javascript:alert(1)">unsafe</a> ' +
|
2020-07-02 02:16:03 +02:00
|
|
|
'<a href="/#fragment" target="_blank">fragment</a>',
|
2020-02-28 23:59:07 +01:00
|
|
|
),
|
2020-05-09 01:52:39 +02:00
|
|
|
'<a href="http://example.com" target="_blank" rel="noopener noreferrer" title="http://example.com/">good</a> ' +
|
|
|
|
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/file.png" target="_blank" rel="noopener noreferrer" title="file.png">upload</a> ' +
|
2020-07-15 01:29:15 +02:00
|
|
|
"<a>invalid</a> " +
|
|
|
|
"<a>unsafe</a> " +
|
2020-07-02 02:16:03 +02:00
|
|
|
'<a href="/#fragment" title="http://zulip.zulipdev.com/#fragment">fragment</a>',
|
2020-02-28 23:59:07 +01:00
|
|
|
);
|
|
|
|
});
|