2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
const {mock_esm, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/muting", {
|
2018-05-02 15:31:36 +02:00
|
|
|
is_topic_muted: () => false,
|
|
|
|
});
|
|
|
|
|
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 {Filter} = zrequire("../js/filter");
|
node tests: Introduce message_store.create_mock_message() helper.
Previously, it was tedious to create actual message
objects in message_store for use in node tests.
This was mainly because, `add_message_metadata`
in message_store has many dependencies and
validation checks. Since it was difficult to create
actual message objects, many tests just mocked
the `message_store.get()` method to return the desired
message.
This commit adds a new helper method (`create_mock_message`)
to message_store, for use in node tests. This just stores
the object passed to it in the `stores_messages` map,
without any validation. We do not add any
default fields to the message object before saving
it from this helper, because doing so would decrease
the utility of this helper, and, if a test
depends on some field having a particular value,
then it would be better to just pass the field: value
pair from the test itself, for readability, rather
than relying on the helper to add the field for us.
This helper allows us to write deeper tests.
This commit also replaces some instances of mocking
`message_store.get()` to use this new helper method.
2021-03-05 06:34:11 +01:00
|
|
|
const message_store = zrequire("message_store");
|
2020-12-01 23:21:38 +01:00
|
|
|
const people = zrequire("people");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const unread = zrequire("unread");
|
2018-05-02 15:31:36 +02:00
|
|
|
// The main code we are testing lives here.
|
2021-02-10 04:53:22 +01:00
|
|
|
const narrow_state = zrequire("narrow_state");
|
2018-05-02 15:31:36 +02:00
|
|
|
|
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2018-05-02 15:31:36 +02:00
|
|
|
user_id: 11,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice",
|
2018-05-02 15:31:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
people.init();
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
2018-05-02 15:31:36 +02:00
|
|
|
people.is_my_user_id = () => false;
|
|
|
|
|
|
|
|
function set_filter(terms) {
|
|
|
|
const filter = new Filter(terms);
|
|
|
|
narrow_state.set_current_filter(filter);
|
|
|
|
}
|
|
|
|
|
2018-05-03 18:58:00 +02:00
|
|
|
function assert_unread_info(expected) {
|
|
|
|
assert.deepEqual(narrow_state.get_first_unread_info(), expected);
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
function candidate_ids() {
|
|
|
|
return narrow_state._possible_unread_message_ids();
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_unread_ids", () => {
|
2021-03-09 15:02:31 +01:00
|
|
|
unread.declare_bankruptcy();
|
|
|
|
narrow_state.reset_current_filter();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let unread_ids;
|
|
|
|
let terms;
|
2018-05-02 15:31:36 +02:00
|
|
|
|
|
|
|
const sub = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "My Stream",
|
2018-05-02 15:31:36 +02:00
|
|
|
stream_id: 55,
|
|
|
|
};
|
2018-05-09 03:28:29 +02:00
|
|
|
|
|
|
|
const stream_msg = {
|
|
|
|
id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-05-09 03:28:29 +02:00
|
|
|
stream_id: sub.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "my topic",
|
2018-05-09 03:28:29 +02:00
|
|
|
unread: true,
|
|
|
|
mentioned: true,
|
2019-09-27 11:11:40 +02:00
|
|
|
mentioned_me_directly: true,
|
2018-05-09 03:28:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const private_msg = {
|
|
|
|
id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2018-05-09 03:28:29 +02:00
|
|
|
unread: true,
|
2020-07-15 00:34:28 +02:00
|
|
|
display_recipient: [{id: alice.user_id}],
|
2018-05-09 03:28:29 +02:00
|
|
|
};
|
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache(stream_msg);
|
|
|
|
message_store.update_message_cache(private_msg);
|
2021-02-24 16:23:24 +01:00
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.equal(unread_ids, undefined);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "search", operand: "whatever"}];
|
2018-05-02 15:31:36 +02:00
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.equal(unread_ids, undefined);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert_unread_info({flavor: "cannot_compute"});
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "bogus_operator", operand: "me@example.com"}];
|
2018-05-31 16:46:39 +02:00
|
|
|
set_filter(terms);
|
|
|
|
unread_ids = candidate_ids();
|
|
|
|
assert.deepEqual(unread_ids, []);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert_unread_info({flavor: "not_found"});
|
2018-05-31 16:46:39 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "stream", operand: "bogus"}];
|
2018-05-02 15:31:36 +02:00
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "stream", operand: sub.name}];
|
2018-05-02 15:31:36 +02:00
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert_unread_info({flavor: "not_found"});
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-09 03:28:29 +02:00
|
|
|
unread.process_loaded_messages([stream_msg]);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
2018-05-03 18:58:00 +02:00
|
|
|
assert_unread_info({
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "found",
|
2018-05-09 03:28:29 +02:00
|
|
|
msg_id: stream_msg.id,
|
2018-05-03 18:58:00 +02:00
|
|
|
});
|
2018-05-02 15:31:36 +02:00
|
|
|
|
|
|
|
terms = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "bogus"},
|
|
|
|
{operator: "topic", operand: "my topic"},
|
2018-05-02 15:31:36 +02:00
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
|
|
|
|
|
|
|
terms = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: sub.name},
|
|
|
|
{operator: "topic", operand: "my topic"},
|
2018-05-02 15:31:36 +02:00
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "is", operand: "mentioned"}];
|
2018-05-02 15:31:36 +02:00
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "sender", operand: "me@example.com"}];
|
2018-05-10 13:55:56 +02:00
|
|
|
set_filter(terms);
|
|
|
|
// note that our candidate ids are just "all" ids now
|
|
|
|
unread_ids = candidate_ids();
|
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
|
|
|
|
|
|
|
// this actually does filtering
|
2020-07-15 01:29:15 +02:00
|
|
|
assert_unread_info({flavor: "not_found"});
|
2018-05-10 13:55:56 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "pm-with", operand: "alice@example.com"}];
|
2018-05-02 15:31:36 +02:00
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
|
|
|
|
2018-05-09 03:28:29 +02:00
|
|
|
unread.process_loaded_messages([private_msg]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [private_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
assert_unread_info({
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "found",
|
2018-05-09 00:08:24 +02:00
|
|
|
msg_id: private_msg.id,
|
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "is", operand: "private"}];
|
2018-05-02 15:31:36 +02:00
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [private_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-31 16:46:39 +02:00
|
|
|
// For a negated search, our candidate ids will be all
|
|
|
|
// unread messages, even ones that don't pass the filter.
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "is", operand: "private", negated: true}];
|
2018-05-31 16:46:39 +02:00
|
|
|
set_filter(terms);
|
|
|
|
unread_ids = candidate_ids();
|
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id, private_msg.id]);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "pm-with", operand: "bob@example.com"}];
|
2018-05-02 15:31:36 +02:00
|
|
|
set_filter(terms);
|
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
2018-05-03 22:57:07 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "is", operand: "starred"}];
|
2018-05-03 22:57:07 +02:00
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-03 22:57:07 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
2018-05-09 00:08:24 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
terms = [{operator: "search", operand: "needle"}];
|
2018-05-09 00:08:24 +02:00
|
|
|
set_filter(terms);
|
|
|
|
|
2018-05-31 12:55:19 +02:00
|
|
|
assert_unread_info({
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "cannot_compute",
|
2018-05-31 12:55:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
narrow_state.reset_current_filter();
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "unexpected call to get_first_unread_info");
|
2018-05-09 00:08:24 +02:00
|
|
|
assert_unread_info({
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "cannot_compute",
|
2018-05-09 00:08:24 +02:00
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-05-31 16:46:39 +02:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
run_test("defensive code", (override) => {
|
2018-05-31 16:46:39 +02:00
|
|
|
// Test defensive code. We actually avoid calling
|
|
|
|
// _possible_unread_message_ids for any case where we
|
|
|
|
// couldn't compute the unread message ids, but that
|
|
|
|
// invariant is hard to future-proof.
|
2021-03-07 17:01:11 +01:00
|
|
|
override(narrow_state, "_possible_unread_message_ids", () => undefined);
|
2020-07-15 00:34:28 +02:00
|
|
|
const terms = [{operator: "some-unhandled-case", operand: "whatever"}];
|
2018-05-31 16:46:39 +02:00
|
|
|
set_filter(terms);
|
|
|
|
assert_unread_info({
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "cannot_compute",
|
2018-05-31 16:46:39 +02:00
|
|
|
});
|
|
|
|
});
|