2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-02-05 21:20:14 +01:00
|
|
|
const MockDate = require("mockdate");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {make_stub} = require("./lib/stub");
|
2023-12-14 23:51:33 +01:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2024-02-13 02:08:16 +01:00
|
|
|
const {current_user} = require("./lib/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-10-06 22:36:26 +02:00
|
|
|
const compose_notifications = mock_esm("../src/compose_notifications");
|
2023-02-22 23:04:10 +01:00
|
|
|
const markdown = mock_esm("../src/markdown");
|
|
|
|
const message_lists = mock_esm("../src/message_lists");
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2019-06-21 07:33:02 +02:00
|
|
|
let disparities = [];
|
|
|
|
|
2023-04-25 05:36:10 +02:00
|
|
|
mock_esm("../src/message_live_update", {
|
|
|
|
update_message_in_all_views() {},
|
2020-07-12 23:21:05 +02:00
|
|
|
});
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/sent_messages", {
|
2022-11-17 23:33:43 +01:00
|
|
|
mark_disparity(local_id) {
|
2019-06-21 07:33:02 +02:00
|
|
|
disparities.push(local_id);
|
|
|
|
},
|
2023-05-03 00:01:10 +02:00
|
|
|
report_event_received() {},
|
2019-06-21 07:33:02 +02:00
|
|
|
});
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const message_store = mock_esm("../src/message_store", {
|
2020-07-16 22:40:18 +02:00
|
|
|
get: () => ({failed_request: true}),
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2022-11-17 23:33:43 +01:00
|
|
|
update_booleans() {},
|
2022-03-24 06:47:56 +01:00
|
|
|
|
2022-11-17 23:33:43 +01:00
|
|
|
set_message_booleans() {},
|
2019-06-21 07:33:02 +02:00
|
|
|
});
|
|
|
|
|
2022-08-17 07:10:19 +02:00
|
|
|
message_lists.current = {
|
|
|
|
view: {
|
|
|
|
rerender_messages: noop,
|
|
|
|
change_message_id: noop,
|
|
|
|
},
|
|
|
|
change_message_id: noop,
|
|
|
|
};
|
2024-04-21 05:31:35 +02:00
|
|
|
const home_msg_list = {
|
2022-08-17 07:10:19 +02:00
|
|
|
view: {
|
|
|
|
rerender_messages: noop,
|
|
|
|
change_message_id: noop,
|
|
|
|
},
|
2024-04-21 05:31:35 +02:00
|
|
|
preserver_rendered_state: true,
|
2022-08-17 07:10:19 +02:00
|
|
|
change_message_id: noop,
|
|
|
|
};
|
2024-04-21 05:31:35 +02:00
|
|
|
message_lists.all_rendered_message_lists = () => [home_msg_list, message_lists.current];
|
2019-06-21 07:33:02 +02:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const echo = zrequire("echo");
|
|
|
|
const people = zrequire("people");
|
2023-07-26 22:07:21 +02:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
|
|
|
|
const general_sub = {
|
|
|
|
stream_id: 101,
|
|
|
|
name: "general",
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(general_sub);
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("process_from_server for un-echoed messages", () => {
|
2020-02-12 06:02:56 +01:00
|
|
|
const waiting_for_ack = new Map();
|
2019-06-21 07:33:02 +02:00
|
|
|
const server_messages = [
|
|
|
|
{
|
2020-02-12 09:32:25 +01:00
|
|
|
local_id: "100.1",
|
2019-06-21 07:33:02 +02:00
|
|
|
},
|
|
|
|
];
|
2020-02-12 06:02:56 +01:00
|
|
|
echo._patch_waiting_for_ack(waiting_for_ack);
|
2019-06-21 07:33:02 +02:00
|
|
|
const non_echo_messages = echo.process_from_server(server_messages);
|
|
|
|
assert.deepEqual(non_echo_messages, server_messages);
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("process_from_server for differently rendered messages", ({override}) => {
|
2021-04-03 18:16:12 +02:00
|
|
|
let messages_to_rerender = [];
|
|
|
|
|
2024-04-21 05:31:35 +02:00
|
|
|
override(home_msg_list.view, "rerender_messages", (msgs) => {
|
2021-04-03 18:16:12 +02:00
|
|
|
messages_to_rerender = msgs;
|
|
|
|
});
|
|
|
|
|
2019-06-21 07:33:02 +02:00
|
|
|
// Test that we update all the booleans and the content of the message
|
|
|
|
// in local echo.
|
2020-07-15 01:29:15 +02:00
|
|
|
const old_value = "old_value";
|
|
|
|
const new_value = "new_value";
|
2020-02-12 06:02:56 +01:00
|
|
|
const waiting_for_ack = new Map([
|
2020-07-15 00:34:28 +02:00
|
|
|
[
|
|
|
|
"100.1",
|
|
|
|
{
|
|
|
|
content: "<p>A client rendered message</p>",
|
|
|
|
timestamp: old_value,
|
|
|
|
is_me_message: old_value,
|
|
|
|
submessages: old_value,
|
|
|
|
topic_links: old_value,
|
|
|
|
},
|
|
|
|
],
|
2020-02-12 06:02:56 +01:00
|
|
|
]);
|
2019-06-21 07:33:02 +02:00
|
|
|
const server_messages = [
|
|
|
|
{
|
2020-02-12 09:32:25 +01:00
|
|
|
local_id: "100.1",
|
2019-06-21 07:33:02 +02:00
|
|
|
content: "<p>A server rendered message</p>",
|
|
|
|
timestamp: new_value,
|
|
|
|
is_me_message: new_value,
|
|
|
|
submessages: new_value,
|
2020-02-07 13:09:17 +01:00
|
|
|
topic_links: new_value,
|
2019-06-21 07:33:02 +02:00
|
|
|
},
|
|
|
|
];
|
2020-02-12 06:02:56 +01:00
|
|
|
echo._patch_waiting_for_ack(waiting_for_ack);
|
2019-06-21 07:33:02 +02:00
|
|
|
disparities = [];
|
|
|
|
const non_echo_messages = echo.process_from_server(server_messages);
|
|
|
|
assert.deepEqual(non_echo_messages, []);
|
|
|
|
assert.equal(disparities.length, 1);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(messages_to_rerender, [
|
|
|
|
{
|
|
|
|
content: server_messages[0].content,
|
|
|
|
timestamp: new_value,
|
|
|
|
is_me_message: new_value,
|
|
|
|
submessages: new_value,
|
|
|
|
topic_links: new_value,
|
|
|
|
},
|
|
|
|
]);
|
2019-06-21 07:33:02 +02:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("build_display_recipient", () => {
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.user_id = 123;
|
2020-02-25 12:16:26 +01:00
|
|
|
|
|
|
|
const params = {};
|
|
|
|
params.realm_users = [
|
2020-01-31 11:02:25 +01:00
|
|
|
{
|
|
|
|
user_id: 123,
|
|
|
|
full_name: "Iago",
|
|
|
|
email: "iago@zulip.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
email: "cordelia@zulip.com",
|
|
|
|
full_name: "Cordelia",
|
|
|
|
user_id: 21,
|
|
|
|
},
|
|
|
|
];
|
2020-02-25 12:16:26 +01:00
|
|
|
params.realm_non_active_users = [];
|
|
|
|
params.cross_realm_bots = [];
|
2024-02-13 02:08:16 +01:00
|
|
|
people.initialize(current_user.user_id, params);
|
2020-01-31 11:02:25 +01:00
|
|
|
|
2020-01-22 11:42:05 +01:00
|
|
|
let message = {
|
|
|
|
type: "stream",
|
2023-07-26 22:07:21 +02:00
|
|
|
stream_id: general_sub.stream_id,
|
2020-01-22 11:42:05 +01:00
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
};
|
|
|
|
let display_recipient = echo.build_display_recipient(message);
|
|
|
|
assert.equal(display_recipient, "general");
|
|
|
|
|
|
|
|
message = {
|
|
|
|
type: "private",
|
|
|
|
private_message_recipient: "cordelia@zulip.com,hamlet@zulip.com",
|
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
};
|
|
|
|
display_recipient = echo.build_display_recipient(message);
|
2020-01-23 07:52:08 +01:00
|
|
|
assert.equal(display_recipient.length, 3);
|
|
|
|
|
|
|
|
let iago = display_recipient.find((recipient) => recipient.email === "iago@zulip.com");
|
|
|
|
assert.equal(iago.full_name, "Iago");
|
|
|
|
assert.equal(iago.id, 123);
|
2020-01-22 11:42:05 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const cordelia = display_recipient.find(
|
|
|
|
(recipient) => recipient.email === "cordelia@zulip.com",
|
|
|
|
);
|
2020-01-22 11:42:05 +01:00
|
|
|
assert.equal(cordelia.full_name, "Cordelia");
|
|
|
|
assert.equal(cordelia.id, 21);
|
|
|
|
|
|
|
|
const hamlet = display_recipient.find((recipient) => recipient.email === "hamlet@zulip.com");
|
|
|
|
assert.equal(hamlet.full_name, "hamlet@zulip.com");
|
|
|
|
assert.equal(hamlet.id, undefined);
|
|
|
|
assert.equal(hamlet.unknown_local_echo_user, true);
|
|
|
|
|
|
|
|
message = {
|
|
|
|
type: "private",
|
|
|
|
private_message_recipient: "iago@zulip.com",
|
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
};
|
|
|
|
display_recipient = echo.build_display_recipient(message);
|
|
|
|
|
|
|
|
assert.equal(display_recipient.length, 1);
|
2020-01-23 07:52:08 +01:00
|
|
|
iago = display_recipient.find((recipient) => recipient.email === "iago@zulip.com");
|
2020-01-22 11:42:05 +01:00
|
|
|
assert.equal(iago.full_name, "Iago");
|
|
|
|
assert.equal(iago.id, 123);
|
|
|
|
});
|
|
|
|
|
2021-03-28 19:08:25 +02:00
|
|
|
run_test("update_message_lists", () => {
|
2024-04-21 05:31:35 +02:00
|
|
|
home_msg_list.view = {};
|
2021-03-28 19:08:25 +02:00
|
|
|
|
|
|
|
const stub = make_stub();
|
|
|
|
const view_stub = make_stub();
|
|
|
|
|
2024-04-21 05:31:35 +02:00
|
|
|
home_msg_list.change_message_id = stub.f;
|
|
|
|
home_msg_list.view.change_message_id = view_stub.f;
|
2021-03-28 19:08:25 +02:00
|
|
|
|
|
|
|
echo.update_message_lists({old_id: 401, new_id: 402});
|
|
|
|
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
const args = stub.get_args("old", "new");
|
|
|
|
assert.equal(args.old, 401);
|
|
|
|
assert.equal(args.new, 402);
|
|
|
|
|
|
|
|
assert.equal(view_stub.num_calls, 1);
|
|
|
|
const view_args = view_stub.get_args("old", "new");
|
|
|
|
assert.equal(view_args.old, 401);
|
|
|
|
assert.equal(view_args.new, 402);
|
|
|
|
});
|
|
|
|
|
2023-06-28 13:02:28 +02:00
|
|
|
run_test("insert_local_message streams", ({override}) => {
|
2021-03-08 18:35:23 +01:00
|
|
|
const fake_now = 555;
|
|
|
|
MockDate.set(new Date(fake_now * 1000));
|
|
|
|
|
2021-06-14 11:08:54 +02:00
|
|
|
const local_id_float = 101.01;
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2024-01-22 07:55:55 +01:00
|
|
|
let render_called = false;
|
2024-01-22 08:10:12 +01:00
|
|
|
let get_topic_links_called = false;
|
2020-01-22 11:37:57 +01:00
|
|
|
let insert_message_called = false;
|
|
|
|
|
2024-01-22 07:55:55 +01:00
|
|
|
override(markdown, "render", () => {
|
|
|
|
render_called = true;
|
2021-02-13 17:05:57 +01:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2024-01-22 08:10:12 +01:00
|
|
|
override(markdown, "get_topic_links", () => {
|
|
|
|
get_topic_links_called = true;
|
2021-02-13 17:05:57 +01:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2023-06-28 13:02:28 +02:00
|
|
|
const insert_new_messages = ([message]) => {
|
2020-01-22 11:37:57 +01:00
|
|
|
assert.equal(message.display_recipient, "general");
|
2021-02-05 21:20:14 +01:00
|
|
|
assert.equal(message.timestamp, fake_now);
|
2020-01-22 11:37:57 +01:00
|
|
|
assert.equal(message.sender_email, "iago@zulip.com");
|
|
|
|
assert.equal(message.sender_full_name, "Iago");
|
|
|
|
assert.equal(message.sender_id, 123);
|
|
|
|
insert_message_called = true;
|
2023-06-28 13:02:28 +02:00
|
|
|
};
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2021-02-24 14:50:59 +01:00
|
|
|
const message_request = {
|
2020-01-22 11:37:57 +01:00
|
|
|
type: "stream",
|
2023-07-26 22:07:21 +02:00
|
|
|
stream_id: general_sub.stream_id,
|
2024-01-22 08:10:12 +01:00
|
|
|
topic: "important note",
|
2020-01-22 11:37:57 +01:00
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
};
|
2023-06-28 13:02:28 +02:00
|
|
|
echo.insert_local_message(message_request, local_id_float, insert_new_messages);
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2024-01-22 07:55:55 +01:00
|
|
|
assert.ok(render_called);
|
2024-01-22 08:10:12 +01:00
|
|
|
assert.ok(get_topic_links_called);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(insert_message_called);
|
2021-02-24 14:50:59 +01:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2023-06-28 13:02:28 +02:00
|
|
|
run_test("insert_local_message direct message", ({override}) => {
|
2021-06-14 11:08:54 +02:00
|
|
|
const local_id_float = 102.01;
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.user_id = 123;
|
2021-02-24 14:50:59 +01:00
|
|
|
|
|
|
|
const params = {};
|
|
|
|
params.realm_users = [
|
|
|
|
{
|
|
|
|
user_id: 123,
|
|
|
|
full_name: "Iago",
|
|
|
|
email: "iago@zulip.com",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
params.realm_non_active_users = [];
|
|
|
|
params.cross_realm_bots = [];
|
2024-02-13 02:08:16 +01:00
|
|
|
people.initialize(current_user.user_id, params);
|
2021-02-24 14:50:59 +01:00
|
|
|
|
2024-01-22 07:55:55 +01:00
|
|
|
let render_called = false;
|
2021-02-24 14:50:59 +01:00
|
|
|
let insert_message_called = false;
|
|
|
|
|
2023-06-28 13:02:28 +02:00
|
|
|
const insert_new_messages = ([message]) => {
|
2020-01-23 07:52:08 +01:00
|
|
|
assert.equal(message.display_recipient.length, 3);
|
2020-01-22 11:37:57 +01:00
|
|
|
insert_message_called = true;
|
2023-06-28 13:02:28 +02:00
|
|
|
};
|
2021-02-24 14:50:59 +01:00
|
|
|
|
2024-01-22 07:55:55 +01:00
|
|
|
override(markdown, "render", () => {
|
|
|
|
render_called = true;
|
2021-02-24 14:50:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const message_request = {
|
2020-01-22 11:37:57 +01:00
|
|
|
private_message_recipient: "cordelia@zulip.com,hamlet@zulip.com",
|
|
|
|
type: "private",
|
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
};
|
2023-06-28 13:02:28 +02:00
|
|
|
echo.insert_local_message(message_request, local_id_float, insert_new_messages);
|
2024-01-22 07:55:55 +01:00
|
|
|
assert.ok(render_called);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(insert_message_called);
|
2020-01-22 11:37:57 +01:00
|
|
|
});
|
2021-02-05 21:20:14 +01:00
|
|
|
|
2023-06-28 13:02:28 +02:00
|
|
|
run_test("test reify_message_id", ({override}) => {
|
2021-06-14 11:08:54 +02:00
|
|
|
const local_id_float = 103.01;
|
|
|
|
|
2024-01-22 07:55:55 +01:00
|
|
|
override(markdown, "render", noop);
|
2021-06-14 11:08:54 +02:00
|
|
|
|
|
|
|
const message_request = {
|
|
|
|
type: "stream",
|
2023-07-26 22:07:21 +02:00
|
|
|
stream_id: general_sub.stream_id,
|
2021-06-14 11:08:54 +02:00
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
draft_id: 100,
|
|
|
|
};
|
2023-12-14 23:51:33 +01:00
|
|
|
echo.insert_local_message(message_request, local_id_float, noop);
|
2021-06-14 11:08:54 +02:00
|
|
|
|
|
|
|
let message_store_reify_called = false;
|
|
|
|
let notifications_reify_called = false;
|
|
|
|
|
|
|
|
override(message_store, "reify_message_id", () => {
|
|
|
|
message_store_reify_called = true;
|
|
|
|
});
|
|
|
|
|
2023-10-06 22:36:26 +02:00
|
|
|
override(compose_notifications, "reify_message_id", () => {
|
2021-06-14 11:08:54 +02:00
|
|
|
notifications_reify_called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
echo.reify_message_id(local_id_float.toString(), 110);
|
|
|
|
|
|
|
|
assert.ok(message_store_reify_called);
|
|
|
|
assert.ok(notifications_reify_called);
|
|
|
|
});
|
|
|
|
|
2022-05-07 02:53:53 +02:00
|
|
|
run_test("reset MockDate", () => {
|
|
|
|
MockDate.reset();
|
|
|
|
});
|