2019-07-20 14:45:56 +02:00
|
|
|
set_global('i18n', global.stub_i18n);
|
|
|
|
|
2017-11-08 20:04:00 +01:00
|
|
|
zrequire('muting');
|
|
|
|
zrequire('people');
|
|
|
|
zrequire('stream_data');
|
2018-04-25 22:48:51 +02:00
|
|
|
zrequire('util');
|
2017-11-08 20:04:00 +01:00
|
|
|
zrequire('unread');
|
2019-07-20 14:45:56 +02:00
|
|
|
zrequire('settings_notifications');
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2017-08-01 14:50:40 +02:00
|
|
|
set_global('page_params', {});
|
2019-07-20 14:45:56 +02:00
|
|
|
set_global('blueslip', {});
|
2017-11-08 20:04:00 +01:00
|
|
|
set_global('narrow_state', {});
|
|
|
|
set_global('current_msg_list', {});
|
|
|
|
set_global('home_msg_list', {});
|
2017-12-16 21:42:41 +01:00
|
|
|
set_global('message_store', {});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2017-02-14 01:15:26 +01:00
|
|
|
var me = {
|
|
|
|
email: 'me@example.com',
|
|
|
|
user_id: 30,
|
|
|
|
full_name: 'Me Myself',
|
|
|
|
};
|
|
|
|
people.add(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
var social = {
|
|
|
|
stream_id: 200,
|
|
|
|
name: 'social',
|
|
|
|
subscribed: true,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: false,
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
|
|
|
stream_data.add_sub('social', social);
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
var zero_counts = {
|
|
|
|
private_message_count: 0,
|
|
|
|
home_unread_messages: 0,
|
|
|
|
mentioned_message_count: 0,
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_count: new Dict(),
|
2017-07-31 14:04:20 +02:00
|
|
|
topic_count: new Dict(),
|
2013-08-07 23:56:51 +02:00
|
|
|
pm_count: new Dict(),
|
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) {
|
|
|
|
set_global('page_params', {
|
|
|
|
desktop_icon_count_display: 1,
|
|
|
|
});
|
|
|
|
var notifiable_counts = unread.get_notifiable_count();
|
|
|
|
assert.deepEqual(notifiable_counts, home_unread_messages);
|
|
|
|
set_global('page_params', {
|
|
|
|
desktop_icon_count_display: 2,
|
|
|
|
});
|
|
|
|
notifiable_counts = unread.get_notifiable_count();
|
|
|
|
assert.deepEqual(notifiable_counts, expected_notifiable_count);
|
2019-08-19 23:33:11 +02:00
|
|
|
set_global('page_params', {
|
|
|
|
desktop_icon_count_display: 3,
|
|
|
|
});
|
|
|
|
notifiable_counts = unread.get_notifiable_count();
|
|
|
|
assert.deepEqual(notifiable_counts, 0);
|
2019-07-20 14:45:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('empty_counts_while_narrowed', () => {
|
2013-07-28 18:40:50 +02:00
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.deepEqual(counts, zero_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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('empty_counts_while_home', () => {
|
2013-07-28 18:40:50 +02:00
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.deepEqual(counts, zero_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
|
|
|
|
2018-12-23 16:49:14 +01:00
|
|
|
run_test('changing_topics', () => {
|
|
|
|
// Summary: change the topic of a message from 'lunch'
|
2016-08-27 03:29:32 +02:00
|
|
|
// to 'dinner' using update_unread_topics().
|
2017-07-31 14:11:18 +02:00
|
|
|
var count = unread.num_unread_for_topic('social', 'lunch');
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, 0);
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
var stream_id = 100;
|
2017-05-24 02:36:49 +02:00
|
|
|
var wrong_stream_id = 110;
|
2017-05-13 19:26:54 +02:00
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_id: stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'luNch',
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
2013-10-15 19:32:29 +02:00
|
|
|
var other_message = {
|
|
|
|
id: 16,
|
|
|
|
type: 'stream',
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_id: stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'lunCH',
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2013-10-15 19:32:29 +02:00
|
|
|
};
|
|
|
|
|
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]), []);
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
var msg_ids = unread.get_msg_ids_for_topic(stream_id, 'LuNcH');
|
|
|
|
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]);
|
2017-12-16 16:53:27 +01:00
|
|
|
assert.deepEqual(unread.get_unread_message_ids([15, 16]), [15, 16]);
|
|
|
|
assert.deepEqual(
|
|
|
|
unread.get_unread_messages([message, other_message]),
|
|
|
|
[message, other_message]
|
|
|
|
);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2017-07-31 14:11:18 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, 'Lunch');
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 2);
|
2017-05-13 19:26:54 +02:00
|
|
|
assert(unread.topic_has_any_unread(stream_id, 'lunch'));
|
2017-05-24 02:36:49 +02:00
|
|
|
assert(!unread.topic_has_any_unread(wrong_stream_id, 'lunch'));
|
2017-08-02 21:08:19 +02:00
|
|
|
assert(!unread.topic_has_any_unread(stream_id, 'NOT lunch'));
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, 'NOT lunch');
|
|
|
|
assert.equal(count, 0);
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
msg_ids = unread.get_msg_ids_for_topic(stream_id, 'NOT lunch');
|
|
|
|
assert.deepEqual(msg_ids, []);
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
var event = {
|
2018-12-23 18:08:37 +01: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
|
|
|
|
2017-07-31 14:11:18 +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
|
|
|
|
2017-07-31 14:11:18 +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 = {
|
2018-12-23 18:08:37 +01: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
|
|
|
|
2017-07-31 14:11:18 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, 'lunch');
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 0);
|
2017-05-13 19:26:54 +02:00
|
|
|
assert(!unread.topic_has_any_unread(stream_id, 'lunch'));
|
2017-05-24 02:36:49 +02:00
|
|
|
assert(!unread.topic_has_any_unread(wrong_stream_id, 'lunch'));
|
2013-10-15 19:32:29 +02:00
|
|
|
|
2017-07-31 14:11:18 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, 'snack');
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 1);
|
2017-05-13 19:26:54 +02:00
|
|
|
assert(unread.topic_has_any_unread(stream_id, 'snack'));
|
2017-05-24 02:36:49 +02:00
|
|
|
assert(!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 = {
|
2018-12-23 18:08:37 +01: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.
|
|
|
|
var sticky_message = {
|
|
|
|
id: 17,
|
|
|
|
type: 'stream',
|
|
|
|
stream_id: stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'sticky',
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var message_dict = new Dict();
|
|
|
|
message_dict.set(message.id, message);
|
|
|
|
message_dict.set(other_message.id, other_message);
|
|
|
|
message_dict.set(sticky_message.id, sticky_message);
|
|
|
|
|
|
|
|
message_store.get = function (msg_id) {
|
|
|
|
return message_dict.get(msg_id);
|
2017-08-10 20:27:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([sticky_message]);
|
|
|
|
count = unread.num_unread_for_topic(stream_id, 'sticky');
|
|
|
|
assert.equal(count, 1);
|
2017-12-16 21:42:41 +01:00
|
|
|
assert(sticky_message.unread);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
|
|
|
unread.mark_as_read(sticky_message.id);
|
|
|
|
count = unread.num_unread_for_topic(stream_id, 'sticky');
|
|
|
|
assert.equal(count, 0);
|
2017-12-16 21:42:41 +01:00
|
|
|
assert(!sticky_message.unread);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
2018-12-23 18:08:37 +01:00
|
|
|
event = {
|
|
|
|
topic: 'sticky',
|
|
|
|
};
|
|
|
|
|
|
|
|
unread.update_unread_topics(sticky_message, event);
|
2017-08-10 20:27:23 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, 'sticky');
|
|
|
|
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);
|
2017-07-31 14:11:18 +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);
|
2017-07-31 14:11:18 +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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('muting', () => {
|
2013-09-28 00:00:44 +02:00
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
var stream_id = social.stream_id;
|
2017-05-13 19:26:54 +02:00
|
|
|
var unknown_stream_id = 555;
|
|
|
|
|
2013-09-28 00:00:44 +02:00
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_id: stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'test_muting',
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2013-09-28 00:00:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
var counts = unread.get_counts();
|
2017-05-13 19:26:54 +02:00
|
|
|
assert.equal(counts.stream_count.get(stream_id), 1);
|
2013-09-28 00:00:44 +02:00
|
|
|
assert.equal(counts.home_unread_messages, 1);
|
2017-05-13 19:26:54 +02:00
|
|
|
assert.equal(unread.num_unread_for_stream(stream_id), 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
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
muting.add_muted_topic(social.stream_id, 'test_muting');
|
2013-09-28 00:00:44 +02:00
|
|
|
counts = unread.get_counts();
|
2017-05-13 19:26:54 +02:00
|
|
|
assert.equal(counts.stream_count.get(stream_id), 0);
|
2013-09-28 00:00:44 +02:00
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
2017-05-13 19:26:54 +02:00
|
|
|
assert.equal(unread.num_unread_for_stream(stream_id), 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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_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.
|
2013-09-28 00:00:44 +02:00
|
|
|
unread.declare_bankruptcy();
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
var stream_id = 301;
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
stream_data.get_sub_by_id = function (arg) {
|
|
|
|
if (arg === stream_id) {
|
|
|
|
return {name: 'Some Stream'};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-31 14:11:18 +02:00
|
|
|
var count = unread.num_unread_for_topic(stream_id, 'lunch');
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, 0);
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
type: 'stream',
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_id: stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'LuncH',
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
// Put messages into list in reverse order to try to confuse
|
|
|
|
// our sort.
|
2017-04-03 22:07:10 +02:00
|
|
|
var num_msgs = 500;
|
2013-07-28 18:40:50 +02:00
|
|
|
var i;
|
2018-04-25 22:48:51 +02:00
|
|
|
for (i = num_msgs; i > 0; i -= 1) {
|
|
|
|
message.id = i;
|
2013-07-28 18:40:50 +02:00
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
}
|
|
|
|
|
2017-07-31 14:11:18 +02:00
|
|
|
count = unread.num_unread_for_topic(stream_id, 'lunch');
|
2013-07-28 18:40:50 +02:00
|
|
|
assert.equal(count, num_msgs);
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
var msg_ids = unread.get_msg_ids_for_topic(stream_id, 'LuNcH');
|
|
|
|
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));
|
|
|
|
|
2018-05-13 11:38:40 +02:00
|
|
|
var topic_dict = new Dict({fold_case: true});
|
|
|
|
|
|
|
|
var missing_topics = unread.get_missing_topics({
|
|
|
|
stream_id: stream_id,
|
|
|
|
topic_dict: topic_dict,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(missing_topics, [
|
|
|
|
{ pretty_name: 'LuncH', message_id: 500 },
|
|
|
|
]);
|
|
|
|
|
|
|
|
topic_dict.set('lUNCh', 'whatever');
|
|
|
|
|
|
|
|
missing_topics = unread.get_missing_topics({
|
|
|
|
stream_id: stream_id,
|
|
|
|
topic_dict: topic_dict,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(missing_topics, []);
|
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < num_msgs; i += 1) {
|
2018-06-04 21:13:07 +02:00
|
|
|
message.id = i + 1;
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2013-07-28 18:40:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-31 14:11:18 +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
|
|
|
|
|
|
|
msg_ids = unread.get_msg_ids_for_topic(stream_id, 'LuNcH');
|
|
|
|
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
|
|
|
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('home_messages', () => {
|
2013-08-15 21:11:07 +02:00
|
|
|
stream_data.is_subscribed = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return true;
|
|
|
|
};
|
2019-05-21 09:33:21 +02:00
|
|
|
stream_data.is_muted = function () {
|
|
|
|
return false;
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
var stream_id = 401;
|
|
|
|
|
|
|
|
stream_data.get_sub_by_id = function () {
|
|
|
|
return {
|
|
|
|
name: 'whatever',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_id: stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'lunch',
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var 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-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 1);
|
2017-05-13 19:26:54 +02:00
|
|
|
assert.equal(counts.stream_count.get(stream_id), 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.
|
|
|
|
stream_data.is_subscribed = function () {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('phantom_messages', () => {
|
2013-09-28 23:09:29 +02:00
|
|
|
var message = {
|
|
|
|
id: 999,
|
|
|
|
type: 'stream',
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_id: 555,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'phantom',
|
2013-09-28 23:09:29 +02:00
|
|
|
};
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
stream_data.get_sub_by_id = function () { return; };
|
|
|
|
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2013-09-28 23:09:29 +02:00
|
|
|
var 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-09-28 23:09:29 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('private_messages', () => {
|
2013-07-28 18:40:50 +02:00
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.private_message_count, 0);
|
|
|
|
|
2016-11-16 00:09:09 +01:00
|
|
|
var anybody = {
|
|
|
|
email: 'anybody@example.com',
|
|
|
|
user_id: 999,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Any Body',
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
2016-11-16 00:09:09 +01:00
|
|
|
people.add_in_realm(anybody);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2017-02-14 01:15:26 +01:00
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'private',
|
|
|
|
display_recipient: [
|
|
|
|
{user_id: anybody.user_id},
|
|
|
|
{id: me.user_id},
|
|
|
|
],
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
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();
|
|
|
|
assert.equal(counts.private_message_count, 1);
|
2016-11-16 00:09:09 +01: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();
|
|
|
|
assert.equal(counts.private_message_count, 0);
|
2016-11-16 00:09:09 +01: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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('private_messages', () => {
|
2016-11-16 00:09:09 +01:00
|
|
|
var alice = {
|
|
|
|
email: 'alice@example.com',
|
|
|
|
user_id: 101,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Alice',
|
2016-11-16 00:09:09 +01:00
|
|
|
};
|
|
|
|
people.add_in_realm(alice);
|
2013-09-10 23:33:46 +02:00
|
|
|
|
2016-11-16 00:09:09 +01:00
|
|
|
var bob = {
|
|
|
|
email: 'bob@example.com',
|
|
|
|
user_id: 102,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Bob',
|
2016-11-16 00:09:09 +01:00
|
|
|
};
|
|
|
|
people.add_in_realm(bob);
|
|
|
|
|
2016-11-18 17:02:06 +01:00
|
|
|
assert.equal(unread.num_unread_for_person(alice.user_id), 0);
|
|
|
|
assert.equal(unread.num_unread_for_person(bob.user_id), 0);
|
2018-05-02 13:02:32 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_person(alice.user_id), []);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_person(bob.user_id), []);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_person(), []);
|
2018-05-02 19:50:25 +02:00
|
|
|
assert.deepEqual(unread.get_msg_ids_for_private(), []);
|
2013-08-22 19:06:04 +02:00
|
|
|
|
|
|
|
var message = {
|
|
|
|
id: 15,
|
2017-02-14 01:15:26 +01:00
|
|
|
display_recipient: [{id: alice.user_id}],
|
2016-12-03 23:17:57 +01:00
|
|
|
type: 'private',
|
2017-12-16 21:42:41 +01:00
|
|
|
unread: true,
|
2013-08-22 19:06:04 +02:00
|
|
|
};
|
2013-08-23 03:23:08 +02:00
|
|
|
|
|
|
|
var read_message = {
|
2016-12-03 23:17:57 +01:00
|
|
|
flags: ['read'],
|
2013-08-23 03:23:08 +02:00
|
|
|
};
|
|
|
|
unread.process_loaded_messages([message, read_message]);
|
2016-11-18 17:02:06 +01:00
|
|
|
assert.equal(unread.num_unread_for_person(alice.user_id), 1);
|
2017-04-03 22:14:46 +02:00
|
|
|
|
|
|
|
assert.equal(unread.num_unread_for_person(''), 0);
|
2018-05-02 13:02:32 +02:00
|
|
|
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_person(alice.user_id), [message.id]);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_person(bob.user_id), []);
|
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);
|
|
|
|
assert.equal(unread.num_unread_for_person(alice.user_id), 0);
|
|
|
|
assert.equal(unread.num_unread_for_person(''), 0);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_person(alice.user_id), []);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_person(bob.user_id), []);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_private(), []);
|
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), []);
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.private_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
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('mentions', () => {
|
2013-07-28 18:40:50 +02:00
|
|
|
var counts = unread.get_counts();
|
|
|
|
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
|
|
|
|
2019-09-27 11:11:40 +02:00
|
|
|
const muted_stream_id = 401;
|
|
|
|
|
|
|
|
muting.add_muted_topic(401, 'lunch');
|
|
|
|
|
|
|
|
const mention_me_message = {
|
2013-07-28 18:40:50 +02:00
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
2019-09-27 11:11:40 +02:00
|
|
|
stream_id: 400,
|
2018-12-23 16:49:14 +01: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,
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
2019-09-27 11:11:40 +02:00
|
|
|
const mention_all_message = {
|
|
|
|
id: 16,
|
|
|
|
type: 'stream',
|
|
|
|
stream_id: 400,
|
|
|
|
topic: 'lunch',
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
unread: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// This message shouldn't affect the unread mention counts.
|
|
|
|
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]);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
counts = unread.get_counts();
|
2019-09-27 11:11:40 +02:00
|
|
|
assert.equal(counts.mentioned_message_count, 2);
|
|
|
|
assert.deepEqual(unread.get_msg_ids_for_mentions(), [mention_me_message.id,
|
|
|
|
mention_all_message.id]);
|
|
|
|
assert.deepEqual(unread.get_all_msg_ids(), [mention_me_message.id,
|
|
|
|
mention_all_message.id,
|
|
|
|
muted_mention_all_message.id]);
|
|
|
|
test_notifiable_count(counts.home_unread_messages, 2);
|
|
|
|
|
|
|
|
unread.mark_as_read(mention_me_message.id);
|
|
|
|
unread.mark_as_read(mention_all_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);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('declare_bankruptcy', () => {
|
2017-07-31 14:30:46 +02:00
|
|
|
var message = {
|
|
|
|
id: 16,
|
|
|
|
type: 'whatever',
|
|
|
|
stream_id: 1999,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'whatever',
|
2017-07-31 14:30:46 +02:00
|
|
|
mentioned: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.deepEqual(counts, zero_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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('message_unread', () => {
|
2013-08-23 03:20:42 +02:00
|
|
|
// Test some code that might be overly defensive, for line coverage sake.
|
|
|
|
assert(!unread.message_unread(undefined));
|
2017-12-16 21:42:41 +01:00
|
|
|
assert(unread.message_unread({unread: true}));
|
|
|
|
assert(!unread.message_unread({unread: false}));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-11-16 00:09:09 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('server_counts', () => {
|
2017-08-01 14:50:40 +02:00
|
|
|
// note that user_id 30 is "me"
|
|
|
|
|
|
|
|
page_params.unread_msgs = {
|
|
|
|
pms: [
|
|
|
|
{
|
|
|
|
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],
|
|
|
|
};
|
|
|
|
|
|
|
|
unread.declare_bankruptcy();
|
|
|
|
unread.initialize();
|
|
|
|
|
|
|
|
assert.equal(unread.num_unread_for_person('101'), 6);
|
|
|
|
assert.equal(unread.num_unread_for_person('4,6,101'), 2);
|
|
|
|
assert.equal(unread.num_unread_for_person('30'), 0);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
assert.equal(unread.unread_mentions_counter.count(), 4);
|
|
|
|
|
|
|
|
unread.mark_as_read(40);
|
|
|
|
assert.equal(unread.unread_mentions_counter.count(), 3);
|
|
|
|
|
|
|
|
unread.mark_as_read(35);
|
|
|
|
assert.equal(unread.num_unread_for_topic(1, 'test'), 2);
|
|
|
|
|
|
|
|
unread.mark_as_read(34);
|
|
|
|
assert.equal(unread.num_unread_for_person('4,6,101'), 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('empty_cases', () => {
|
2018-04-25 22:48:51 +02:00
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
|
|
|
const stream_id = 999;
|
2018-05-01 21:22:49 +02:00
|
|
|
var msg_ids = unread.get_msg_ids_for_topic(stream_id, 'LuNcH');
|
|
|
|
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({
|
|
|
|
stream_id: stream_id,
|
|
|
|
topic_dict: 'should-never-be-referenced',
|
|
|
|
});
|
|
|
|
assert.deepEqual(missing_topics, []);
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-25 22:48:51 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('errors', () => {
|
2017-08-01 14:50:40 +02:00
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
2016-11-16 00:09:09 +01:00
|
|
|
// Test unknown message leads to zero count
|
|
|
|
var message = {
|
|
|
|
id: 9,
|
|
|
|
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);
|
2016-11-16 00:09:09 +01:00
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.private_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
|
|
|
});
|