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");
|
|
|
|
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const {rewiremock, set_global, 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-02-28 00:49:57 +01:00
|
|
|
const local_message = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/local_message").with(local_message);
|
2021-02-28 00:49:36 +01:00
|
|
|
const markdown = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/markdown").with(markdown);
|
2021-02-23 03:54:07 +01:00
|
|
|
const page_params = set_global("page_params", {});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2021-02-05 21:20:14 +01:00
|
|
|
const fake_now = 555;
|
|
|
|
MockDate.set(new Date(fake_now * 1000));
|
|
|
|
|
2019-06-21 07:33:02 +02:00
|
|
|
let disparities = [];
|
|
|
|
let messages_to_rerender = [];
|
|
|
|
|
2021-02-28 21:33:10 +01:00
|
|
|
rewiremock("../../static/js/ui").with({
|
2020-07-12 23:21:05 +02:00
|
|
|
show_failed_message_success: () => {},
|
|
|
|
});
|
|
|
|
|
2021-02-28 00:50:19 +01:00
|
|
|
rewiremock("../../static/js/sent_messages").with({
|
2019-06-21 07:33:02 +02:00
|
|
|
mark_disparity: (local_id) => {
|
|
|
|
disparities.push(local_id);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-28 01:10:03 +01:00
|
|
|
rewiremock("../../static/js/message_store").with({
|
2020-07-16 22:40:18 +02:00
|
|
|
get: () => ({failed_request: true}),
|
2019-06-21 07:33:02 +02:00
|
|
|
update_booleans: () => {},
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("home_msg_list", {
|
2019-06-21 07:33:02 +02:00
|
|
|
view: {
|
|
|
|
rerender_messages: (msgs) => {
|
|
|
|
messages_to_rerender = msgs;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-28 21:31:33 +01:00
|
|
|
rewiremock("../../static/js/message_list").with({});
|
2019-06-21 07:33:02 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("current_msg_list", "");
|
2019-06-21 07:33:02 +02:00
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("process_from_server for differently rendered messages", () => {
|
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
|
|
|
messages_to_rerender = [];
|
|
|
|
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-02-24 14:50:59 +01:00
|
|
|
run_test("insert_local_message streams", (override) => {
|
|
|
|
const local_id_float = 101;
|
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
|
|
|
|
2021-02-24 14:50:59 +01:00
|
|
|
override(local_message, "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
|
|
|
|
|
|
|
assert(apply_markdown_called);
|
|
|
|
assert(add_topic_links_called);
|
|
|
|
assert(insert_message_called);
|
2021-02-24 14:50:59 +01:00
|
|
|
});
|
2020-01-22 11:37:57 +01:00
|
|
|
|
2021-02-24 14:50:59 +01:00
|
|
|
run_test("insert_local_message PM", (override) => {
|
|
|
|
const local_id_float = 102;
|
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;
|
|
|
|
|
|
|
|
override(local_message, "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);
|
2020-01-22 11:37:57 +01:00
|
|
|
assert(add_topic_links_called);
|
|
|
|
assert(apply_markdown_called);
|
|
|
|
assert(insert_message_called);
|
|
|
|
});
|
2021-02-05 21:20:14 +01:00
|
|
|
|
|
|
|
MockDate.reset();
|