2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-10-01 22:03:44 +02:00
|
|
|
const hash_parser = zrequire("hash_parser");
|
2020-12-01 23:21:38 +01:00
|
|
|
const hash_util = zrequire("hash_util");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const people = zrequire("people");
|
2023-10-01 21:54:23 +02:00
|
|
|
const spectators = zrequire("spectators");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const hamlet = {
|
2020-07-03 17:02:30 +02:00
|
|
|
user_id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "hamlet@example.com",
|
|
|
|
full_name: "Hamlet",
|
2018-04-13 23:44:10 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(hamlet);
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const frontend = {
|
2018-04-13 23:44:10 +02:00
|
|
|
stream_id: 99,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "frontend",
|
2018-04-13 23:44:10 +02:00
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(frontend);
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("hash_util", () => {
|
2018-04-13 23:44:10 +02:00
|
|
|
// Test encode_operand and decode_operand
|
|
|
|
function encode_decode_operand(operator, operand, expected_val) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const encode_result = hash_util.encode_operand(operator, operand);
|
2018-04-13 23:44:10 +02:00
|
|
|
assert.equal(encode_result, expected_val);
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_operand = encode_result;
|
|
|
|
const decode_result = hash_util.decode_operand(operator, new_operand);
|
2018-04-13 23:44:10 +02:00
|
|
|
assert.equal(decode_result, operand);
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let operator = "sender";
|
2019-11-02 00:06:25 +01:00
|
|
|
let operand = hamlet.email;
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2022-10-25 14:38:45 +02:00
|
|
|
encode_decode_operand(operator, operand, "15-Hamlet");
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
operator = "stream";
|
|
|
|
operand = "frontend";
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
encode_decode_operand(operator, operand, "99-frontend");
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
operator = "topic";
|
|
|
|
operand = "testing 123";
|
2018-04-13 23:44:10 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
encode_decode_operand(operator, operand, "testing.20123");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-08-05 01:30:23 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_get_hash_category", () => {
|
2024-04-30 13:30:24 +02:00
|
|
|
assert.deepEqual(hash_parser.get_hash_category("channels/subscribed"), "channels");
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.deepEqual(hash_parser.get_hash_category("#settings/preferences"), "settings");
|
|
|
|
assert.deepEqual(hash_parser.get_hash_category("#drafts"), "drafts");
|
|
|
|
assert.deepEqual(hash_parser.get_hash_category("invites"), "invites");
|
2021-03-23 05:14:54 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#settings/profile";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.deepEqual(hash_parser.get_current_hash_category(), "settings");
|
2018-12-04 23:48:07 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_get_hash_section", () => {
|
2024-04-30 13:30:24 +02:00
|
|
|
assert.equal(hash_parser.get_hash_section("channels/subscribed"), "subscribed");
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.get_hash_section("#settings/profile"), "profile");
|
2018-12-05 20:33:52 +01:00
|
|
|
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.get_hash_section("settings/10/general/"), "10");
|
2018-12-05 20:33:52 +01:00
|
|
|
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.get_hash_section("#drafts"), "");
|
|
|
|
assert.equal(hash_parser.get_hash_section(""), "");
|
2021-03-23 05:14:54 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#settings/profile";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.deepEqual(hash_parser.get_current_hash_section(), "profile");
|
2018-12-05 20:33:52 +01:00
|
|
|
});
|
|
|
|
|
2023-11-09 20:28:32 +01:00
|
|
|
run_test("get_current_nth_hash_section", () => {
|
|
|
|
window.location.hash = "#settings/profile";
|
2024-01-26 23:17:49 +01:00
|
|
|
assert.equal(hash_parser.get_current_nth_hash_section(0), "#settings");
|
|
|
|
assert.equal(hash_parser.get_current_nth_hash_section(1), "profile");
|
2023-11-09 20:28:32 +01:00
|
|
|
|
|
|
|
window.location.hash = "#settings/10/general";
|
2024-01-26 23:17:49 +01:00
|
|
|
assert.equal(hash_parser.get_current_nth_hash_section(0), "#settings");
|
|
|
|
assert.equal(hash_parser.get_current_nth_hash_section(1), "10");
|
|
|
|
assert.equal(hash_parser.get_current_nth_hash_section(2), "general");
|
|
|
|
assert.equal(hash_parser.get_current_nth_hash_section(3), "");
|
2023-11-09 20:28:32 +01:00
|
|
|
});
|
|
|
|
|
2024-01-22 20:03:18 +01:00
|
|
|
run_test("test_is_same_server_message_link", () => {
|
|
|
|
const dm_message_link = "#narrow/dm/9,15-dm/near/43";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(dm_message_link), true);
|
|
|
|
|
|
|
|
const group_message_link = "#narrow/dm/9,16,15-group/near/68";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(group_message_link), true);
|
|
|
|
|
|
|
|
const stream_message_link = "#narrow/stream/8-design/topic/desktop/near/82";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(stream_message_link), true);
|
|
|
|
|
|
|
|
const stream_link = "#narrow/stream/8-design";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(stream_link), false);
|
|
|
|
|
|
|
|
const topic_link = "#narrow/stream/8-design/topic/desktop";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(topic_link), false);
|
|
|
|
|
|
|
|
const dm_link = "#narrow/dm/15-John";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(dm_link), false);
|
|
|
|
|
|
|
|
const search_link = "#narrow/search/database";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(search_link), false);
|
|
|
|
|
|
|
|
const different_server_message_link =
|
|
|
|
"https://fakechat.zulip.org/#narrow/dm/8,1848,2369-group/near/1717378";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(different_server_message_link), false);
|
|
|
|
|
|
|
|
const drafts_link = "#drafts";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(drafts_link), false);
|
|
|
|
|
|
|
|
const empty_link = "#";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(empty_link), false);
|
|
|
|
|
|
|
|
const non_zulip_link = "https://www.google.com";
|
|
|
|
assert.equal(hash_parser.is_same_server_message_link(non_zulip_link), false);
|
|
|
|
});
|
|
|
|
|
2021-03-04 13:36:30 +01:00
|
|
|
run_test("build_reload_url", () => {
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#settings/profile";
|
2021-06-17 18:42:31 +02:00
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=settings%2Fprofile");
|
2021-03-04 13:36:30 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#test";
|
2021-03-04 13:36:30 +01:00
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=test");
|
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#";
|
2021-03-04 13:36:30 +01:00
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=");
|
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "";
|
2021-03-04 13:36:30 +01:00
|
|
|
assert.equal(hash_util.build_reload_url(), "+oldhash=");
|
|
|
|
});
|
|
|
|
|
2021-12-20 20:40:42 +01:00
|
|
|
run_test("test is_editing_stream", () => {
|
2024-04-30 13:30:24 +02:00
|
|
|
window.location.hash = "#channels/1/announce";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.is_editing_stream(1), true);
|
|
|
|
assert.equal(hash_parser.is_editing_stream(2), false);
|
2021-12-20 20:40:42 +01:00
|
|
|
|
|
|
|
// url is missing name at end
|
2024-04-30 13:30:24 +02:00
|
|
|
window.location.hash = "#channels/1";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.is_editing_stream(1), false);
|
2021-12-20 20:40:42 +01:00
|
|
|
|
2024-04-30 13:30:24 +02:00
|
|
|
window.location.hash = "#channels/bogus/bogus";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.is_editing_stream(1), false);
|
2021-03-15 06:57:14 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#test/narrow";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.is_editing_stream(1), false);
|
2021-03-15 06:57:14 +01:00
|
|
|
});
|
|
|
|
|
2021-03-15 09:24:10 +01:00
|
|
|
run_test("test_is_create_new_stream_narrow", () => {
|
2024-04-30 13:30:24 +02:00
|
|
|
window.location.hash = "#channels/new";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.is_create_new_stream_narrow(), true);
|
2021-03-15 09:24:10 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#some/random/hash";
|
2023-10-01 22:03:44 +02:00
|
|
|
assert.equal(hash_parser.is_create_new_stream_narrow(), false);
|
2021-03-15 09:24:10 +01:00
|
|
|
});
|
|
|
|
|
2024-04-25 12:34:44 +02:00
|
|
|
run_test("test_is_subscribers_section_opened_for_stream", () => {
|
2024-04-30 13:30:24 +02:00
|
|
|
window.location.hash = "#channels/1/Design/subscribers";
|
2024-04-25 12:34:44 +02:00
|
|
|
assert.equal(hash_parser.is_subscribers_section_opened_for_stream(), true);
|
|
|
|
|
2024-04-30 13:30:24 +02:00
|
|
|
window.location.hash = "#channels/99/.EC.A1.B0.EB.A6.AC.EB.B2.95.20.F0.9F.98.8E/subscribers";
|
2024-04-25 12:34:44 +02:00
|
|
|
assert.equal(hash_parser.is_subscribers_section_opened_for_stream(), true);
|
|
|
|
|
2024-04-30 13:30:24 +02:00
|
|
|
window.location.hash = "#channels/random/subscribers";
|
2024-04-25 12:34:44 +02:00
|
|
|
assert.equal(hash_parser.is_subscribers_section_opened_for_stream(), false);
|
|
|
|
|
|
|
|
window.location.hash = "#some/random/place/subscribers";
|
|
|
|
assert.equal(hash_parser.is_subscribers_section_opened_for_stream(), false);
|
|
|
|
|
|
|
|
window.location.hash = "#";
|
|
|
|
assert.equal(hash_parser.is_subscribers_section_opened_for_stream(), false);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_parse_narrow", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "99-frontend"]), [
|
|
|
|
{negated: false, operator: "stream", operand: "frontend"},
|
|
|
|
]);
|
2020-03-20 19:56:01 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.parse_narrow(["narrow", "-stream", "99-frontend"]), [
|
|
|
|
{negated: true, operator: "stream", operand: "frontend"},
|
|
|
|
]);
|
2018-12-04 23:24:03 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.parse_narrow(["narrow", "BOGUS"]), undefined);
|
2020-03-20 19:56:01 +01:00
|
|
|
|
|
|
|
// For nonexistent streams, we get the full slug.
|
|
|
|
// We possibly should remove the prefix and fix this test.
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "42-bogus"]), [
|
|
|
|
{negated: false, operator: "stream", operand: "42-bogus"},
|
|
|
|
]);
|
2018-12-04 23:24:03 +01:00
|
|
|
});
|
|
|
|
|
2024-04-30 14:46:11 +02:00
|
|
|
run_test("test_channels_settings_edit_url", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "research & development",
|
2018-12-02 19:42:34 +01:00
|
|
|
stream_id: 42,
|
|
|
|
};
|
2023-11-09 20:28:32 +01:00
|
|
|
assert.equal(
|
2024-04-30 14:46:11 +02:00
|
|
|
hash_util.channels_settings_edit_url(sub, "general"),
|
2024-04-30 13:30:24 +02:00
|
|
|
"#channels/42/research.20.26.20development/general",
|
2023-11-09 20:28:32 +01:00
|
|
|
);
|
2018-12-02 19:42:34 +01:00
|
|
|
});
|
|
|
|
|
2022-03-01 19:14:26 +01:00
|
|
|
run_test("test_by_conversation_and_time_url", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-14 19:02:26 +01:00
|
|
|
stream_id: frontend.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "testing",
|
2018-08-05 01:30:23 +02:00
|
|
|
id: 42,
|
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
2022-03-01 19:14:26 +01:00
|
|
|
hash_util.by_conversation_and_time_url(message),
|
2021-10-14 00:23:21 +02:00
|
|
|
"http://zulip.zulipdev.com/#narrow/stream/99-frontend/topic/testing/near/42",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2018-08-05 01:30:23 +02:00
|
|
|
|
|
|
|
message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2018-10-18 22:05:43 +02:00
|
|
|
display_recipient: [
|
|
|
|
{
|
2020-01-01 13:02:34 +01:00
|
|
|
id: hamlet.user_id,
|
2018-10-18 22:05:43 +02:00
|
|
|
},
|
|
|
|
],
|
2018-08-05 01:30:23 +02:00
|
|
|
id: 43,
|
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
2022-03-01 19:14:26 +01:00
|
|
|
hash_util.by_conversation_and_time_url(message),
|
2023-04-11 21:04:33 +02:00
|
|
|
"http://zulip.zulipdev.com/#narrow/dm/15-dm/near/43",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2018-08-05 01:30:23 +02:00
|
|
|
});
|
2020-06-20 05:48:18 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_search_public_streams_notice_url", () => {
|
2023-12-22 00:26:14 +01:00
|
|
|
function get_terms(url) {
|
2023-04-09 05:22:23 +02:00
|
|
|
return hash_util.parse_narrow(url.split("/"));
|
2020-06-20 05:48:18 +02:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:48:17 +01:00
|
|
|
assert.equal(
|
2023-12-22 00:26:14 +01:00
|
|
|
hash_util.search_public_streams_notice_url(get_terms("#narrow/search/abc")),
|
2021-11-29 07:48:17 +01:00
|
|
|
"#narrow/streams/public/search/abc",
|
|
|
|
);
|
2020-06-20 05:48:18 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
2021-11-29 07:48:17 +01:00
|
|
|
hash_util.search_public_streams_notice_url(
|
2023-12-22 00:26:14 +01:00
|
|
|
get_terms("#narrow/has/link/has/image/has/attachment"),
|
2021-11-29 07:48:17 +01:00
|
|
|
),
|
2020-07-15 00:34:28 +02:00
|
|
|
"#narrow/streams/public/has/link/has/image/has/attachment",
|
|
|
|
);
|
2020-06-20 05:48:18 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
2023-12-22 00:26:14 +01:00
|
|
|
hash_util.search_public_streams_notice_url(get_terms("#narrow/sender/15")),
|
2022-10-25 14:38:45 +02:00
|
|
|
"#narrow/streams/public/sender/15-Hamlet",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2020-06-20 05:48:18 +02:00
|
|
|
});
|
2021-06-23 15:21:25 +02:00
|
|
|
|
|
|
|
run_test("test_current_hash_as_next", () => {
|
|
|
|
window.location.hash = "#foo";
|
2023-10-01 21:54:23 +02:00
|
|
|
assert.equal(spectators.current_hash_as_next(), "next=/%23foo");
|
2021-06-23 15:21:25 +02:00
|
|
|
});
|