2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
const {mock_esm, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const ui_report = mock_esm("../../static/js/ui_report", {
|
2020-07-10 16:46:59 +02:00
|
|
|
displayed_error: false,
|
2021-02-28 00:58:55 +01:00
|
|
|
|
2020-07-10 16:46:59 +02:00
|
|
|
error: () => {
|
|
|
|
ui_report.displayed_error = true;
|
|
|
|
},
|
2021-03-07 13:57:14 +01:00
|
|
|
});
|
2018-08-05 01:30:23 +02:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const hash_util = zrequire("hash_util");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const people = zrequire("people");
|
|
|
|
|
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 encodeHashComponent
|
2020-07-15 01:29:15 +02:00
|
|
|
const str = "https://www.zulipexample.com";
|
2019-11-02 00:06:25 +01:00
|
|
|
const result1 = hash_util.encodeHashComponent(str);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(result1, "https.3A.2F.2Fwww.2Ezulipexample.2Ecom");
|
2018-04-13 23:44:10 +02:00
|
|
|
|
|
|
|
// Test decodeHashComponent
|
2019-11-02 00:06:25 +01:00
|
|
|
const result2 = hash_util.decodeHashComponent(result1);
|
2018-04-13 23:44:10 +02:00
|
|
|
assert.equal(result2, str);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-07-15 01:29:15 +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");
|
2020-07-10 16:46:59 +02:00
|
|
|
|
|
|
|
// Test invalid url decode.
|
|
|
|
const result = hash_util.decodeHashComponent("foo.foo");
|
|
|
|
assert.equal(result, "");
|
|
|
|
assert.equal(ui_report.displayed_error, true);
|
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", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(hash_util.get_hash_category("streams/subscribed"), "streams");
|
|
|
|
assert.deepEqual(hash_util.get_hash_category("#settings/display-settings"), "settings");
|
|
|
|
assert.deepEqual(hash_util.get_hash_category("#drafts"), "drafts");
|
|
|
|
assert.deepEqual(hash_util.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";
|
2021-03-23 05:14:54 +01:00
|
|
|
assert.deepEqual(hash_util.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", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.get_hash_section("streams/subscribed"), "subscribed");
|
2021-06-17 18:42:31 +02:00
|
|
|
assert.equal(hash_util.get_hash_section("#settings/profile"), "profile");
|
2018-12-05 20:33:52 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.get_hash_section("settings/10/general/"), "10");
|
2018-12-05 20:33:52 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.get_hash_section("#drafts"), "");
|
|
|
|
assert.equal(hash_util.get_hash_section(""), "");
|
2021-03-23 05:14:54 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#settings/profile";
|
2021-06-17 18:42:31 +02:00
|
|
|
assert.deepEqual(hash_util.get_current_hash_section(), "profile");
|
2018-12-05 20:33:52 +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", () => {
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#streams/1/announce";
|
2021-12-20 20:40:42 +01:00
|
|
|
assert.equal(hash_util.is_editing_stream(1), true);
|
|
|
|
assert.equal(hash_util.is_editing_stream(2), false);
|
|
|
|
|
|
|
|
// url is missing name at end
|
|
|
|
window.location.hash = "#streams/1";
|
|
|
|
assert.equal(hash_util.is_editing_stream(1), false);
|
|
|
|
|
|
|
|
window.location.hash = "#streams/bogus/bogus";
|
|
|
|
assert.equal(hash_util.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";
|
2021-12-20 20:40:42 +01:00
|
|
|
assert.equal(hash_util.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", () => {
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#streams/new";
|
2021-03-15 09:24:10 +01:00
|
|
|
assert.equal(hash_util.is_create_new_stream_narrow(), true);
|
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#some/random/hash";
|
2021-03-15 09:24:10 +01:00
|
|
|
assert.equal(hash_util.is_create_new_stream_narrow(), 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
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_stream_edit_uri", () => {
|
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,
|
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(hash_util.stream_edit_uri(sub), "#streams/42/research.20.26.20development");
|
2018-12-02 19:42:34 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_by_conversation_and_time_uri", () => {
|
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(
|
|
|
|
hash_util.by_conversation_and_time_uri(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(
|
|
|
|
hash_util.by_conversation_and_time_uri(message),
|
2021-10-14 00:23:21 +02:00
|
|
|
"http://zulip.zulipdev.com/#narrow/pm-with/15-pm/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", () => {
|
2021-11-29 07:48:17 +01:00
|
|
|
function get_operators(uri) {
|
|
|
|
return hash_util.parse_narrow(uri.split("/"));
|
2020-06-20 05:48:18 +02:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:48:17 +01:00
|
|
|
assert.equal(
|
|
|
|
hash_util.search_public_streams_notice_url(get_operators("#narrow/search/abc")),
|
|
|
|
"#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(
|
|
|
|
get_operators("#narrow/has/link/has/image/has/attachment"),
|
|
|
|
),
|
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(
|
2021-11-29 07:48:17 +01:00
|
|
|
hash_util.search_public_streams_notice_url(get_operators("#narrow/sender/15")),
|
2020-07-15 00:34:28 +02:00
|
|
|
"#narrow/streams/public/sender/15-hamlet",
|
|
|
|
);
|
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";
|
2021-11-27 09:46:26 +01:00
|
|
|
assert.equal(hash_util.current_hash_as_next(), "next=/%23foo");
|
2021-06-23 15:21:25 +02:00
|
|
|
});
|