2020-02-13 22:34:29 +01:00
|
|
|
const util = zrequire('util');
|
2017-11-08 18:20:18 +01:00
|
|
|
zrequire('pm_conversations');
|
|
|
|
zrequire('people');
|
|
|
|
zrequire('message_store');
|
2016-11-18 19:08:41 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const noop = function () {};
|
2016-11-18 19:08:41 +01:00
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-03-22 18:40:05 +01:00
|
|
|
set_global('stream_topic_history', {
|
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,
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const me = {
|
2016-11-18 19:08:41 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2016-11-18 19:08:41 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob = {
|
2016-11-18 19:08:41 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const cindy = {
|
2016-11-18 19:08:41 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-01-02 14:42:55 +01:00
|
|
|
const denise = {
|
|
|
|
email: 'denise@example.com',
|
|
|
|
user_id: 105,
|
|
|
|
full_name: 'Denise ',
|
|
|
|
};
|
|
|
|
|
2020-03-21 20:19:30 +01:00
|
|
|
people.add(me);
|
|
|
|
people.add(alice);
|
|
|
|
people.add(bob);
|
|
|
|
people.add(cindy);
|
|
|
|
people.add(denise);
|
2016-11-18 19:08:41 +01:00
|
|
|
|
2020-03-21 21:54:16 +01:00
|
|
|
people.initialize_current_user(me.user_id);
|
2017-01-19 23:04:52 +01:00
|
|
|
|
2020-01-01 13:02:34 +01:00
|
|
|
function convert_recipients(people) {
|
|
|
|
// Display_recipient uses `id` for user_ids.
|
js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitCallExpression(path) {
const { callee, arguments: args } = path.node;
if (
n.MemberExpression.check(callee) &&
!callee.computed &&
n.Identifier.check(callee.object) &&
callee.object.name === "_" &&
n.Identifier.check(callee.property) &&
callee.property.name === "map" &&
args.length === 2 &&
checkExpression(args[0]) &&
checkExpression(args[1])
) {
const [arr, fn] = args;
path.replace(
b.callExpression(b.memberExpression(arr, b.identifier("map")), [
n.FunctionExpression.check(fn) ||
n.ArrowFunctionExpression.check(fn)
? b.arrowFunctionExpression(
fn.params,
n.BlockStatement.check(fn.body) &&
fn.body.body.length === 1 &&
n.ReturnStatement.check(fn.body.body[0])
? fn.body.body[0].argument || b.identifier("undefined")
: fn.body
)
: fn,
])
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 02:43:49 +01:00
|
|
|
return people.map(p => ({
|
|
|
|
email: p.email,
|
|
|
|
id: p.user_id,
|
|
|
|
full_name: p.full_name,
|
|
|
|
}));
|
2020-01-01 13:02:34 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('add_message_metadata', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = {
|
2016-11-18 19:08:41 +01:00
|
|
|
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',
|
2020-01-01 13:02:34 +01:00
|
|
|
display_recipient: convert_recipients([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
|
|
|
|
2020-01-02 14:42:55 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
message_store.user_ids().sort(),
|
|
|
|
[me.user_id, bob.user_id, cindy.user_id]);
|
|
|
|
|
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
|
|
|
|
2020-04-09 23:12:03 +02:00
|
|
|
let retrieved_message = message_store.get(2067);
|
|
|
|
assert.equal(retrieved_message, message);
|
|
|
|
|
|
|
|
blueslip.expect('error', 'message_store got non-number: 2067');
|
|
|
|
retrieved_message = message_store.get('2067');
|
|
|
|
blueslip.reset();
|
2017-06-20 00:40:43 +02:00
|
|
|
assert.equal(retrieved_message, message);
|
|
|
|
|
|
|
|
// access cached previous message, and test match subject/content
|
|
|
|
message = {
|
|
|
|
id: 2067,
|
2018-11-15 16:59:41 +01:00
|
|
|
match_subject: "topic foo",
|
2017-06-20 00:40:43 +02:00
|
|
|
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');
|
2018-11-15 16:59:41 +01:00
|
|
|
assert.equal(util.get_match_topic(message), 'topic foo');
|
2017-06-20 00:40:43 +02:00
|
|
|
assert.equal(message.match_content, 'bar content');
|
|
|
|
|
|
|
|
message = {
|
2020-01-02 14:42:55 +01:00
|
|
|
sender_email: denise.email,
|
|
|
|
sender_id: denise.user_id,
|
2017-06-20 00:40:43 +02:00
|
|
|
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);
|
2020-01-02 14:42:55 +01:00
|
|
|
assert.equal(message.reply_to, 'denise@example.com');
|
2018-07-19 19:29:07 +02:00
|
|
|
assert.deepEqual(message.flags, undefined);
|
|
|
|
assert.equal(message.alerted, false);
|
2020-01-02 14:42:55 +01:00
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
message_store.user_ids().sort(),
|
|
|
|
[me.user_id, bob.user_id, cindy.user_id, denise.user_id]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-20 02:23:34 +02:00
|
|
|
|
2019-09-27 11:21:27 +02:00
|
|
|
run_test('message_booleans_parity', () => {
|
|
|
|
// We have two code paths that update/set message booleans.
|
|
|
|
// This test asserts that both have identical behavior for the
|
|
|
|
// flags common between them.
|
|
|
|
const assert_bool_match = (flags, expected_message) => {
|
|
|
|
const set_message = {topic: 'set_message_booleans', flags: flags};
|
|
|
|
const update_message = {topic: 'update_booleans'};
|
|
|
|
message_store.set_message_booleans(set_message);
|
|
|
|
message_store.update_booleans(update_message, flags);
|
|
|
|
Object.keys(expected_message).forEach((key) => {
|
|
|
|
assert.equal(set_message[key], expected_message[key], `'${key}' != ${expected_message[key]}`);
|
|
|
|
assert.equal(update_message[key], expected_message[key]);
|
|
|
|
});
|
|
|
|
assert.equal(set_message.topic, 'set_message_booleans');
|
|
|
|
assert.equal(update_message.topic, 'update_booleans');
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_bool_match(['wildcard_mentioned'],
|
|
|
|
{
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
alerted: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_bool_match(['mentioned'],
|
|
|
|
{
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
alerted: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_bool_match(['has_alert_word'],
|
|
|
|
{
|
|
|
|
mentioned: false,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
alerted: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = {
|
2017-06-20 02:23:34 +02:00
|
|
|
type: 'private',
|
2020-01-01 13:02:34 +01:00
|
|
|
display_recipient: [{id: 92714}],
|
2017-06-20 02:23:34 +02:00
|
|
|
};
|
2017-06-20 03:20:02 +02:00
|
|
|
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.expect('error', 'Unknown user_id in get_by_user_id: 92714');
|
|
|
|
blueslip.expect('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
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = message_store.get_pm_emails(message);
|
2017-06-20 02:23:34 +02:00
|
|
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const names = message_store.get_pm_full_names(message);
|
2017-06-20 02:23:34 +02:00
|
|
|
assert.equal(names, '?');
|
2018-05-01 01:16:02 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 4);
|
|
|
|
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.reset();
|
2017-06-20 02:23:34 +02:00
|
|
|
|
|
|
|
message = {
|
|
|
|
type: 'stream',
|
|
|
|
display_recipient: [{}],
|
|
|
|
};
|
|
|
|
|
|
|
|
// This should early return and not run pm_conversation.set_partner
|
2019-11-02 00:06:25 +01:00
|
|
|
let num_partner = 0;
|
2017-06-20 02:23:34 +02:00
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = {};
|
2017-12-16 23:25:31 +01:00
|
|
|
|
|
|
|
// First, test fields that we do actually want to update.
|
|
|
|
message.mentioned = false;
|
|
|
|
message.mentioned_me_directly = false;
|
|
|
|
message.alerted = false;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let flags = ['mentioned', 'has_alert_word', 'read'];
|
2017-12-16 23:25:31 +01:00
|
|
|
message_store.update_booleans(message, flags);
|
|
|
|
assert.equal(message.mentioned, true);
|
|
|
|
assert.equal(message.mentioned_me_directly, true);
|
|
|
|
assert.equal(message.alerted, true);
|
|
|
|
|
2019-09-27 11:11:40 +02:00
|
|
|
flags = ['wildcard_mentioned', 'unread'];
|
|
|
|
message_store.update_booleans(message, flags);
|
|
|
|
assert.equal(message.mentioned, true);
|
|
|
|
assert.equal(message.mentioned_me_directly, false);
|
|
|
|
|
2017-12-16 23:25:31 +01:00
|
|
|
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
|
|
|
|
2019-02-25 19:01:23 +01:00
|
|
|
run_test('each', () => {
|
|
|
|
message_store.each((message) => {
|
|
|
|
assert(message.alerted !== undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('message_id_change', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = {
|
2017-06-20 03:20:02 +02:00
|
|
|
sender_email: 'me@example.com',
|
|
|
|
sender_id: me.user_id,
|
|
|
|
type: 'private',
|
2020-01-01 13:02:34 +01:00
|
|
|
display_recipient: convert_recipients([me, bob, cindy]),
|
2017-06-20 03:20:02 +02:00
|
|
|
flags: ['has_alert_word'],
|
|
|
|
id: 401,
|
|
|
|
};
|
|
|
|
message_store.add_message_metadata(message);
|
|
|
|
|
|
|
|
set_global('pointer', {
|
|
|
|
furthest_read: 401,
|
2018-08-04 08:37:23 +02:00
|
|
|
set_furthest_read: function (value) { this.furthest_read = value; },
|
2017-06-20 03:20:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
set_global('message_list', {});
|
|
|
|
set_global('home_msg_list', {});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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);
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_id = stub.get_args('old', 'new');
|
2017-06-20 03:20:02 +02:00
|
|
|
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);
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_id = stub.get_args('old', 'new');
|
2017-06-20 03:20:02 +02:00
|
|
|
assert.equal(msg_id.old, 401);
|
|
|
|
assert.equal(msg_id.new, 402);
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2020-04-09 23:12:03 +02:00
|
|
|
|
|
|
|
run_test('errors', () => {
|
|
|
|
blueslip.expect('error', 'message_store.get got bad value: undefined');
|
|
|
|
message_store.get(undefined);
|
|
|
|
blueslip.reset();
|
|
|
|
});
|