zulip/frontend_tests/node_tests/people_errors.js

142 lines
4.8 KiB
JavaScript
Raw Normal View History

zrequire('people');
const return_false = function () { return false; };
const return_true = function () { return true; };
set_global('reload_state', {
is_in_progress: return_false,
});
set_global('blueslip', global.make_zblueslip());
const me = {
email: 'me@example.com',
user_id: 30,
full_name: 'Me Myself',
timezone: 'US/Pacific',
};
people.init();
people.add(me);
people.initialize_current_user(me.user_id);
run_test('report_late_add', () => {
blueslip.set_test_data('error', 'Added user late: user_id=55 email=foo@example.com');
people.report_late_add(55, 'foo@example.com');
assert.equal(blueslip.get_test_logs('error').length, 1);
blueslip.clear_test_data();
reload_state.is_in_progress = return_true;
people.report_late_add(55, 'foo@example.com');
assert.equal(blueslip.get_test_logs('log').length, 1);
assert.equal(blueslip.get_test_logs('log')[0].message, 'Added user late: user_id=55 email=foo@example.com');
assert.equal(blueslip.get_test_logs('error').length, 0);
blueslip.clear_test_data();
});
run_test('is_my_user_id', () => {
blueslip.clear_test_data();
blueslip.set_test_data('error', 'user_id is a string in my_user_id: 999');
assert.equal(people.is_my_user_id('999'), false);
blueslip.set_test_data('error', 'user_id is a string in my_user_id: 30');
assert.equal(people.is_my_user_id(me.user_id.toString()), true);
assert.equal(blueslip.get_test_logs('error').length, 2);
});
run_test('blueslip', () => {
const unknown_email = "alicebobfred@example.com";
blueslip.set_test_data('debug', 'User email operand unknown: ' + unknown_email);
people.id_matches_email_operand(42, unknown_email);
assert.equal(blueslip.get_test_logs('debug').length, 1);
blueslip.clear_test_data();
blueslip.set_test_data('error', 'Unknown email for get_user_id: ' + unknown_email);
people.get_user_id(unknown_email);
assert.equal(blueslip.get_test_logs('error').length, 1);
blueslip.clear_test_data();
blueslip.set_test_data('warn', 'No user_id provided for person@example.com');
const person = {
email: 'person@example.com',
user_id: undefined,
full_name: 'Person Person',
};
people.add(person);
assert.equal(blueslip.get_test_logs('warn').length, 1);
blueslip.clear_test_data();
blueslip.set_test_data('error', 'No user_id found for person@example.com');
const user_id = people.get_user_id('person@example.com');
assert.equal(user_id, undefined);
assert.equal(blueslip.get_test_logs('error').length, 1);
blueslip.clear_test_data();
blueslip.set_test_data('warn', 'Unknown user ids: 1,2');
people.user_ids_string_to_emails_string('1,2');
assert.equal(blueslip.get_test_logs('warn').length, 1);
blueslip.clear_test_data();
blueslip.set_test_data('warn', 'Unknown emails: ' + unknown_email);
people.email_list_to_user_ids_string([unknown_email]);
assert.equal(blueslip.get_test_logs('warn').length, 1);
blueslip.clear_test_data();
let message = {
type: 'private',
display_recipient: [],
sender_id: me.user_id,
};
blueslip.set_test_data('error', 'Empty recipient list in message');
people.pm_with_user_ids(message);
people.group_pm_with_user_ids(message);
2018-10-18 22:05:28 +02:00
people.all_user_ids_in_pm(message);
assert.equal(people.pm_perma_link(message), undefined);
assert.equal(blueslip.get_test_logs('error').length, 4);
blueslip.clear_test_data();
const charles = {
email: 'charles@example.com',
user_id: 451,
full_name: 'Charles Dickens',
avatar_url: 'charles.com/foo.png',
};
const maria = {
email: 'athens@example.com',
user_id: 452,
full_name: 'Maria Athens',
};
people.add(charles);
people.add(maria);
message = {
type: 'private',
display_recipient: [
{id: maria.user_id},
{id: 42},
{id: charles.user_id},
],
sender_id: charles.user_id,
};
blueslip.set_test_data('error', 'Unknown user id in message: 42');
const reply_to = people.pm_reply_to(message);
js: Convert a.indexOf(…) !== -1 to a.includes(…). Babel polyfills this for us for Internet Explorer. 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, { visitBinaryExpression(path) { const { operator, left, right } = path.node; if ( n.CallExpression.check(left) && n.MemberExpression.check(left.callee) && !left.callee.computed && n.Identifier.check(left.callee.property) && left.callee.property.name === "indexOf" && left.arguments.length === 1 && checkExpression(left.arguments[0]) && ((["===", "!==", "==", "!=", ">", "<="].includes(operator) && n.UnaryExpression.check(right) && right.operator == "-" && n.Literal.check(right.argument) && right.argument.value === 1) || ([">=", "<"].includes(operator) && n.Literal.check(right) && right.value === 0)) ) { const test = b.callExpression( b.memberExpression(left.callee.object, b.identifier("includes")), [left.arguments[0]] ); path.replace( ["!==", "!=", ">", ">="].includes(operator) ? test : b.unaryExpression("!", test) ); 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 04:55:06 +01:00
assert(reply_to.includes('?'));
assert.equal(blueslip.get_test_logs('error').length, 1);
blueslip.clear_test_data();
people.pm_with_user_ids = function () { return [42]; };
people.get_by_user_id = function () { return; };
blueslip.set_test_data('error', 'Unknown people in message');
const uri = people.pm_with_url({});
assert.equal(uri.indexOf('unk'), uri.length - 3);
assert.equal(blueslip.get_test_logs('error').length, 1);
blueslip.clear_test_data();
blueslip.set_test_data('error', 'Undefined field id');
assert.equal(people.my_custom_profile_data(undefined), undefined);
blueslip.set_test_data('error', 'Trying to set undefined field id');
people.set_custom_profile_field_data(maria.user_id, {});
});