2013-07-28 18:40:50 +02:00
|
|
|
// Unit test the unread.js module, which depends on these global variables:
|
|
|
|
//
|
|
|
|
// _, narrow, current_msg_list, home_msg_list, subs
|
|
|
|
//
|
|
|
|
// These tests are framework-free and run sequentially; they are invoked
|
|
|
|
// immediately after being defined. The contract here is that tests should
|
|
|
|
// clean up after themselves, and they should explicitly stub all
|
|
|
|
// dependencies (except _).
|
|
|
|
|
2016-11-16 00:09:09 +01:00
|
|
|
global.stub_out_jquery();
|
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
add_dependencies({
|
2016-03-24 13:51:14 +01:00
|
|
|
muting: 'js/muting.js',
|
2016-11-16 00:09:09 +01:00
|
|
|
people: 'js/people.js',
|
2016-12-03 23:17:57 +01:00
|
|
|
unread: 'js/unread.js',
|
2013-08-21 20:27:14 +02:00
|
|
|
});
|
|
|
|
|
2013-08-21 23:53:37 +02:00
|
|
|
var stream_data = require('js/stream_data.js');
|
|
|
|
|
|
|
|
stream_data = {
|
2016-12-03 23:17:57 +01:00
|
|
|
canonicalized_name: stream_data.canonicalized_name,
|
2013-08-21 23:53:37 +02:00
|
|
|
};
|
|
|
|
set_global('stream_data', stream_data);
|
2016-11-16 00:09:09 +01:00
|
|
|
set_global('blueslip', {});
|
2013-08-21 23:53:37 +02:00
|
|
|
|
2013-08-07 23:56:51 +02:00
|
|
|
var Dict = global.Dict;
|
2013-09-28 00:00:44 +02:00
|
|
|
var muting = global.muting;
|
2016-11-16 00:09:09 +01:00
|
|
|
var people = global.people;
|
|
|
|
|
2013-08-01 17:53:10 +02:00
|
|
|
var unread = require('js/unread.js');
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
var narrow = {};
|
|
|
|
global.narrow = narrow;
|
|
|
|
|
|
|
|
var current_msg_list = {};
|
|
|
|
global.current_msg_list = current_msg_list;
|
|
|
|
|
|
|
|
var home_msg_list = {};
|
|
|
|
global.home_msg_list = home_msg_list;
|
|
|
|
|
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);
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
var zero_counts = {
|
|
|
|
private_message_count: 0,
|
|
|
|
home_unread_messages: 0,
|
|
|
|
mentioned_message_count: 0,
|
2016-11-30 03:20:29 +01:00
|
|
|
stream_count: new Dict({fold_case: true}),
|
|
|
|
subject_count: new Dict({fold_case: true}),
|
2013-08-07 23:56:51 +02:00
|
|
|
pm_count: new Dict(),
|
2016-12-03 23:17:57 +01:00
|
|
|
unread_in_current_view: 0,
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
(function test_empty_counts_while_narrowed() {
|
|
|
|
narrow.active = function () {
|
|
|
|
return true;
|
|
|
|
};
|
2016-04-23 00:56:44 +02:00
|
|
|
current_msg_list.all_messages = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.deepEqual(counts, zero_counts);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_empty_counts_while_home() {
|
|
|
|
narrow.active = function () {
|
|
|
|
return false;
|
|
|
|
};
|
2016-04-23 00:56:44 +02:00
|
|
|
current_msg_list.all_messages = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.deepEqual(counts, zero_counts);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_changing_subjects() {
|
|
|
|
// Summary: change the subject of a message from 'lunch'
|
2016-08-27 03:29:32 +02:00
|
|
|
// to 'dinner' using update_unread_topics().
|
2013-07-28 18:40:50 +02:00
|
|
|
var count = unread.num_unread_for_subject('social', 'lunch');
|
|
|
|
assert.equal(count, 0);
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'social',
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'lunch',
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
2013-10-15 19:32:29 +02:00
|
|
|
var other_message = {
|
|
|
|
id: 16,
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'social',
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'lunch',
|
2013-10-15 19:32:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([message, other_message]);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
2013-08-23 04:49:56 +02:00
|
|
|
count = unread.num_unread_for_subject('Social', 'lunch');
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 2);
|
2017-04-21 18:00:19 +02:00
|
|
|
assert(unread.topic_has_any_unread('Social', 'lunch'));
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
var event = {
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: '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
|
|
|
|
|
|
|
count = unread.num_unread_for_subject('social', 'lunch');
|
2013-10-15 19:32:29 +02:00
|
|
|
assert.equal(count, 1);
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
count = unread.num_unread_for_subject('social', 'dinner');
|
|
|
|
assert.equal(count, 1);
|
|
|
|
|
2013-10-15 19:32:29 +02:00
|
|
|
event = {
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: '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
|
|
|
|
|
|
|
count = unread.num_unread_for_subject('social', 'lunch');
|
|
|
|
assert.equal(count, 0);
|
2017-04-21 18:00:19 +02:00
|
|
|
assert(!unread.topic_has_any_unread('social', 'lunch'));
|
2013-10-15 19:32:29 +02:00
|
|
|
|
|
|
|
count = unread.num_unread_for_subject('social', 'snack');
|
|
|
|
assert.equal(count, 1);
|
2017-04-21 18:00:19 +02:00
|
|
|
assert(unread.topic_has_any_unread('social', '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 = {
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: '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
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
// cleanup
|
|
|
|
message.subject = 'dinner';
|
|
|
|
unread.process_read_message(message);
|
|
|
|
count = unread.num_unread_for_subject('social', 'dinner');
|
|
|
|
assert.equal(count, 0);
|
2013-10-15 19:32:29 +02:00
|
|
|
|
|
|
|
other_message.subject = 'snack';
|
|
|
|
unread.process_read_message(other_message);
|
|
|
|
count = unread.num_unread_for_subject('social', 'snack');
|
|
|
|
assert.equal(count, 0);
|
2013-07-28 18:40:50 +02:00
|
|
|
}());
|
|
|
|
|
2013-09-28 00:00:44 +02:00
|
|
|
(function test_muting() {
|
|
|
|
stream_data.is_subscribed = function () {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.in_home_view = function () {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'social',
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'test_muting',
|
2013-09-28 00:00:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.stream_count.get('social'), 1);
|
|
|
|
assert.equal(counts.home_unread_messages, 1);
|
2017-01-15 16:44:33 +01:00
|
|
|
assert.equal(unread.num_unread_for_stream('social'), 1);
|
2013-09-28 00:00:44 +02:00
|
|
|
|
2017-02-11 09:17:19 +01:00
|
|
|
muting.add_muted_topic('social', 'test_muting');
|
2013-09-28 00:00:44 +02:00
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.stream_count.get('social'), 0);
|
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
2017-01-15 16:44:33 +01:00
|
|
|
assert.equal(unread.num_unread_for_stream('social'), 0);
|
2017-04-03 22:14:46 +02:00
|
|
|
|
|
|
|
assert.equal(unread.num_unread_for_stream('unknown'), 0);
|
2013-09-28 00:00:44 +02:00
|
|
|
}());
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
(function test_num_unread_for_subject() {
|
|
|
|
// Test the num_unread_for_subject() function using many
|
|
|
|
// messages.
|
2013-09-28 00:00:44 +02:00
|
|
|
unread.declare_bankruptcy();
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
var count = unread.num_unread_for_subject('social', 'lunch');
|
|
|
|
assert.equal(count, 0);
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'social',
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'lunch',
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
2017-04-03 22:07:10 +02:00
|
|
|
var num_msgs = 500;
|
2013-07-28 18:40:50 +02:00
|
|
|
var i;
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < num_msgs; i += 1) {
|
2013-07-28 18:40:50 +02:00
|
|
|
message.id = i+1;
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
}
|
|
|
|
|
|
|
|
count = unread.num_unread_for_subject('social', 'lunch');
|
|
|
|
assert.equal(count, num_msgs);
|
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < num_msgs; i += 1) {
|
2013-07-28 18:40:50 +02:00
|
|
|
message.id = i+1;
|
|
|
|
unread.process_read_message(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
count = unread.num_unread_for_subject('social', 'lunch');
|
|
|
|
assert.equal(count, 0);
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
(function test_home_messages() {
|
|
|
|
narrow.active = function () {
|
|
|
|
return false;
|
|
|
|
};
|
2013-08-15 21:11:07 +02:00
|
|
|
stream_data.is_subscribed = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return true;
|
|
|
|
};
|
2013-08-15 21:11:07 +02:00
|
|
|
stream_data.in_home_view = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'social',
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'lunch',
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
home_msg_list.get = function (msg_id) {
|
|
|
|
return (msg_id === '15') ? message : undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 1);
|
2013-08-23 04:56:11 +02:00
|
|
|
assert.equal(counts.stream_count.get('social'), 1);
|
2013-07-28 18:40:50 +02:00
|
|
|
unread.process_read_message(message);
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(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);
|
|
|
|
|
|
|
|
// Now unsubscribe all our streams.
|
|
|
|
stream_data.is_subscribed = function () {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
}());
|
|
|
|
|
2013-09-28 23:09:29 +02:00
|
|
|
(function test_phantom_messages() {
|
|
|
|
var message = {
|
|
|
|
id: 999,
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'foo',
|
2016-12-03 23:17:57 +01:00
|
|
|
subject: 'phantom',
|
2013-09-28 23:09:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_read_message(message);
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.home_unread_messages, 0);
|
|
|
|
}());
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
(function test_private_messages() {
|
|
|
|
narrow.active = function () {
|
|
|
|
return false;
|
|
|
|
};
|
2013-08-15 21:11:07 +02:00
|
|
|
stream_data.is_subscribed = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
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},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
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);
|
2013-07-28 18:40:50 +02:00
|
|
|
unread.process_read_message(message);
|
|
|
|
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);
|
2013-07-28 18:40:50 +02:00
|
|
|
}());
|
|
|
|
|
2013-08-22 19:06:04 +02:00
|
|
|
(function test_num_unread_for_person() {
|
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);
|
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',
|
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);
|
2013-08-22 19:06:04 +02:00
|
|
|
}());
|
|
|
|
|
2013-07-28 18:40:50 +02:00
|
|
|
|
|
|
|
(function test_mentions() {
|
|
|
|
narrow.active = function () {
|
|
|
|
return false;
|
|
|
|
};
|
2013-08-15 21:11:07 +02:00
|
|
|
stream_data.is_subscribed = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.mentioned_message_count, 0);
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
id: 15,
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'social',
|
|
|
|
subject: 'lunch',
|
2016-12-03 23:17:57 +01:00
|
|
|
mentioned: true,
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_loaded_messages([message]);
|
|
|
|
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.mentioned_message_count, 1);
|
|
|
|
unread.process_read_message(message);
|
|
|
|
counts = unread.get_counts();
|
|
|
|
assert.equal(counts.mentioned_message_count, 0);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_declare_bankruptcy() {
|
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.deepEqual(counts, zero_counts);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_num_unread_current_messages() {
|
|
|
|
var count = unread.num_unread_current_messages();
|
|
|
|
assert.equal(count, 0);
|
|
|
|
|
|
|
|
var message = {
|
2016-12-03 23:17:57 +01:00
|
|
|
id: 15,
|
2013-07-28 18:40:50 +02:00
|
|
|
};
|
2016-04-23 00:56:44 +02:00
|
|
|
current_msg_list.all_messages = function () {
|
2013-07-28 18:40:50 +02:00
|
|
|
return [message];
|
|
|
|
};
|
|
|
|
|
|
|
|
// It's a little suspicious that num_unread_current_messages()
|
|
|
|
// is using the pointer as a hint for filtering out unread
|
|
|
|
// messages, but right now, it's impossible for unread messages
|
|
|
|
// to be above the pointer in a narrowed view, so unread.js uses
|
|
|
|
// this for optimization purposes.
|
|
|
|
current_msg_list.selected_id = function () {
|
|
|
|
return 11; // less than our message's id
|
|
|
|
};
|
|
|
|
|
|
|
|
count = unread.num_unread_current_messages();
|
|
|
|
assert.equal(count, 1);
|
|
|
|
}());
|
|
|
|
|
2013-08-23 03:20:42 +02:00
|
|
|
|
|
|
|
(function test_message_unread() {
|
|
|
|
// Test some code that might be overly defensive, for line coverage sake.
|
|
|
|
assert(!unread.message_unread(undefined));
|
|
|
|
assert(unread.message_unread({flags: []}));
|
|
|
|
assert(!unread.message_unread({flags: ['read']}));
|
|
|
|
}());
|
2016-11-16 00:09:09 +01:00
|
|
|
|
|
|
|
(function test_errors() {
|
2017-01-19 04:13:27 +01:00
|
|
|
global.blueslip.warn = function () {};
|
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
|
|
|
};
|
|
|
|
|
|
|
|
unread.process_read_message(message);
|
|
|
|
var counts = unread.get_counts();
|
|
|
|
assert.equal(counts.private_message_count, 0);
|
|
|
|
}());
|
|
|
|
|