2017-11-08 18:20:18 +01:00
|
|
|
zrequire('pm_conversations');
|
|
|
|
zrequire('util');
|
|
|
|
zrequire('people');
|
|
|
|
zrequire('message_store');
|
2016-11-18 19:08:41 +01:00
|
|
|
|
|
|
|
var noop = function () {};
|
|
|
|
var people = global.people;
|
|
|
|
|
2017-06-20 03:20:02 +02:00
|
|
|
set_global('$', global.make_zjquery());
|
2017-07-08 15:16:19 +02:00
|
|
|
set_global('document', 'document-stub');
|
2017-06-20 03:20:02 +02:00
|
|
|
|
2016-11-18 19:08:41 +01:00
|
|
|
set_global('alert_words', {
|
2016-12-03 23:17:57 +01:00
|
|
|
process_message: noop,
|
2016-11-18 19:08:41 +01:00
|
|
|
});
|
|
|
|
|
2017-07-24 18:22:37 +02:00
|
|
|
set_global('topic_data' , {
|
2017-07-26 14:05:25 +02:00
|
|
|
add_message: noop,
|
2017-06-20 00:40:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
set_global('recent_senders', {
|
|
|
|
process_message_for_senders: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
set_global('page_params', {
|
|
|
|
realm_allow_message_editing: true,
|
|
|
|
is_admin: true,
|
|
|
|
});
|
|
|
|
|
2018-05-01 01:16:02 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2017-06-20 00:40:43 +02:00
|
|
|
|
2016-11-18 19:08:41 +01:00
|
|
|
var me = {
|
|
|
|
email: 'me@example.com',
|
|
|
|
user_id: 101,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Me Myself',
|
2016-11-18 19:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var alice = {
|
|
|
|
email: 'alice@example.com',
|
|
|
|
user_id: 102,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Alice',
|
2016-11-18 19:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var bob = {
|
|
|
|
email: 'bob@example.com',
|
|
|
|
user_id: 103,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Bob',
|
2016-11-18 19:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var cindy = {
|
|
|
|
email: 'cindy@example.com',
|
|
|
|
user_id: 104,
|
2016-12-03 23:17:57 +01:00
|
|
|
full_name: 'Cindy',
|
2016-11-18 19:08:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
people.add_in_realm(me);
|
|
|
|
people.add_in_realm(alice);
|
|
|
|
people.add_in_realm(bob);
|
|
|
|
people.add_in_realm(cindy);
|
|
|
|
|
2017-01-20 23:16:28 +01:00
|
|
|
global.people.initialize_current_user(me.user_id);
|
2017-01-19 23:04:52 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('add_message_metadata', () => {
|
2016-11-18 19:08:41 +01:00
|
|
|
var message = {
|
|
|
|
sender_email: 'me@example.com',
|
2017-01-24 23:10:01 +01:00
|
|
|
sender_id: me.user_id,
|
2016-11-18 19:08:41 +01:00
|
|
|
type: 'private',
|
|
|
|
display_recipient: [me, bob, cindy],
|
2016-12-03 23:17:57 +01:00
|
|
|
flags: ['has_alert_word'],
|
2017-08-27 18:10:36 +02:00
|
|
|
is_me_message: false,
|
2017-06-20 00:40:43 +02:00
|
|
|
id: 2067,
|
2016-11-18 19:08:41 +01:00
|
|
|
};
|
2017-12-16 23:05:32 +01:00
|
|
|
message_store.set_message_booleans(message);
|
2017-03-19 18:19:48 +01:00
|
|
|
message_store.add_message_metadata(message);
|
2016-11-18 19:08:41 +01:00
|
|
|
|
|
|
|
assert.equal(message.is_private, true);
|
|
|
|
assert.equal(message.reply_to, 'bob@example.com,cindy@example.com');
|
2017-02-25 00:19:02 +01:00
|
|
|
assert.equal(message.to_user_ids, '103,104');
|
2016-11-18 19:08:41 +01:00
|
|
|
assert.equal(message.display_reply_to, 'Bob, Cindy');
|
|
|
|
assert.equal(message.alerted, true);
|
|
|
|
assert.equal(message.is_me_message, false);
|
2017-06-20 00:40:43 +02:00
|
|
|
|
|
|
|
var retrieved_message = message_store.get(2067);
|
|
|
|
assert.equal(retrieved_message, message);
|
|
|
|
|
|
|
|
// access cached previous message, and test match subject/content
|
|
|
|
message = {
|
|
|
|
id: 2067,
|
|
|
|
match_subject: "subject foo",
|
|
|
|
match_content: "bar content",
|
|
|
|
};
|
|
|
|
message = message_store.add_message_metadata(message);
|
|
|
|
|
|
|
|
assert.equal(message.reply_to, 'bob@example.com,cindy@example.com');
|
|
|
|
assert.equal(message.to_user_ids, '103,104');
|
|
|
|
assert.equal(message.display_reply_to, 'Bob, Cindy');
|
|
|
|
assert.equal(message.match_subject, 'subject foo');
|
|
|
|
assert.equal(message.match_content, 'bar content');
|
|
|
|
|
|
|
|
message = {
|
|
|
|
sender_email: 'me@example.com',
|
|
|
|
sender_id: me.user_id,
|
|
|
|
type: 'stream',
|
2018-07-19 19:29:07 +02:00
|
|
|
display_recipient: 'Zoolippy',
|
2017-06-20 00:40:43 +02:00
|
|
|
topic: 'cool thing',
|
|
|
|
subject: 'the_subject',
|
|
|
|
id: 2068,
|
|
|
|
};
|
|
|
|
|
2018-07-19 19:29:07 +02:00
|
|
|
message_store.set_message_booleans(message);
|
|
|
|
message_store.add_message_metadata(message);
|
|
|
|
assert.deepEqual(message.stream, message.display_recipient);
|
|
|
|
assert.equal(message.reply_to, 'me@example.com');
|
|
|
|
assert.deepEqual(message.flags, undefined);
|
|
|
|
assert.equal(message.alerted, false);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-20 02:23:34 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('errors', () => {
|
2017-06-20 02:23:34 +02:00
|
|
|
// Test a user that doesn't exist
|
|
|
|
var message = {
|
|
|
|
type: 'private',
|
|
|
|
display_recipient: [{user_id: 92714}],
|
|
|
|
};
|
2017-06-20 03:20:02 +02:00
|
|
|
|
2018-05-01 01:16:02 +02:00
|
|
|
blueslip.set_test_data('error', 'Unknown user_id in get_person_from_user_id: 92714');
|
|
|
|
blueslip.set_test_data('error', 'Unknown user id 92714'); // From person.js
|
2017-06-20 03:20:02 +02:00
|
|
|
|
|
|
|
// Expect each to throw two blueslip errors
|
|
|
|
// One from message_store.js, one from person.js
|
2017-06-20 02:23:34 +02:00
|
|
|
var emails = message_store.get_pm_emails(message);
|
|
|
|
assert.equal(emails, '?');
|
2018-05-01 01:16:02 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 2);
|
2017-06-20 02:23:34 +02:00
|
|
|
|
|
|
|
var names = message_store.get_pm_full_names(message);
|
|
|
|
assert.equal(names, '?');
|
2018-05-01 01:16:02 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 4);
|
|
|
|
|
|
|
|
blueslip.clear_test_data();
|
2017-06-20 02:23:34 +02:00
|
|
|
|
|
|
|
message = {
|
|
|
|
type: 'stream',
|
|
|
|
display_recipient: [{}],
|
|
|
|
};
|
|
|
|
|
|
|
|
// This should early return and not run pm_conversation.set_partner
|
|
|
|
var num_partner = 0;
|
|
|
|
set_global('pm_conversation', {
|
|
|
|
set_partner: function () {
|
|
|
|
num_partner += 1;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
message_store.process_message_for_recent_private_messages(message);
|
|
|
|
assert.equal(num_partner, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-20 03:20:02 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('update_booleans', () => {
|
2017-12-16 23:25:31 +01:00
|
|
|
var message = {};
|
|
|
|
|
|
|
|
// First, test fields that we do actually want to update.
|
|
|
|
message.mentioned = false;
|
|
|
|
message.mentioned_me_directly = false;
|
|
|
|
message.alerted = false;
|
|
|
|
|
|
|
|
var flags = ['mentioned', 'has_alert_word', 'read'];
|
|
|
|
message_store.update_booleans(message, flags);
|
|
|
|
assert.equal(message.mentioned, true);
|
|
|
|
assert.equal(message.mentioned_me_directly, true);
|
|
|
|
assert.equal(message.alerted, true);
|
|
|
|
|
|
|
|
flags = ['read'];
|
|
|
|
message_store.update_booleans(message, flags);
|
|
|
|
assert.equal(message.mentioned, false);
|
|
|
|
assert.equal(message.mentioned_me_directly, false);
|
|
|
|
assert.equal(message.alerted, false);
|
|
|
|
|
|
|
|
// Make sure we don't muck with unread.
|
|
|
|
message.unread = false;
|
|
|
|
flags = [''];
|
|
|
|
message_store.update_booleans(message, flags);
|
|
|
|
assert.equal(message.unread, false);
|
|
|
|
|
|
|
|
message.unread = true;
|
|
|
|
flags = ['read'];
|
|
|
|
message_store.update_booleans(message, flags);
|
|
|
|
assert.equal(message.unread, true);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-16 23:25:31 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('message_id_change', () => {
|
2017-06-20 03:20:02 +02:00
|
|
|
var message = {
|
|
|
|
sender_email: 'me@example.com',
|
|
|
|
sender_id: me.user_id,
|
|
|
|
type: 'private',
|
|
|
|
display_recipient: [me, bob, cindy],
|
|
|
|
flags: ['has_alert_word'],
|
|
|
|
id: 401,
|
|
|
|
};
|
|
|
|
message_store.add_message_metadata(message);
|
|
|
|
|
|
|
|
set_global('pointer', {
|
|
|
|
furthest_read: 401,
|
|
|
|
});
|
|
|
|
|
|
|
|
set_global('message_list', {});
|
|
|
|
set_global('home_msg_list', {});
|
|
|
|
|
2017-07-19 12:49:49 +02:00
|
|
|
var opts = {
|
2017-07-06 15:13:19 +02:00
|
|
|
old_id: 401,
|
|
|
|
new_id: 402,
|
2017-06-20 03:20:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
home_msg_list.change_message_id = stub.f;
|
2017-07-19 12:49:49 +02:00
|
|
|
message_store.reify_message_id(opts);
|
2017-06-20 03:20:02 +02:00
|
|
|
var msg_id = stub.get_args('old', 'new');
|
|
|
|
assert.equal(msg_id.old, 401);
|
|
|
|
assert.equal(msg_id.new, 402);
|
|
|
|
});
|
|
|
|
|
|
|
|
home_msg_list.view = {};
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
home_msg_list.view.change_message_id = stub.f;
|
2017-07-19 12:49:49 +02:00
|
|
|
message_store.reify_message_id(opts);
|
2017-06-20 03:20:02 +02:00
|
|
|
var msg_id = stub.get_args('old', 'new');
|
|
|
|
assert.equal(msg_id.old, 401);
|
|
|
|
assert.equal(msg_id.new, 402);
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|