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");
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
const {mock_esm, zrequire} = require("../zjsunit/namespace");
|
2021-03-28 19:08:25 +02:00
|
|
|
const {make_stub} = require("../zjsunit/stub");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const markdown = mock_esm("../../static/js/markdown");
|
2021-03-30 02:21:21 +02:00
|
|
|
const message_lists = mock_esm("../../static/js/message_lists");
|
2021-06-14 11:08:54 +02:00
|
|
|
const notifications = mock_esm("../../static/js/notifications");
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2019-06-21 07:33:02 +02:00
|
|
|
let disparities = [];
|
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/ui", {
|
2020-07-12 23:21:05 +02:00
|
|
|
show_failed_message_success: () => {},
|
|
|
|
});
|
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/sent_messages", {
|
2019-06-21 07:33:02 +02:00
|
|
|
mark_disparity: (local_id) => {
|
|
|
|
disparities.push(local_id);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-06-14 11:08:54 +02:00
|
|
|
const message_store = mock_esm("../../static/js/message_store", {
|
2020-07-16 22:40:18 +02:00
|
|
|
get: () => ({failed_request: true}),
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2019-06-21 07:33:02 +02:00
|
|
|
update_booleans: () => {},
|
2022-03-24 06:47:56 +01:00
|
|
|
|
|
|
|
set_message_booleans: () => {},
|
2019-06-21 07:33:02 +02:00
|
|
|
});
|
|
|
|
|
2022-08-17 07:10:19 +02:00
|
|
|
const noop = () => {};
|
|
|
|
message_lists.current = {
|
|
|
|
view: {
|
|
|
|
rerender_messages: noop,
|
|
|
|
change_message_id: noop,
|
|
|
|
},
|
|
|
|
change_message_id: noop,
|
|
|
|
};
|
|
|
|
message_lists.home = {
|
|
|
|
view: {
|
|
|
|
rerender_messages: noop,
|
|
|
|
change_message_id: noop,
|
|
|
|
},
|
|
|
|
change_message_id: noop,
|
|
|
|
};
|
|
|
|
message_lists.all_rendered_message_lists = () => [message_lists.home, message_lists.current];
|
2019-06-21 07:33:02 +02:00
|
|
|
|
2021-06-14 11:08:54 +02:00
|
|
|
const drafts = zrequire("drafts");
|
2020-12-01 23:21:38 +01:00
|
|
|
const echo = zrequire("echo");
|
|
|
|
const people = zrequire("people");
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
override(message_lists.home.view, "rerender_messages", (msgs) => {
|
|
|
|
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", () => {
|
2020-01-31 11:02:25 +01:00
|
|
|
page_params.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 = [];
|
|
|
|
people.initialize(page_params.user_id, params);
|
2020-01-31 11:02:25 +01:00
|
|
|
|
2020-01-22 11:42:05 +01:00
|
|
|
let message = {
|
|
|
|
type: "stream",
|
|
|
|
stream: "general",
|
|
|
|
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", () => {
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.home.view = {};
|
2021-03-28 19:08:25 +02:00
|
|
|
|
|
|
|
const stub = make_stub();
|
|
|
|
const view_stub = make_stub();
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.home.change_message_id = stub.f;
|
|
|
|
message_lists.home.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);
|
|
|
|
});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
run_test("insert_local_message streams", ({override, override_rewire}) => {
|
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
|
|
|
|
|
|
|
let apply_markdown_called = false;
|
|
|
|
let add_topic_links_called = false;
|
|
|
|
let insert_message_called = false;
|
|
|
|
|
2021-02-13 17:05:57 +01:00
|
|
|
override(markdown, "apply_markdown", () => {
|
2020-01-22 11:37:57 +01:00
|
|
|
apply_markdown_called = true;
|
2021-02-13 17:05:57 +01:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2021-02-13 17:05:57 +01:00
|
|
|
override(markdown, "add_topic_links", () => {
|
2020-01-22 11:37:57 +01:00
|
|
|
add_topic_links_called = true;
|
2021-02-13 17:05:57 +01:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(echo, "insert_message", (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;
|
2021-02-24 14:50:59 +01: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",
|
|
|
|
stream: "general",
|
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
};
|
2020-02-12 09:32:25 +01:00
|
|
|
echo.insert_local_message(message_request, local_id_float);
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(apply_markdown_called);
|
|
|
|
assert.ok(add_topic_links_called);
|
|
|
|
assert.ok(insert_message_called);
|
2021-02-24 14:50:59 +01:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
run_test("insert_local_message PM", ({override, override_rewire}) => {
|
2021-06-14 11:08:54 +02:00
|
|
|
const local_id_float = 102.01;
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2021-02-24 14:50:59 +01:00
|
|
|
page_params.user_id = 123;
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
params.realm_users = [
|
|
|
|
{
|
|
|
|
user_id: 123,
|
|
|
|
full_name: "Iago",
|
|
|
|
email: "iago@zulip.com",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
params.realm_non_active_users = [];
|
|
|
|
params.cross_realm_bots = [];
|
|
|
|
people.initialize(page_params.user_id, params);
|
|
|
|
|
|
|
|
let add_topic_links_called = false;
|
|
|
|
let apply_markdown_called = false;
|
|
|
|
let insert_message_called = false;
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(echo, "insert_message", (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;
|
2021-02-24 14:50:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
override(markdown, "apply_markdown", () => {
|
|
|
|
apply_markdown_called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
override(markdown, "add_topic_links", () => {
|
|
|
|
add_topic_links_called = true;
|
|
|
|
});
|
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
|
|
|
private_message_recipient: "cordelia@zulip.com,hamlet@zulip.com",
|
|
|
|
type: "private",
|
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
};
|
2020-02-12 09:32:25 +01:00
|
|
|
echo.insert_local_message(message_request, local_id_float);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(add_topic_links_called);
|
|
|
|
assert.ok(apply_markdown_called);
|
|
|
|
assert.ok(insert_message_called);
|
2020-01-22 11:37:57 +01:00
|
|
|
});
|
2021-02-05 21:20:14 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
run_test("test reify_message_id", ({override, override_rewire}) => {
|
2021-06-14 11:08:54 +02:00
|
|
|
const local_id_float = 103.01;
|
|
|
|
|
|
|
|
override(markdown, "apply_markdown", () => {});
|
|
|
|
override(markdown, "add_topic_links", () => {});
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(echo, "insert_message", () => {});
|
2021-06-14 11:08:54 +02:00
|
|
|
|
|
|
|
const message_request = {
|
|
|
|
type: "stream",
|
|
|
|
stream: "general",
|
|
|
|
sender_email: "iago@zulip.com",
|
|
|
|
sender_full_name: "Iago",
|
|
|
|
sender_id: 123,
|
|
|
|
draft_id: 100,
|
|
|
|
};
|
|
|
|
echo.insert_local_message(message_request, local_id_float);
|
|
|
|
|
|
|
|
let message_store_reify_called = false;
|
|
|
|
let notifications_reify_called = false;
|
|
|
|
let draft_deleted = false;
|
|
|
|
|
|
|
|
override(message_store, "reify_message_id", () => {
|
|
|
|
message_store_reify_called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
override(notifications, "reify_message_id", () => {
|
|
|
|
notifications_reify_called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const draft_model = drafts.draft_model;
|
|
|
|
override(draft_model, "deleteDraft", (draft_id) => {
|
|
|
|
assert.ok(draft_id, 100);
|
|
|
|
draft_deleted = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
echo.reify_message_id(local_id_float.toString(), 110);
|
|
|
|
|
|
|
|
assert.ok(message_store_reify_called);
|
|
|
|
assert.ok(notifications_reify_called);
|
|
|
|
assert.ok(draft_deleted);
|
|
|
|
});
|
|
|
|
|
2022-05-07 02:53:53 +02:00
|
|
|
run_test("reset MockDate", () => {
|
|
|
|
MockDate.reset();
|
|
|
|
});
|