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
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
const message_link_test_cases = require("../../zerver/tests/fixtures/message_link_test_cases.json");
|
2024-06-17 16:55:33 +02:00
|
|
|
|
2024-11-13 07:05:32 +01:00
|
|
|
const {zrequire} = require("./lib/namespace.cjs");
|
|
|
|
const {run_test} = require("./lib/test.cjs");
|
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
|
|
|
|
2024-08-03 03:05:34 +02:00
|
|
|
const frontend_id = 99;
|
2019-11-02 00:06:25 +01:00
|
|
|
const frontend = {
|
2024-08-03 03:05:34 +02:00
|
|
|
stream_id: frontend_id,
|
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";
|
2024-08-03 03:05:34 +02:00
|
|
|
operand = frontend_id.toString();
|
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", () => {
|
2024-06-17 16:55:33 +02:00
|
|
|
for (const message_link_test_case of message_link_test_cases) {
|
|
|
|
assert.equal(
|
|
|
|
hash_parser.is_same_server_message_link(message_link_test_case.message_link),
|
|
|
|
message_link_test_case.expected_output,
|
|
|
|
);
|
|
|
|
}
|
2024-01-22 20:03:18 +01:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2024-04-30 16:47:50 +02:00
|
|
|
run_test("test_is_in_specified_hash_category", () => {
|
|
|
|
window.location.hash = "#channels/1/Design/subscribers";
|
|
|
|
assert.equal(hash_parser.is_in_specified_hash_category(["channels", "channel"]), true);
|
|
|
|
|
|
|
|
window.location.hash = "#channels/99/.EC.A1.B0.EB.A6.AC.EB.B2.95.20.F0.9F.98.8E/subscribers";
|
|
|
|
assert.equal(hash_parser.is_in_specified_hash_category(["channels", "channel"]), true);
|
|
|
|
|
|
|
|
window.location.hash = "#gro/channels/channel";
|
|
|
|
assert.equal(hash_parser.is_in_specified_hash_category(["stream", "channel", "group"]), false);
|
|
|
|
|
|
|
|
window.location.hash = "#some/stream/channel/group";
|
|
|
|
assert.equal(hash_parser.is_in_specified_hash_category(["stream", "channel", "group"]), false);
|
|
|
|
|
|
|
|
window.location.hash = "#some/stream/channel/group";
|
|
|
|
assert.equal(hash_parser.is_in_specified_hash_category([""]), false);
|
|
|
|
|
|
|
|
window.location.hash = "#some/stream/channel/group";
|
|
|
|
assert.equal(hash_parser.is_in_specified_hash_category([]), 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"]), [
|
2024-08-03 03:05:34 +02:00
|
|
|
{negated: false, operator: "stream", operand: frontend_id.toString()},
|
2020-07-15 00:34:28 +02:00
|
|
|
]);
|
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"]), [
|
2024-08-03 03:05:34 +02:00
|
|
|
{negated: true, operator: "stream", operand: frontend_id.toString()},
|
2020-07-15 00:34:28 +02:00
|
|
|
]);
|
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
|
|
|
|
2024-08-03 03:05:34 +02:00
|
|
|
// For nonexistent streams, we get an empty string. We don't use this
|
|
|
|
// anywhere, and just show "Invalid stream" in the navbar.
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "42-bogus"]), [
|
2024-08-03 03:05:34 +02:00
|
|
|
{negated: false, operator: "stream", operand: ""},
|
2020-07-15 00:34:28 +02:00
|
|
|
]);
|
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),
|
2024-10-03 18:37:58 +02:00
|
|
|
"http://zulip.zulipdev.com/#narrow/channel/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")),
|
2024-10-03 18:37:58 +02:00
|
|
|
"#narrow/channels/public/search/abc",
|
2021-11-29 07:48:17 +01:00
|
|
|
);
|
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
|
|
|
),
|
2024-10-03 18:37:58 +02:00
|
|
|
"#narrow/channels/public/has/link/has/image/has/attachment",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
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")),
|
2024-10-03 18:37:58 +02:00
|
|
|
"#narrow/channels/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
|
|
|
});
|