2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2024-01-02 15:13:39 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {run_test} = require("./lib/test");
|
2023-12-06 10:42:25 +01:00
|
|
|
const blueslip = require("./lib/zblueslip");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {page_params} = require("./lib/zpage_params");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-02-10 04:53:22 +01:00
|
|
|
const user_pill = zrequire("user_pill");
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2024-01-02 15:13:39 +01:00
|
|
|
const settings_data = mock_esm("../src/settings_data");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2018-03-07 15:28:11 +01:00
|
|
|
user_id: 99,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Barson",
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const isaac = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "isaac@example.com",
|
2018-03-07 15:28:11 +01:00
|
|
|
user_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Isaac Newton",
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bogus_item = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bogus@example.com",
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "user",
|
2020-07-15 01:29:15 +02:00
|
|
|
display_value: "bogus@example.com",
|
2022-02-05 19:15:30 +01:00
|
|
|
// status_emoji_info: undefined,
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const isaac_item = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "isaac@example.com",
|
|
|
|
display_value: "Isaac Newton",
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "user",
|
2018-03-07 15:28:11 +01:00
|
|
|
user_id: isaac.user_id,
|
2020-07-29 03:27:36 +02:00
|
|
|
deactivated: false,
|
2021-10-14 00:48:32 +02:00
|
|
|
img_src: `http://zulip.zulipdev.com/avatar/${isaac.user_id}?s=50`,
|
2022-02-05 19:15:30 +01:00
|
|
|
status_emoji_info: undefined,
|
2023-09-13 19:30:52 +02:00
|
|
|
should_add_guest_user_indicator: false,
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2024-01-02 15:13:39 +01:00
|
|
|
const inaccessible_user_id = 103;
|
|
|
|
|
|
|
|
const inaccessible_user_item = {
|
|
|
|
email: "user103@example.com",
|
|
|
|
display_value: "translated: Unknown user",
|
|
|
|
type: "user",
|
|
|
|
user_id: inaccessible_user_id,
|
|
|
|
deactivated: false,
|
|
|
|
img_src: `http://zulip.zulipdev.com/avatar/${inaccessible_user_id}?s=50`,
|
|
|
|
status_emoji_info: undefined,
|
|
|
|
should_add_guest_user_indicator: false,
|
|
|
|
};
|
|
|
|
|
2021-05-03 15:31:09 +02:00
|
|
|
let pill_widget = {};
|
|
|
|
|
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-05-03 15:31:09 +02:00
|
|
|
people.init();
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(isaac);
|
|
|
|
pill_widget = {};
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-05-03 15:31:09 +02:00
|
|
|
});
|
|
|
|
}
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2021-05-03 15:31:09 +02:00
|
|
|
test("create_item", () => {
|
2024-01-02 15:13:39 +01:00
|
|
|
function test_create_item(email, current_items, expected_item, pill_config) {
|
|
|
|
const item = user_pill.create_item_from_email(email, current_items, pill_config);
|
2018-03-07 15:28:11 +01:00
|
|
|
assert.deepEqual(item, expected_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = true;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test_create_item("bogus@example.com", [], bogus_item);
|
|
|
|
test_create_item("bogus@example.com", [bogus_item], undefined);
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test_create_item("isaac@example.com", [], isaac_item);
|
|
|
|
test_create_item("isaac@example.com", [isaac_item], undefined);
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test_create_item("bogus@example.com", [], undefined);
|
|
|
|
test_create_item("isaac@example.com", [], isaac_item);
|
|
|
|
test_create_item("isaac@example.com", [isaac_item], undefined);
|
2024-01-02 15:13:39 +01:00
|
|
|
|
|
|
|
settings_data.user_can_access_all_other_users = () => false;
|
|
|
|
page_params.realm_bot_domain = "example.com";
|
|
|
|
people.add_inaccessible_user(inaccessible_user_id);
|
|
|
|
|
|
|
|
test_create_item("user103@example.com", [], undefined, {exclude_inaccessible_users: true});
|
|
|
|
test_create_item("user103@example.com", [], inaccessible_user_item, {
|
|
|
|
exclude_inaccessible_users: false,
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2021-05-03 15:31:09 +02:00
|
|
|
test("get_email", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(user_pill.get_email_from_item({email: "foo@example.com"}), "foo@example.com");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2021-05-03 15:31:09 +02:00
|
|
|
test("append", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let appended;
|
|
|
|
let cleared;
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
function fake_append(opts) {
|
|
|
|
appended = true;
|
|
|
|
assert.equal(opts.email, isaac.email);
|
|
|
|
assert.equal(opts.display_value, isaac.full_name);
|
|
|
|
assert.equal(opts.user_id, isaac.user_id);
|
2018-06-27 20:55:56 +02:00
|
|
|
assert.equal(opts.img_src, isaac_item.img_src);
|
2018-03-07 15:28:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function fake_clear() {
|
|
|
|
cleared = true;
|
|
|
|
}
|
|
|
|
|
2021-05-03 15:31:09 +02:00
|
|
|
pill_widget.appendValidatedData = fake_append;
|
|
|
|
pill_widget.clear_text = fake_clear;
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
user_pill.append_person({
|
|
|
|
person: isaac,
|
2020-07-20 22:18:43 +02:00
|
|
|
pill_widget,
|
2018-03-07 15:28:11 +01:00
|
|
|
});
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(appended);
|
|
|
|
assert.ok(cleared);
|
2023-12-06 10:42:25 +01:00
|
|
|
|
|
|
|
blueslip.expect("warn", "Undefined user in function append_user");
|
|
|
|
user_pill.append_user(undefined, pill_widget);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2021-05-03 15:31:09 +02:00
|
|
|
test("get_items", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = [isaac_item, bogus_item];
|
2021-05-03 15:31:09 +02:00
|
|
|
pill_widget.items = () => items;
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
assert.deepEqual(user_pill.get_user_ids(pill_widget), [isaac.user_id]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2021-05-03 15:31:09 +02:00
|
|
|
test("typeahead", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = [isaac_item, bogus_item];
|
2021-05-03 15:31:09 +02:00
|
|
|
pill_widget.items = () => items;
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
// Both alice and isaac are in our realm, but isaac will be
|
|
|
|
// excluded by virtue of already being one of the widget items.
|
|
|
|
// And then bogus_item is just a red herring to test robustness.
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = user_pill.typeahead_source(pill_widget);
|
2018-03-07 15:28:11 +01:00
|
|
|
assert.deepEqual(result, [alice]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|