2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
2024-03-27 12:14:48 +01:00
|
|
|
const MockDate = require("mockdate");
|
2020-02-28 23:59:07 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {set_global, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2024-02-15 07:15:59 +01:00
|
|
|
const blueslip = zrequire("blueslip");
|
2024-10-09 08:44:21 +02:00
|
|
|
const {initialize_user_settings} = zrequire("user_settings");
|
2024-02-15 07:15:59 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", {});
|
|
|
|
const util = zrequire("util");
|
2016-10-05 03:38:12 +02:00
|
|
|
|
2024-10-09 08:44:21 +02:00
|
|
|
initialize_user_settings({user_settings: {}});
|
|
|
|
|
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({
|
2020-07-20 22:18:43 +02:00
|
|
|
compute_value() {
|
2013-08-08 22:04:41 +02:00
|
|
|
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("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("lower_bound", () => {
|
2021-07-15 21:02:18 +02:00
|
|
|
const arr = [{x: 10}, {x: 20}, {x: 30}, {x: 40}, {x: 50}];
|
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);
|
2021-07-15 21:02:18 +02:00
|
|
|
assert.equal(util.lower_bound(arr, 50, compare), 4);
|
|
|
|
assert.equal(util.lower_bound(arr, 55, compare), 5);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2024-02-15 07:15:59 +01:00
|
|
|
run_test("lower_same", () => {
|
|
|
|
assert.ok(util.lower_same("abc", "AbC"));
|
|
|
|
assert.ok(!util.lower_same("abbc", "AbC"));
|
|
|
|
|
|
|
|
blueslip.expect("error", "Cannot compare strings; at least one value is undefined");
|
|
|
|
util.lower_same("abc", undefined);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("same_recipient", () => {
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
2020-07-15 00:34:28 +02:00
|
|
|
util.same_recipient(
|
|
|
|
{type: "stream", stream_id: 101, topic: "Bar"},
|
|
|
|
{type: "stream", stream_id: 101, topic: "bar"},
|
|
|
|
),
|
|
|
|
);
|
2017-02-24 01:44:38 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
2020-07-15 00:34:28 +02:00
|
|
|
!util.same_recipient(
|
|
|
|
{type: "stream", stream_id: 101, topic: "Bar"},
|
|
|
|
{type: "stream", stream_id: 102, topic: "whatever"},
|
|
|
|
),
|
|
|
|
);
|
2017-02-24 01:44:38 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
2020-07-15 00:34:28 +02:00
|
|
|
util.same_recipient(
|
|
|
|
{type: "private", to_user_ids: "101,102"},
|
|
|
|
{type: "private", to_user_ids: "101,102"},
|
|
|
|
),
|
|
|
|
);
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
2020-07-15 00:34:28 +02:00
|
|
|
!util.same_recipient(
|
|
|
|
{type: "private", to_user_ids: "101,102"},
|
|
|
|
{type: "private", to_user_ids: "103"},
|
|
|
|
),
|
|
|
|
);
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
|
|
|
!util.same_recipient({type: "stream", stream_id: 101, topic: "Bar"}, {type: "private"}),
|
|
|
|
);
|
2014-03-13 16:39:29 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!util.same_recipient({type: "private", to_user_ids: undefined}, {type: "private"}));
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!util.same_recipient(undefined, {type: "private"}));
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!util.same_recipient(undefined, undefined));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2023-04-09 05:22:23 +02:00
|
|
|
run_test("robust_url_decode", ({override}) => {
|
|
|
|
assert.equal(util.robust_url_decode("xxx%3Ayyy"), "xxx:yyy");
|
|
|
|
assert.equal(util.robust_url_decode("xxx%3"), "xxx");
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2022-07-09 23:25:05 +02:00
|
|
|
override(global, "decodeURIComponent", () => {
|
|
|
|
throw new Error("foo");
|
|
|
|
});
|
|
|
|
assert.throws(
|
2021-03-10 13:33:55 +01:00
|
|
|
() => {
|
2023-04-09 05:22:23 +02:00
|
|
|
util.robust_url_decode("%E0%A4%A");
|
2021-03-10 13:33:55 +01:00
|
|
|
},
|
2022-07-09 23:25:05 +02:00
|
|
|
{name: "Error", message: "foo"},
|
2021-03-10 13:33:55 +01:00
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2022-07-09 23:25:05 +02:00
|
|
|
run_test("dumb_strcmp", ({override}) => {
|
|
|
|
override(Intl, "Collator", undefined);
|
|
|
|
const strcmp = util.make_strcmp();
|
|
|
|
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("is_mobile", () => {
|
2020-12-01 00:57:57 +01:00
|
|
|
window.navigator = {userAgent: "Android"};
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(util.is_mobile());
|
2017-06-03 20:54:43 +02:00
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
window.navigator = {userAgent: "Not mobile"};
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!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", () => {
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(util.array_compare([], []));
|
|
|
|
assert.ok(util.array_compare([1, 2, 3], [1, 2, 3]));
|
|
|
|
assert.ok(!util.array_compare([1, 2], [1, 2, 3]));
|
|
|
|
assert.ok(!util.array_compare([1, 2, 3], [1, 2]));
|
|
|
|
assert.ok(!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 "),
|
2020-07-15 00:34:28 +02:00
|
|
|
"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);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(min <= val);
|
|
|
|
assert.ok(val <= max);
|
2014-03-13 17:28:50 +01:00
|
|
|
assert.equal(val, Math.floor(val));
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-03-13 17:28:50 +01:00
|
|
|
|
2023-08-18 22:48:22 +02:00
|
|
|
run_test("wildcard_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**",
|
2024-03-21 20:11:29 +01:00
|
|
|
'"@**everyone**"',
|
|
|
|
"@**everyone**: Look at this!",
|
2024-04-04 02:13:35 +02:00
|
|
|
"The <@**everyone**> channel",
|
|
|
|
'I have to say "@**everyone**" to ding the bell',
|
2020-07-15 01:29:15 +02:00
|
|
|
"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
|
|
|
];
|
|
|
|
|
2024-03-13 19:19:24 +01:00
|
|
|
const messages_with_channel_mentions = [
|
|
|
|
"@**channel**",
|
|
|
|
"some text before @**channel** some text after",
|
|
|
|
"@**channel** some text after only",
|
|
|
|
"some text before only @**channel**",
|
|
|
|
];
|
|
|
|
|
2023-08-18 22:48:22 +02:00
|
|
|
const messages_with_topic_mentions = [
|
|
|
|
"@**topic**",
|
|
|
|
"some text before @**topic** some text after",
|
|
|
|
"@**topic** some text after only",
|
|
|
|
"some text before only @**topic**",
|
|
|
|
];
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
2024-03-13 19:19:24 +01:00
|
|
|
const messages_without_channel_mentions = [
|
|
|
|
"some text before @channel some text after",
|
|
|
|
"@channel",
|
|
|
|
"`@channel`",
|
|
|
|
"some_email@channel.com",
|
|
|
|
"`@**channel**`",
|
|
|
|
"some_email@**channel**.com",
|
|
|
|
];
|
|
|
|
|
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) {
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(util.find_stream_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) {
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(util.find_stream_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) {
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(util.find_stream_wildcard_mentions(messages_with_stream_mentions[i]));
|
2018-04-03 17:55:57 +02:00
|
|
|
}
|
|
|
|
|
2024-03-13 19:19:24 +01:00
|
|
|
for (i = 0; i < messages_with_channel_mentions.length; i += 1) {
|
|
|
|
assert.ok(util.find_stream_wildcard_mentions(messages_with_channel_mentions[i]));
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:48:22 +02:00
|
|
|
for (i = 0; i < messages_with_topic_mentions.length; i += 1) {
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(!util.find_stream_wildcard_mentions(messages_with_topic_mentions[i]));
|
2023-08-18 22:48:22 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 0; i < messages_without_all_mentions.length; i += 1) {
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(!util.find_stream_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) {
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(!util.find_stream_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) {
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(!util.find_stream_wildcard_mentions(messages_without_stream_mentions[i]));
|
2018-04-03 17:55:57 +02:00
|
|
|
}
|
2024-03-13 19:19:24 +01:00
|
|
|
|
|
|
|
for (i = 0; i < messages_without_channel_mentions.length; i += 1) {
|
|
|
|
assert.ok(!util.find_stream_wildcard_mentions(messages_without_channel_mentions[i]));
|
|
|
|
}
|
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", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
const strings = ["string1", "string3", "string2", "string4"];
|
|
|
|
const strings_selection = ["string4", "string1"];
|
|
|
|
const strings_expected = ["string1", "string4", "string3", "string2"];
|
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
|
|
|
];
|
2020-07-15 00:34:28 +02:00
|
|
|
const emails_selection = ["test@test.com", "test@localhost", "test@invalid@email"];
|
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);
|
2021-06-08 05:06:16 +02:00
|
|
|
assert.deepEqual(strings_no_selection, strings);
|
|
|
|
assert.deepEqual(strings_no_array, []);
|
|
|
|
assert.deepEqual(strings_actual, strings_expected);
|
|
|
|
assert.deepEqual(emails_actual, emails_expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2020-02-28 23:59:07 +01:00
|
|
|
|
2022-06-16 11:39:05 +02:00
|
|
|
run_test("filter_by_word_prefix_match", () => {
|
|
|
|
const strings = ["stream-hyphen_underscore/slash", "three word stream"];
|
|
|
|
const values = [0, 1];
|
|
|
|
const item_to_string = (idx) => strings[idx];
|
|
|
|
|
|
|
|
// Default settings will match words with a space delimiter before them.
|
|
|
|
assert.deepEqual(util.filter_by_word_prefix_match(values, "stream", item_to_string), [0, 1]);
|
|
|
|
assert.deepEqual(util.filter_by_word_prefix_match(values, "word stream", item_to_string), [1]);
|
|
|
|
|
|
|
|
// Since - appears before `hyphen` in
|
|
|
|
// stream-hyphen_underscore/slash, we require `-` in the set of
|
|
|
|
// characters for it to match.
|
|
|
|
assert.deepEqual(util.filter_by_word_prefix_match(values, "hyphe", item_to_string), []);
|
|
|
|
assert.deepEqual(util.filter_by_word_prefix_match(values, "hyphe", item_to_string, /[\s/_-]/), [
|
|
|
|
0,
|
|
|
|
]);
|
|
|
|
assert.deepEqual(util.filter_by_word_prefix_match(values, "hyphe", item_to_string, /[\s-]/), [
|
|
|
|
0,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Similarly `_` must be in the set of allowed characters to match "underscore".
|
|
|
|
assert.deepEqual(util.filter_by_word_prefix_match(values, "unders", item_to_string, /[\s_]/), [
|
|
|
|
0,
|
|
|
|
]);
|
|
|
|
assert.deepEqual(util.filter_by_word_prefix_match(values, "unders", item_to_string, /\s/), []);
|
|
|
|
});
|
2022-12-19 22:23:38 +01:00
|
|
|
|
|
|
|
run_test("get_string_diff", () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
util.get_string_diff("#ann is for updates", "#**announce** is for updates"),
|
|
|
|
[1, 4, 13],
|
|
|
|
);
|
|
|
|
assert.deepEqual(util.get_string_diff("/p", "/poll"), [2, 2, 5]);
|
|
|
|
assert.deepEqual(util.get_string_diff("Hey @Aa", "Hey @**aaron** "), [5, 7, 15]);
|
|
|
|
assert.deepEqual(util.get_string_diff("same", "same"), [0, 0, 0]);
|
|
|
|
assert.deepEqual(util.get_string_diff("same-end", "two same-end"), [0, 0, 4]);
|
|
|
|
assert.deepEqual(util.get_string_diff("space", "sp ace"), [2, 2, 3]);
|
|
|
|
});
|
2023-09-28 08:54:36 +02:00
|
|
|
|
|
|
|
run_test("is_valid_url", () => {
|
|
|
|
assert.equal(util.is_valid_url("http://"), false);
|
|
|
|
assert.equal(util.is_valid_url("random_string"), true);
|
|
|
|
assert.equal(util.is_valid_url("http://google.com/something?q=query#hash"), true);
|
|
|
|
assert.equal(util.is_valid_url("/abc/"), true);
|
|
|
|
|
|
|
|
assert.equal(util.is_valid_url("http://", true), false);
|
|
|
|
assert.equal(util.is_valid_url("random_string", true), false);
|
|
|
|
assert.equal(util.is_valid_url("http://google.com/something?q=query#hash", true), true);
|
|
|
|
assert.equal(util.is_valid_url("/abc/", true), false);
|
|
|
|
});
|
2024-01-05 15:22:54 +01:00
|
|
|
|
|
|
|
run_test("format_array_as_list", () => {
|
|
|
|
const array = ["apple", "banana", "orange"];
|
|
|
|
// when Intl exist
|
|
|
|
assert.equal(
|
|
|
|
util.format_array_as_list(array, "long", "conjunction"),
|
|
|
|
"apple, banana, and orange",
|
|
|
|
);
|
|
|
|
|
|
|
|
// when Intl.ListFormat does not exist
|
|
|
|
global.Intl.ListFormat = undefined;
|
|
|
|
assert.equal(util.format_array_as_list(array, "long", "conjunction"), "apple, banana, orange");
|
|
|
|
});
|
2024-03-27 12:14:48 +01:00
|
|
|
|
|
|
|
run_test("get_remaining_time", () => {
|
|
|
|
// When current time is less than start time
|
|
|
|
// Set a random start time
|
|
|
|
const start_time = new Date(1000).getTime();
|
|
|
|
// Set current time to 400ms ahead of the start time
|
|
|
|
MockDate.set(start_time + 400);
|
|
|
|
const duration = 500;
|
|
|
|
let expected_remaining_time = 100;
|
|
|
|
assert.equal(util.get_remaining_time(start_time, duration), expected_remaining_time);
|
|
|
|
|
|
|
|
// When current time is greater than start time + duration
|
|
|
|
// Set current time to 100ms after the start time + duration
|
|
|
|
MockDate.set(start_time + duration + 100);
|
|
|
|
expected_remaining_time = 0;
|
|
|
|
assert.equal(util.get_remaining_time(start_time, duration), expected_remaining_time);
|
|
|
|
|
|
|
|
MockDate.reset();
|
|
|
|
});
|
2024-06-26 22:36:33 +02:00
|
|
|
|
|
|
|
run_test("the", () => {
|
|
|
|
const list_with_one_item = ["foo"];
|
|
|
|
assert.equal(util.the(list_with_one_item), "foo");
|
2024-08-26 07:01:09 +02:00
|
|
|
|
|
|
|
blueslip.expect("error", "the: expected only 1 item, got more");
|
|
|
|
const list_with_more_items = ["foo", "bar"];
|
|
|
|
// Error is thrown, but we still return the first item to avoid
|
|
|
|
// unnecessarily breaking the app.
|
|
|
|
assert.equal(util.the(list_with_more_items), "foo");
|
|
|
|
|
|
|
|
blueslip.expect("error", "the: expected only 1 item, got none");
|
|
|
|
// Error is thrown, but we still return the "first" item to avoid
|
|
|
|
// unnecessarily breaking the app for places we refactored this that
|
|
|
|
// were previously typed wrong but not breaking the app.
|
|
|
|
assert.equal(util.the([]), undefined);
|
2024-06-26 22:36:33 +02:00
|
|
|
});
|