2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2022-07-09 23:25:05 +02:00
|
|
|
const {mock_esm, set_global, with_overrides, 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 channel = mock_esm("../../static/js/channel");
|
|
|
|
const ui = mock_esm("../../static/js/ui");
|
2021-03-07 13:57:14 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/starred_messages", {
|
2020-12-01 23:21:38 +01:00
|
|
|
add: () => {},
|
2021-03-20 11:11:32 +01:00
|
|
|
get_starred_msg_ids: () => [1, 2, 3, 4, 5],
|
2020-12-01 23:21:38 +01:00
|
|
|
remove: () => {},
|
|
|
|
});
|
|
|
|
|
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 message_flags = zrequire("message_flags");
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("starred", ({override}) => {
|
2018-07-03 01:30:43 +02:00
|
|
|
const message = {
|
|
|
|
id: 50,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
let ui_updated;
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2021-03-26 11:14:22 +01:00
|
|
|
override(ui, "update_starred_view", () => {
|
2018-07-03 01:30:43 +02:00
|
|
|
ui_updated = true;
|
2021-03-26 11:14:22 +01:00
|
|
|
});
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let posted_data;
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2021-03-10 15:24:49 +01:00
|
|
|
override(channel, "post", (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts.url, "/json/messages/flags");
|
2018-07-03 01:30:43 +02:00
|
|
|
posted_data = opts.data;
|
2021-03-10 15:24:49 +01:00
|
|
|
});
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2018-08-01 20:09:12 +02:00
|
|
|
message_flags.toggle_starred_and_update_server(message);
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(ui_updated);
|
2018-07-03 01:30:43 +02:00
|
|
|
|
|
|
|
assert.deepEqual(posted_data, {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[50]",
|
|
|
|
flag: "starred",
|
|
|
|
op: "add",
|
2018-07-03 01:30:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(message, {
|
|
|
|
id: 50,
|
|
|
|
starred: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
ui_updated = false;
|
|
|
|
|
2018-08-01 20:09:12 +02:00
|
|
|
message_flags.toggle_starred_and_update_server(message);
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(ui_updated);
|
2018-07-03 01:30:43 +02:00
|
|
|
|
|
|
|
assert.deepEqual(posted_data, {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[50]",
|
|
|
|
flag: "starred",
|
|
|
|
op: "remove",
|
2018-07-03 01:30:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(message, {
|
|
|
|
id: 50,
|
|
|
|
starred: false,
|
|
|
|
});
|
|
|
|
});
|
2021-03-20 09:41:03 +01:00
|
|
|
|
|
|
|
run_test("starring local echo", () => {
|
|
|
|
// verify early return for locally echoed message
|
|
|
|
const locally_echoed_message = {
|
|
|
|
id: 51,
|
|
|
|
starred: false,
|
|
|
|
locally_echoed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
message_flags.toggle_starred_and_update_server(locally_echoed_message);
|
|
|
|
|
|
|
|
// ui.update_starred_view not called
|
|
|
|
|
|
|
|
// channel post request not made
|
|
|
|
|
|
|
|
// starred flag unchanged
|
|
|
|
assert.deepEqual(locally_echoed_message, {
|
|
|
|
id: 51,
|
|
|
|
locally_echoed: true,
|
|
|
|
starred: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("unstar_all", ({override}) => {
|
2021-03-20 11:11:32 +01:00
|
|
|
// Way to capture posted info in every request
|
|
|
|
let posted_data;
|
|
|
|
override(channel, "post", (opts) => {
|
|
|
|
assert.equal(opts.url, "/json/messages/flags");
|
|
|
|
posted_data = opts.data;
|
|
|
|
});
|
|
|
|
|
|
|
|
// we've set get_starred_msg_ids to return [1, 2, 3, 4, 5]
|
|
|
|
const expected_data = {messages: "[1,2,3,4,5]", flag: "starred", op: "remove"};
|
|
|
|
|
|
|
|
message_flags.unstar_all_messages();
|
|
|
|
|
|
|
|
assert.deepEqual(posted_data, expected_data);
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("unstar_all_in_topic", ({override}) => {
|
2021-03-24 05:44:40 +01:00
|
|
|
// Way to capture posted info in every request
|
starred messages: Fix "unstar all in topic" is incomplete.
Currently, when there are some old starred messages
in a topic, it is possible that some of them won't be
unstarred on doing "Unstar all messages in topic".
This happens when those messages haven't been fetched
yet from the server, and we have no way to verify if
they actually belong to the topic.
This commit fixes that by changing this mechanism to
first fetch all starred messages in the topic from
the server, and then send their IDs back to the backend
to unstar them.
While doing this, we assume that the user does not
have more than 1000 starred messages in that topic.
Note that, we still depend on the local data to
decide whether or not the "Unstar all messages in
topic" option should be shown in the topic popover.
A method similar to the above cannot be used here, because
making server requests before opening the popover
could visually slow down the popover opening.
Using local data for the topic popover would probably
not be a big problem, because users would want to
unstar all messages in a topic probably after noticing
that there are a lot of them, meaning there was at least
one starred message from that topic which was already
fetched, which is sufficient for us to conclude that
we need to show the option in the topic popover.
Fixes #17790
2021-04-02 08:10:11 +02:00
|
|
|
let channel_post_opts;
|
|
|
|
let channel_get_opts;
|
|
|
|
|
|
|
|
override(channel, "get", (opts) => {
|
|
|
|
assert.equal(opts.url, "/json/messages");
|
|
|
|
channel_get_opts = opts;
|
|
|
|
opts.success({
|
|
|
|
messages: [{id: 2}, {id: 3}, {id: 5}],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-24 05:44:40 +01:00
|
|
|
override(channel, "post", (opts) => {
|
|
|
|
assert.equal(opts.url, "/json/messages/flags");
|
starred messages: Fix "unstar all in topic" is incomplete.
Currently, when there are some old starred messages
in a topic, it is possible that some of them won't be
unstarred on doing "Unstar all messages in topic".
This happens when those messages haven't been fetched
yet from the server, and we have no way to verify if
they actually belong to the topic.
This commit fixes that by changing this mechanism to
first fetch all starred messages in the topic from
the server, and then send their IDs back to the backend
to unstar them.
While doing this, we assume that the user does not
have more than 1000 starred messages in that topic.
Note that, we still depend on the local data to
decide whether or not the "Unstar all messages in
topic" option should be shown in the topic popover.
A method similar to the above cannot be used here, because
making server requests before opening the popover
could visually slow down the popover opening.
Using local data for the topic popover would probably
not be a big problem, because users would want to
unstar all messages in a topic probably after noticing
that there are a lot of them, meaning there was at least
one starred message from that topic which was already
fetched, which is sufficient for us to conclude that
we need to show the option in the topic popover.
Fixes #17790
2021-04-02 08:10:11 +02:00
|
|
|
channel_post_opts = opts;
|
2021-03-24 05:44:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
message_flags.unstar_all_messages_in_topic(20, "topic");
|
|
|
|
|
starred messages: Fix "unstar all in topic" is incomplete.
Currently, when there are some old starred messages
in a topic, it is possible that some of them won't be
unstarred on doing "Unstar all messages in topic".
This happens when those messages haven't been fetched
yet from the server, and we have no way to verify if
they actually belong to the topic.
This commit fixes that by changing this mechanism to
first fetch all starred messages in the topic from
the server, and then send their IDs back to the backend
to unstar them.
While doing this, we assume that the user does not
have more than 1000 starred messages in that topic.
Note that, we still depend on the local data to
decide whether or not the "Unstar all messages in
topic" option should be shown in the topic popover.
A method similar to the above cannot be used here, because
making server requests before opening the popover
could visually slow down the popover opening.
Using local data for the topic popover would probably
not be a big problem, because users would want to
unstar all messages in a topic probably after noticing
that there are a lot of them, meaning there was at least
one starred message from that topic which was already
fetched, which is sufficient for us to conclude that
we need to show the option in the topic popover.
Fixes #17790
2021-04-02 08:10:11 +02:00
|
|
|
assert.deepEqual(channel_get_opts.data, {
|
|
|
|
anchor: "newest",
|
|
|
|
num_before: 1000,
|
|
|
|
num_after: 0,
|
|
|
|
narrow: JSON.stringify([
|
|
|
|
{operator: "stream", operand: 20},
|
|
|
|
{operator: "topic", operand: "topic"},
|
|
|
|
{operator: "is", operand: "starred"},
|
|
|
|
]),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(channel_post_opts.data, {
|
|
|
|
messages: "[2,3,5]",
|
|
|
|
flag: "starred",
|
|
|
|
op: "remove",
|
|
|
|
});
|
2021-03-24 05:44:40 +01:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("read", ({override}) => {
|
2019-05-04 04:10:05 +02:00
|
|
|
// Way to capture posted info in every request
|
2019-11-02 00:06:25 +01:00
|
|
|
let channel_post_opts;
|
2021-03-10 15:24:49 +01:00
|
|
|
override(channel, "post", (opts) => {
|
2019-05-04 04:10:05 +02:00
|
|
|
channel_post_opts = opts;
|
2021-03-10 15:24:49 +01:00
|
|
|
});
|
2019-05-04 04:10:05 +02:00
|
|
|
|
|
|
|
// For testing purpose limit the batch size value to 5 instead of 1000
|
2021-03-10 15:24:49 +01:00
|
|
|
function send_read(messages) {
|
2022-07-09 23:25:05 +02:00
|
|
|
with_overrides(({override_rewire}) => {
|
|
|
|
override_rewire(message_flags, "_unread_batch_size", 5);
|
2021-03-10 15:24:49 +01:00
|
|
|
message_flags.send_read(messages);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let msgs_to_flag_read = [
|
2020-07-16 22:40:18 +02:00
|
|
|
{locally_echoed: false, id: 1},
|
|
|
|
{locally_echoed: false, id: 2},
|
|
|
|
{locally_echoed: false, id: 3},
|
|
|
|
{locally_echoed: false, id: 4},
|
|
|
|
{locally_echoed: false, id: 5},
|
|
|
|
{locally_echoed: false, id: 6},
|
|
|
|
{locally_echoed: false, id: 7},
|
2019-05-04 04:10:05 +02:00
|
|
|
];
|
2021-03-10 15:24:49 +01:00
|
|
|
send_read(msgs_to_flag_read);
|
2019-05-04 04:10:05 +02:00
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[1,2,3,4,5]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mock successful flagging of ids
|
2019-11-02 00:06:25 +01:00
|
|
|
let success_response_data = {
|
2019-05-04 04:10:05 +02:00
|
|
|
messages: [1, 2, 3, 4, 5],
|
|
|
|
};
|
|
|
|
channel_post_opts.success(success_response_data);
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[6,7]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
success_response_data = {
|
|
|
|
messages: [6, 7],
|
|
|
|
};
|
|
|
|
channel_post_opts.success(success_response_data);
|
|
|
|
|
|
|
|
// Don't flag locally echoed messages as read
|
2020-07-16 22:40:18 +02:00
|
|
|
const local_msg_1 = {locally_echoed: true, id: 1};
|
|
|
|
const local_msg_2 = {locally_echoed: true, id: 2};
|
2019-05-04 04:10:05 +02:00
|
|
|
msgs_to_flag_read = [
|
|
|
|
local_msg_1,
|
|
|
|
local_msg_2,
|
2020-07-16 22:40:18 +02:00
|
|
|
{locally_echoed: false, id: 3},
|
|
|
|
{locally_echoed: false, id: 4},
|
|
|
|
{locally_echoed: false, id: 5},
|
|
|
|
{locally_echoed: false, id: 6},
|
|
|
|
{locally_echoed: false, id: 7},
|
2019-05-04 04:10:05 +02:00
|
|
|
];
|
2021-03-10 15:24:49 +01:00
|
|
|
send_read(msgs_to_flag_read);
|
2019-05-04 04:10:05 +02:00
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[3,4,5,6,7]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
|
2021-03-19 19:17:22 +01:00
|
|
|
// Messages still not acked yet
|
|
|
|
const events = {};
|
|
|
|
const stub_delay = 100;
|
|
|
|
function set_timeout(f, delay) {
|
|
|
|
assert.equal(delay, stub_delay);
|
|
|
|
events.f = f;
|
|
|
|
events.timer_set = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set_global("setTimeout", set_timeout);
|
|
|
|
// Mock successful flagging of ids
|
|
|
|
success_response_data = {
|
|
|
|
messages: [3, 4, 5, 6, 7],
|
|
|
|
};
|
|
|
|
channel_post_opts.success(success_response_data);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(events.timer_set);
|
2021-03-19 19:17:22 +01:00
|
|
|
|
2022-04-28 05:15:11 +02:00
|
|
|
// Mark them non-local
|
2019-05-04 04:10:05 +02:00
|
|
|
local_msg_1.locally_echoed = false;
|
|
|
|
local_msg_2.locally_echoed = false;
|
|
|
|
|
|
|
|
// Mock successful flagging of ids
|
|
|
|
success_response_data = {
|
|
|
|
messages: [3, 4, 5, 6, 7],
|
|
|
|
};
|
|
|
|
channel_post_opts.success(success_response_data);
|
|
|
|
|
|
|
|
// Former locally echoed messages flagging retried
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[1,2]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
});
|
2021-03-19 18:01:00 +01:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("read_empty_data", ({override}) => {
|
2021-03-19 18:11:04 +01:00
|
|
|
// Way to capture posted info in every request
|
|
|
|
let channel_post_opts;
|
|
|
|
override(channel, "post", (opts) => {
|
|
|
|
channel_post_opts = opts;
|
|
|
|
});
|
|
|
|
|
|
|
|
// For testing purpose limit the batch size value to 5 instead of 1000
|
|
|
|
function send_read(messages) {
|
2022-07-09 23:25:05 +02:00
|
|
|
with_overrides(({override_rewire}) => {
|
|
|
|
override_rewire(message_flags, "_unread_batch_size", 5);
|
2021-03-19 18:11:04 +01:00
|
|
|
message_flags.send_read(messages);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// send read to obtain success callback
|
|
|
|
send_read({locally_echoed: false, id: 1});
|
|
|
|
|
|
|
|
// verify early return on empty data
|
|
|
|
const success_callback = channel_post_opts.success;
|
|
|
|
channel_post_opts = {};
|
|
|
|
let empty_data;
|
|
|
|
success_callback(empty_data);
|
|
|
|
assert.deepEqual(channel_post_opts, {});
|
|
|
|
empty_data = {messages: undefined};
|
|
|
|
success_callback(empty_data);
|
|
|
|
assert.deepEqual(channel_post_opts, {});
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("collapse_and_uncollapse", ({override}) => {
|
2021-03-19 18:01:00 +01:00
|
|
|
// Way to capture posted info in every request
|
|
|
|
let channel_post_opts;
|
|
|
|
override(channel, "post", (opts) => {
|
|
|
|
channel_post_opts = opts;
|
|
|
|
});
|
|
|
|
|
|
|
|
const msg = {id: 5};
|
|
|
|
|
|
|
|
message_flags.save_collapsed(msg);
|
|
|
|
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
|
|
|
url: "/json/messages/flags",
|
|
|
|
data: {
|
|
|
|
messages: "[5]",
|
|
|
|
op: "add",
|
|
|
|
flag: "collapsed",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
message_flags.save_uncollapsed(msg);
|
|
|
|
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
|
|
|
url: "/json/messages/flags",
|
|
|
|
data: {
|
|
|
|
messages: "[5]",
|
|
|
|
op: "remove",
|
|
|
|
flag: "collapsed",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|