Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

136 KB
Raw Blame
"use strict";

const assert = require("node:assert/strict");

const {get_final_topic_display_name} = require("../src/util.ts");

const {mock_banners} = require("./lib/compose_banner.cjs");
const {make_user_group} = require("./lib/example_group.cjs");
const {make_realm} = require("./lib/example_realm.cjs");
const {server_supported_permission_settings} = require("./lib/example_settings.cjs");
const {make_stream} = require("./lib/example_stream.cjs");
const {make_user, make_cross_realm_bot} = require("./lib/example_user.cjs");
const {mock_esm, set_global, with_overrides, zrequire} = require("./lib/namespace.cjs");
const {run_test, noop} = require("./lib/test.cjs");
const {$} = require("./lib/zjquery.cjs");

let autosize_called;
const REALM_EMPTY_TOPIC_DISPLAY_NAME = "general chat";

const bootstrap_typeahead = mock_esm("../src/bootstrap_typeahead");
const message_lists = mock_esm("../src/message_lists");
const pm_conversations = mock_esm("../src/pm_conversations", {
    get_latest_direct_message_id_with_user: () => undefined,
});
const compose_ui = mock_esm("../src/compose_ui", {
    autosize_textarea() {
        autosize_called = true;
    },
    cursor_inside_code_block: () => false,
    set_code_formatting_button_triggered: noop,
    maybe_set_compose_textarea_typeahead: noop,
});
const compose_validate = mock_esm("../src/compose_validate", {
    validate_message_length: () => true,
    warn_if_topic_resolved: noop,
    stream_wildcard_mention_allowed: () => true,
    warn_if_mentioning_unsubscribed_group: noop,
    initialize: noop,
});
const input_pill = mock_esm("../src/input_pill");
const message_util = mock_esm("../src/message_util", {
    get_direct_message_permission_hints: () => ({
        is_known_empty_conversation: false,
        is_local_echo_safe: true,
    }),
    user_can_send_direct_message(user_ids_string) {
        return (
            (!message_util.get_direct_message_permission_hints(user_ids_string)
                .is_known_empty_conversation ||
                people.user_can_initiate_direct_message_thread(user_ids_string)) &&
            people.user_can_direct_message(user_ids_string)
        );
    },
    // currently is synced with the actual function.
    make_check_message_permission_for_dm_candidate(recipient_ids) {
        const current_user_id = people.my_current_user_id();
        const is_current_user_in_initiator_group = user_groups.is_user_in_setting_group(
            realm.realm_direct_message_initiator_group,
            current_user_id,
        );
        const is_current_user_in_permission_group = user_groups.is_user_in_setting_group(
            realm.realm_direct_message_permission_group,
            current_user_id,
        );

        if (is_current_user_in_initiator_group && is_current_user_in_permission_group) {
            return null;
        }

        const recipient_is_in_permission_group = recipient_ids.some(
            (user_id) =>
                !people.is_valid_bot_user(user_id) &&
                user_id !== current_user_id &&
                user_groups.is_user_in_setting_group(
                    realm.realm_direct_message_permission_group,
                    user_id,
                ),
        );

        if (is_current_user_in_initiator_group && recipient_is_in_permission_group) {
            return null;
        }

        const all_recipients_are_bots = recipient_ids.every(
            (user_id) => people.is_valid_bot_user(user_id) || user_id === current_user_id,
        );

        const permission_group_user_ids = user_groups.get_user_ids_in_setting_group(
            realm.realm_direct_message_permission_group,
        );

        return (candidate_user_id) => {
            if (all_recipients_are_bots && people.is_valid_bot_user(candidate_user_id)) {
                return true;
            }

            const is_candidate_in_permission_group =
                permission_group_user_ids.has(candidate_user_id);

            if (is_current_user_in_initiator_group && is_candidate_in_permission_group) {
                return true;
            }

            if (
                !is_current_user_in_permission_group &&
                !recipient_is_in_permission_group &&
                !is_candidate_in_permission_group
            ) {
                return false;
            }

            const conversation_user_ids_string = [...recipient_ids, candidate_user_id].join(",");
            return !message_util.get_direct_message_permission_hints(conversation_user_ids_string)
                .is_known_empty_conversation;
        };
    },
});
const message_user_ids = mock_esm("../src/message_user_ids", {
    user_ids: () => [],
});
const stream_topic_history_util = mock_esm("../src/stream_topic_history_util");

let set_timeout_called;
set_global("setTimeout", (f, time) => {
    f();
    assert.equal(time, 0);
    set_timeout_called = true;
});
set_global("document", "document-stub");

const typeahead = zrequire("typeahead");
const stream_topic_history = zrequire("stream_topic_history");
const compose_state = zrequire("compose_state");
const emoji = zrequire("emoji");
const emoji_picker = zrequire("emoji_picker");
const typeahead_helper = zrequire("typeahead_helper");
const muted_users = zrequire("muted_users");
const people = zrequire("people");
const peer_data = zrequire("peer_data");
const user_groups = zrequire("user_groups");
// const pm_conversations = zrequire("pm_conversations");
const user_pill = zrequire("user_pill");
const stream_data = zrequire("stream_data");
const stream_list_sort = zrequire("stream_list_sort");
const compose_pm_pill = zrequire("compose_pm_pill");
const compose_recipient = zrequire("compose_recipient");
const composebox_typeahead = zrequire("composebox_typeahead");
const settings_config = zrequire("settings_config");
const {set_current_user, set_realm} = zrequire("state_data");
const {initialize_user_settings} = zrequire("user_settings");
const current_user = {};
set_current_user(current_user);
const realm = make_realm({
    realm_empty_topic_display_name: REALM_EMPTY_TOPIC_DISPLAY_NAME,
    realm_topics_policy: "allow_empty_topic",
});
set_realm(realm);
const user_settings = {
    web_channel_default_view: settings_config.web_channel_default_view_values.channel_feed.code,
};
initialize_user_settings({user_settings});

const ct = composebox_typeahead;

function user_item(user) {
    return {type: "user", user};
}

function broadcast_item(user) {
    return {type: "broadcast", user};
}

function slash_item(slash) {
    return {
        ...slash,
        type: "slash",
    };
}

function stream_item(stream) {
    return {
        ...stream,
        type: "stream",
    };
}

function user_group_item(item) {
    return {
        ...item,
        type: "user_group",
    };
}

function language_item(language) {
    return {
        language,
        type: "syntax",
    };
}

run_test("verify wildcard mentions typeahead for stream message", () => {
    compose_state.set_message_type("stream");
    const mention_all = ct.broadcast_mentions()[0];
    const mention_everyone = ct.broadcast_mentions()[1];
    const mention_stream = ct.broadcast_mentions()[2];
    const mention_channel = ct.broadcast_mentions()[3];
    const mention_topic = ct.broadcast_mentions()[4];
    assert.equal(mention_all.email, "all");
    assert.equal(mention_all.full_name, "all");
    assert.equal(mention_everyone.email, "everyone");
    assert.equal(mention_everyone.full_name, "everyone");
    assert.equal(mention_stream.email, "stream");
    assert.equal(mention_stream.full_name, "stream");
    assert.equal(mention_channel.email, "channel");
    assert.equal(mention_channel.full_name, "channel");
    assert.equal(mention_topic.email, "topic");
    assert.equal(mention_topic.full_name, "topic");

    assert.equal(mention_all.special_item_text, "all");
    assert.equal(mention_all.secondary_text, "translated: Notify all channel subscribers");
    assert.equal(mention_everyone.special_item_text, "everyone");
    assert.equal(mention_everyone.secondary_text, "translated: Notify all channel subscribers");
    assert.equal(mention_stream.special_item_text, "stream");
    assert.equal(mention_stream.secondary_text, "translated: Notify all channel subscribers");
    assert.equal(mention_channel.special_item_text, "channel");
    assert.equal(mention_channel.secondary_text, "translated: Notify all channel subscribers");
    assert.equal(mention_topic.special_item_text, "topic");
    assert.equal(
        mention_topic.secondary_text,
        "translated: Notify participants in this conversation",
    );

    compose_validate.stream_wildcard_mention_allowed = () => false;
    compose_validate.topic_wildcard_mention_allowed = () => true;
    const mention_topic_only = ct.broadcast_mentions()[0];
    assert.equal(mention_topic_only.full_name, "topic");

    compose_validate.stream_wildcard_mention_allowed = () => false;
    compose_validate.topic_wildcard_mention_allowed = () => false;
    const mentionNobody = ct.broadcast_mentions();
    assert.equal(mentionNobody.length, 0);
    compose_validate.stream_wildcard_mention_allowed = () => true;
});

run_test("verify wildcard mentions typeahead for direct message", () => {
    compose_state.set_message_type("private");
    assert.equal(ct.broadcast_mentions().length, 2);
    const mention_all = ct.broadcast_mentions()[0];
    const mention_everyone = ct.broadcast_mentions()[1];
    assert.equal(mention_all.email, "all");
    assert.equal(mention_all.full_name, "all");
    assert.equal(mention_everyone.email, "everyone");
    assert.equal(mention_everyone.full_name, "everyone");

    assert.equal(mention_all.special_item_text, "all");
    assert.equal(mention_all.secondary_text, "translated: Notify recipients");
    assert.equal(mention_everyone.special_item_text, "everyone");
    assert.equal(mention_all.secondary_text, "translated: Notify recipients");
});

const emoji_stadium = {
    name: "stadium",
    aliases: ["stadium"],
    emoji_url: "TBD",
    emoji_code: "1f3df",
};
const emoji_tada = {
    name: "tada",
    aliases: ["tada"],
    emoji_url: "TBD",
    emoji_code: "1f389",
};
const emoji_moneybag = {
    name: "moneybag",
    aliases: ["moneybag"],
    emoji_url: "TBD",
    emoji_code: "1f4b0",
};
const emoji_japanese_post_office = {
    name: "japanese_post_office",
    aliases: ["japanese_post_office"],
    emoji_url: "TBD",
    emoji_code: "1f3e3",
};
const emoji_panda_face = {
    name: "panda_face",
    aliases: ["panda_face"],
    emoji_url: "TBD",
    emoji_code: "1f43c",
};
const emoji_see_no_evil = {
    name: "see_no_evil",
    aliases: ["see_no_evil"],
    emoji_url: "TBD",
    emoji_code: "1f648",
};
const emoji_thumbs_up = {
    name: "thumbs_up",
    aliases: ["thumbs_up"],
    emoji_url: "TBD",
    emoji_code: "1f44d",
};
const emoji_thermometer = {
    name: "thermometer",
    aliases: ["thermometer"],
    emoji_url: "TBD",
    emoji_code: "1f321",
};
const emoji_heart = {
    name: "heart",
    aliases: ["heart"],
    emoji_url: "TBD",
    emoji_code: "2764",
};
const emoji_headphones = {
    name: "headphones",
    aliases: ["headphones"],
    emoji_url: "TBD",
    emoji_code: "1f3a7",
};

const emojis_by_name = new Map(
    Object.entries({
        tada: emoji_tada,
        moneybag: emoji_moneybag,
        stadium: emoji_stadium,
        japanese_post_office: emoji_japanese_post_office,
        panda_face: emoji_panda_face,
        see_no_evil: emoji_see_no_evil,
        thumbs_up: emoji_thumbs_up,
        thermometer: emoji_thermometer,
        heart: emoji_heart,
        headphones: emoji_headphones,
    }),
);

const me_command = {
    name: "me",
    aliases: "",
    text: "/me",
    placeholder: "translated: is …",
    info: "translated: Action message",
};
const me_command_item = slash_item(me_command);

const my_command_item = slash_item({
    name: "my",
    aliases: "",
    text: "/my (Test)",
});

const dark_command = {
    name: "dark",
    aliases: "night",
    text: "/dark",
    info: "translated: Switch to the dark theme",
};
const dark_command_item = slash_item(dark_command);

const light_command = {
    name: "light",
    aliases: "day",
    text: "/light",
    info: "translated: Switch to light theme",
};
const light_command_item = slash_item(light_command);

const name_to_codepoint = {};
for (const [key, val] of emojis_by_name) {
    name_to_codepoint[key] = val.emoji_code;
}

const codepoint_to_name = {};
for (const [key, val] of emojis_by_name) {
    codepoint_to_name[val.emoji_code] = key;
}

const emoji_codes = {
    name_to_codepoint,
    names: emojis_by_name.keys().toArray(),
    emoji_catalog: {},
    emoticon_conversions: {},
    codepoint_to_name,
};

emoji.initialize({
    realm_emoji: {},
    emoji_codes,
});
emoji.active_realm_emojis.clear();
emoji.emojis_by_name.clear();
for (const [key, val] of emojis_by_name) {
    emoji.emojis_by_name.set(key, val);
}
typeahead.set_frequently_used_emojis(
    [emoji_thumbs_up.emoji_code, emoji_heart.emoji_code].map((emoji_code) => ({
        emoji_type: "unicode_emoji",
        emoji_code,
    })),
);
emoji_picker.rebuild_catalog();
const emoji_list = composebox_typeahead.emoji_collection.map((emoji) => ({
    ...emoji,
    type: "emoji",
}));
const emoji_list_by_name = new Map(emoji_list.map((emoji) => [emoji.emoji_name, emoji]));
function emoji_objects(emoji_names) {
    return emoji_names.map((emoji_name) => emoji_list_by_name.get(emoji_name));
}

const ali = make_user({
    email: "[email protected]",
    user_id: 98,
    full_name: "Ali",
});
const ali_item = user_item(ali);

const alice = make_user({
    email: "[email protected]",
    user_id: 99,
    full_name: "Alice",
});
const alice_item = user_item(alice);

const hamlet = make_user({
    email: "[email protected]",
    user_id: 100,
    full_name: "King Hamlet",
});
const hamlet_item = user_item(hamlet);

const othello = make_user({
    email: "[email protected]",
    user_id: 101,
    full_name: "Othello, the Moor of Venice",
});
const othello_item = user_item(othello);

const cordelia = make_user({
    email: "[email protected]",
    user_id: 102,
    full_name: "Cordelia, Lear's daughter",
});
const cordelia_item = user_item(cordelia);

const deactivated_user = make_user({
    email: "[email protected]",
    user_id: 103,
    full_name: "Deactivated User",
});
const deactivated_user_item = user_item(deactivated_user);

const lear = make_user({
    email: "[email protected]",
    user_id: 104,
    full_name: "King Lear",
});
const lear_item = user_item(lear);

const twin1 = make_user({
    full_name: "Mark Twin",
    user_id: 105,
    email: "[email protected]",
});
const twin1_item = user_item(twin1);

const twin2 = make_user({
    full_name: "Mark Twin",
    user_id: 106,
    email: "[email protected]",
});
const twin2_item = user_item(twin2);

const gael = make_user({
    full_name: "Gaël Twin",
    user_id: 107,
    email: "[email protected]",
});
const gael_item = user_item(gael);

const hal = make_user({
    full_name: "Earl Hal",
    user_id: 108,
    email: "[email protected]",
});
const hal_item = user_item(hal);

const harry = make_user({
    full_name: "Harry",
    user_id: 109,
    email: "[email protected]",
});
const harry_item = user_item(harry);

const welcome_bot = make_cross_realm_bot({
    full_name: "Welcome Bot",
    user_id: 110,
    email: "[email protected]",
});

const welcome_bot_item = user_item(welcome_bot);

const notification_bot = make_cross_realm_bot({
    full_name: "Notification Bot",
    user_id: 111,
    email: "[email protected]",
});

const notification_bot_item = user_item(notification_bot);

const hamletcharacters = user_group_item(
    make_user_group({
        name: "hamletcharacters",
        id: 1,
        creator_id: null,
        date_created: 1596710000,
        description: "Characters of Hamlet",
        members: new Set([100, 104]),
        is_system_group: false,
        direct_subgroup_ids: new Set(),
        can_add_members_group: 2,
        can_join_group: 2,
        can_leave_group: 2,
        can_manage_group: 2,
        can_mention_group: 2,
        can_remove_members_group: 2,
        deactivated: false,
    }),
);

const backend = user_group_item(
    make_user_group({
        name: "Backend",
        id: 2,
        creator_id: null,
        date_created: 1596710000,
        description: "Backend team",
        members: new Set([101]),
        is_system_group: false,
        direct_subgroup_ids: new Set([1]),
        can_add_members_group: 1,
        can_join_group: 1,
        can_leave_group: 2,
        can_manage_group: 1,
        can_mention_group: 1,
        can_remove_members_group: 2,
        deactivated: false,
    }),
);

const call_center = user_group_item(
    make_user_group({
        name: "Call Center",
        id: 3,
        creator_id: null,
        date_created: 1596710000,
        description: "folks working in support",
        members: new Set([102]),
        is_system_group: false,
        direct_subgroup_ids: new Set(),
        can_add_members_group: 2,
        can_join_group: 2,
        can_leave_group: 2,
        can_manage_group: 2,
        can_mention_group: 2,
        can_remove_members_group: 2,
        deactivated: false,
    }),
);

const support = user_group_item(
    make_user_group({
        name: "support",
        id: 4,
        creator_id: null,
        date_created: 1596710000,
        description: "Support team",
        members: new Set(),
        is_system_group: false,
        direct_subgroup_ids: new Set(),
        can_add_members_group: 2,
        can_join_group: 2,
        can_leave_group: 2,
        can_manage_group: 2,
        can_mention_group: 2,
        deactivated: false,
    }),
);

const admins = user_group_item(
    make_user_group({
        name: "Administrators",
        id: 5,
        creator_id: null,
        date_created: 1596710000,
        description: "Administrators",
        members: new Set([102, 103]),
        is_system_group: true,
        direct_subgroup_ids: new Set(),
        can_add_members_group: 2,
        can_join_group: 2,
        can_leave_group: 2,
        can_manage_group: 2,
        can_mention_group: 2,
        can_remove_members_group: 2,
        deactivated: false,
    }),
);

const members = user_group_item(
    make_user_group({
        name: "role:members",
        id: 6,
        creator_id: null,
        date_created: 1596710000,
        description: "Members",
        members: new Set([100, 101, 104]),
        is_system_group: true,
        direct_subgroup_ids: new Set([5]),
        can_add_members_group: 2,
        can_join_group: 2,
        can_leave_group: 2,
        can_manage_group: 2,
        can_mention_group: 2,
        can_remove_members_group: 2,
        deactivated: false,
    }),
);
const full_members = user_group_item(
    make_user_group({
        name: "role:fullmembers",
        id: 7,
        creator_id: null,
        date_created: 1596710000,
        description: "Full members",
        members: new Set([100, 101, 104]),
        is_system_group: true,
        direct_subgroup_ids: new Set(),
        can_add_members_group: 2,
        can_join_group: 2,
        can_leave_group: 2,
        can_manage_group: 2,
        can_mention_group: 2,
        can_remove_members_group: 2,
        deactivated: false,
    }),
);

const nobody = user_group_item(
    make_user_group({
        name: "role:nobody",
        id: 8,
        creator_id: null,
        date_created: 1596710000,
        description: "Nobody",
        members: new Set(),
        is_system_group: true,
        direct_subgroup_ids: new Set(),
        can_add_members_group: 2,
        can_join_group: 2,
        can_leave_group: 2,
        can_manage_group: 2,
        can_mention_group: 2,
        can_remove_members_group: 2,
        deactivated: false,
    }),
);

const sweden_stream = stream_item(
    make_stream({
        name: "Sweden",
        description: "Cold, mountains and home decor.",
        stream_id: 1,
        subscribed: true,
        can_administer_channel_group: support.id,
        can_add_subscribers_group: support.id,
        can_create_topic_group: admins.id,
        can_subscribe_group: support.id,
    }),
);
const denmark_stream = stream_item(
    make_stream({
        name: "Denmark",
        description: "Vikings and boats, in a serene and cold weather.",
        stream_id: 2,
        subscribed: true,
        can_administer_channel_group: support.id,
        can_add_subscribers_group: support.id,
        can_create_topic_group: members.id,
        can_subscribe_group: support.id,
    }),
);
const netherland_stream = stream_item(
    make_stream({
        name: "The Netherlands",
        description: "The Netherlands, city of dream.",
        stream_id: 3,
        subscribed: false,
        can_administer_channel_group: support.id,
        can_add_subscribers_group: support.id,
        can_create_topic_group: members.id,
        can_subscribe_group: support.id,
    }),
);
const mobile_stream = stream_item(
    make_stream({
        name: "Mobile",
        description: "Mobile development",
        stream_id: 4,
        subscribed: false,
        can_administer_channel_group: support.id,
        can_add_subscribers_group: support.id,
        can_create_topic_group: members.id,
        can_subscribe_group: support.id,
    }),
);
const mobile_team_stream = stream_item(
    make_stream({
        name: "Mobile team",
        description: "Mobile development team",
        stream_id: 5,
        subscribed: true,
        can_administer_channel_group: support.id,
        can_add_subscribers_group: support.id,
        can_create_topic_group: members.id,
        can_subscribe_group: support.id,
    }),
);
const broken_link_stream = stream_item(
    make_stream({
        name: "A* Algorithm",
        description: "A `*` in the stream name produces a broken #**stream>topic** link",
        stream_id: 6,
        subscribed: true,
        can_administer_channel_group: support.id,
        can_add_subscribers_group: support.id,
        can_create_topic_group: members.id,
    }),
);
const empty_topic_only_stream = stream_item(
    make_stream({
        name: "Announce",
        description: "A channel that only allows the empty topic",
        stream_id: 7,
        subscribed: true,
        topics_policy: "empty_topic_only",
        can_administer_channel_group: support.id,
        can_add_subscribers_group: support.id,
        can_create_topic_group: members.id,
    }),
);

stream_data.add_sub_for_tests(sweden_stream);
stream_data.add_sub_for_tests(denmark_stream);
stream_data.add_sub_for_tests(netherland_stream);
stream_data.add_sub_for_tests(mobile_stream);
stream_data.add_sub_for_tests(mobile_team_stream);
stream_data.add_sub_for_tests(broken_link_stream);
stream_data.add_sub_for_tests(empty_topic_only_stream);

const make_emoji = (emoji_dict) => ({
    emoji_name: emoji_dict.name,
    emoji_code: emoji_dict.emoji_code,
    reaction_type: "unicode_emoji",
    is_realm_emoji: false,
    type: "emoji",
});

// Empty query: compare_users_for_dms sorts alphabetically
// by full_name (no direct message history, and no name-length
// tiebreaker without a query).
const sorted_user_list = [
    ali_item, // Ali
    alice_item, // Alice
    cordelia_item, // Cordelia, Lear's daughter
    hal_item, // Earl Hal
    gael_item, // Gaël Twin
    harry_item, // Harry
    hamlet_item, // King Hamlet
    lear_item, // King Lear
    twin1_item, // Mark Twin
    twin2_item, // Mark Twin
    othello_item, // Othello, the Moor of Venice
];

function test(label, f) {
    run_test(label, (helpers) => {
        people.init();
        user_groups.init();
        helpers.override(
            realm,
            "server_supported_permission_settings",
            server_supported_permission_settings,
        );
        helpers.override(realm, "realm_can_access_all_users_group", members.id);
        helpers.override(realm, "realm_direct_message_permission_group", members.id);
        helpers.override(realm, "realm_direct_message_initiator_group", members.id);

        people.add_active_user(ali);
        people.add_active_user(alice);
        people.add_active_user(hamlet);
        people.add_active_user(othello);
        people.add_active_user(cordelia);
        people.add_active_user(lear);
        people.add_active_user(twin1);
        people.add_active_user(twin2);
        people.add_active_user(gael);
        people.add_active_user(hal);
        people.add_active_user(harry);
        people.add_active_user(deactivated_user);
        people.add_cross_realm_user(welcome_bot);
        people.add_cross_realm_user(notification_bot);
        people.deactivate(deactivated_user);
        people.initialize_current_user(hamlet.user_id);

        user_groups.add(hamletcharacters);
        user_groups.add(backend);
        user_groups.add(call_center);
        user_groups.add(support);
        user_groups.add(admins);
        user_groups.add(members);
        user_groups.add(full_members);
        user_groups.add(nobody);

        muted_users.set_muted_users([]);

        f(helpers);
    });
}

test("topics_seen_for", ({override}) => {
    // Initialize topic history with test data, using message_ids
    // to control the recency ordering.
    for (const [message_id, topic_name] of [
        [3, "With Twisted Metal"],
        [2, "acceptance"],
        [1, "civil fears"],
    ]) {
        stream_topic_history.add_message({
            stream_id: denmark_stream.stream_id,
            message_id,
            topic_name,
        });
    }

    override(stream_topic_history_util, "get_server_history", (stream_id) => {
        assert.equal(stream_id, denmark_stream.stream_id);
    });

    assert.deepEqual(ct.topics_seen_for(denmark_stream.stream_id), [
        "With Twisted Metal",
        "acceptance",
        "civil fears",
    ]);

    // Test when the stream doesn't exist (there are no topics)
    assert.deepEqual(ct.topics_seen_for(""), []);
});

test("should_suppress_topic_typeahead", () => {
    // An empty query is never suppressed, even when the empty topic (which
    // renders as a non-empty display name) is among the items.
    assert.ok(!ct.should_suppress_topic_typeahead("", ["", "design"]));

    // An exact match against an existing topic suppresses the typeahead,
    // normalizing case and surrounding whitespace.
    assert.ok(ct.should_suppress_topic_typeahead("design", ["design", "other"]));
    assert.ok(ct.should_suppress_topic_typeahead("  DESIGN ", ["Design"]));

    // An exact match is not suppressed while another topic still matches
    // the query, since the user may be typing toward it.
    assert.ok(!ct.should_suppress_topic_typeahead("design", ["design", "designs"]));
    assert.ok(!ct.should_suppress_topic_typeahead("design", ["design", "my design doc"]));

    // Another topic keeps the typeahead open only if the typeahead would
    // actually display it: a multi-word query must match other topics at
    // a word boundary, not as a mid-word substring ("design office"
    // contains "sign off" but is not a suggestion for it).
    assert.ok(!ct.should_suppress_topic_typeahead("sign off", ["sign off", "sign offsite"]));
    assert.ok(ct.should_suppress_topic_typeahead("sign off", ["sign off", "design office"]));

    // A query that names no existing topic is not suppressed.
    assert.ok(!ct.should_suppress_topic_typeahead("new topic", ["design"]));

    // People suggestions keep the typeahead open even on an exact match.
    assert.ok(!ct.should_suppress_topic_typeahead("design", ["design", cordelia_item]));
});

test("topic typeahead auto-opens when topic creation is disabled", ({
    override,
    override_rewire,
}) => {
    let topic_edit_config;
    override(bootstrap_typeahead, "Typeahead", (_input_element, config) => {
        topic_edit_config = config;
    });

    let topic_creation_allowed = true;
    override_rewire(stream_data, "can_create_new_topics_in_stream", () => topic_creation_allowed);

    ct.initialize_topic_edit_typeahead($.create("fake-topic-edit-input"), "Denmark", false);

    // The same typeahead instance reflects the channel's live permission,
    // so it auto-opens on focus (showOnFocus) and shows for an empty query
    // (helpOnEmptyStrings) exactly when topic creation is disabled.
    topic_creation_allowed = false;
    assert.ok(topic_edit_config.showOnFocus());
    assert.ok(topic_edit_config.helpOnEmptyStrings());

    topic_creation_allowed = true;
    assert.ok(!topic_edit_config.showOnFocus());
    assert.ok(!topic_edit_config.helpOnEmptyStrings());

    // With no channel resolved, topic creation is treated as allowed, so
    // the typeahead does not auto-open.
    ct.initialize_topic_edit_typeahead($.create("fake-missing-stream-input"), "Nonexistent", false);
    topic_creation_allowed = false;
    assert.ok(!topic_edit_config.showOnFocus());
    assert.ok(!topic_edit_config.helpOnEmptyStrings());
});

test("topic typeahead source suppresses an exact-match topic", ({override}) => {
    let topic_edit_config;
    override(bootstrap_typeahead, "Typeahead", (_input_element, config) => {
        topic_edit_config = config;
    });
    override(stream_topic_history_util, "get_server_history", noop);
    // Higher message_ids are more recent, so "design" sorts before
    // "design docs" in the source's output.
    for (const [message_id, topic_name] of [
        [2, "design"],
        [1, "design docs"],
    ]) {
        stream_topic_history.add_message({
            stream_id: netherland_stream.stream_id,
            message_id,
            topic_name,
        });
    }

    ct.initialize_topic_edit_typeahead($.create("fake-source-input"), "The Netherlands", false);

    // "design docs" still has "design" as a substring, so an exact match on
    // "design" keeps the existing topics listed.
    assert.deepEqual(topic_edit_config.source("design"), ["design", "design docs"]);
    // Matching the longest topic exactly leaves nothing to suggest, so the
    // source returns no items and the typeahead stays hidden.
    assert.deepEqual(topic_edit_config.source("design docs"), []);
    // A query that names no existing topic lists the topics as usual.
    assert.deepEqual(topic_edit_config.source("new"), ["design", "design docs"]);
});

test("content_typeahead_selected", ({override}) => {
    const input_element = {
        $element: {},
        type: "textarea",
    };
    let caret_called1 = false;
    let caret_called2 = false;
    let query;
    input_element.$element.caret = function (...args) {
        if (args.length === 0) {
            // .caret() used in split_at_cursor
            caret_called1 = true;
            return query.length;
        }
        caret_called2 = true;
        return this;
    };
    let range_called = false;
    input_element.$element.range = function (...args) {
        const [arg1, arg2] = args;
        // .range() used in setTimeout
        assert.ok(arg2 > arg1);
        range_called = true;
        return this;
    };
    autosize_called = false;
    set_timeout_called = false;

    // emoji
    ct.get_or_set_completing_for_tests("emoji");
    query = ":octo";
    ct.get_or_set_token_for_testing("octo");
    const item = {
        emoji_name: "octopus",
        type: "emoji",
    };

    let actual_value = ct.content_typeahead_selected(item, query, input_element);
    let expected_value = ":octopus: ";
    assert.equal(actual_value, expected_value);

    query = " :octo";
    ct.get_or_set_token_for_testing("octo");
    actual_value = ct.content_typeahead_selected(item, query, input_element);
    expected_value = " :octopus: ";
    assert.equal(actual_value, expected_value);

    query = "{:octo";
    ct.get_or_set_token_for_testing("octo");
    actual_value = ct.content_typeahead_selected(item, query, input_element);
    expected_value = "{ :octopus: ";
    assert.equal(actual_value, expected_value);

    // mention
    ct.get_or_set_completing_for_tests("mention");

    override(compose_validate, "warn_if_mentioning_unsubscribed_user", noop);
    override(
        compose_validate,
        "convert_mentions_to_silent_in_direct_messages",
        (mention_text) => mention_text,
    );

    query = "@**Mark Tw";
    ct.get_or_set_token_for_testing("Mark Tw");
    actual_value = ct.content_typeahead_selected(twin1_item, query, input_element);
    expected_value = "@**Mark Twin|105** ";
    assert.equal(actual_value, expected_value);

    let warned_for_mention = false;
    override(compose_validate, "warn_if_mentioning_unsubscribed_user", (mentioned) => {
        assert.equal(mentioned, othello_item);
        warned_for_mention = true;
    });

    query = "@oth";
    ct.get_or_set_token_for_testing("oth");
    actual_value = ct.content_typeahead_selected(othello_item, query, input_element);
    expected_value = "@**Othello, the Moor of Venice** ";
    assert.equal(actual_value, expected_value);
    assert.ok(warned_for_mention);

    query = "Hello @oth";
    ct.get_or_set_token_for_testing("oth");
    actual_value = ct.content_typeahead_selected(othello_item, query, input_element);
    expected_value = "Hello @**Othello, the Moor of Venice** ";
    assert.equal(actual_value, expected_value);

    query = "@**oth";
    ct.get_or_set_token_for_testing("oth");
    actual_value = ct.content_typeahead_selected(othello_item, query, input_element);
    expected_value = "@**Othello, the Moor of Venice** ";
    assert.equal(actual_value, expected_value);

    query = "@*oth";
    ct.get_or_set_token_for_testing("oth");
    actual_value = ct.content_typeahead_selected(othello_item, query, input_element);
    expected_value = "@**Othello, the Moor of Venice** ";
    assert.equal(actual_value, expected_value);

    query = "@back";
    ct.get_or_set_token_for_testing("back");
    with_overrides(({disallow}) => {
        disallow(compose_validate, "warn_if_mentioning_unsubscribed_user");
        actual_value = ct.content_typeahead_selected(backend, query, input_element);
    });
    expected_value = "@*Backend* ";
    assert.equal(actual_value, expected_value);

    query = "@*back";
    ct.get_or_set_token_for_testing("back");
    actual_value = ct.content_typeahead_selected(backend, query, input_element);
    expected_value = "@*Backend* ";
    assert.equal(actual_value, expected_value);

    // silent mention
    ct.get_or_set_completing_for_tests("silent_mention");
    const silent_hamlet = {
        ...hamlet_item,
        is_silent: true,
    };
    query = "@_kin";
    ct.get_or_set_token_for_testing("kin");
    with_overrides(({disallow}) => {
        disallow(compose_validate, "warn_if_mentioning_unsubscribed_user");
        actual_value = ct.content_typeahead_selected(silent_hamlet, query, input_element);
    });

    expected_value = "@_**King Hamlet** ";
    assert.equal(actual_value, expected_value);

    query = "Hello @_kin";
    ct.get_or_set_token_for_testing("kin");
    actual_value = ct.content_typeahead_selected(silent_hamlet, query, input_element);
    expected_value = "Hello @_**King Hamlet** ";
    assert.equal(actual_value, expected_value);

    query = "@_*kin";
    ct.get_or_set_token_for_testing("kin");
    actual_value = ct.content_typeahead_selected(silent_hamlet, query, input_element);
    expected_value = "@_**King Hamlet** ";
    assert.equal(actual_value, expected_value);

    query = "@_**kin";
    ct.get_or_set_token_for_testing("kin");
    actual_value = ct.content_typeahead_selected(silent_hamlet, query, input_element);
    expected_value = "@_**King Hamlet** ";
    assert.equal(actual_value, expected_value);

    query = "@_back";
    ct.get_or_set_token_for_testing("back");
    const silent_backend = {
        ...backend,
        is_silent: true,
    };
    with_overrides(({disallow}) => {
        disallow(compose_validate, "warn_if_mentioning_unsubscribed_user");
        actual_value = ct.content_typeahead_selected(silent_backend, query, input_element);
    });
    expected_value = "@_*Backend* ";
    assert.equal(actual_value, expected_value);

    query = "@_*back";
    ct.get_or_set_token_for_testing("back");
    actual_value = ct.content_typeahead_selected(silent_backend, query, input_element);
    expected_value = "@_*Backend* ";
    assert.equal(actual_value, expected_value);

    query = "/m";
    ct.get_or_set_completing_for_tests("slash");
    actual_value = ct.content_typeahead_selected(me_command_item, query, input_element);
    expected_value = "/me translated: is …";
    assert.equal(actual_value, expected_value);

    query = "/da";
    ct.get_or_set_completing_for_tests("slash");
    actual_value = ct.content_typeahead_selected(dark_command_item, query, input_element);
    expected_value = "/dark ";
    assert.equal(actual_value, expected_value);

    query = "/ni";
    ct.get_or_set_completing_for_tests("slash");
    actual_value = ct.content_typeahead_selected(dark_command_item, query, input_element);
    expected_value = "/dark ";
    assert.equal(actual_value, expected_value);

    query = "/li";
    ct.get_or_set_completing_for_tests("slash");
    actual_value = ct.content_typeahead_selected(light_command_item, query, input_element);
    expected_value = "/light ";
    assert.equal(actual_value, expected_value);

    query = "/da";
    ct.get_or_set_completing_for_tests("slash");
    actual_value = ct.content_typeahead_selected(light_command_item, query, input_element);
    expected_value = "/light ";
    assert.equal(actual_value, expected_value);

    // stream
    ct.get_or_set_completing_for_tests("stream");
    let warned_for_stream_link = false;
    override(compose_validate, "warn_if_private_stream_is_linked", (linked_stream) => {
        assert.ok(
            linked_stream === sweden_stream ||
                linked_stream === broken_link_stream ||
                linked_stream === empty_topic_only_stream,
        );
        warned_for_stream_link = true;
    });

    // Selecting a channel inserts a complete channel link; the topic
    // typeahead then reopens to offer topic completion (see the
    // get_candidates tests), so no broken `#**channel>` syntax is left
    // behind if the user dismisses it without picking a topic.
    query = "#swed";
    ct.get_or_set_token_for_testing("swed");
    actual_value = ct.content_typeahead_selected(sweden_stream, query, input_element);
    expected_value = "#**Sweden**";
    assert.equal(actual_value, expected_value);

    query = "Hello #swed";
    ct.get_or_set_token_for_testing("swed");
    actual_value = ct.content_typeahead_selected(sweden_stream, query, input_element);
    expected_value = "Hello #**Sweden**";
    assert.equal(actual_value, expected_value);

    query = "#**swed";
    ct.get_or_set_token_for_testing("swed");
    actual_value = ct.content_typeahead_selected(sweden_stream, query, input_element);
    expected_value = "#**Sweden**";
    assert.equal(actual_value, expected_value);

    // A channel name with special characters falls back to the markdown
    // link syntax, still as a complete link.
    query = "#**A* al";
    ct.get_or_set_token_for_testing("A* al");
    actual_value = ct.content_typeahead_selected(broken_link_stream, query, input_element);
    expected_value = "[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)";
    assert.equal(actual_value, expected_value);

    query = "#>";
    ct.get_or_set_token_for_testing("#");
    actual_value = ct.content_typeahead_selected(broken_link_stream, query, input_element);
    expected_value = "[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)";
    assert.equal(actual_value, expected_value);

    // A channel that only allows the empty topic gets a complete channel
    // link, and no topic typeahead is reopened for it.
    query = "#Announ";
    ct.get_or_set_token_for_testing("Announ");
    actual_value = ct.content_typeahead_selected(empty_topic_only_stream, query, input_element);
    expected_value = "#**Announce**";
    assert.equal(actual_value, expected_value);

    // Pressing ">" on such a channel still opens topic completion via the
    // partial syntax, for the user who explicitly wants a topic link.
    query = "#Announ";
    ct.get_or_set_token_for_testing("Announ");
    actual_value = ct.content_typeahead_selected(empty_topic_only_stream, query, input_element, {
        type: "keydown",
        key: ">",
    });
    expected_value = "#**Announce>";
    assert.equal(actual_value, expected_value);

    // topic_list
    ct.get_or_set_completing_for_tests("topic_list");

    query = "Hello #**Sweden>test";
    ct.get_or_set_token_for_testing("test");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "testing",
            topic_display_name: "testing",
            type: "topic_list",
            used_syntax_prefix: "#**",
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello #**Sweden>testing** ";
    assert.equal(actual_value, expected_value);

    query = "Hello #**Sweden>";
    ct.get_or_set_token_for_testing("");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "testing",
            topic_display_name: "testing",
            type: "topic_list",
            used_syntax_prefix: "#**",
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello #**Sweden>testing** ";
    assert.equal(actual_value, expected_value);

    // shortcut syntax for topic_list
    compose_state.set_stream_id(sweden_stream.stream_id);
    query = "Hello #>";
    ct.get_or_set_token_for_testing("");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "testing",
            type: "topic_list",
            used_syntax_prefix: "#>",
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello #**Sweden>testing** ";
    assert.equal(actual_value, expected_value);

    query = "Hello #**Sweden>";
    ct.get_or_set_token_for_testing("");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "Sweden",
            topic_display_name: "Sweden",
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_channel_link: false,
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello #**Sweden>Sweden** ";
    assert.equal(actual_value, expected_value);

    query = "Hello #**Sweden>general";
    ct.get_or_set_token_for_testing("");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "",
            topic_display_name: get_final_topic_display_name(""),
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_channel_link: false,
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = `Hello #**Sweden>** `;
    assert.equal(actual_value, expected_value);

    ct.get_or_set_token_for_testing("");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "Sweden",
            topic_display_name: "Sweden",
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_channel_link: true,
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello #**Sweden** ";
    assert.equal(actual_value, expected_value);

    compose_state.set_stream_id(broken_link_stream.stream_id);
    query = "Hello #>";
    ct.get_or_set_token_for_testing("");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "",
            type: "topic_list",
            used_syntax_prefix: "#>",
            is_channel_link: true,
            stream_data: {
                name: "A* Algorithm",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello [#A* Algorithm](#narrow/channel/6-A.2A-Algorithm) ";
    assert.equal(actual_value, expected_value);

    query = "Hello #**A* Algorithm>";
    ct.get_or_set_token_for_testing("");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "fast",
            topic_display_name: "fast",
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_channel_link: false,
            stream_data: {
                name: "A* Algorithm",
            },
        },
        query,
        input_element,
    );
    expected_value =
        "Hello [#A* Algorithm > fast](#narrow/channel/6-A.2A-Algorithm/topic/fast) ";
    assert.equal(actual_value, expected_value);

    // Selecting a topic from the complete channel link form (the link
    // inserted on channel selection, followed by the partially typed
    // topic) rewrites the whole span into the full stream+topic link.
    query = "Hello #**Sweden**plan";
    ct.get_or_set_token_for_testing("plan");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "planning",
            topic_display_name: "planning",
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_channel_link: false,
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello #**Sweden>planning** ";
    assert.equal(actual_value, expected_value);

    // The same, but for the complete fallback markdown link form used
    // for channel names with special characters.
    query = "Hello [#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)fas";
    ct.get_or_set_token_for_testing("fas");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "fast",
            topic_display_name: "fast",
            type: "topic_list",
            used_syntax_prefix: "[#",
            is_channel_link: false,
            stream_data: {
                name: "A* Algorithm",
            },
        },
        query,
        input_element,
    );
    expected_value =
        "Hello [#A* Algorithm > fast](#narrow/channel/6-A.2A-Algorithm/topic/fast) ";
    assert.equal(actual_value, expected_value);

    // Selecting the "(link to channel)" option from the complete link
    // form keeps the channel link as-is, dropping any partially typed
    // topic text.
    query = "Hello #**Sweden**plan";
    ct.get_or_set_token_for_testing("plan");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "Sweden",
            topic_display_name: "Sweden",
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_channel_link: true,
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
    );
    expected_value = "Hello #**Sweden** ";
    assert.equal(actual_value, expected_value);

    // Escape resolves the partial stream+topic syntax to a channel
    // link, keeping any partially typed topic as plain text.
    query = "Hello #**Sweden>plan";
    ct.get_or_set_token_for_testing("plan");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "planning",
            topic_display_name: "planning",
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_channel_link: false,
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
        {key: "Escape"},
    );
    expected_value = "Hello #**Sweden** plan";
    assert.equal(actual_value, expected_value);

    // Escape with the shortcut syntax for the channel being composed to.
    compose_state.set_stream_id(sweden_stream.stream_id);
    query = "Hello #>plan";
    ct.get_or_set_token_for_testing("plan");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "planning",
            topic_display_name: "planning",
            type: "topic_list",
            used_syntax_prefix: "#>",
            is_channel_link: false,
            stream_data: {
                name: "Sweden",
            },
        },
        query,
        input_element,
        {key: "Escape"},
    );
    expected_value = "Hello #**Sweden** plan";
    assert.equal(actual_value, expected_value);

    // Escape with the fallback markdown link syntax used for channel
    // names that would produce broken `#**channel**` links.
    query = "Hello [#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)>fast";
    ct.get_or_set_token_for_testing("fast");
    actual_value = ct.content_typeahead_selected(
        {
            topic: "fast",
            topic_display_name: "fast",
            type: "topic_list",
            used_syntax_prefix: "[#",
            is_channel_link: false,
            stream_data: {
                name: "A* Algorithm",
            },
        },
        query,
        input_element,
        {key: "Escape"},
    );
    expected_value = "Hello [#A* Algorithm](#narrow/channel/6-A.2A-Algorithm) fast";
    assert.equal(actual_value, expected_value);

    // syntax
    ct.get_or_set_completing_for_tests("syntax");

    query = "~~~p";
    ct.get_or_set_token_for_testing("p");
    actual_value = ct.content_typeahead_selected(language_item("python"), query, input_element);
    expected_value = "~~~python\n\n~~~";
    assert.equal(actual_value, expected_value);

    query = "Hello ~~~p";
    ct.get_or_set_token_for_testing("p");
    actual_value = ct.content_typeahead_selected(language_item("python"), query, input_element);
    expected_value = "Hello ~~~python\n\n~~~";
    assert.equal(actual_value, expected_value);

    query = "```p";
    ct.get_or_set_token_for_testing("p");
    actual_value = ct.content_typeahead_selected(language_item("python"), query, input_element);
    expected_value = "```python\n\n```";
    assert.equal(actual_value, expected_value);

    query = "```spo";
    ct.get_or_set_token_for_testing("spo");
    actual_value = ct.content_typeahead_selected(language_item("spoiler"), query, input_element);
    expected_value = "```spoiler translated: Header\n\n```";
    assert.equal(actual_value, expected_value);

    // Test special case to not close code blocks if there is text afterward
    query = "```p\nsome existing code";
    ct.get_or_set_token_for_testing("p");
    input_element.$element.caret = () => 4; // Put cursor right after ```p
    actual_value = ct.content_typeahead_selected(language_item("python"), query, input_element);
    expected_value = "```python\nsome existing code";
    assert.equal(actual_value, expected_value);

    ct.get_or_set_completing_for_tests("something-else");

    query = "foo";
    actual_value = ct.content_typeahead_selected({}, query, input_element);
    expected_value = query;
    assert.equal(actual_value, expected_value);

    assert.ok(caret_called1);
    assert.ok(caret_called2);
    assert.ok(range_called);
    assert.ok(autosize_called);
    assert.ok(set_timeout_called);
    assert.ok(warned_for_stream_link);
});

function sorted_names_from(subs) {
    return subs.map((sub) => sub.name).toSorted();
}

const sweden_topics_to_show = [
    "<&>",
    "even more ice",
    "furniture",
    "ice",
    "kronor",
    "more ice",
    "",
];

// Initialize stream_topic_history with test data so we can query
// get_recent_topic_names without rewiring. Assign message_ids in
// reverse so the recency order matches the alphabetical order of
// sweden_topics_to_show.
for (const [index, topic_name] of sweden_topics_to_show.entries()) {
    stream_topic_history.add_message({
        stream_id: sweden_stream.stream_id,
        message_id: sweden_topics_to_show.length - index,
        topic_name,
    });
}

test("initialize", ({override, override_rewire, mock_template}) => {
    mock_banners();
    $("#private_message_recipient").set_parent($.create("pm-recipient-container"));

    let pill_items = [];
    let cleared = false;
    let appended_names = [];
    override(input_pill, "create", () => ({
        clear_text() {
            cleared = true;
        },
        items: () => pill_items,
        onPillCreate() {},
        onPillRemove() {},
        appendValidatedData(item) {
            appended_names.push(user_pill.get_display_value_from_item(item));
        },
    }));
    compose_pm_pill.initialize({
        on_pill_create_or_remove: compose_recipient.update_compose_area_placeholder_text,
    });

    let expected_value;
    override(realm, "custom_profile_field_types", {
        PRONOUNS: {id: 8, name: "Pronouns"},
    });

    mock_template("typeahead_list_item.hbs", true, (data, html) => {
        assert.equal(typeof data.primary, "string");
        if (data.has_secondary) {
            assert.equal(typeof data.secondary, "string");
        } else {
            assert.equal(data.has_secondary, false);
        }
        assert.equal(typeof data.has_image, "boolean");
        return html;
    });
    override(stream_topic_history_util, "get_server_history", noop);
    override_rewire(ct, "max_num_items", 17);

    let topic_typeahead_called = false;
    let pm_recipient_typeahead_called = false;
    let compose_textarea_typeahead_called = false;
    message_lists.current = {
        data: {
            participants: {
                visible() {
                    return new Set([104]);
                },
            },
        },
    };
    override(pm_conversations, "get_partners", () => [100]);
    override(bootstrap_typeahead, "Typeahead", (input_element, options) => {
        switch (input_element.$element[0]) {
            case $("input#stream_message_recipient_topic")[0]: {
                compose_state.set_stream_id(sweden_stream.stream_id);
                const lear_user_data = [
                    {
                        type: "user",
                        user: {
                            ...lear,
                            name_with_diacritics_removed: "King Lear",
                        },
                    },
                ];
                const hamlet_user_data = [
                    {
                        type: "user",
                        user: {
                            ...hamlet,
                            name_with_diacritics_removed: "King Hamlet",
                        },
                    },
                ];
                let actual_value = options.source();
                // Topics should be sorted alphabetically, not by addition order.
                let expected_value = sweden_topics_to_show;
                assert.deepEqual(actual_value, expected_value);

                let query = "lear";
                actual_value = options.source(query);
                expected_value = [...lear_user_data, ...sweden_topics_to_show];
                assert.deepEqual(actual_value, expected_value);

                query = "haml";
                actual_value = options.source(query);
                expected_value = [...hamlet_user_data, ...sweden_topics_to_show];
                assert.deepEqual(actual_value, expected_value);

                // options.item_html()
                options.query = "Kro";
                actual_value = options.item_html()("kronor");
                expected_value =
                    '<div class="typeahead-content">\n' +
                    '    <div class="typeahead-text-container">\n' +
                    '        <strong class="typeahead-strong-section">kronor</strong>    </div>\n' +
                    "</div>\n";
                assert.equal(actual_value, expected_value);

                // Highlighted content should be escaped.
                options.query = "<";
                actual_value = options.item_html()("<&>");
                expected_value =
                    '<div class="typeahead-content">\n' +
                    '    <div class="typeahead-text-container">\n' +
                    '        <strong class="typeahead-strong-section">&lt;&amp;&gt;</strong>    </div>\n' +
                    "</div>\n";
                assert.equal(actual_value, expected_value);

                options.query = "even m";
                actual_value = options.item_html()("even more ice");
                expected_value =
                    '<div class="typeahead-content">\n' +
                    '    <div class="typeahead-text-container">\n' +
                    '        <strong class="typeahead-strong-section">even more ice</strong>    </div>\n' +
                    "</div>\n";
                assert.equal(actual_value, expected_value);

                // options.sorter()
                //
                // Notice that alphabetical sorting isn't managed by this sorter,
                // it is a result of the topics already being sorted after adding
                // them with add_topic().
                query = "furniture";
                actual_value = options.sorter(["furniture"], query);
                expected_value = ["furniture"];
                assert.deepEqual(actual_value, expected_value);

                // A literal match at the beginning of an element puts it at the top.
                query = "ice";
                actual_value = options.sorter(["even more ice", "ice", "more ice"], query);
                expected_value = ["ice", "even more ice", "more ice"];
                assert.deepEqual(actual_value, expected_value);

                // User dm suggestion is shown at last.
                query = "hamlet";
                actual_value = options.sorter(
                    [...hamlet_user_data, "even more hamlet", "hamlet", "more hamlet"],
                    query,
                );
                expected_value = ["hamlet", "even more hamlet", "more hamlet", ...hamlet_user_data];
                assert.deepEqual(actual_value, expected_value);

                // The sorter should return the query as the first element if there
                // isn't a topic with such name only if user has permission to
                // create new topics.
                // This only happens if typeahead is providing other suggestions.
                override(current_user, "user_id", 102);
                query = "e"; // Letter present in "furniture" and "ice"
                actual_value = options.sorter(["furniture", "ice"], query);
                expected_value = ["e", "furniture", "ice"];
                assert.deepEqual(actual_value, expected_value);

                override(current_user, "user_id", 100);
                actual_value = options.sorter(["furniture", "ice"], query);
                expected_value = ["furniture", "ice"];
                assert.deepEqual(actual_value, expected_value);

                // Suggest the query if this query doesn't match any existing topic.
                query = "non-existing-topic";
                actual_value = options.sorter([], query);
                expected_value = [];
                assert.deepEqual(actual_value, expected_value);

                topic_typeahead_called = true;

                // Unset the stream.
                compose_state.set_stream_id("");

                break;
            }
            case $("#private_message_recipient")[0]: {
                pill_items = [];

                // Empty query: alphabetical by full_name (no direct
                // message history), then groups, then bot.
                let actual_value = options.source("");
                let expected_value = [
                    ali_item, // Ali
                    alice_item, // Alice
                    cordelia_item, // Cordelia, Lear's daughter
                    hal_item, // Earl Hal
                    gael_item, // Gaël Twin
                    harry_item, // Harry
                    hamlet_item, // King Hamlet
                    lear_item, // King Lear
                    twin1_item, // Mark Twin
                    twin2_item, // Mark Twin
                    othello_item, // Othello, the Moor of Venice
                    hamletcharacters,
                    backend,
                    call_center,
                    admins,
                    members,
                    welcome_bot_item,
                ];
                assert.deepEqual(actual_value, expected_value);

                function matcher(query, person) {
                    query = typeahead.clean_query_lowercase(query);
                    return typeahead_helper.query_matches_person(query, person);
                }

                let query = "el"; // Matches both "othELlo" and "cordELia"
                assert.equal(matcher(query, othello_item), true);
                assert.equal(matcher(query, cordelia_item), true);

                query = "bender"; // Doesn't exist
                assert.equal(matcher(query, othello_item), false);
                assert.equal(matcher(query, cordelia_item), false);

                query = "gael";
                assert.equal(matcher(query, gael_item), true);

                query = "Gaël";
                assert.equal(matcher(query, gael_item), true);

                query = "gaël";
                assert.equal(matcher(query, gael_item), true);

                query = "gaël twi";
                assert.equal(matcher(query, gael_item), true);

                query = "gael twi";
                assert.equal(matcher(query, gael_item), true);

                // Don't make suggestions if the last name only has whitespaces
                // (we're between typing names).
                query = "[email protected],     ";
                assert.equal(matcher(query, othello_item), false);
                assert.equal(matcher(query, cordelia_item), false);

                // query = '[email protected],, , cord';
                query = "cord";
                assert.equal(matcher(query, othello_item), false);
                assert.equal(matcher(query, cordelia_item), true);

                // If the user is already in the list, typeahead doesn't include it
                // again.
                query = "[email protected], cord";
                assert.equal(matcher(query, othello_item), false);
                assert.equal(matcher(query, cordelia_item), false);

                // Matching by email
                query = "oth";
                deactivated_user.delivery_email = null;
                assert.equal(matcher(query, deactivated_user_item), false);

                deactivated_user.delivery_email = "[email protected]";
                assert.equal(matcher(query, deactivated_user_item), true);

                function sorter(query, people) {
                    return typeahead_helper.sort_recipients({
                        users: people,
                        query,
                        current_stream_id: compose_state.stream_id(),
                        current_topic: compose_state.topic(),
                    });
                }

                // The sorter's output has the items that match the query from the
                // beginning first, and then the rest of them in REVERSE order of
                // the input.
                query = "othello";
                actual_value = sorter(query, [othello_item]);
                expected_value = [othello_item];
                assert.deepEqual(actual_value, expected_value);

                query = "Ali";
                actual_value = sorter(query, [alice_item, ali_item]);
                expected_value = [ali_item, alice_item];
                assert.deepEqual(actual_value, expected_value);

                // A literal match at the beginning of an element puts it at the top.
                query = "co"; // Matches everything ("[email protected]")
                actual_value = sorter(query, [othello_item, deactivated_user_item, cordelia_item]);
                expected_value = [cordelia_item, deactivated_user_item, othello_item];
                actual_value.sort((a, b) => a.user.user_id - b.user.user_id);
                expected_value.sort((a, b) => a.user.user_id - b.user.user_id);
                assert.deepEqual(actual_value, expected_value);

                query = "non-existing-user";
                actual_value = sorter(query, []);
                expected_value = [];
                assert.deepEqual(actual_value, expected_value);

                // Adds a `no break-space` at the end. This should fail
                // if there wasn't any logic replacing `no break-space`
                // with normal space.
                query = "cordelia, lear's\u{A0}";
                assert.equal(matcher(query, cordelia_item), true);
                assert.equal(matcher(query, othello_item), false);

                const event = {
                    target: "#doesnotmatter",
                };

                // options.updater()
                options.query = "othello";
                appended_names = [];
                options.updater(othello_item, event);
                assert.deepEqual(appended_names, ["Othello, the Moor of Venice"]);

                options.query = "[email protected], cor";
                appended_names = [];
                options.updater(cordelia_item, event);
                assert.deepEqual(appended_names, ["Cordelia, Lear's daughter"]);

                const click_event = {type: "click", target: "#doesnotmatter"};
                options.query = "othello";
                // Focus lost (caused by the click event in the typeahead list)
                $("#private_message_recipient").trigger("blur");
                appended_names = [];
                options.updater(othello_item, click_event);
                assert.deepEqual(appended_names, ["Othello, the Moor of Venice"]);

                cleared = false;
                options.query = "hamletchar";
                appended_names = [];
                options.updater(hamletcharacters, event);
                assert.deepEqual(appended_names, ["King Lear"]);
                assert.ok(cleared);

                pill_items = [{user_id: lear.user_id, type: "user"}];
                appended_names = [];
                cleared = false;
                options.updater(hamletcharacters, event);
                assert.deepEqual(appended_names, []);
                assert.ok(cleared);

                pm_recipient_typeahead_called = true;

                break;
            }
            case $("textarea#compose-textarea")[0]: {
                // options.source()
                //
                // For now we only test that get_sorted_filtered_items has been
                // properly set as the .source(). All its features are tested later on
                // in test_begins_typeahead().
                const input_element = {
                    $element: {},
                    type: "input",
                };
                let caret_called = false;
                input_element.$element.caret = () => {
                    caret_called = true;
                    return 7;
                };
                let actual_value = options.source("test #s", input_element);
                assert.deepEqual(sorted_names_from(actual_value), ["Sweden", "The Netherlands"]);
                assert.ok(caret_called);

                othello.delivery_email = "[email protected]";
                // options.item_html()
                //
                // Again, here we only verify that the item_html has been set to
                // content_item_html.
                ct.get_or_set_completing_for_tests("mention");
                ct.get_or_set_token_for_testing("othello");
                actual_value = options.item_html("othello")(othello_item);
                expected_value =
                    '<div class="typeahead-content">\n' +
                    '        <div class="typeahead-image">\n' +
                    `            <img class="typeahead-image-avatar" src="/avatar/${othello.user_id}" />\n` +
                    '            <span class="zulip-icon zulip-icon-user-circle-offline user-circle-offline user-circle"></span>\n' +
                    "        </div>\n" +
                    '    <div class="typeahead-text-container">\n' +
                    '        <strong class="typeahead-strong-section">Othello, the Moor of Venice</strong>        <span class="autocomplete_secondary">[email protected]</span>' +
                    "    </div>\n" +
                    "</div>\n";
                assert.equal(actual_value, expected_value);
                // Reset the email such that this does not affect further tests.
                othello.delivery_email = null;

                ct.get_or_set_completing_for_tests("mention");
                ct.get_or_set_token_for_testing("hamletcharacters");
                actual_value = options.item_html("hamletcharacters")(hamletcharacters);
                expected_value =
                    '<div class="typeahead-content">\n' +
                    '        <i class="typeahead-image zulip-icon zulip-icon-user-group" aria-hidden="true"></i>\n' +
                    '        <div class="typeahead-text-container">\n' +
                    '        <strong class="typeahead-strong-section">hamletcharacters</strong>        <span class="autocomplete_secondary">Characters of Hamlet</span>' +
                    "    </div>\n" +
                    "</div>\n";
                assert.equal(actual_value, expected_value);

                // matching
                let matcher = typeahead.get_emoji_matcher("ta");
                assert.equal(matcher(make_emoji(emoji_tada)), true);
                assert.equal(matcher(make_emoji(emoji_moneybag)), false);

                matcher = ct.get_stream_matcher("swed");
                assert.equal(matcher(sweden_stream), true);
                assert.equal(matcher(denmark_stream), false);

                matcher = ct.get_language_matcher("py");
                assert.equal(matcher("python"), true);
                assert.equal(matcher("javascript"), false);

                // Language matching is diacritics-agnostic: a diacritic query
                // matches an ASCII language name and vice versa.
                matcher = ct.get_language_matcher("café");
                assert.equal(matcher("cafe"), true);
                matcher = ct.get_language_matcher("cafe");
                assert.equal(matcher("café"), true);

                // options.sorter()
                actual_value = typeahead.sort_emojis(
                    [make_emoji(emoji_stadium), make_emoji(emoji_tada)],
                    "ta",
                );
                expected_value = [make_emoji(emoji_tada), make_emoji(emoji_stadium)];
                assert.deepEqual(actual_value, expected_value);

                actual_value = typeahead.sort_emojis(
                    [make_emoji(emoji_thermometer), make_emoji(emoji_thumbs_up)],
                    "th",
                );
                expected_value = [make_emoji(emoji_thumbs_up), make_emoji(emoji_thermometer)];
                assert.deepEqual(actual_value, expected_value);

                actual_value = typeahead.sort_emojis(
                    [make_emoji(emoji_headphones), make_emoji(emoji_heart)],
                    "he",
                );
                expected_value = [make_emoji(emoji_heart), make_emoji(emoji_headphones)];
                assert.deepEqual(actual_value, expected_value);

                actual_value = typeahead_helper.sort_slash_commands(
                    [my_command_item, me_command_item],
                    "m",
                );
                expected_value = [me_command_item, my_command_item];
                assert.deepEqual(actual_value, expected_value);

                actual_value = typeahead_helper.sort_slash_commands(
                    [dark_command_item, light_command_item],
                    "da",
                );
                expected_value = [dark_command_item, light_command_item];
                assert.deepEqual(actual_value, expected_value);

                actual_value = typeahead_helper.sort_streams([sweden_stream, denmark_stream], "de");
                expected_value = [denmark_stream, sweden_stream];
                assert.deepEqual(actual_value, expected_value);

                // Matches in the descriptions affect the order as well.
                // Testing "co" for "cold", in both streams' description. It's at the
                // beginning of Sweden's description, so that one should go first.
                actual_value = typeahead_helper.sort_streams([denmark_stream, sweden_stream], "co");
                expected_value = [sweden_stream, denmark_stream];
                assert.deepEqual(actual_value, expected_value);

                actual_value = typeahead_helper.sort_languages(
                    [language_item("abap"), language_item("applescript")],
                    "ap",
                );
                expected_value = [language_item("applescript"), language_item("abap")];
                assert.deepEqual(actual_value, expected_value);

                const serbia_stream = {
                    name: "Serbia",
                    description: "Snow and cold",
                    stream_id: 3,
                    subscribed: false,
                    type: "stream",
                };
                // Subscribed stream is active
                override(
                    user_settings,
                    "demote_inactive_streams",
                    settings_config.demote_inactive_streams_values.never.code,
                );

                stream_list_sort.set_filter_out_inactives();
                actual_value = typeahead_helper.sort_streams([sweden_stream, serbia_stream], "s");
                expected_value = [sweden_stream, serbia_stream];
                assert.deepEqual(actual_value, expected_value);
                // Subscribed stream is inactive
                override(
                    user_settings,
                    "demote_inactive_streams",
                    settings_config.demote_inactive_streams_values.always.code,
                );

                stream_list_sort.set_filter_out_inactives();
                actual_value = typeahead_helper.sort_streams([sweden_stream, serbia_stream], "s");
                expected_value = [sweden_stream, serbia_stream];
                assert.deepEqual(actual_value, expected_value);

                actual_value = typeahead_helper.sort_streams(
                    [denmark_stream, serbia_stream],
                    "ser",
                );
                expected_value = [serbia_stream, denmark_stream];
                assert.deepEqual(actual_value, expected_value);

                compose_textarea_typeahead_called = true;

                break;
            }
            // No default
        }
    });

    override(user_settings, "enter_sends", false);
    let compose_finish_called = false;
    function finish() {
        compose_finish_called = true;
    }

    ct.initialize({
        on_enter_send: finish,
    });

    // the UI of selecting a stream is tested in puppeteer tests.
    compose_state.set_stream_id(sweden_stream.stream_id);

    const $stub_target = $.create("<stub-target>");
    let event = {
        type: "keydown",
        key: "Tab",
        shiftKey: false,
        target: "<stub-target>",
        preventDefault: noop,
        stopPropagation: noop,
    };
    $stub_target.attr("id", "stream_message_recipient_topic");
    $("form#send_message_form").trigger(event);
    $stub_target.attr("id", "compose-textarea");
    $("form#send_message_form").trigger(event);
    $stub_target.attr("id", "some_non_existing_id");
    $("form#send_message_form").trigger(event);

    $("textarea#compose-textarea")[0].selectionStart = 0;
    $("textarea#compose-textarea")[0].selectionEnd = 0;
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "\n");
    });

    event.key = "Enter";
    $stub_target.attr("id", "stream_message_recipient_topic");
    $("form#send_message_form").trigger(event);
    $stub_target.attr("id", "compose-textarea");
    override(user_settings, "enter_sends", false);
    event.metaKey = true;

    $("form#send_message_form").trigger(event);
    assert.ok(compose_finish_called);
    event.metaKey = false;
    event.ctrlKey = true;
    $("form#send_message_form").trigger(event);
    override(user_settings, "enter_sends", true);
    event.ctrlKey = false;
    event.altKey = true;
    $("form#send_message_form").trigger(event);

    // Cover cases where there's at least one character there.

    // Test automatic bulleting.
    $("textarea#compose-textarea").val("- List item 1\n- List item 2");
    $("textarea#compose-textarea")[0].selectionStart = 27;
    $("textarea#compose-textarea")[0].selectionEnd = 27;
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "\n- ");
    });
    $("form#send_message_form").trigger(event);

    // Test removal of bullet.
    $("textarea#compose-textarea").val("- List item 1\n- List item 2\n- ");
    $("textarea#compose-textarea")[0].selectionStart = 30;
    $("textarea#compose-textarea")[0].selectionEnd = 30;
    $("textarea#compose-textarea")[0].setSelectionRange = (start, end) => {
        assert.equal(start, 28);
        assert.equal(end, 30);
    };
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "");
    });
    $("form#send_message_form").trigger(event);

    // Test automatic numbering.
    $("textarea#compose-textarea").val("1. List item 1\n2. List item 2");
    $("textarea#compose-textarea")[0].selectionStart = 29;
    $("textarea#compose-textarea")[0].selectionEnd = 29;
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "\n3. ");
    });
    $("form#send_message_form").trigger(event);

    // Test removal of numbering.
    $("textarea#compose-textarea").val("1. List item 1\n2. List item 2\n3. ");
    $("textarea#compose-textarea")[0].selectionStart = 33;
    $("textarea#compose-textarea")[0].selectionEnd = 33;
    $("textarea#compose-textarea")[0].setSelectionRange = (start, end) => {
        assert.equal(start, 30);
        assert.equal(end, 33);
    };
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "");
    });
    $("form#send_message_form").trigger(event);

    // Test automatic bulleting with indentation (sub-list).
    $("textarea#compose-textarea").val("- Message 1\n  - Message 2");
    $("textarea#compose-textarea")[0].selectionStart = 25;
    $("textarea#compose-textarea")[0].selectionEnd = 25;
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "\n  - ");
    });
    $("form#send_message_form").trigger(event);

    // Test removal of indented bullet.
    $("textarea#compose-textarea").val("- Message 1\n  - Message 2\n  - ");
    $("textarea#compose-textarea")[0].selectionStart = 30;
    $("textarea#compose-textarea")[0].selectionEnd = 30;
    $("textarea#compose-textarea")[0].setSelectionRange = (start, end) => {
        assert.equal(start, 26);
        assert.equal(end, 30);
    };
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "");
    });
    $("form#send_message_form").trigger(event);

    // Test automatic numbering with indentation (sub-list).
    $("textarea#compose-textarea").val("- Message 1\n  1. Message 2");
    $("textarea#compose-textarea")[0].selectionStart = 26;
    $("textarea#compose-textarea")[0].selectionEnd = 26;
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "\n  2. ");
    });
    $("form#send_message_form").trigger(event);

    // Test removal of indented numbering.
    $("textarea#compose-textarea").val("- Message 1\n  1. Message 2\n  1. ");
    $("textarea#compose-textarea")[0].selectionStart = 32;
    $("textarea#compose-textarea")[0].selectionEnd = 32;
    $("textarea#compose-textarea")[0].setSelectionRange = (start, end) => {
        assert.equal(start, 27);
        assert.equal(end, 32);
    };
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "");
    });
    $("form#send_message_form").trigger(event);

    $("textarea#compose-textarea").val("A");
    $("textarea#compose-textarea")[0].selectionStart = 4;
    $("textarea#compose-textarea")[0].selectionEnd = 4;
    override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {
        assert.equal(content, "\n");
    });
    event.altKey = false;
    event.metaKey = true;
    $("form#send_message_form").trigger(event);
    $stub_target.attr("id", "private_message_recipient");
    $("form#send_message_form").trigger(event);

    event.key = "a";
    $("form#send_message_form").trigger(event);

    // the UI of selecting a stream is tested in puppeteer tests.
    compose_state.set_stream_id(sweden_stream.stream_id);
    // handle_keyup()
    event = {
        type: "keydown",
        key: "Enter",
        target: "<stub-target>",
        preventDefault: noop,
    };
    $stub_target.attr("id", "stream_message_recipient_topic");
    // We trigger keydown in order to make nextFocus !== false
    $("form#send_message_form").trigger(event);
    $("input#stream_message_recipient_topic").off("mouseup");
    event.type = "keyup";
    $("form#send_message_form").trigger(event);
    event.key = "Tab";
    event.shiftKey = false;
    $("form#send_message_form").trigger(event);
    event.key = "a";
    $("form#send_message_form").trigger(event);

    $("input#stream_message_recipient_topic").off("focus");
    $("#private_message_recipient").off("focus");
    $("form#send_message_form").off("keydown");
    $("form#send_message_form").off("keyup");
    $("#private_message_recipient").off("blur");
    ct.initialize({
        on_enter_send: finish,
    });

    // Now let's make sure that all the stub functions have been called
    // during the initialization.
    assert.ok(topic_typeahead_called);
    assert.ok(pm_recipient_typeahead_called);
    assert.ok(compose_textarea_typeahead_called);
});

test("get_person_suggestion_for_topic_typeahead excludes deactivated users", ({override}) => {
    override(pm_conversations, "get_partners", () => []);

    // Deactivated user from participants should be excluded.
    message_lists.current = {
        data: {
            participants: {
                visible: () => new Set([deactivated_user.user_id]),
            },
        },
    };
    assert.deepEqual(ct.get_person_suggestion_for_topic_typeahead("deactivated"), []);

    // Deactivated user from DM partners should be excluded.
    message_lists.current = undefined;
    override(pm_conversations, "get_partners", () => [deactivated_user.user_id]);
    assert.deepEqual(ct.get_person_suggestion_for_topic_typeahead("deactivated"), []);

    // Active user from DM partners should be included.
    override(pm_conversations, "get_partners", () => [lear.user_id]);
    const results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.ok(results[0].user.user_id === lear.user_id);
});

test("get_person_suggestion_for_topic_typeahead respects DM permissions", ({override}) => {
    function set_visible_participants(user_ids) {
        message_lists.current = {
            data: {
                participants: {
                    visible: () => new Set(user_ids),
                },
            },
        };
    }
    // Set message history to empty
    override(pm_conversations, "get_partners", () => []);
    override(message_util, "get_direct_message_permission_hints", () => ({
        is_known_empty_conversation: true,
        is_local_echo_safe: true,
    }));

    // Bot suggestion doesn't show up if there is no past conversation.
    override(realm, "realm_direct_message_permission_group", nobody.id);
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    let results = ct.get_person_suggestion_for_topic_typeahead("notification");
    assert.deepEqual(results, []);

    // Don't show suggestion when sender/recipient is in direct_message_permission_group
    // but the sender isn't in initiator group.
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id, hamlet.user_id],
        direct_subgroups: [],
    });
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.deepEqual(results, []);

    set_visible_participants([lear.user_id]);
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.deepEqual(results, []);

    // When no conversation history exists between the sender and recipient,
    // we show the suggestion if the recipient is visible in the current narrow’s
    // participants and either the sender or recipient is in the
    // direct_message_permission_group, and the sender is also in the initiator group.
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_initiator_group", {
        direct_members: [hamlet.user_id],
        direct_subgroups: [],
    });
    set_visible_participants([]);
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.deepEqual(results, []);

    // Set current narrow participant
    set_visible_participants([lear.user_id]);
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.equal(results[0].user.user_id, lear.user_id);

    // We don't show suggestion when sender doesn't have initiator
    // permission when past conversation doesn't exist.
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [hamlet.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.deepEqual(results, []);

    // We dont show suggestion if the sender doesn't have initiator permission
    // even if recipient is in permission group
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.deepEqual(results, []);

    // Don't show suggestion if direct message is disabled.
    override(realm, "realm_direct_message_permission_group", nobody.id);
    override(realm, "realm_direct_message_initiator_group", {
        direct_members: [hamlet.user_id, lear.user_id],
        direct_subgroups: [],
    });
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.deepEqual(results, []);

    // Set message history
    override(pm_conversations, "get_partners", () => [notification_bot.user_id, lear.user_id]);
    override(message_util, "get_direct_message_permission_hints", () => ({
        is_known_empty_conversation: false,
        is_local_echo_safe: true,
    }));

    // Suggestion for bot always show up even if direct message is disabled,
    // considering past conversation exists.
    override(realm, "realm_direct_message_permission_group", nobody.id);
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    results = ct.get_person_suggestion_for_topic_typeahead("notification");
    assert.equal(results[0].user.user_id, notification_bot.user_id);

    // Show suggestion when both sender and recipient has permission, and
    // past conversation exists
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [hamlet.user_id, lear.user_id],
        direct_subgroups: [],
    });
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.equal(results[0].user.user_id, lear.user_id);

    // Sender in initiator group, recipient in permission group, show suggestion
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_initiator_group", {
        direct_members: [hamlet.user_id],
        direct_subgroups: [],
    });
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.equal(results[0].user.user_id, lear.user_id);

    // Show suggestion when sender has permission and past conversation exists
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [hamlet.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.equal(results[0].user.user_id, lear.user_id);

    // Show suggestion when recipient has permission and past conversation exists
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.equal(results[0].user.user_id, lear.user_id);

    // No suggestion when direct message is disabled
    override(realm, "realm_direct_message_permission_group", nobody.id);
    override(realm, "realm_direct_message_initiator_group", {
        direct_members: [hamlet.user_id, lear.user_id],
        direct_subgroups: [],
    });
    results = ct.get_person_suggestion_for_topic_typeahead("lear");
    assert.deepEqual(results, []);
});

test("get_person_suggestion_for_topic_typeahead: diacritic query prioritizes exact diacritic matches", ({
    override,
}) => {
    // "Gael" has the shorter name, so the within-bucket tiebreaker would rank
    // it above "Gaël Twin" -- only the diacritic-prefix bucket puts "Gaël
    // Twin" first.
    const gael_ascii = make_user({
        full_name: "Gael",
        user_id: 112,
        email: "[email protected]",
    });
    people.add_active_user(gael_ascii);

    message_lists.current = undefined;
    override(pm_conversations, "get_partners", () => [
        gael.user_id,
        gael_ascii.user_id,
        lear.user_id,
    ]);

    const results = ct.get_person_suggestion_for_topic_typeahead("gaë");
    assert.deepEqual(
        results.map((result) => result.user.user_id),
        [gael.user_id, gael_ascii.user_id],
    );
});

test("begins_typeahead", ({override, override_rewire}) => {
    override(stream_topic_history_util, "get_server_history", noop);

    const input_element = {
        $element: {
            closest: () => [],
        },
        type: "input",
    };

    function get_values(input, rest) {
        // Stub out split_at_cursor that uses $(':focus')
        override_rewire(ct, "split_at_cursor", () => [input, rest]);
        const values = ct.get_candidates(input, input_element, {shown: false});
        return values;
    }

    function assert_typeahead_equals(input, rest, reference) {
        // Usage:
        // assert_typeahead_equals('#some', reference); => '#some|'
        // assert_typeahead_equals('#some', 'thing', reference) => '#some|thing'
        // In the above examples, '|' serves as the cursor.
        if (reference === undefined) {
            reference = rest;
            rest = "";
        }
        const values = get_values(input, rest);
        assert.deepEqual(values, reference);
    }

    function assert_typeahead_starts_with(input, rest, reference) {
        if (reference === undefined) {
            reference = rest;
            rest = "";
        }
        const values = get_values(input, rest);
        assert.ok(reference.length > 0);
        assert.deepEqual(values.slice(0, reference.length), reference);
    }

    assert_typeahead_equals("test", []);
    assert_typeahead_equals("test one two", []);
    assert_typeahead_equals("*", []);
    assert_typeahead_equals("* ", []);
    assert_typeahead_equals(" *", []);
    assert_typeahead_equals("test *", []);

    // Make sure that the last token is the one we read.
    assert_typeahead_equals("~~~ @zulip", []); // zulip isn't set up as a user group
    assert_typeahead_equals("@zulip :ta", emoji_objects(["tada", "stadium"]));
    function language_objects(languages) {
        return languages.map((language) => language_item(language));
    }
    assert_typeahead_equals(
        "#foo\n~~~py",
        language_objects([
            "py",
            "py+ul4",
            "py2",
            "py2tb",
            "py3tb",
            "pycon",
            "pypy",
            "pyrex",
            "antlr-python",
            "bst-pybtex",
            "ipython",
            "ipython3",
            "ipythonconsole",
            "numpy",
        ]),
    );
    assert_typeahead_equals(":tada: <time:", [
        {
            type: "time_jump",
            message: "translated: Mention a time-zone-aware time",
        },
    ]);

    const mention_all = broadcast_item(ct.broadcast_mentions()[0]);
    // No stream context and no direct message history: broadcast sorts
    // before all these users; bots alphabetical by full_name.
    const users_and_all_mention = [
        mention_all,
        ...sorted_user_list,
        notification_bot_item, // Notification Bot
        welcome_bot_item, // Welcome Bot
    ];
    const users_and_user_groups = [
        ...sorted_user_list,
        // Groups alphabetical by display name.
        hamletcharacters, // "Characters of Hamlet"
        backend,
        call_center, // "folks working in support"
        admins,
        members,
        notification_bot_item, // Notification Bot
        welcome_bot_item, // Welcome Bot
    ];
    const mention_everyone = broadcast_item(ct.broadcast_mentions()[1]);
    function mentions_with_silent_marker(mentions, is_silent) {
        return mentions.map((item) => ({
            ...item,
            is_silent,
        }));
    }
    assert_typeahead_equals("@", mentions_with_silent_marker(users_and_all_mention, false));
    // The user we're testing for is only allowed to do silent mentions of groups
    assert_typeahead_equals("@_", mentions_with_silent_marker(users_and_user_groups, true));
    assert_typeahead_equals(" @", mentions_with_silent_marker(users_and_all_mention, false));
    assert_typeahead_equals(" @_", mentions_with_silent_marker(users_and_user_groups, true));
    assert_typeahead_equals("@*", mentions_with_silent_marker(users_and_all_mention, false));
    assert_typeahead_equals("@_*", mentions_with_silent_marker(users_and_user_groups, true));
    assert_typeahead_equals("@**", mentions_with_silent_marker(users_and_all_mention, false));
    assert_typeahead_equals("@_**", mentions_with_silent_marker(users_and_user_groups, true));
    // "o" is anywhere in each name; Othello starts with "o" so
    // sorts first.  Broadcast before users with no direct message
    // history (none in test), then bots by name length (shorter first).
    assert_typeahead_equals(
        "test @**o",
        mentions_with_silent_marker(
            [
                othello_item, // Othello starts with "o"
                mention_everyone, // broadcast: before users with no DMs
                cordelia_item, // "Cordelia" contains "o"
                welcome_bot_item, // Welcome Bot
                notification_bot_item, // Notification Bot
            ],
            false,
        ),
    );
    assert_typeahead_equals(
        "test @_**o",
        mentions_with_silent_marker(
            [othello_item, cordelia_item, admins, members, welcome_bot_item, notification_bot_item],
            true,
        ),
    );
    // Same as @**o above.
    assert_typeahead_equals(
        "test @*o",
        mentions_with_silent_marker(
            [
                othello_item,
                mention_everyone,
                cordelia_item,
                welcome_bot_item,
                notification_bot_item,
            ],
            false,
        ),
    );
    // "k" matches: "King Hamlet" and "King Lear" start with "k"
    // (best, shorter name first); "Mark Twin" x2 contain "k" (ok,
    // word-boundary match); "backend" group also matches.
    assert_typeahead_equals(
        "test @_*k",
        mentions_with_silent_marker(
            [lear_item, hamlet_item, twin1_item, twin2_item, backend],
            true,
        ),
    );
    // "h": Harry starts with "h" (best); Earl Hal and King Hamlet
    // have word-boundary match (ok, shorter name first); Cordelia and
    // Othello contain "h" but no word-boundary match (worst,
    // shorter name first).
    assert_typeahead_equals(
        "test @*h",
        mentions_with_silent_marker(
            [harry_item, hal_item, hamlet_item, cordelia_item, othello_item],
            false,
        ),
    );
    assert_typeahead_equals(
        "test @_*h",
        mentions_with_silent_marker(
            [harry_item, hal_item, hamlet_item, hamletcharacters, cordelia_item, othello_item],
            true,
        ),
    );
    assert_typeahead_equals("test @", mentions_with_silent_marker(users_and_all_mention, false));
    assert_typeahead_equals("test @_", mentions_with_silent_marker(users_and_user_groups, true));
    assert_typeahead_equals("test no@o", []);
    assert_typeahead_equals("test no@_k", []);
    assert_typeahead_equals("@ ", []);
    assert_typeahead_equals("@_ ", []);
    assert_typeahead_equals("@* ", []);
    assert_typeahead_equals("@_* ", []);
    assert_typeahead_equals("@** ", []);
    assert_typeahead_equals("@_** ", []);
    // "i" has no name-start match; all users whose name contains "i"
    // are in the worst tier, sorted by name length (shorter first);
    // equal-length names alphabetical by full_name.
    assert_typeahead_equals(
        "test\n@i",
        mentions_with_silent_marker(
            [
                ali_item, // Ali (3)
                alice_item, // Alice (5)
                gael_item, // Gaël Twin (9)
                lear_item, // King Lear (9)
                twin1_item, // Mark Twin (9)
                twin2_item, // Mark Twin (9)
                hamlet_item, // King Hamlet (11)
                cordelia_item, // Cordelia, Lear's daughter (25)
                othello_item, // Othello, the Moor of Venice (28)
                notification_bot_item, // Notification Bot (bot tier)
            ],
            false,
        ),
    );
    // Unlike the non-silent "@i" above, the silent "@_" typeahead offers
    // every user group (not just those you're allowed to mention), so the
    // "admins" group appears here; it's the only group whose name has "i".
    assert_typeahead_equals(
        "test\n@_i",
        mentions_with_silent_marker(
            [
                ali_item, // Ali (3)
                alice_item, // Alice (5)
                gael_item, // Gaël Twin (9)
                lear_item, // King Lear (9)
                twin1_item, // Mark Twin (9)
                twin2_item, // Mark Twin (9)
                hamlet_item, // King Hamlet (11)
                cordelia_item, // Cordelia, Lear's daughter (25)
                othello_item, // Othello, the Moor of Venice (28)
                admins, // group: admins
                notification_bot_item, // Notification Bot (bot tier)
            ],
            true,
        ),
    );
    // "l": King Lear and Cordelia have word-boundary match on "Lear"
    // (best, shorter name first).  Broadcast before users with no direct
    // message history (worst); remaining users by name length; then bot.
    assert_typeahead_equals(
        "test\n @l",
        mentions_with_silent_marker(
            [
                lear_item, // King *L*ear (9)
                cordelia_item, // Cordelia, *L*ear's daughter (25)
                mention_all, // broadcast "all" (before users with no DMs)
                ali_item, // Ali (3)
                alice_item, // Alice (5)
                hal_item, // Earl Hal (8)
                gael_item, // Gaël Twin (9)
                hamlet_item, // King Hamlet (11)
                othello_item, // Othello, the Moor of Venice (28)
                welcome_bot_item, // Welcome Bot (bot tier)
            ],
            false,
        ),
    );
    assert_typeahead_equals(
        "test\n @_l",
        mentions_with_silent_marker(
            [
                lear_item,
                cordelia_item,
                ali_item,
                alice_item,
                hal_item,
                gael_item,
                hamlet_item,
                othello_item,
                hamletcharacters,
                call_center,
                members,
                welcome_bot_item,
            ],
            true,
        ),
    );
    assert_typeahead_equals("@zuli", []);
    assert_typeahead_equals("@_zuli", []);
    assert_typeahead_equals("@ zuli", []);
    assert_typeahead_equals("@_ zuli", []);
    assert_typeahead_equals(" @zuli", []);
    assert_typeahead_equals(" @_zuli", []);
    // Same ordering logic as @**o above: name-start match first, then
    // the broadcast mention before users with no direct message
    // history, then remaining matches by name length (shorter first).
    assert_typeahead_equals(
        "test @o",
        mentions_with_silent_marker(
            [
                othello_item, // Othello starts with "o"
                mention_everyone, // broadcast: before users with no DMs
                cordelia_item, // "Cordelia" contains "o"
                welcome_bot_item, // Welcome Bot (11)
                notification_bot_item, // Notification Bot (16)
            ],
            false,
        ),
    );
    // Silent variant: user groups appear in place of the broadcast
    // mention; bots still sort by name length (shorter first).
    assert_typeahead_equals(
        "test @_o",
        mentions_with_silent_marker(
            [othello_item, cordelia_item, admins, members, welcome_bot_item, notification_bot_item],
            true,
        ),
    );
    assert_typeahead_equals("test @z", []);
    assert_typeahead_equals("test @_z", []);

    assert_typeahead_equals(":", []);
    assert_typeahead_equals(": ", []);
    assert_typeahead_equals(" :", []);
    assert_typeahead_equals(":)", []);
    assert_typeahead_equals(":4", []);
    assert_typeahead_equals(": la", []);
    assert_typeahead_equals("test :-P", []);
    assert_typeahead_equals("hi emoji :", []);
    assert_typeahead_equals("hi emoj:i", []);
    assert_typeahead_equals("hi emoji :D", []);
    assert_typeahead_equals("hi emoji : t", []);
    assert_typeahead_equals(
        "hi emoji :t",
        emoji_objects([
            "thumbs_up",
            "tada",
            "thermometer",
            "heart",
            "stadium",
            "japanese_post_office",
        ]),
    );
    assert_typeahead_equals("hi emoji :ta", emoji_objects(["tada", "stadium"]));
    assert_typeahead_equals("hi emoji :da", emoji_objects(["panda_face", "tada"]));
    // We store the emoji panda_face with underscore, but that's not part of the emoji's name
    assert_typeahead_equals("hi emoji :da_", emoji_objects([]));
    assert_typeahead_equals("hi emoji :da ", emoji_objects([]));
    assert_typeahead_equals("hi emoji\n:da", emoji_objects(["panda_face", "tada"]));
    assert_typeahead_equals("hi emoji\n :ra", []);
    assert_typeahead_equals(":+", []);
    assert_typeahead_equals(":la", []);
    assert_typeahead_equals(" :lee", []);
    assert_typeahead_equals("hi :see no", emoji_objects(["see_no_evil"]));
    assert_typeahead_equals("hi :japanese post of", emoji_objects(["japanese_post_office"]));

    assert_typeahead_equals("#", []);
    assert_typeahead_equals("# ", []);
    assert_typeahead_equals(" #", []);
    assert_typeahead_equals("# s", []);
    assert_typeahead_equals("test #", []);
    assert_typeahead_equals("test # a", []);
    assert_typeahead_equals("test no#o", []);

    const poll_command = {
        text: "/poll",
        name: "poll",
        info: "translated: Create a poll",
        aliases: "",
        placeholder: "translated: Question",
        type: "slash",
    };
    const todo_command = {
        text: "/todo",
        name: "todo",
        info: "translated: Create a collaborative to-do list",
        aliases: "",
        placeholder: "translated: Task list",
        type: "slash",
    };

    assert_typeahead_equals("/", [me_command_item, poll_command, todo_command]);
    assert_typeahead_equals("/m", [me_command_item]);
    // Slash commands can only occur at the start of a message
    assert_typeahead_equals(" /m", []);
    assert_typeahead_equals("abc/me", []);
    assert_typeahead_equals("hello /me", []);
    assert_typeahead_equals("\n/m", []);
    assert_typeahead_equals("/poll", [poll_command]);
    assert_typeahead_equals(" /pol", []);
    assert_typeahead_equals("abc/po", []);
    assert_typeahead_equals("hello /poll", []);
    assert_typeahead_equals("\n/pol", []);
    assert_typeahead_equals("/todo", [todo_command]);
    assert_typeahead_equals("my /todo", []);
    assert_typeahead_equals("\n/to", []);
    assert_typeahead_equals(" /tod", []);

    assert_typeahead_equals("x/", []);
    // We don't open the typeahead until there's a letter after ```
    assert_typeahead_equals("```", []);
    assert_typeahead_equals("``` ", []);
    assert_typeahead_equals(" ```", []);
    assert_typeahead_equals("test ```", []);
    assert_typeahead_equals("test ``` py", []);
    assert_typeahead_equals("test ```a", []);
    assert_typeahead_equals("test\n```", []);
    assert_typeahead_equals("``c", []);
    // But if the typeahead is already open (e.g. the user typed ```py
    // and then deleted the "py"), a bare ``` keeps it open.
    override_rewire(ct, "split_at_cursor", () => ["```", ""]);
    const shown_typeahead_values = ct.get_candidates("```", input_element, {shown: true});
    assert.ok(shown_typeahead_values.length > 0);
    assert.equal(shown_typeahead_values[0].type, "syntax");
    // Languages filtered by a single letter is a very long list.
    // The typeahead displays languages sorted by popularity, so to
    // avoid typing out all of them here we'll just test that the
    // first several match up.
    assert_typeahead_starts_with(
        "```b",
        language_objects(["bash", "b3d", "bare", "basemake", "basic", "bat"]),
    );
    assert_typeahead_starts_with(
        "``` d",
        language_objects(["d", "dart", "d-objdump", "dasm16", "dax", "debcontrol"]),
    );
    const p_langs = language_objects(["python", "powershell", "php", "perl", "pacmanconf", "pan"]);
    assert_typeahead_starts_with("test\n``` p", p_langs);
    // Too many spaces between ``` and the p to
    // trigger the typeahead.
    assert_typeahead_equals("test\n```  p", []);
    assert_typeahead_equals("~~~", []);
    assert_typeahead_equals("~~~ ", []);
    assert_typeahead_equals(" ~~~", []);
    // Only valid when ``` or ~~~ is at the beginning of a line.
    assert_typeahead_equals(" ~~~ g", []);
    assert_typeahead_equals("test ~~~", []);
    assert_typeahead_equals("test ~~~p", []);
    assert_typeahead_equals("test\n~~~", []);
    assert_typeahead_starts_with(
        "~~~e",
        language_objects(["earl-grey", "easytrieve", "ebnf", "ec", "ecl", "eiffel"]),
    );
    assert_typeahead_starts_with(
        "~~~ f",
        language_objects(["f#", "f90", "factor", "fan", "fancy", "fc"]),
    );
    assert_typeahead_starts_with("test\n~~~ p", p_langs);
    // Too many spaces before the p
    assert_typeahead_equals("test\n~~~  p", []);

    // topic_jump
    assert_typeahead_equals("@**a person**>", []);
    assert_typeahead_equals("@**a person** >", []);
    const topic_jump = [
        {
            // this is deliberately a blank choice.
            message: "",
            type: "topic_jump",
        },
    ];
    assert_typeahead_equals("#**stream**>", topic_jump);
    assert_typeahead_equals("#**stream** >", topic_jump);
    assert_typeahead_equals("[#A&#42; Algorithm](#narrow/channel/6-A.2A-Algorithm) >", topic_jump);
    assert_typeahead_equals("#**Sweden>some topic** >", []); // Already completed a topic.

    // topic_list
    // includes "more ice"
    function typed_topics(stream, topics, is_new_topic = false) {
        const matches_list = topics.map((topic, index) => ({
            is_channel_link: topic === stream && index === 0,
            stream_data: {
                ...stream_data.get_sub_by_name("Sweden"),
                rendered_description: "",
            },
            topic,
            is_empty_string_topic: topic === "",
            topic_display_name: get_final_topic_display_name(topic),
            type: "topic_list",
            used_syntax_prefix: "#**",
            is_new_topic,
        }));
        return matches_list;
    }
    assert_typeahead_equals(
        "#**Sweden>more ice",
        typed_topics("Sweden", ["more ice", "even more ice"]),
    );
    assert_typeahead_equals(
        "#**Sweden>",
        typed_topics("Sweden", ["Sweden", ...sweden_topics_to_show]),
    );
    const is_new_topic = true;
    assert_typeahead_equals(
        "#**Sweden>totally new topic",
        typed_topics("Sweden", ["totally new topic"], is_new_topic),
    );
    assert_typeahead_equals("#**Sweden>\n\nmore ice", typed_topics("Sweden", []));

    // A complete channel link is a valid final form, so it only triggers
    // topic completion while the typeahead is already open (the state
    // right after a channel is selected). With the typeahead closed, the
    // cursor merely sitting after a channel link offers nothing.
    assert_typeahead_equals("#**Sweden**", []);

    // The complete channel link inserted on channel selection reopens the
    // topic typeahead, so the user can still pick a topic; selecting one
    // rewrites the link into the partial-then-completed form. Typing after
    // the link filters topics just like the `>` syntax does.
    compose_ui.compose_textarea_typeahead = {shown: true};
    assert_typeahead_equals(
        "#**Sweden**",
        typed_topics("Sweden", ["Sweden", ...sweden_topics_to_show]),
    );
    assert_typeahead_equals(
        "#**Sweden**more ice",
        typed_topics("Sweden", ["more ice", "even more ice"]),
    );
    assert_typeahead_equals(
        "#**Sweden**totally new topic",
        typed_topics("Sweden", ["totally new topic"], is_new_topic),
    );
    // Punctuation right after the complete link ends the sentence rather
    // than starting a topic.
    for (const punctuation of [
        ",",
        ".",
        ":",
        ";",
        "?",
        "!",
        "-",
        "(",
        ")",
        "[",
        "]",
        '"',
        "'",
        "@",
        // Chinese and Japanese full stop.
        "。",
        // Chinese comma.
        ",",
        "¿",
        "—",
    ]) {
        assert_typeahead_equals(`#**Sweden**${punctuation}`, []);
    }
    // Symbols are not punctuation, so they still start a topic.
    assert_typeahead_equals("#**Sweden**~", typed_topics("Sweden", ["~"], is_new_topic));
    assert_typeahead_equals("#**Sweden**. Now see", []);
    assert.deepEqual(
        get_values("#**Sweden**. Now see #den", "").map((item) => item.name),
        ["Denmark", "Sweden"],
    );
    // The same applies to the markdown fallback link form.
    assert.deepEqual(
        get_values("[#A&#42; Algorithm](#narrow/channel/6-A.2A-Algorithm)", "").map(
            (item) => item.topic,
        ),
        ["A* Algorithm"],
    );
    assert_typeahead_equals("[#A&#42; Algorithm](#narrow/channel/6-A.2A-Algorithm).", []);
    // A space immediately after the complete link means the user has
    // moved on to the message body: the complete-link pattern doesn't
    // match, and with no other token present there are no suggestions.
    assert_typeahead_equals("#**Sweden** more ice", []);
    // The pattern rejects leading whitespace, so tokens typed after a
    // complete channel link on the same line get their own typeaheads.
    assert.deepEqual(
        get_values("#**Sweden** see #den", "").map((item) => item.name),
        ["Denmark", "Sweden"],
    );
    // The timestamp typeahead also still runs after a channel link.
    assert_typeahead_equals("#**Sweden** at <time:something", [
        {
            message: "translated: Mention a time-zone-aware time",
            type: "time_jump",
        },
    ]);
    // A channel that only allows the empty topic gets a complete channel
    // link with no topic completion offered.
    assert_typeahead_equals("#**Announce**", []);
    // Once the user presses ">", completion is offered to just that
    // single (empty) topic, and never to named ones.
    const announce_empty_topic_suggestion = [
        {
            topic: "",
            topic_display_name: get_final_topic_display_name(""),
            is_empty_string_topic: true,
            type: "topic_list",
            is_channel_link: false,
            used_syntax_prefix: "#**",
            stream_data: {
                ...stream_data.get_sub_by_name("Announce"),
                rendered_description: "",
            },
            is_new_topic: false,
        },
    ];
    assert_typeahead_equals("#**Announce>", announce_empty_topic_suggestion);
    // Text typed after ">" cannot create a named topic; only the empty
    // topic is still offered.
    assert_typeahead_equals("#**Announce>random", announce_empty_topic_suggestion);
    compose_ui.compose_textarea_typeahead = undefined;

    // time_jump
    const time_jump = [
        {
            message: "translated: Mention a time-zone-aware time",
            type: "time_jump",
        },
    ];
    assert_typeahead_equals("<tim", []);
    assert_typeahead_equals("<timerandom", []);
    assert_typeahead_equals("<time", time_jump);
    assert_typeahead_equals("<time:", time_jump);
    assert_typeahead_equals("<time:something", time_jump);
    assert_typeahead_equals("<time:something", "> ", time_jump);
    assert_typeahead_equals("<time:something>", time_jump);
    assert_typeahead_equals("<time:something> ", []); // Already completed the mention

    // Following tests place the cursor before the second string
    assert_typeahead_equals("#test", "ing", []);
    assert_typeahead_equals("@test", "ing", []);
    assert_typeahead_equals(":test", "ing", []);
    assert_typeahead_equals("```test", "ing", []);
    assert_typeahead_equals("~~~test", "ing", []);
    const terminal_symbols = ",.;?!()[]> \u{A0}\"'\n\t";
    for (const symbol of terminal_symbols) {
        assert_typeahead_equals(
            "@othello",
            symbol,
            mentions_with_silent_marker([othello_item], false),
        );
        assert_typeahead_equals(":tada", symbol, emoji_objects(["tada"]));
        assert_typeahead_starts_with("```p", symbol, p_langs);
        assert_typeahead_starts_with("~~~p", symbol, p_langs);
    }
});

test("tokenizing", () => {
    assert.equal(ct.tokenize_compose_str("/m"), "/m");
    assert.equal(ct.tokenize_compose_str("1/3"), "");
    assert.equal(ct.tokenize_compose_str("foo bar"), "");
    assert.equal(ct.tokenize_compose_str("foo#@:bar"), "");
    assert.equal(ct.tokenize_compose_str("foo bar [#alic"), "#alic");
    assert.equal(ct.tokenize_compose_str("foo bar (#alic"), "#alic");
    assert.equal(ct.tokenize_compose_str("foo bar {#alic"), "#alic");
    assert.equal(ct.tokenize_compose_str("foo bar /#alic"), "#alic");
    assert.equal(ct.tokenize_compose_str("foo bar <#alic"), "#alic");
    assert.equal(ct.tokenize_compose_str("foo bar '#alic"), "#alic");
    assert.equal(ct.tokenize_compose_str('foo bar "#alic'), "#alic");
    assert.equal(ct.tokenize_compose_str("#foo @bar [#alic"), "#alic");
    assert.equal(ct.tokenize_compose_str("foo bar #alic"), "#alic");
    assert.equal(ct.tokenize_compose_str("foo bar @alic"), "@alic");
    assert.equal(ct.tokenize_compose_str("foo bar :smil"), ":smil");
    assert.equal(ct.tokenize_compose_str(":smil"), ":smil");
    assert.equal(ct.tokenize_compose_str("foo @alice sm"), "@alice sm");
    assert.equal(ct.tokenize_compose_str("foo ```p"), "");
    assert.equal(ct.tokenize_compose_str("``` py"), "``` py");
    assert.equal(ct.tokenize_compose_str("foo``bar ~~~ py"), "");
    assert.equal(ct.tokenize_compose_str("foo ~~~why = why_not\n~~~"), "~~~");

    // The following cases are kinda judgment calls...
    // max scanning limit is 40 characters until chars like @, # , / are found
    assert.equal(
        ct.tokenize_compose_str(
            "foo @toomanycharactersistooridiculoustoautocompletethatitexceedsalllimitsusingthewildessequenceofstringsforthispurpose",
        ),
        "",
    );
    assert.equal(ct.tokenize_compose_str("foo #bar@foo"), "#bar@foo");
});

test("content_item_html", ({override_rewire}) => {
    ct.get_or_set_completing_for_tests("emoji");
    const emoji = {emoji_name: "person shrugging", emoji_url: "¯\\_(ツ)_/¯", type: "emoji"};
    let th_render_typeahead_item_called;
    override_rewire(typeahead_helper, "render_emoji", (item) => {
        assert.deepEqual(item, emoji);
        th_render_typeahead_item_called = true;
    });
    ct.content_item_html("")(emoji);

    ct.get_or_set_completing_for_tests("mention");
    let th_render_person_called = false;
    override_rewire(typeahead_helper, "render_person", (person, opts) => {
        assert.deepEqual(person, othello_item);
        assert.ok(opts === undefined || typeof opts === "object");
        th_render_person_called = true;
    });
    ct.content_item_html("")(othello_item);

    let th_render_user_group_called = false;
    override_rewire(typeahead_helper, "render_user_group", (user_group) => {
        assert.deepEqual(user_group, backend);
        th_render_user_group_called = true;
    });
    ct.content_item_html("")(backend);

    // We don't have any fancy rendering for slash commands yet.
    ct.get_or_set_completing_for_tests("slash");
    let th_render_slash_command_called = false;
    const me_slash = {
        text: "/me",
        type: "slash",
        info: "translated: Action message",
    };
    override_rewire(typeahead_helper, "render_typeahead_item", (item) => {
        assert.deepEqual(item, {
            primary: "/me",
            secondary: "translated: Action message",
        });
        th_render_slash_command_called = true;
    });
    ct.content_item_html("")(me_slash);

    ct.get_or_set_completing_for_tests("stream");
    let th_render_stream_called = false;
    override_rewire(typeahead_helper, "render_stream", (stream) => {
        assert.deepEqual(stream, denmark_stream);
        th_render_stream_called = true;
    });
    ct.content_item_html("")(denmark_stream);

    ct.get_or_set_completing_for_tests("syntax");
    th_render_typeahead_item_called = false;
    override_rewire(typeahead_helper, "render_typeahead_item", (item) => {
        assert.deepEqual(item, {
            is_default_language: false,
            primary: "py",
        });
        th_render_typeahead_item_called = true;
    });
    ct.content_item_html("")({type: "syntax", language: "py"});

    // Verify that all stub functions have been called.
    assert.ok(th_render_typeahead_item_called);
    assert.ok(th_render_person_called);
    assert.ok(th_render_user_group_called);
    assert.ok(th_render_stream_called);
    assert.ok(th_render_typeahead_item_called);
    assert.ok(th_render_slash_command_called);
});

function possibly_silent_list(list, is_silent) {
    return list.map((item) => ({
        ...item,
        is_silent,
    }));
}

test("filter_and_sort_mentions (normal)", ({override}) => {
    compose_state.set_message_type("stream");
    const is_silent = false;
    override(current_user, "user_id", 101);
    let suggestions = ct.filter_and_sort_mentions(is_silent, "al");

    const mention_all = broadcast_item(ct.broadcast_mentions()[0]);
    assert.deepEqual(
        suggestions,
        possibly_silent_list([mention_all, ali_item, alice_item, hal_item, call_center], is_silent),
    );

    // call_center group is shown in typeahead even when user is member of
    // one of the subgroups of can_mention_group.
    override(current_user, "user_id", 104);
    suggestions = ct.filter_and_sort_mentions(is_silent, "al");
    assert.deepEqual(
        suggestions,
        possibly_silent_list([mention_all, ali_item, alice_item, hal_item, call_center], is_silent),
    );

    // call_center group is not shown in typeahead when user is neither
    // a direct member of can_mention_group nor a member of any of its
    // recursive subgroups.
    override(current_user, "user_id", 102);
    suggestions = ct.filter_and_sort_mentions(is_silent, "al");
    assert.deepEqual(
        suggestions,
        possibly_silent_list([mention_all, ali_item, alice_item, hal_item], is_silent),
    );
});

test("filter_and_sort_mentions (silent)", ({override}) => {
    const is_silent = true;

    let suggestions = ct.filter_and_sort_mentions(is_silent, "al");

    assert.deepEqual(
        suggestions,
        possibly_silent_list([ali_item, alice_item, hal_item, call_center], is_silent),
    );

    // call_center group is shown in typeahead irrespective of whether
    // user is member of can_mention_group or its subgroups for a
    // silent mention.
    override(current_user, "user_id", 102);
    suggestions = ct.filter_and_sort_mentions(is_silent, "al");
    assert.deepEqual(
        suggestions,
        possibly_silent_list([ali_item, alice_item, hal_item, call_center], is_silent),
    );
});

test("typeahead_results", ({override}) => {
    const stream_list = [
        denmark_stream,
        sweden_stream,
        netherland_stream,
        mobile_team_stream,
        mobile_stream,
    ];

    function assert_emoji_matches(input, expected) {
        const matcher = typeahead.get_emoji_matcher(input);
        const returned = emoji_list.filter((item) => matcher(item));
        assert.deepEqual(returned, expected);
    }

    function assert_mentions_matches(input, expected) {
        const is_silent = false;
        const returned = ct.filter_and_sort_mentions(is_silent, input);
        assert.deepEqual(returned, expected);
    }
    function assert_stream_matches(input, expected) {
        const matcher = ct.get_stream_matcher(input);
        const returned = stream_list.filter((item) => matcher(item));
        assert.deepEqual(returned, expected);
    }

    function assert_slash_matches(input, expected) {
        const matcher = ct.get_slash_matcher(input);
        const returned = composebox_typeahead.all_slash_commands.filter((item) => matcher(item));
        assert.deepEqual(returned, expected);
    }
    assert_emoji_matches("da", [
        {
            emoji_name: "panda_face",
            emoji_code: "1f43c",
            reaction_type: "unicode_emoji",
            is_realm_emoji: false,
            type: "emoji",
        },
        {
            emoji_name: "tada",
            emoji_code: "1f389",
            reaction_type: "unicode_emoji",
            is_realm_emoji: false,
            type: "emoji",
        },
    ]);
    assert_emoji_matches("da_", []);
    assert_emoji_matches("da ", []);
    assert_emoji_matches("panda ", [
        {
            emoji_name: "panda_face",
            emoji_code: "1f43c",
            reaction_type: "unicode_emoji",
            is_realm_emoji: false,
            type: "emoji",
        },
    ]);
    assert_emoji_matches("panda_", [
        {
            emoji_name: "panda_face",
            emoji_code: "1f43c",
            reaction_type: "unicode_emoji",
            is_realm_emoji: false,
            type: "emoji",
        },
    ]);
    assert_emoji_matches("japanese_post_", [
        {
            emoji_name: "japanese_post_office",
            emoji_code: "1f3e3",
            reaction_type: "unicode_emoji",
            is_realm_emoji: false,
            type: "emoji",
        },
    ]);
    assert_emoji_matches("japanese post ", [
        {
            emoji_name: "japanese_post_office",
            emoji_code: "1f3e3",
            reaction_type: "unicode_emoji",
            is_realm_emoji: false,
            type: "emoji",
        },
    ]);
    assert_emoji_matches("notaemoji", []);

    // Autocomplete user mentions by user name.
    function not_silent(item) {
        return {
            ...item,
            is_silent: false,
        };
    }
    assert_mentions_matches("cordelia", [not_silent(cordelia_item)]);
    assert_mentions_matches("cordelia, le", [not_silent(cordelia_item)]);
    assert_mentions_matches("cordelia, le ", []);
    assert_mentions_matches("moor", [not_silent(othello_item)]);
    assert_mentions_matches("moor ", [not_silent(othello_item)]);
    assert_mentions_matches("moor of", [not_silent(othello_item)]);
    assert_mentions_matches("moor of ven", [not_silent(othello_item)]);
    assert_mentions_matches("oor", [not_silent(othello_item)]);
    assert_mentions_matches("oor ", []);
    assert_mentions_matches("oor o", []);
    assert_mentions_matches("oor of venice", []);
    // Both start with "King "; shorter name first: Lear (9) < Hamlet (11).
    assert_mentions_matches("King ", [not_silent(lear_item), not_silent(hamlet_item)]);
    assert_mentions_matches("King H", [not_silent(hamlet_item)]);
    assert_mentions_matches("King L", [not_silent(lear_item)]);
    assert_mentions_matches("delia lear", []);
    assert_mentions_matches("Mark Tw", [not_silent(twin1_item), not_silent(twin2_item)]);

    // Earlier user group and stream mentions were autocompleted by their
    // description too. This is now removed as it often led to unexpected
    // behaviour, and did not have any great discoverability advantage.
    override(current_user, "user_id", 101);
    // Autocomplete user group mentions by group name.
    assert_mentions_matches("hamletchar", [not_silent(hamletcharacters)]);

    // Verify we're not matching on a terms that only appear in the description.
    assert_mentions_matches("characters of", []);

    // Verify we suggest only the first matching stream wildcard mention,
    // irrespective of how many equivalent stream wildcard mentions match.
    const mention_everyone = not_silent(broadcast_item(ct.broadcast_mentions()[1]));
    // compose_state is "stream" here, so broadcasts sort before users.
    // "everyone" and Earl Hal start with "e" (best, broadcast first);
    // remaining users contain "e" (worst, shorter name first); then
    // groups and bots.
    assert_mentions_matches("e", [
        not_silent(mention_everyone), // broadcast: "everyone" starts with "e"
        not_silent(hal_item), // Earl Hal (8) starts with "e"
        not_silent(alice_item), // Alice (5)
        not_silent(gael_item), // Gaël Twin (9)
        not_silent(lear_item), // King Lear (9)
        not_silent(hamlet_item), // King Hamlet (11)
        not_silent(cordelia_item), // Cordelia, Lear's daughter (25)
        not_silent(othello_item), // Othello, the Moor of Venice (28)
        not_silent(hamletcharacters), // group
        not_silent(call_center), // group
        not_silent(welcome_bot_item), // Welcome Bot (bot tier)
    ]);

    // Verify we suggest both 'the first matching stream wildcard' and
    // 'topic wildcard' mentions. Not only one matching wildcard mention.
    const mention_topic = broadcast_item(ct.broadcast_mentions()[4]);
    // Othello starts with "o" (best); everyone/topic broadcasts and
    // Cordelia contain "o" (worst, broadcasts first in stream mode,
    // then user); bots by name length (shorter first).
    assert_mentions_matches("o", [
        not_silent(othello_item), // Othello starts with "o"
        not_silent(mention_everyone), // broadcast (stream mode: first)
        not_silent(mention_topic), // broadcast (stream mode: first)
        not_silent(cordelia_item), // "Cordelia" contains "o"
        not_silent(welcome_bot_item), // Welcome Bot
        not_silent(notification_bot_item), // Notification Bot
    ]);

    // Autocomplete by slash commands.
    assert_slash_matches("me", [me_command]);
    assert_slash_matches("dark", [dark_command]);
    assert_slash_matches("night", [dark_command]);
    assert_slash_matches("light", [light_command]);
    assert_slash_matches("day", [light_command]);

    // Autocomplete stream by stream name
    assert_stream_matches("den", [denmark_stream, sweden_stream]);
    assert_stream_matches("denmark", [denmark_stream]);
    assert_stream_matches("denmark ", []);
    assert_stream_matches("den ", []);
    assert_stream_matches("the ", [netherland_stream]);
    // Do not match stream descriptions
    assert_stream_matches("cold", []);
    assert_stream_matches("city", []);
    // Always prioritise exact matches, irrespective of activity
    assert_stream_matches("Mobile", [mobile_team_stream, mobile_stream]);
});

test("message people", ({override, override_rewire}) => {
    let results;

    /*
        We will initially simulate that we talk to Hal and Harry, while
        we don't talk to King Hamlet or Characters of Hamlet. This
        will knock these 2 out of consideration in the filtering pass.
    */

    let user_ids = [hal.user_id, harry.user_id];
    override(message_user_ids, "user_ids", () => user_ids);
    override_rewire(ct, "max_num_items", 2);

    const opts = {
        want_broadcast: false,
        want_groups: true,
        filter_pills: false,
    };

    results = ct.get_person_suggestions("Ha", opts);
    assert.deepEqual(results, [harry_item, hal_item]);

    // Now let's exclude Hal and include King Hamlet.
    user_ids = [hamlet.user_id, harry.user_id];

    results = ct.get_person_suggestions("Ha", opts);
    assert.deepEqual(results, [harry_item, hamlet_item]);

    // Reincluding Hal and deactivating harry
    user_ids = [hamlet.user_id, harry.user_id, hal.user_id];
    people.deactivate(harry);
    results = ct.get_person_suggestions("Ha", opts);
    // harry is excluded since it has been deactivated.
    assert.deepEqual(results, [hal_item, hamlet_item]);

    // Test that members group is not include in DM typeahead
    // as it has more than 20 members.
    opts.filter_groups_for_dm = true;
    override_rewire(ct, "max_group_size_for_dm", 4);
    results = ct.get_person_suggestions("rs", opts);
    assert.deepEqual(results, [hamletcharacters, admins]);

    // Test that full members group is only included if
    // waiting_period_threshold is not 0.
    override(realm, "realm_waiting_period_threshold", 10);
    results = ct.get_person_suggestions("fu", opts);
    assert.deepEqual(results, [full_members]);

    override(realm, "realm_waiting_period_threshold", 0);
    results = ct.get_person_suggestions("fu", opts);
    assert.deepEqual(results, []);
});

test("person suggestion for unique full name syntax", () => {
    let results = ct.get_person_suggestions(`${ali.full_name}|${ali.user_id}`, {});
    // Ali is not a valid user, so we should get no results.
    assert.deepEqual(results, []);

    // Add Ali as a valid user.
    people.add_valid_user_id(ali.user_id);
    results = ct.get_person_suggestions(`${ali.full_name}|${ali.user_id}`, {});
    assert.deepEqual(results, [ali_item]);
});

test("muted users excluded from results", () => {
    // This logic is common to direct message recipients as
    // well as mentions typeaheads, so we need only test once.
    let results;
    const opts = {
        want_broadcast: true,
    };

    // Nobody is muted
    results = ct.get_person_suggestions("corde", opts);
    assert.deepEqual(results, [cordelia_item]);

    // Mute Cordelia, and test that she's excluded from results.
    muted_users.add_muted_user(cordelia.user_id);
    results = ct.get_person_suggestions("corde", opts);
    assert.deepEqual(results, []);

    // Make sure our muting logic doesn't break wildcard mentions
    // or user group mentions.
    results = ct.get_person_suggestions("all", opts);
    const mention_all = broadcast_item(ct.broadcast_mentions()[0]);
    assert.deepEqual(results, [mention_all, call_center]);
});

test("direct message recipients sorted according to stream / topic being viewed", () => {
    // This tests that direct message recipient results are sorted with
    // subscribers of the stream / topic being viewed being given priority.
    // If no stream is being viewed, the sort is alphabetical (for testing,
    // since we do not simulate direct message history)
    let results;

    // Simulating just cordelia being subscribed to denmark.
    peer_data.set_subscribers(denmark_stream.stream_id, [cordelia.user_id]);
    mock_banners();

    // When viewing no stream, sorting is alphabetical
    compose_state.set_stream_id("");
    results = ct.get_pm_people("li");
    // `get_pm_people` can't return mentions, so the items are all user items.
    assert.deepEqual(results, [ali_item, alice_item, cordelia_item]);

    // When viewing denmark stream, subscriber cordelia is placed higher
    compose_state.set_stream_id(denmark_stream.stream_id);
    results = ct.get_pm_people("li");
    assert.deepEqual(results, [cordelia_item, ali_item, alice_item]);

    // Simulating just alice being subscribed to denmark.
    peer_data.set_subscribers(denmark_stream.stream_id, [alice.user_id]);

    // When viewing denmark stream to which alice is subscribed, ali is not
    // 1st despite having an exact name match with the query.
    results = ct.get_pm_people("ali");
    assert.deepEqual(results, [alice_item, ali_item]);
});

test("get_pm_people respects DM permissions", ({override}) => {
    $("#private_message_recipient").set_parent($.create("pm-recipient-container"));

    let pill_items = [];

    override(input_pill, "create", () => ({
        clear() {
            pill_items = [];
        },
        clear_text() {},
        items: () => pill_items,
        onPillCreate() {},
        onPillRemove() {},
        appendValidatedData(item) {
            pill_items.push(item);
        },
    }));

    compose_pm_pill.initialize({on_pill_create_or_remove: noop});

    mock_banners();
    compose_state.set_stream_id("");

    compose_state.set_private_message_recipient_ids([]);

    override(message_util, "get_direct_message_permission_hints", () => ({
        is_known_empty_conversation: true,
        is_local_echo_safe: true,
    }));

    // When DMs are disabled realm-wide, lear should not appear in suggestions.
    override(realm, "realm_direct_message_permission_group", nobody.id);
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    let results = ct.get_pm_people("king lear");
    assert.deepEqual(results, []);

    // Bot suggestions always appear regardless of DM permissions.
    results = ct.get_pm_people("welcome");
    assert.deepEqual(results, [welcome_bot_item]);

    // When DMs are allowed, lear should appear in suggestions.
    override(realm, "realm_direct_message_permission_group", members.id);
    override(realm, "realm_direct_message_initiator_group", members.id);
    results = ct.get_pm_people("king lear");
    assert.deepEqual(results, [lear_item]);

    // Sender is not in initiator group and no past conversation history;
    // lear should not appear even though lear is in the permission group.
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    results = ct.get_pm_people("king lear");
    assert.deepEqual(results, []);

    // Sender is in initiator group; lear should appear.
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_initiator_group", {
        direct_members: [hamlet.user_id],
        direct_subgroups: [],
    });
    results = ct.get_pm_people("king lear");
    assert.deepEqual(results, [lear_item]);

    // Othello is not in the permission group, so othello should not appear.
    results = ct.get_pm_people("othello");
    assert.deepEqual(results, []);

    // With lear already in the recipient box, othello can be added to a group
    // DM because lear is in the permission group, satisfying the check.
    compose_state.set_private_message_recipient_ids([lear.user_id]);
    results = ct.get_pm_people("othello");
    assert.deepEqual(results, [othello_item]);

    // Sender is not in initiator group but past conversation history exists;
    // lear should appear because the conversation is not known to be empty.
    compose_state.set_private_message_recipient_ids([]);
    override(message_util, "get_direct_message_permission_hints", () => ({
        is_known_empty_conversation: false,
        is_local_echo_safe: true,
    }));
    override(realm, "realm_direct_message_initiator_group", nobody.id);
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    results = ct.get_pm_people("king lear");
    assert.deepEqual(results, [lear_item]);

    // With a bot already in the recipient box and sender in the initiator
    // group, another bot should always appear in suggestions.
    compose_state.set_private_message_recipient_ids([notification_bot.user_id]);
    override(realm, "realm_direct_message_initiator_group", {
        direct_members: [hamlet.user_id],
        direct_subgroups: [],
    });
    override(realm, "realm_direct_message_permission_group", {
        direct_members: [lear.user_id],
        direct_subgroups: [],
    });
    results = ct.get_pm_people("welcome");
    assert.deepEqual(results, [welcome_bot_item]);

    // With a bot already in the recipient box, a user in the permission
    // group should appear because the sender can initiate.
    results = ct.get_pm_people("king lear");
    assert.deepEqual(results, [lear_item]);

    // With a bot already in the recipient box, a user not in the
    // permission group should not appear.
    results = ct.get_pm_people("othello");
    assert.deepEqual(results, []);
});