2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire, set_global} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const {page_params, user_settings} = require("./lib/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.realm_push_notifications_enabled = false;
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2022-10-14 17:37:47 +02:00
|
|
|
set_global("document", "document-stub");
|
2021-03-07 13:57:14 +01:00
|
|
|
const {FoldDict} = zrequire("fold_dict");
|
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");
|
2022-08-14 15:33:13 +02:00
|
|
|
const user_topics = zrequire("user_topics");
|
2020-12-01 23:21:38 +01:00
|
|
|
const people = zrequire("people");
|
|
|
|
const stream_data = zrequire("stream_data");
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub_store = zrequire("sub_store");
|
2020-12-01 23:21:38 +01:00
|
|
|
const unread = zrequire("unread");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@example.com",
|
2017-02-14 01:15:26 +01:00
|
|
|
user_id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Me Myself",
|
2017-02-14 01:15:26 +01:00
|
|
|
};
|
2023-05-05 16:28:06 +02:00
|
|
|
|
|
|
|
const anybody = {
|
|
|
|
email: "anybody@example.com",
|
|
|
|
user_id: 999,
|
|
|
|
full_name: "Any Body",
|
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(me);
|
2023-05-05 16:28:06 +02:00
|
|
|
people.add_active_user(anybody);
|
2017-02-14 01:15:26 +01:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const social = {
|
2018-12-13 22:26:10 +01:00
|
|
|
stream_id: 200,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "social",
|
2018-12-13 22:26:10 +01:00
|
|
|
subscribed: true,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: false,
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(social);
|
2018-12-13 22:26:10 +01:00
|
|
|
|
2020-02-12 11:49:02 +01:00
|
|
|
function assert_zero_counts(counts) {
|
2023-09-12 20:58:40 +02:00
|
|
|
assert.equal(counts.direct_message_count, 0);
|
2020-02-12 11:49:02 +01:00
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
|
|
|
assert.equal(counts.mentioned_message_count, 0);
|
|
|
|
assert.equal(counts.stream_count.size, 0);
|
|
|
|
assert.equal(counts.pm_count.size, 0);
|
|
|
|
}
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2019-07-20 14:45:56 +02:00
|
|
|
function test_notifiable_count(home_unread_messages, expected_notifiable_count) {
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.desktop_icon_count_display = 1;
|
2019-11-02 00:06:25 +01:00
|
|
|
let notifiable_counts = unread.get_notifiable_count();
|
2019-07-20 14:45:56 +02:00
|
|
|
assert.deepEqual(notifiable_counts, home_unread_messages);
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.desktop_icon_count_display = 2;
|
2019-07-20 14:45:56 +02:00
|
|
|
notifiable_counts = unread.get_notifiable_count();
|
|
|
|
assert.deepEqual(notifiable_counts, expected_notifiable_count);
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.desktop_icon_count_display = 3;
|
2019-08-19 23:33:11 +02:00
|
|
|
notifiable_counts = unread.get_notifiable_count();
|
|
|
|
assert.deepEqual(notifiable_counts, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2021-03-09 15:18:57 +01:00
|
|
|
unread.declare_bankruptcy();
|
2022-08-29 14:16:25 +02:00
|
|
|
user_topics.set_user_topics([]);
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2021-03-09 15:18:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("empty_counts_while_narrowed", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const counts = unread.get_counts();
|
2020-02-12 11:49:02 +01:00
|
|
|
assert_zero_counts(counts);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("empty_counts_while_home", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const counts = unread.get_counts();
|
2020-02-12 11:49:02 +01:00
|
|
|
assert_zero_counts(counts);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
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
|
|
|
test("changing_topics", () => {
|
2018-12-23 16:49:14 +01:00
|
|
|
// Summary: change the topic of a message from 'lunch'
|
2016-08-27 03:29:32 +02:00
|
|
|
// to 'dinner' using update_unread_topics().
|
2020-07-15 01:29:15 +02:00
|
|
|
let count = unread.num_unread_for_topic(social.stream_id, "lunch");
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, 0);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = 100;
|
|
|
|
const wrong_stream_id = 110;
|
2017-05-13 19:26:54 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2013-07-28 18:40:50 +02:00
|
|
|
id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "luNch",
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const other_message = {
|
2013-10-15 19:32:29 +02:00
|
|
|
id: 16,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunCH",
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2022-10-14 17:37:47 +02:00
|
|
|
message_store.update_message_cache(message);
|
|
|
|
message_store.update_message_cache(other_message);
|
2013-10-15 19:32:29 +02:00
|
|
|
|
2021-08-04 19:47:51 +02:00
|
|
|
assert.deepEqual(unread.get_read_message_ids([15, 16]), [15, 16]);
|
2017-12-16 16:53:27 +01:00
|
|
|
assert.deepEqual(unread.get_unread_message_ids([15, 16]), []);
|
|
|
|
assert.deepEqual(unread.get_unread_messages([message, other_message]), []);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");
|
2018-04-25 22:48:51 +02:00
|
|
|
assert.deepEqual(msg_ids, []);
|
|
|
|
|
2018-05-01 21:22:49 +02:00
|
|
|
msg_ids = unread.get_msg_ids_for_stream(stream_id);
|
|
|
|
assert.deepEqual(msg_ids, []);
|
|
|
|
|
2013-10-15 19:32:29 +02:00
|
|
|
unread.process_loaded_messages([message, other_message]);
|
2017-12-16 16:53:27 +01:00
|
|
|
|
2018-05-10 13:11:53 +02:00
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), [15, 16]);
|
2021-08-04 19:47:51 +02:00
|
|
|
assert.deepEqual(unread.get_read_message_ids([15, 16]), []);
|
2017-12-16 16:53:27 +01:00
|
|
|
assert.deepEqual(unread.get_unread_message_ids([15, 16]), [15, 16]);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(unread.get_unread_messages([message, other_message]), [
|
|
|
|
message,
|
|
|
|
other_message,
|
|
|
|
]);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "Lunch");
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 2);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(unread.topic_has_any_unread(stream_id, "lunch"));
|
|
|
|
assert.ok(!unread.topic_has_any_unread(wrong_stream_id, "lunch"));
|
|
|
|
assert.ok(!unread.topic_has_any_unread(stream_id, "NOT lunch"));
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "NOT lunch");
|
2017-08-03 05:01:47 +02:00
|
|
|
assert.equal(count, 0);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_ids = unread.get_msg_ids_for_topic(stream_id, "NOT lunch");
|
2018-04-25 22:48:51 +02:00
|
|
|
assert.deepEqual(msg_ids, []);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "dinner",
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
2016-08-27 03:29:32 +02:00
|
|
|
unread.update_unread_topics(message, event);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "lUnch");
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 1);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "dinner");
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, 1);
|
|
|
|
|
2013-10-15 19:32:29 +02:00
|
|
|
event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "snack",
|
2013-10-15 19:32:29 +02:00
|
|
|
};
|
|
|
|
|
2016-08-27 03:29:32 +02:00
|
|
|
unread.update_unread_topics(other_message, event);
|
2013-10-15 19:32:29 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "lunch");
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 0);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!unread.topic_has_any_unread(stream_id, "lunch"));
|
|
|
|
assert.ok(!unread.topic_has_any_unread(wrong_stream_id, "lunch"));
|
2013-10-15 19:32:29 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "snack");
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(unread.topic_has_any_unread(stream_id, "snack"));
|
|
|
|
assert.ok(!unread.topic_has_any_unread(wrong_stream_id, "snack"));
|
2013-10-15 19:32:29 +02:00
|
|
|
|
|
|
|
// Test defensive code. Trying to update a message we don't know
|
|
|
|
// about should be a no-op.
|
|
|
|
event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "brunch",
|
2013-10-15 19:32:29 +02:00
|
|
|
};
|
2016-08-27 03:29:32 +02:00
|
|
|
unread.update_unread_topics(other_message, event);
|
2013-10-15 19:32:29 +02:00
|
|
|
|
2017-08-10 20:27:23 +02:00
|
|
|
// Update a message that was never marked as unread.
|
2020-02-04 02:48:42 +01:00
|
|
|
const sticky_message = {
|
2017-08-10 20:27:23 +02:00
|
|
|
id: 17,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "sticky",
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache(message);
|
|
|
|
message_store.update_message_cache(other_message);
|
|
|
|
message_store.update_message_cache(sticky_message);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
|
|
|
unread.process_loaded_messages([sticky_message]);
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "sticky");
|
2017-08-10 20:27:23 +02:00
|
|
|
assert.equal(count, 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(sticky_message.unread);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
|
|
|
unread.mark_as_read(sticky_message.id);
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "sticky");
|
2017-08-10 20:27:23 +02:00
|
|
|
assert.equal(count, 0);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!sticky_message.unread);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
2018-12-23 18:08:37 +01:00
|
|
|
event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "sticky",
|
2018-12-23 18:08:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.update_unread_topics(sticky_message, event);
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "sticky");
|
2017-08-10 20:27:23 +02:00
|
|
|
assert.equal(count, 0);
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
// cleanup
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "dinner");
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, 0);
|
2013-10-15 19:32:29 +02:00
|
|
|
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(other_message.id);
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "snack");
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 0);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
|
|
|
// test coverage
|
|
|
|
unread.update_unread_topics(sticky_message, {});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("muting", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = social.stream_id;
|
|
|
|
const unknown_stream_id = 555;
|
2017-05-13 19:26:54 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2013-09-28 00:00:44 +02:00
|
|
|
id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "test_muting",
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2013-09-28 00:00:44 +02:00
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
2019-11-02 00:06:25 +01:00
|
|
|
let counts = unread.get_counts();
|
2023-04-22 18:24:59 +02:00
|
|
|
assert.equal(counts.stream_count.get(stream_id).unmuted_count, 1);
|
2013-09-28 00:00:44 +02:00
|
|
|
assert.equal(counts.home_unread_messages, 1);
|
2023-04-22 18:24:59 +02:00
|
|
|
assert.equal(unread.num_unread_for_stream(stream_id).unmuted_count, 1);
|
2018-05-01 21:22:49 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), [message.id]);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2013-09-28 00:00:44 +02:00
|
|
|
|
2023-04-11 07:48:13 +02:00
|
|
|
user_topics.update_user_topics(
|
|
|
|
social.stream_id,
|
|
|
|
"test_muting",
|
|
|
|
user_topics.all_visibility_policies.MUTED,
|
|
|
|
);
|
2013-09-28 00:00:44 +02:00
|
|
|
counts = unread.get_counts();
|
2023-04-22 18:24:59 +02:00
|
|
|
assert.equal(counts.stream_count.get(stream_id).unmuted_count, 0);
|
2013-09-28 00:00:44 +02:00
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
2023-04-22 18:24:59 +02:00
|
|
|
assert.equal(unread.num_unread_for_stream(stream_id).unmuted_count, 0);
|
2018-05-01 21:22:49 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), []);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2017-04-03 22:14:46 +02:00
|
|
|
|
2018-05-10 13:11:53 +02:00
|
|
|
// we still find the message id here (muting is ignored)
|
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), [message.id]);
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
assert.equal(unread.num_unread_for_stream(unknown_stream_id), 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-09-28 00:00:44 +02:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test("num_unread_for_topic", () => {
|
2017-07-31 14:11:18 +02:00
|
|
|
// Test the num_unread_for_topic() function using many
|
2013-07-28 18:40:50 +02:00
|
|
|
// messages.
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = 301;
|
2022-07-10 01:06:33 +02:00
|
|
|
sub_store.add_hydrated_sub(stream_id, {stream_id, name: "Some stream"});
|
2018-05-01 21:22:49 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let count = unread.num_unread_for_topic(stream_id, "lunch");
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, 0);
|
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "LuncH",
|
2020-02-04 02:48:42 +01:00
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
// Put messages into list in reverse order to try to confuse
|
|
|
|
// our sort.
|
2019-11-02 00:06:25 +01:00
|
|
|
const num_msgs = 500;
|
|
|
|
let i;
|
2018-04-25 22:48:51 +02:00
|
|
|
for (i = num_msgs; i > 0; i -= 1) {
|
2020-02-04 02:48:42 +01:00
|
|
|
message.id = i;
|
2022-10-14 17:37:47 +02:00
|
|
|
message_store.update_message_cache(message);
|
2013-07-28 18:40:50 +02:00
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "lunch");
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, num_msgs);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");
|
2018-04-25 22:48:51 +02:00
|
|
|
assert.deepEqual(msg_ids, _.range(1, 501));
|
|
|
|
|
2018-05-01 21:22:49 +02:00
|
|
|
msg_ids = unread.get_msg_ids_for_stream(stream_id);
|
|
|
|
assert.deepEqual(msg_ids, _.range(1, 501));
|
|
|
|
|
2019-12-26 15:34:17 +01:00
|
|
|
const topic_dict = new FoldDict();
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let missing_topics = unread.get_missing_topics({
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
|
|
|
topic_dict,
|
2018-05-13 11:38:40 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(missing_topics, [{pretty_name: "LuncH", message_id: 500}]);
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
topic_dict.set("lUNCh", "whatever");
|
2018-05-13 11:38:40 +02:00
|
|
|
|
|
|
|
missing_topics = unread.get_missing_topics({
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
|
|
|
topic_dict,
|
2018-05-13 11:38:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(missing_topics, []);
|
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
for (i = 0; i < num_msgs; i += 1) {
|
|
|
|
message.id = i + 1;
|
|
|
|
unread.mark_as_read(message.id);
|
2013-07-28 18:40:50 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, "lunch");
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, 0);
|
2018-04-25 22:48:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");
|
2018-04-25 22:48:51 +02:00
|
|
|
assert.deepEqual(msg_ids, []);
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
msg_ids = unread.get_msg_ids_for_stream(stream_id);
|
|
|
|
assert.deepEqual(msg_ids, []);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test("home_messages", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = 401;
|
2022-07-10 01:06:33 +02:00
|
|
|
const sub = {
|
|
|
|
stream_id,
|
2021-02-23 14:37:26 +01:00
|
|
|
name: "whatever",
|
2022-07-10 01:06:33 +02:00
|
|
|
subscribed: true,
|
|
|
|
is_muted: false,
|
|
|
|
};
|
|
|
|
sub_store.add_hydrated_sub(stream_id, sub);
|
2017-05-13 19:26:54 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2013-07-28 18:40:50 +02:00
|
|
|
id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let counts = unread.get_counts();
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 1);
|
2023-04-22 18:24:59 +02:00
|
|
|
assert.equal(counts.stream_count.get(stream_id).unmuted_count, 1);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2013-07-28 18:40:50 +02:00
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2013-08-23 04:56:11 +02:00
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 1);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2013-08-23 04:56:11 +02:00
|
|
|
|
|
|
|
// Now unsubscribe all our streams.
|
2022-07-10 01:06:33 +02:00
|
|
|
sub.subscribed = false;
|
2013-08-23 04:56:11 +02:00
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("phantom_messages", () => {
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2013-09-28 23:09:29 +02:00
|
|
|
id: 999,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_id: 555,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "phantom",
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2022-10-14 17:37:47 +02:00
|
|
|
message_store.update_message_cache(message);
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2019-11-02 00:06:25 +01:00
|
|
|
const counts = unread.get_counts();
|
2013-09-28 23:09:29 +02:00
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-09-28 23:09:29 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("private_messages", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let counts = unread.get_counts();
|
2023-09-12 20:58:40 +02:00
|
|
|
assert.equal(counts.direct_message_count, 0);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2017-02-14 01:15:26 +01:00
|
|
|
id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2020-07-15 00:34:28 +02:00
|
|
|
display_recipient: [{id: anybody.user_id}, {id: me.user_id}],
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2017-02-14 01:15:26 +01:00
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
|
|
|
|
counts = unread.get_counts();
|
2023-09-12 20:58:40 +02:00
|
|
|
assert.equal(counts.direct_message_count, 1);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(counts.pm_count.get("999"), 1);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 1);
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2013-07-28 18:40:50 +02:00
|
|
|
counts = unread.get_counts();
|
2023-09-12 20:58:40 +02:00
|
|
|
assert.equal(counts.direct_message_count, 0);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(counts.pm_count.get("999"), 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("private_messages", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2016-11-16 00:09:09 +01:00
|
|
|
user_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice",
|
2016-11-16 00:09:09 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
2013-09-10 23:33:46 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bob@example.com",
|
2016-11-16 00:09:09 +01:00
|
|
|
user_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Bob",
|
2016-11-16 00:09:09 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(bob);
|
2016-11-16 00:09:09 +01:00
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
assert.equal(unread.num_unread_for_user_ids_string(alice.user_id.toString()), 0);
|
|
|
|
assert.equal(unread.num_unread_for_user_ids_string(bob.user_id.toString()), 0);
|
2022-10-22 07:13:57 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_user_ids_string(alice.user_id.toString()), []);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_user_ids_string(bob.user_id.toString()), []);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_user_ids_string(), []);
|
2018-05-02 19:50:25 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_private(), []);
|
2013-08-22 19:06:04 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2013-08-22 19:06:04 +02:00
|
|
|
id: 15,
|
2017-02-14 01:15:26 +01:00
|
|
|
display_recipient: [{id: alice.user_id}],
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2022-04-24 06:13:19 +02:00
|
|
|
to_user_ids: alice.user_id.toString(),
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2013-08-23 03:23:08 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const read_message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
flags: ["read"],
|
2013-08-23 03:23:08 +02:00
|
|
|
};
|
|
|
|
unread.process_loaded_messages([message, read_message]);
|
2022-10-22 07:15:44 +02:00
|
|
|
assert.equal(unread.num_unread_for_user_ids_string(alice.user_id.toString()), 1);
|
2017-04-03 22:14:46 +02:00
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
assert.equal(unread.num_unread_for_user_ids_string(""), 0);
|
2018-05-02 13:02:32 +02:00
|
|
|
|
2022-10-22 07:13:57 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_user_ids_string(alice.user_id.toString()), [
|
|
|
|
message.id,
|
|
|
|
]);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_user_ids_string(bob.user_id.toString()), []);
|
2018-05-02 19:50:25 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_private(), [message.id]);
|
2018-05-10 13:11:53 +02:00
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), [message.id]);
|
2019-07-20 13:42:49 +02:00
|
|
|
|
|
|
|
unread.mark_as_read(message.id);
|
2022-10-22 07:15:44 +02:00
|
|
|
assert.equal(unread.num_unread_for_user_ids_string(alice.user_id.toString()), 0);
|
|
|
|
assert.equal(unread.num_unread_for_user_ids_string(""), 0);
|
2022-10-22 07:13:57 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_user_ids_string(alice.user_id.toString()), []);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_user_ids_string(bob.user_id.toString()), []);
|
2019-07-20 13:42:49 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_private(), []);
|
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), []);
|
2019-11-02 00:06:25 +01:00
|
|
|
const counts = unread.get_counts();
|
2023-09-12 20:58:40 +02:00
|
|
|
assert.equal(counts.direct_message_count, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-22 19:06:04 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("mentions", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let counts = unread.get_counts();
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(counts.mentioned_message_count, 0);
|
2018-05-02 19:54:36 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_mentions(), []);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2023-09-20 13:25:50 +02:00
|
|
|
const muted_stream_id = 900;
|
|
|
|
const unmuted_stream_id = 901;
|
2019-09-27 11:11:40 +02:00
|
|
|
|
2023-09-20 13:25:50 +02:00
|
|
|
sub_store.add_hydrated_sub(muted_stream_id, {
|
|
|
|
muted_stream_id,
|
|
|
|
name: "muted stream for testing unread mentions",
|
|
|
|
subscribed: true,
|
|
|
|
is_muted: true,
|
|
|
|
});
|
|
|
|
sub_store.add_hydrated_sub(unmuted_stream_id, {
|
|
|
|
unmuted_stream_id,
|
|
|
|
name: "unmuted stream for testing unread mention",
|
|
|
|
subscribed: true,
|
|
|
|
is_muted: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
user_topics.update_user_topics(
|
|
|
|
muted_stream_id,
|
|
|
|
"lunch",
|
|
|
|
user_topics.all_visibility_policies.MUTED,
|
|
|
|
);
|
2019-09-27 11:11:40 +02:00
|
|
|
|
2020-04-11 03:13:50 +02:00
|
|
|
const already_read_message = {
|
|
|
|
id: 14,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2023-09-20 13:25:50 +02:00
|
|
|
stream_id: unmuted_stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
2020-04-11 03:13:50 +02:00
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
unread: false,
|
|
|
|
};
|
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const mention_me_message = {
|
2013-07-28 18:40:50 +02:00
|
|
|
id: 15,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2023-09-20 13:25:50 +02:00
|
|
|
stream_id: unmuted_stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
2016-12-03 23:17:57 +01:00
|
|
|
mentioned: true,
|
2019-09-27 11:11:40 +02:00
|
|
|
mentioned_me_directly: true,
|
2018-12-18 19:34:45 +01:00
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
const mention_all_message = {
|
2019-09-27 11:11:40 +02:00
|
|
|
id: 16,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2023-09-20 13:25:50 +02:00
|
|
|
stream_id: unmuted_stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
2019-09-27 11:11:40 +02:00
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2019-09-27 11:11:40 +02:00
|
|
|
|
|
|
|
// This message shouldn't affect the unread mention counts.
|
2020-02-04 02:48:42 +01:00
|
|
|
const muted_mention_all_message = {
|
2019-09-27 11:11:40 +02:00
|
|
|
id: 17,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2019-09-27 11:11:40 +02:00
|
|
|
stream_id: muted_stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
2019-09-27 11:11:40 +02:00
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
unread: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2019-09-27 11:11:40 +02:00
|
|
|
|
2021-08-04 18:46:15 +02:00
|
|
|
const muted_direct_mention_message = {
|
2022-10-25 00:34:47 +02:00
|
|
|
id: 18,
|
2021-08-04 18:46:15 +02:00
|
|
|
type: "stream",
|
|
|
|
stream_id: muted_stream_id,
|
|
|
|
topic: "lunch",
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
2023-05-05 16:28:06 +02:00
|
|
|
const private_mention_me_message = {
|
|
|
|
id: 19,
|
|
|
|
type: "private",
|
|
|
|
display_recipient: [{id: anybody.user_id}],
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
2020-04-11 03:13:50 +02:00
|
|
|
unread.process_loaded_messages([
|
|
|
|
already_read_message,
|
|
|
|
mention_me_message,
|
|
|
|
mention_all_message,
|
|
|
|
muted_mention_all_message,
|
2021-08-04 18:46:15 +02:00
|
|
|
muted_direct_mention_message,
|
2023-05-05 16:28:06 +02:00
|
|
|
private_mention_me_message,
|
2020-04-11 03:13:50 +02:00
|
|
|
]);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
counts = unread.get_counts();
|
2023-05-05 16:28:06 +02:00
|
|
|
assert.equal(counts.mentioned_message_count, 4);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_mentions(), [
|
|
|
|
mention_me_message.id,
|
|
|
|
mention_all_message.id,
|
2021-08-04 18:46:15 +02:00
|
|
|
muted_direct_mention_message.id,
|
2023-05-05 16:28:06 +02:00
|
|
|
private_mention_me_message.id,
|
2020-07-15 00:34:28 +02:00
|
|
|
]);
|
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), [
|
|
|
|
mention_me_message.id,
|
|
|
|
mention_all_message.id,
|
|
|
|
muted_mention_all_message.id,
|
2022-10-25 00:34:47 +02:00
|
|
|
muted_direct_mention_message.id,
|
2023-05-05 16:28:06 +02:00
|
|
|
private_mention_me_message.id,
|
2020-07-15 00:34:28 +02:00
|
|
|
]);
|
2023-09-12 20:30:02 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 4);
|
2019-09-27 11:11:40 +02:00
|
|
|
|
|
|
|
unread.mark_as_read(mention_me_message.id);
|
|
|
|
unread.mark_as_read(mention_all_message.id);
|
2021-08-04 18:46:15 +02:00
|
|
|
unread.mark_as_read(muted_direct_mention_message.id);
|
2023-05-05 16:28:06 +02:00
|
|
|
unread.mark_as_read(private_mention_me_message.id);
|
2013-07-28 18:40:50 +02:00
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.mentioned_message_count, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2021-08-04 18:46:15 +02:00
|
|
|
|
|
|
|
// redundantly read a message to make sure nothing explodes
|
|
|
|
unread.mark_as_read(muted_direct_mention_message.id);
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.mentioned_message_count, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("mention updates", () => {
|
2023-09-20 13:25:50 +02:00
|
|
|
// Unread message in an unmuted stream.
|
2020-04-11 02:51:45 +02:00
|
|
|
const message = {
|
|
|
|
id: 17,
|
2023-09-20 13:25:50 +02:00
|
|
|
stream_id: 901,
|
2020-04-11 02:51:45 +02:00
|
|
|
unread: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2022-10-14 17:37:47 +02:00
|
|
|
topic: "hello",
|
2020-04-11 02:51:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function test_counted(counted) {
|
|
|
|
unread.update_message_for_mention(message);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(unread.unread_mentions_counter.has(message.id), counted);
|
2020-04-11 02:51:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
test_counted(false);
|
|
|
|
|
|
|
|
message.unread = true;
|
|
|
|
message.mentioned = true;
|
|
|
|
test_counted(true);
|
|
|
|
|
|
|
|
message.mentioned = false;
|
|
|
|
test_counted(false);
|
|
|
|
|
|
|
|
message.mentioned = true;
|
|
|
|
test_counted(true);
|
|
|
|
|
|
|
|
message.unread = false;
|
|
|
|
test_counted(false);
|
|
|
|
|
|
|
|
message.unread = true;
|
|
|
|
test_counted(true);
|
|
|
|
});
|
|
|
|
|
2022-08-05 22:45:20 +02:00
|
|
|
test("stream_has_any_unread_mentions", () => {
|
|
|
|
const muted_stream_id = 401;
|
2023-04-11 07:48:13 +02:00
|
|
|
user_topics.update_user_topics(401, "lunch", user_topics.all_visibility_policies.MUTED);
|
2022-08-05 22:45:20 +02:00
|
|
|
|
|
|
|
const mention_me_message = {
|
|
|
|
id: 15,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 400,
|
|
|
|
topic: "lunch",
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const mention_all_message = {
|
|
|
|
id: 16,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 400,
|
|
|
|
topic: "lunch",
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// This message's stream_id should not be present in `streams_with_mentions`.
|
|
|
|
const muted_mention_all_message = {
|
|
|
|
id: 17,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: muted_stream_id,
|
|
|
|
topic: "lunch",
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([
|
|
|
|
mention_me_message,
|
|
|
|
mention_all_message,
|
|
|
|
muted_mention_all_message,
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.equal(unread.stream_has_any_unread_mentions(400), true);
|
|
|
|
assert.equal(unread.stream_has_any_unread_mentions(muted_stream_id), false);
|
|
|
|
});
|
|
|
|
|
2022-08-21 23:05:56 +02:00
|
|
|
test("topics with unread mentions", () => {
|
|
|
|
const message_with_mention = {
|
|
|
|
id: 98,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 999,
|
|
|
|
topic: "topic with mention",
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const message_without_mention = {
|
|
|
|
id: 99,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 999,
|
|
|
|
topic: "topic without mention",
|
|
|
|
mentioned: false,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([message_with_mention, message_without_mention]);
|
|
|
|
assert.equal(unread.get_topics_with_unread_mentions(999).size, 1);
|
|
|
|
assert.deepEqual(unread.get_topics_with_unread_mentions(999), new Set(["topic with mention"]));
|
|
|
|
unread.mark_as_read(message_with_mention.id);
|
|
|
|
assert.equal(unread.get_topics_with_unread_mentions(999).size, 0);
|
|
|
|
assert.deepEqual(unread.get_topics_with_unread_mentions(999), new Set([]));
|
|
|
|
});
|
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("starring", () => {
|
2018-05-03 22:57:07 +02:00
|
|
|
// We don't need any setup here, because we just hard code
|
|
|
|
// this to [] in the code.
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_starred(), []);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-05-03 22:57:07 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("declare_bankruptcy", () => {
|
2020-02-04 02:48:42 +01:00
|
|
|
const message = {
|
2017-07-31 14:30:46 +02:00
|
|
|
id: 16,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "whatever",
|
2017-07-31 14:30:46 +02:00
|
|
|
stream_id: 1999,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "whatever",
|
2017-07-31 14:30:46 +02:00
|
|
|
mentioned: true,
|
2020-02-04 02:48:42 +01:00
|
|
|
};
|
2017-07-31 14:30:46 +02:00
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const counts = unread.get_counts();
|
2020-02-12 11:49:02 +01:00
|
|
|
assert_zero_counts(counts);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("message_unread", () => {
|
2013-08-23 03:20:42 +02:00
|
|
|
// Test some code that might be overly defensive, for line coverage sake.
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!unread.message_unread(undefined));
|
|
|
|
assert.ok(unread.message_unread({unread: true}));
|
|
|
|
assert.ok(!unread.message_unread({unread: false}));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-11-16 00:09:09 +01:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("server_counts", () => {
|
2017-08-01 14:50:40 +02:00
|
|
|
// note that user_id 30 is "me"
|
|
|
|
|
2022-10-27 06:15:43 +02:00
|
|
|
const unread_params = {
|
|
|
|
unread_msgs: {
|
|
|
|
pms: [
|
|
|
|
{
|
|
|
|
other_user_id: 101,
|
|
|
|
// sender_id is deprecated.
|
|
|
|
sender_id: 101,
|
|
|
|
unread_message_ids: [31, 32, 60, 61, 62, 63],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
huddles: [
|
|
|
|
{
|
|
|
|
user_ids_string: "4,6,30,101",
|
|
|
|
unread_message_ids: [34, 50],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
streams: [
|
|
|
|
{
|
|
|
|
stream_id: 1,
|
|
|
|
topic: "test",
|
|
|
|
unread_message_ids: [33, 35, 36],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
mentions: [31, 34, 40, 41],
|
|
|
|
},
|
2017-08-01 14:50:40 +02:00
|
|
|
};
|
|
|
|
|
2022-10-27 06:15:43 +02:00
|
|
|
unread.initialize(unread_params);
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
assert.equal(unread.num_unread_for_user_ids_string("101"), 6);
|
|
|
|
assert.equal(unread.num_unread_for_user_ids_string("4,6,101"), 2);
|
|
|
|
assert.equal(unread.num_unread_for_user_ids_string("30"), 0);
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(unread.num_unread_for_topic(0, "bogus"), 0);
|
|
|
|
assert.equal(unread.num_unread_for_topic(1, "bogus"), 0);
|
|
|
|
assert.equal(unread.num_unread_for_topic(1, "test"), 3);
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2020-02-01 03:13:57 +01:00
|
|
|
assert.equal(unread.unread_mentions_counter.size, 4);
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
unread.mark_as_read(40);
|
2020-02-01 03:13:57 +01:00
|
|
|
assert.equal(unread.unread_mentions_counter.size, 3);
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
unread.mark_as_read(35);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(unread.num_unread_for_topic(1, "test"), 2);
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
unread.mark_as_read(34);
|
2022-10-22 07:15:44 +02:00
|
|
|
assert.equal(unread.num_unread_for_user_ids_string("4,6,101"), 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("empty_cases", () => {
|
2018-04-25 22:48:51 +02:00
|
|
|
const stream_id = 999;
|
2020-07-15 01:29:15 +02:00
|
|
|
let msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");
|
2018-05-01 21:22:49 +02:00
|
|
|
assert.deepEqual(msg_ids, []);
|
|
|
|
|
|
|
|
msg_ids = unread.get_msg_ids_for_stream(stream_id);
|
2018-04-25 22:48:51 +02:00
|
|
|
assert.deepEqual(msg_ids, []);
|
2018-05-10 13:11:53 +02:00
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), []);
|
2018-05-13 11:38:40 +02:00
|
|
|
|
|
|
|
const missing_topics = unread.get_missing_topics({
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic_dict: "should-never-be-referenced",
|
2018-05-13 11:38:40 +02:00
|
|
|
});
|
|
|
|
assert.deepEqual(missing_topics, []);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-25 22:48:51 +02:00
|
|
|
|
2021-03-09 15:18:57 +01:00
|
|
|
test("errors", () => {
|
2016-11-16 00:09:09 +01:00
|
|
|
// Test unknown message leads to zero count
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = {
|
2016-11-16 00:09:09 +01:00
|
|
|
id: 9,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2017-02-14 01:15:26 +01:00
|
|
|
display_recipient: [{id: 9999}],
|
2016-11-16 00:09:09 +01:00
|
|
|
};
|
|
|
|
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2019-11-02 00:06:25 +01:00
|
|
|
const counts = unread.get_counts();
|
2023-09-12 20:58:40 +02:00
|
|
|
assert.equal(counts.direct_message_count, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
test_notifiable_count(counts.home_unread_messages, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|