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.

58.3 KB
Raw Blame
"use strict";

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

const {make_realm} = require("./lib/example_realm.cjs");
const {clock, mock_esm, zrequire} = require("./lib/namespace.cjs");
const {run_test, noop} = require("./lib/test.cjs");
const blueslip = require("./lib/zblueslip.cjs");
const {$} = require("./lib/zjquery.cjs");
const {page_params} = require("./lib/zpage_params.cjs");

const {initialize_user_settings} = zrequire("user_settings");

const user_settings = {};
initialize_user_settings({user_settings});

window.scrollTo = noop;
const test_url = () => "https://www.example.com";
const test_permalink = () => "https://www.example.com/with/12";

// We assign this in our test() wrapper.
let messages;

const private_messages = [];

// sender1 == current user
// sender2 == any other user
const sender1 = 1;
const sender2 = 2;

// New stream
const stream1 = 1;
const stream2 = 2;
const stream3 = 3;
const stream4 = 4;
const stream5 = 5; // Deleted stream
const stream6 = 6; // Muted stream

// Topics in the stream, all unread except topic1 & stream1.
const topic1 = "topic-1"; // No other sender & read.
const topic2 = "topic-2"; // Other sender
const topic3 = "topic-3"; // User not present
const topic4 = "topic-4"; // User not present
const topic5 = "topic-5"; // other sender
const topic6 = "topic-6"; // other sender
const topic7 = "topic-7"; // muted topic
const topic8 = "topic-8";
const topic9 = "topic-9";
const topic10 = "topic-10";
const topic11 = "topic-11"; // unmuted topic
const topic12 = "topic-12"; // followed topic

const all_visibility_policies = {
    INHERIT: 0,
    MUTED: 1,
    UNMUTED: 2,
    FOLLOWED: 3,
};

let expected_data_to_replace_in_list_widget;

const ListWidget = mock_esm("../src/list_widget", {
    modifier_html: noop,
    generic_sort_functions: noop,
    create(_container, mapped_topic_values, opts) {
        const formatted_topics = [];
        ListWidget.modifier_html = opts.modifier_html;
        for (const item of mapped_topic_values) {
            formatted_topics.push(opts.modifier_html(item));
            opts.filter.predicate(item);
        }
        // Just for coverage, the mechanisms
        // are tested in list_widget.test.cjs
        if (mapped_topic_values.length >= 2) {
            opts.sort_fields.channel_sort(mapped_topic_values[0], mapped_topic_values[1]);
            opts.sort_fields.channel_sort(mapped_topic_values[1], mapped_topic_values[0]);
            opts.sort_fields.channel_sort(mapped_topic_values[0], mapped_topic_values[0]);
            opts.sort_fields.conversation_sort(mapped_topic_values[0], mapped_topic_values[1]);
            opts.sort_fields.conversation_sort(mapped_topic_values[1], mapped_topic_values[0]);
            opts.sort_fields.conversation_sort(mapped_topic_values[0], mapped_topic_values[0]);
        }
        return ListWidget;
    },

    hard_redraw: noop,
    filter_and_sort: () => [],
    get_current_list: () => [],
    get_rendered_count: () => ListWidget.get_current_list().length,
    all_rendered: () => true,
    replace_list_data(data) {
        assert.notEqual(
            expected_data_to_replace_in_list_widget,
            undefined,
            "You must set expected_data_to_replace_in_list_widget",
        );
        assert.deepEqual(data, expected_data_to_replace_in_list_widget);
        expected_data_to_replace_in_list_widget = undefined;
    },
});

mock_esm("../src/compose_closed_ui", {
    set_standard_text_for_reply_button: noop,
    update_buttons: noop,
    update_reply_button: noop,
});
mock_esm("../src/hash_util", {
    channel_url_by_user_setting: test_url,
    by_stream_topic_url: test_url,
    by_conversation_and_time_url: test_url,
});
mock_esm("../src/stream_topic_history", {
    channel_topic_permalink_hash: test_permalink,
});
mock_esm("../src/message_list_data", {
    MessageListData: class {},
});
const message_store = zrequire("message_store");
mock_esm("../src/message_view_header", {
    render_title_area: noop,
});
mock_esm("../src/settings_data", {
    user_can_access_all_other_users: () => true,
});
mock_esm("../src/user_topics", {
    is_topic_muted(stream_id, topic) {
        if (stream_id === stream1 && topic === topic7) {
            return true;
        }
        return false;
    },
    is_topic_unmuted_or_followed(stream_id, topic) {
        if (stream_id === stream6 && (topic === topic11 || topic === topic12)) {
            return true;
        }
        return false;
    },
    get_topic_visibility_policy(stream_id, topic) {
        if (stream_id === stream1 && topic === topic7) {
            return all_visibility_policies.MUTED;
        }
        if (stream_id === stream6 && topic === topic11) {
            return all_visibility_policies.UNMUTED;
        }
        if (stream_id === stream6 && topic === topic12) {
            return all_visibility_policies.FOLLOWED;
        }
        return all_visibility_policies.INHERIT;
    },
    all_visibility_policies,
});
mock_esm("../src/narrow_title", {
    update_narrow_title() {},
});
mock_esm("../src/pm_list", {
    update_private_messages: noop,
    handle_message_view_deactivated: noop,
});
const recent_senders = mock_esm("../src/recent_senders", {
    get_topic_recent_senders: () => [2, 1],
    get_topic_message_ids_for_sender: () => new Set(),
    get_pm_recent_senders(user_ids_string) {
        return {
            participants: user_ids_string.split(",").map((user_id) => Number.parseInt(user_id, 10)),
        };
    },
});
mock_esm("../src/stream_data", {
    is_muted(stream_id) {
        return stream_id === stream6;
    },
    get_stream_name_from_id: () => "stream_name",
    get_sub_by_name: noop,
});
mock_esm("../src/stream_list", {
    handle_message_view_deactivated: noop,
});
mock_esm("../src/timerender", {
    relative_time_string_from_date: () => "Just now",
    get_full_datetime_clarification: () => "date at time",
});
mock_esm("../src/left_sidebar_navigation_area", {
    highlight_recent_view: noop,
});
const unread = mock_esm("../src/unread", {
    num_unread_for_topic(stream_id, topic) {
        if (stream_id === 1 && topic === "topic-1") {
            return 0;
        }
        return 1;
    },
    get_unread_message_count: () => 0,
    num_unread_for_user_ids_string() {
        return 0;
    },
    topic_has_any_unread_mentions: () => false,
    num_unread_mentions_for_user_ids_strings(user_ids_string) {
        if (user_ids_string === "2,3") {
            return false;
        }
        return true;
    },
});
mock_esm("../src/resize", {
    update_recent_view: noop,
    set_recent_view_participants_rerender: noop,
    set_recent_view_participants_column_class_update: noop,
});
mock_esm("../src/popup_banners", {
    close_found_missing_unreads_banner: noop,
});
const mock_user_has_folders = false;
mock_esm("../src/channel_folders", {
    user_has_folders: () => mock_user_has_folders,
    get_folders_with_accessible_channels: () => [],
    is_valid_folder_id: () => false,
    get_channel_folder_by_id: () => ({name: "Test folder"}),
});
mock_esm("../src/folder_dropdown_widget", {
    FOLDER_FILTERS: {
        UNCATEGORIZED_DROPDOWN_OPTION: -101,
        ANY_FOLDER_DROPDOWN_OPTION: -102,
    },
    get_tooltip_text_for_folder_filter: () => "Filter by folder",
});
const dropdown_widget = mock_esm("../src/dropdown_widget");
dropdown_widget.DropdownWidget = function DropdownWidget() {
    this.setup = noop;
    this.render = noop;
};

const {recent_view_messages_data} = zrequire("recent_view_messages_data");
const {buddy_list} = zrequire("buddy_list");
const activity_ui = zrequire("activity_ui");
const people = zrequire("people");
const rt = zrequire("recent_view_ui");
const views_util = zrequire("views_util");
rt.set_hide_other_views(noop);
const recent_view_util = zrequire("recent_view_util");
const rt_data = zrequire("recent_view_data");
const muted_users = zrequire("muted_users");
const {set_realm} = zrequire("state_data");
const sub_store = zrequire("sub_store");
const util = zrequire("util");

const REALM_EMPTY_TOPIC_DISPLAY_NAME = "test general chat";
set_realm(make_realm({realm_empty_topic_display_name: REALM_EMPTY_TOPIC_DISPLAY_NAME}));

for (const stream_id of [stream1, stream2, stream3, stream4, stream6]) {
    sub_store.add_hydrated_sub(stream_id, {
        color: "",
        invite_only: false,
        is_web_public: true,
        is_archived: false,
        subscribed: true,
    });
}

people.add_active_user({
    email: "[email protected]",
    user_id: 1,
    full_name: "Alice Smith",
});
people.add_active_user({
    email: "[email protected]",
    user_id: 2,
    full_name: "Fred Flintstone",
});
people.add_active_user({
    email: "[email protected]",
    user_id: 3,
    full_name: "Spike Spiegel",
});
people.add_active_user({
    email: "[email protected]",
    user_id: 4,
    full_name: "Eren Yeager",
});

people.initialize_current_user(1);
muted_users.add_muted_user(2, 17947949);
muted_users.add_muted_user(4, 17947949);

let id = 0;

const sample_messages = [];
sample_messages[0] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic1,
    sender_id: sender1,
    type: "stream",
};

sample_messages[1] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic2,
    sender_id: sender1,
    type: "stream",
};

sample_messages[2] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic2,
    sender_id: sender2,
    type: "stream",
};

sample_messages[3] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic3,
    sender_id: sender2,
    type: "stream",
};

sample_messages[4] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic4,
    sender_id: sender2,
    type: "stream",
};

sample_messages[5] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic5,
    sender_id: sender1,
    type: "stream",
};

sample_messages[6] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic5,
    sender_id: sender2,
    type: "stream",
};

sample_messages[7] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic6,
    sender_id: sender1,
    type: "stream",
};

sample_messages[8] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic6,
    sender_id: sender2,
    type: "stream",
};

sample_messages[9] = {
    stream_id: stream1,
    id: (id += 1),
    topic: topic7,
    sender_id: sender1,
    type: "stream",
};

// a message of stream4
sample_messages[10] = {
    stream_id: stream4,
    id: (id += 1),
    topic: topic10,
    sender_id: sender1,
    type: "stream",
};

// normal topic in muted stream
sample_messages[11] = {
    stream_id: stream6,
    id: (id += 1),
    topic: topic8,
    sender_id: sender1,
    type: "stream",
};

// unmuted topic in muted stream
sample_messages[12] = {
    stream_id: stream6,
    id: (id += 1),
    topic: topic11,
    sender_id: sender1,
    type: "stream",
};

// followed topic in muted stream
sample_messages[13] = {
    stream_id: stream6,
    id: (id += 1),
    topic: topic12,
    sender_id: sender1,
    type: "stream",
};

private_messages[0] = {
    id: (id += 1),
    sender_id: sender1,
    to_user_ids: "2,3",
    type: "private",
    display_recipient: [{id: 1}, {id: 2}, {id: 3}],
    pm_with_url: test_url(),
};
private_messages[1] = {
    id: (id += 1),
    sender_id: sender1,
    to_user_ids: "2,4",
    type: "private",
    display_recipient: [{id: 1}, {id: 2}, {id: 4}],
    pm_with_url: test_url(),
};
private_messages[2] = {
    id: (id += 1),
    sender_id: sender1,
    to_user_ids: "3",
    type: "private",
    display_recipient: [{id: 1}, {id: 3}],
    pm_with_url: test_url(),
};

function get_topic_key(stream_id, topic) {
    return stream_id + ":" + topic.toLowerCase();
}

function generate_topic_data(topic_info_array) {
    // Since most of the fields are common, this function helps generate fixtures
    // with non-common fields.
    $.clear_all_elements();
    const data = [];

    for (const [stream_id, topic, unread_count, visibility_policy] of topic_info_array) {
        data.push({
            other_senders_count: 0,
            other_sender_names_html: "",
            invite_only: false,
            is_web_public: true,
            is_private: false,
            is_archived: false,
            last_msg_time: "Just now",
            last_msg_url: "https://www.example.com",
            full_last_msg_date_time: "date at time",
            senders: people.sender_info_for_recent_view_row([1, 2]),
            stream_name: "stream_name",
            stream_color: "",
            stream_id,
            stream_url: "https://www.example.com",
            topic,
            topic_display_name: util.get_final_topic_display_name(topic),
            is_empty_string_topic: topic === "",
            conversation_key: get_topic_key(stream_id, topic),
            topic_url: "https://www.example.com/with/12",
            unread_count,
            mention_in_unread: false,
            visibility_policy,
            all_visibility_policies,
            is_spectator: page_params.is_spectator,
            column_indexes: rt.COLUMNS,
        });
    }
    return data;
}

function verify_topic_data(all_topics, stream, topic, last_msg_id, participated) {
    const topic_data = all_topics.get(stream + ":" + topic);
    assert.equal(topic_data.last_msg_id, last_msg_id);
    assert.equal(topic_data.participated, participated);
}

function stub_out_filter_buttons() {
    // TODO: We probably want more direct tests that make sure
    //       the widgets get updated correctly, but the stubs here
    //       should accurately simulate toggling the filters.
    //
    //       See show_selected_filters() and set_filter() in the
    //       implementation.
    for (const filter of ["unread", "muted", "participated", "include_private"]) {
        const $stub = $.create(`filter-${filter}-stub`);
        const selector = `[data-filter="${filter}"]`;
        $("#recent_view_filter_buttons").set_find_results(selector, $stub);
    }
}

function show_recent_view_with_messages() {
    $.clear_all_elements();
    recent_view_util.set_visible(true);
    rt.clear_for_tests();
    rt.set_filters_for_tests();
    stub_out_filter_buttons();
    rt.process_messages(messages);
}

function conversation_for(topic) {
    return rt_data.conversations.get(get_topic_key(stream1, topic));
}

function row_selector(conversation_key) {
    return `#${CSS.escape(`recent_conversation:${conversation_key}`)}`;
}

// zjquery reports a row for any selector by default.
function stub_no_row_for(conversation_key) {
    $.set_results(row_selector(conversation_key), []);
}

function bulk_rerender(keys) {
    expected_data_to_replace_in_list_widget = rt_data.get_conversations().values().toArray();
    rt.bulk_inplace_rerender(new Set(keys));
}

function record_row_updates(override) {
    const updates = [];
    override(ListWidget, "render_item", (conversation) => {
        updates.push(["rerender", conversation]);
    });
    override(ListWidget, "insert_rendered_row", (conversation, get_insert_index) => {
        updates.push(["insert", conversation, get_insert_index()]);
    });
    return updates;
}

// Shows recent view with a row for each of `topics`, in that order, and
// the keyboard focus on the row of `focused_topic`. A test models a
// rerender with what is returned: set_list() sets the widget's list,
// set_rows() the rows in the table, and replace_row() gives a topic a
// new row, as rerendering its conversation or redrawing the view does.
function show_recent_view_with_focused_row(override, topics, focused_topic) {
    show_recent_view_with_messages();
    let list;
    override(ListWidget, "get_current_list", () => list);
    function set_list(listed_topics) {
        list = listed_topics.map((topic) => conversation_for(topic));
    }

    const $table = $("#recent-view-content-tbody");
    const row_of_topic = new Map();
    let rows_made = 0;
    function make_row(topic) {
        rows_made += 1;
        const $row = $.create(`row-${rows_made}-of-${topic}`);
        $row.attr("id", `recent_conversation:${get_topic_key(stream1, topic)}`);
        $row.set_parent($table);
        // Focusing a row reads its channel and topic for the reply button.
        $row.set_find_results(".recent-view-channel-name", $.create(`channel-${rows_made}`));
        $row.set_find_results(".recent-view-conversation-link", $.create(`link-${rows_made}`));
        row_of_topic.set(topic, $row[0]);
    }
    const fixture_topics = new Set(messages.map((message) => message.topic));
    for (const topic of fixture_topics) {
        make_row(topic);
    }
    function set_results(selector, elements) {
        $.reset_selector(selector);
        $.set_results(selector, elements);
    }
    function set_rows(topics_with_rows) {
        const rows = topics_with_rows.map((topic) => row_of_topic.get(topic));
        for (const [topic, row] of row_of_topic) {
            const selector = row_selector(get_topic_key(stream1, topic));
            set_results(selector, rows.includes(row) ? [row] : []);
        }
        set_results("#recent-view-content-tbody tr", rows);
        $table.set_children(rows);
    }
    set_list(topics);
    set_rows(topics);
    rt.focus_clicked_element(topics.indexOf(focused_topic), rt.COLUMNS.topic);
    return {set_list, set_rows, replace_row: make_row};
}

function get_focused_topic() {
    return rt.get_focused_row_message()?.topic;
}

function test(label, f) {
    run_test(label, (helpers) => {
        page_params.development_environment = true;
        page_params.is_node_test = true;
        messages = sample_messages.map((message) => ({...message}));
        message_store.set_messages_for_tests(
            [...messages, ...private_messages].map((message) => ({message})),
        );
        rt.set_default_focus();
        f(helpers);
    });
}

test("test_recent_view_show", ({override, mock_template}) => {
    // Note: unread count and urls are fake,
    // since they are generated in external libraries
    // and are not to be tested here.
    page_params.is_spectator = false;
    const expected = {
        filter_unread: false,
        filter_participated: false,
        filter_pm: false,
        search_val: "",
        is_spectator: false,
        show_folder_filter: false,
        folder_filter_tooltip: "Filter by folder",
    };

    activity_ui.set_cursor_and_filter();

    mock_template("recent_view_table.hbs", false, (data) => {
        assert.deepEqual(data, expected);
        return "<recent_view table stub>";
    });

    mock_template("recent_view_row.hbs", false, () => "<recent-view-row-stub>");

    let buddy_list_populated = false;
    override(buddy_list, "populate", () => {
        buddy_list_populated = true;
    });

    stub_out_filter_buttons();

    rt.clear_for_tests();
    rt.set_filters_for_tests();
    rt.process_messages(messages);

    rt.show();

    assert.ok(buddy_list_populated);

    // incorrect topic_key
    assert.equal(rt.inplace_rerender("stream_unknown:topic_unknown"), false);
});

test("test_filter_is_spectator", ({mock_template}) => {
    page_params.is_spectator = true;
    const expected = {
        filter_unread: false,
        filter_participated: false,
        filter_pm: false,
        search_val: "",
        is_spectator: true,
        show_folder_filter: false,
        folder_filter_tooltip: "Filter by folder",
    };
    let row_data;
    let i;

    mock_template("recent_view_table.hbs", false, (data) => {
        assert.deepEqual(data, expected);
        return "<recent-view-table-stub>";
    });

    mock_template("recent_view_row.hbs", false, (data) => {
        i -= 1;
        assert.deepEqual(data, row_data[i]);
        return "<recent_view row stub>";
    });

    row_data = generate_topic_data([[1, "topic-1", 0, all_visibility_policies.INHERIT]]);
    i = row_data.length;
    rt.clear_for_tests();
    rt.set_filters_for_tests();
    stub_out_filter_buttons();
    recent_view_util.set_visible(true);
    rt.process_messages([messages[0]]);
});

test("test_no_filter", ({mock_template}) => {
    // Just tests inplace rerender of a message
    // in All topics filter.
    page_params.is_spectator = false;
    const expected = {
        filter_unread: false,
        filter_participated: false,
        filter_pm: false,
        search_val: "",
        is_spectator: false,
        show_folder_filter: false,
        folder_filter_tooltip: "Filter by folder",
    };
    let row_data;
    let i;

    mock_template("recent_view_table.hbs", false, (data) => {
        assert.deepEqual(data, expected);
        return "<recent-view-table-stub>";
    });

    mock_template("recent_view_row.hbs", false, (data) => {
        i -= 1;
        assert.deepEqual(data, row_data[i]);
        return "<recent_view row stub>";
    });

    // topic is not muted
    row_data = generate_topic_data([[1, "topic-1", 0, all_visibility_policies.INHERIT]]);
    i = row_data.length;
    rt.clear_for_tests();
    rt.set_filters_for_tests();
    stub_out_filter_buttons();
    recent_view_util.set_visible(true);
    rt.process_messages([messages[0]]);
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 1, participated: true, type: "stream"}),
        false,
    );

    // TODO: Modify this test to work with dropdown widget.
    // expected_data_to_replace_in_list_widget = [
    //     {last_msg_id: 10, participated: true, type: "stream"},
    //     {last_msg_id: 1, participated: true, type: "stream"},
    // ];

    // // topic is muted
    // row_data = [
    //     ...row_data,
    //     ...generate_topic_data([[1, "topic-7", 1, all_visibility_policies.MUTED]]),
    // ];
    // i = row_data.length;
    // stub_out_filter_buttons();
    // rt.process_messages([messages[9]]);
    // assert.equal(
    //     rt.filters_should_hide_row({last_msg_id: 10, participated: true, type: "stream"}),
    //     true,
    // );

    // expected_data_to_replace_in_list_widget = [
    //     {last_msg_id: 12, participated: true, type: "stream"},
    //     {last_msg_id: 10, participated: true, type: "stream"},
    //     {last_msg_id: 1, participated: true, type: "stream"},
    // ];
    // // normal topic in muted stream
    // row_data = [
    //     ...row_data,
    //     ...generate_topic_data([[6, "topic-8", 1, all_visibility_policies.INHERIT]]),
    // ];
    // i = row_data.length;
    // stub_out_filter_buttons();
    // rt.process_messages([messages[11]]);
    // assert.equal(
    //     rt.filters_should_hide_row({last_msg_id: 12, participated: true, type: "stream"}),
    //     true,
    // );

    // expected_data_to_replace_in_list_widget = [
    //     {last_msg_id: 13, participated: true, type: "stream"},
    //     {last_msg_id: 12, participated: true, type: "stream"},
    //     {last_msg_id: 10, participated: true, type: "stream"},
    //     {last_msg_id: 1, participated: true, type: "stream"},
    // ];
    // // unmuted topic in muted stream
    // row_data = [
    //     ...row_data,
    //     ...generate_topic_data([[6, "topic-11", 1, all_visibility_policies.UNMUTED]]),
    // ];
    // i = row_data.length;
    // stub_out_filter_buttons();
    // rt.process_messages([messages[12]]);
    // assert.equal(
    //     rt.filters_should_hide_row({last_msg_id: 13, participated: true, type: "stream"}),
    //     false,
    // );

    // expected_data_to_replace_in_list_widget = [
    //     {last_msg_id: 14, participated: true, type: "stream"},
    //     {last_msg_id: 13, participated: true, type: "stream"},
    //     {last_msg_id: 12, participated: true, type: "stream"},
    //     {last_msg_id: 10, participated: true, type: "stream"},
    //     {last_msg_id: 1, participated: true, type: "stream"},
    // ];
    // // followed topic in muted stream
    // row_data = [
    //     ...row_data,
    //     ...generate_topic_data([[6, "topic-12", 1, all_visibility_policies.FOLLOWED]]),
    // ];
    // i = row_data.length;
    // stub_out_filter_buttons();
    // rt.process_messages([messages[13]]);
    // assert.equal(
    //     rt.filters_should_hide_row({last_msg_id: 14, participated: true, type: "stream"}),
    //     false,
    // );

    // Test search
    expected.search_val = "topic-1";
    row_data = generate_topic_data([[1, "topic-1", 0, all_visibility_policies.INHERIT]]);
    i = row_data.length;
    rt.set_default_focus();
    $("#search_query").trigger("focus");
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 1, participated: true, type: "stream"}),
        false,
    );
});

test("test_filter_pm", ({mock_template}) => {
    page_params.is_spectator = false;
    const expected = {
        filter_unread: false,
        filter_participated: false,
        filter_pm: true,
        search_val: "",
        is_spectator: false,
        show_folder_filter: false,
        folder_filter_tooltip: "Filter by folder",
    };

    const expected_users_with_icons = [
        {name: "Spike Spiegel", status_emoji_info: undefined},
        {name: "translated: Muted user", status_emoji_info: undefined},
    ];

    mock_template("recent_view_table.hbs", false, (data) => {
        assert.deepEqual(data, expected);
        return "<recent-view-table-stub>";
    });

    mock_template("users_with_status_icons.hbs", false, (data) => {
        assert.deepEqual(data, {users: expected_users_with_icons});
        return "<users_with_status_icons stub>";
    });

    mock_template("recent_view_row.hbs", true, (_data, html) => {
        assert.ok(html.startsWith('<tr id="recent_conversation'));
        return "<recent-view-row-stub>";
    });

    rt.clear_for_tests();
    rt.set_filters_for_tests();
    stub_out_filter_buttons();
    recent_view_util.set_visible(true);
    rt.set_filter("include_private");

    expected_data_to_replace_in_list_widget = [
        {last_msg_id: 15, participated: true, type: "private"},
    ];

    rt.process_messages([private_messages[0]]);

    assert.deepEqual(rt.filters_should_hide_row({type: "private", last_msg_id: 15}), false);
    assert.deepEqual(rt.filters_should_hide_row({type: "private", last_msg_id: 16}), true);
    assert.deepEqual(rt.filters_should_hide_row({type: "private", last_msg_id: 17}), false);
});

test("test_filter_participated", ({mock_template}) => {
    let expected_filter_participated;

    page_params.is_spectator = false;
    mock_template("recent_view_table.hbs", false, (data) => {
        assert.deepEqual(data, {
            filter_unread: false,
            filter_participated: expected_filter_participated,
            filter_pm: false,
            search_val: "",
            is_spectator: false,
            show_folder_filter: false,
            folder_filter_tooltip: "Filter by folder",
        });
        return "<recent-view-table-stub>";
    });

    mock_template("recent_view_filters.hbs", false, (data) => {
        assert.equal(data.filter_participated, expected_filter_participated);
        return "<recent-view-filters-stub>";
    });

    const row_data = generate_topic_data([
        // stream_id, topic, unread_count, visibility_policy
        [6, "topic-12", 1, all_visibility_policies.FOLLOWED],
        [6, "topic-11", 1, all_visibility_policies.UNMUTED],
        [6, "topic-8", 1, all_visibility_policies.INHERIT],
        [4, "topic-10", 1, all_visibility_policies.INHERIT],
        [1, "topic-7", 1, all_visibility_policies.MUTED],
        [1, "topic-6", 1, all_visibility_policies.INHERIT],
        [1, "topic-5", 1, all_visibility_policies.INHERIT],
        [1, "topic-4", 1, all_visibility_policies.INHERIT],
        [1, "topic-3", 1, all_visibility_policies.INHERIT],
        [1, "topic-2", 1, all_visibility_policies.INHERIT],
        [1, "topic-1", 0, all_visibility_policies.INHERIT],
    ]);
    let i = 0;

    mock_template("recent_view_row.hbs", false, (data) => {
        // All the row will be processed.
        if (row_data[i] !== undefined) {
            assert.deepEqual(data, row_data[i]);
            i += 1;
        }
        return "<recent_view row stub>";
    });

    rt.clear_for_tests();
    rt.set_filters_for_tests();
    recent_view_util.set_visible(true);
    rt.set_default_focus();
    stub_out_filter_buttons();
    expected_filter_participated = false;
    rt.process_messages(messages);

    $("#search_query").trigger("focus");
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 4, participated: true, type: "stream"}),
        false,
    );

    // Set muted filter
    rt.set_filter("muted");
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 7, participated: true, type: "stream"}),
        false,
    );

    // remove muted filter
    rt.set_filter("muted");

    $("#recent_view_filter_buttons").removeClass("button-recent-selected");

    expected_filter_participated = true;

    rt.set_filter("participated");
    rt.update_filters_view();

    assert.equal(i, row_data.length);

    expected_data_to_replace_in_list_widget = [
        {
            last_msg_id: 11,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 10,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 9,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 7,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 5,
            participated: false,
            type: "stream",
        },
        {
            last_msg_id: 4,
            participated: false,
            type: "stream",
        },
        {
            last_msg_id: 3,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 1,
            participated: true,
            type: "stream",
        },
    ];

    rt.process_messages([messages[4]]);
});

test("bulk_inplace_rerender updates the requested rows in list order", ({override}) => {
    show_recent_view_with_messages();
    const [with_row, not_requested, without_row] = [topic1, topic2, topic3].map((topic) =>
        conversation_for(topic),
    );
    stub_no_row_for(get_topic_key(stream1, topic3));
    const updates = record_row_updates(override);
    override(ListWidget, "get_current_list", () => [without_row, with_row, not_requested]);

    bulk_rerender([get_topic_key(stream1, topic1), get_topic_key(stream1, topic3)]);
    assert.deepEqual(updates, [
        ["insert", without_row, 0],
        ["rerender", with_row],
    ]);
});

test("bulk_inplace_rerender falls back from the unread sort before updating rows", ({override}) => {
    // The last unread conversation is read while the unread sort is active.
    show_recent_view_with_messages();
    const conversation = conversation_for(topic2);
    override(ListWidget, "get_current_list", () => [conversation]);
    let unread_message_count = 1;
    override(unread, "get_unread_message_count", () => unread_message_count);
    // Recent view first has to see the conversation as unread.
    bulk_rerender([]);
    $("#recent-view-table-headers .recent-view-unread-sort-header").addClass("active");

    const calls = [];
    override(ListWidget, "set_reverse_mode", noop);
    override(ListWidget, "sort", () => {
        calls.push("sort");
    });
    override(ListWidget, "render_item", () => {
        calls.push("rerender");
    });
    unread_message_count = 0;
    bulk_rerender([get_topic_key(stream1, topic2)]);
    assert.deepEqual(calls, ["sort", "rerender"]);
});

test("inplace_rerender updates one row the way a bulk rerender does", ({override}) => {
    show_recent_view_with_messages();
    const [with_row, without_row] = [topic1, topic2].map((topic) => conversation_for(topic));
    stub_no_row_for(get_topic_key(stream1, topic2));
    const updates = record_row_updates(override);
    override(ListWidget, "get_current_list", () => [with_row, without_row]);

    assert.ok(rt.inplace_rerender(get_topic_key(stream1, topic1)));
    assert.ok(rt.inplace_rerender(get_topic_key(stream1, topic2)));
    assert.deepEqual(updates, [
        ["rerender", with_row],
        ["insert", without_row, 1],
    ]);

    // A conversation the filters hide is not in the list; the widget's
    // resort has removed its row, so no row is updated for it.
    assert.ok(rt.inplace_rerender(get_topic_key(stream1, topic7)));
    assert.equal(updates.length, 2);
});

test("rerender_conversations_with_user rerenders the rows that show the user", ({override}) => {
    // A row shows the senders of its topic, or the participants of its
    // direct message conversation. sender2 sent to topics 2 to 6 and is
    // in the direct message conversations "2,3" and "2,4".
    show_recent_view_with_messages();
    for (const message of private_messages) {
        rt_data.process_message(message);
    }
    override(recent_senders, "get_topic_message_ids_for_sender", (stream_id, topic, sender_id) => {
        const messages_sent = messages.filter(
            (message) =>
                message.stream_id === stream_id &&
                message.topic === topic &&
                message.sender_id === sender_id,
        );
        return new Set(messages_sent.map((message) => message.id));
    });
    const rerendered_conversations = new Set();
    override(ListWidget, "render_item", (conversation) => {
        rerendered_conversations.add(conversation);
    });
    const inserted_conversations = new Set();
    override(ListWidget, "insert_rendered_row", (conversation) => {
        inserted_conversations.add(conversation);
    });
    override(ListWidget, "get_current_list", () => rt_data.get_conversations().values().toArray());
    function rerender_conversations_with(user_id) {
        rerendered_conversations.clear();
        inserted_conversations.clear();
        expected_data_to_replace_in_list_widget = rt_data.get_conversations().values().toArray();
        rt.rerender_conversations_with_user(user_id);
    }

    stub_no_row_for(get_topic_key(stream1, topic6));
    stub_no_row_for("2,4");
    const rows_showing_sender2 = new Set([
        ...[topic2, topic3, topic4, topic5].map((topic) => conversation_for(topic)),
        rt_data.conversations.get("2,3"),
    ]);
    rerender_conversations_with(sender2);
    assert.deepEqual(rerendered_conversations, rows_showing_sender2);
    assert.equal(inserted_conversations.size, 0);

    // A search matches direct message conversations by their
    // participants, so it may now list one that has no row.
    $("#recent_view_search").val("fred");
    rerender_conversations_with(sender2);
    assert.deepEqual(rerendered_conversations, rows_showing_sender2);
    assert.deepEqual(inserted_conversations, new Set([rt_data.conversations.get("2,4")]));
    $("#recent_view_search").val("");

    // The mocked widget rejects the list update that a rerender would
    // start with, as none is expected for a user in no conversation.
    const user_id_in_no_conversation = 5;
    rt.rerender_conversations_with_user(user_id_in_no_conversation);
});

test("rerender renders more rows until they reach the bottom of the viewport", ({
    override,
    override_rewire,
}) => {
    show_recent_view_with_messages();
    let batches_rendered = 0;
    override(ListWidget, "render", () => {
        batches_rendered += 1;
    });

    const batches_until_viewport_is_filled = 2;
    override(ListWidget, "all_rendered", () => false);
    override_rewire(
        views_util,
        "is_bottom_padding_in_view",
        () => batches_rendered < batches_until_viewport_is_filled,
    );
    bulk_rerender([]);
    assert.equal(batches_rendered, batches_until_viewport_is_filled);

    batches_rendered = 0;
    const batches_until_all_rendered = 3;
    override_rewire(views_util, "is_bottom_padding_in_view", () => true);
    override(ListWidget, "all_rendered", () => batches_rendered === batches_until_all_rendered);
    bulk_rerender([]);
    assert.equal(batches_rendered, batches_until_all_rendered);
});

test("rerender inserts rows only within the rendered range", ({override}) => {
    // Two new conversations sort before the only rendered row. Each insert
    // extends the rendered range, which lets the second one in; a third,
    // past the range, is left for the widget to render.
    show_recent_view_with_messages();
    const topics = [topic1, topic2, topic3, topic4];
    const list = topics.map((topic) => conversation_for(topic));
    const [new_first, new_second, with_row] = list;
    for (const topic of [topic1, topic2, topic4]) {
        stub_no_row_for(get_topic_key(stream1, topic));
    }
    override(ListWidget, "get_current_list", () => list);
    const updates = record_row_updates(override);
    override(ListWidget, "get_rendered_count", () => {
        const rows_inserted = updates.filter(([kind]) => kind === "insert").length;
        return 1 + rows_inserted;
    });

    bulk_rerender(topics.map((topic) => get_topic_key(stream1, topic)));
    assert.deepEqual(updates, [
        ["insert", new_first, 0],
        ["insert", new_second, 1],
        ["rerender", with_row],
    ]);
});

test("a topic's visibility update rerenders its row after the delay", ({override_rewire}) => {
    show_recent_view_with_messages();
    const rerendered_keys = [];
    override_rewire(rt, "inplace_rerender", (key) => {
        rerendered_keys.push(key);
    });

    assert.ok(rt.schedule_topic_visibility_update(stream1, topic1, 500));
    clock.tick(499);
    assert.deepEqual(rerendered_keys, []);
    clock.tick(1);
    assert.deepEqual(rerendered_keys, [get_topic_key(stream1, topic1)]);

    // Recent view has no row to update for a topic it is not tracking.
    assert.ok(!rt.schedule_topic_visibility_update(stream1, "topic-10", 500));
    clock.tick(500);
    assert.deepEqual(rerendered_keys, [get_topic_key(stream1, topic1)]);
});

test("a topic's row stays listed until its delayed visibility update", () => {
    // The filters would hide the muted topic at once, but a popover may
    // be anchored on its row.
    show_recent_view_with_messages();
    const muted_conversation = conversation_for(topic7);
    assert.ok(rt.filters_should_hide_row(muted_conversation));

    rt.schedule_topic_visibility_update(stream1, topic7, 500);
    clock.tick(499);
    assert.ok(!rt.filters_should_hide_row(muted_conversation));

    clock.tick(1);
    assert.ok(rt.filters_should_hide_row(muted_conversation));
});

test("a conversation without a row stays unlisted until its visibility update", () => {
    // The update gives a topic that is no longer hidden its row. A
    // rerender before that must not list the topic: the widget counts a
    // row for every conversation it lists in its rendered range.
    show_recent_view_with_messages();
    const conversation = conversation_for(topic1);
    stub_no_row_for(get_topic_key(stream1, topic1));
    assert.ok(!rt.filters_should_hide_row(conversation));

    rt.schedule_topic_visibility_update(stream1, topic1, 500);
    clock.tick(499);
    assert.ok(rt.filters_should_hide_row(conversation));

    clock.tick(1);
    assert.ok(!rt.filters_should_hide_row(conversation));
});

test("rerender reports rows its resort removed without being asked", ({override}) => {
    // The widget's resort removes the row of a conversation the filters
    // hide. For any conversation but the one being rerendered, that means
    // an event hid it without updating the view, which is reported.
    show_recent_view_with_messages();
    const muted_key = get_topic_key(stream1, topic7);
    const visible_key = get_topic_key(stream1, topic1);
    override(ListWidget, "render_item", noop);
    override(ListWidget, "get_current_list", () => [conversation_for(topic1)]);
    override(ListWidget, "filter_and_sort", () => [conversation_for(topic7)]);

    rt.inplace_rerender(muted_key);
    assert.deepEqual(blueslip.get_test_logs("error"), []);

    blueslip.expect("error", "Recent view rows hidden without a rerender");
    rt.inplace_rerender(visible_key);
    const [{more_info}] = blueslip.get_test_logs("error");
    assert.equal(more_info.count, 1);
    assert.deepEqual(more_info.types, ["stream"]);
    blueslip.reset();

    // While a search is being typed, its debounced handler has yet to
    // redraw the view, and the resort removes the rows it hides as well.
    $("#recent_view_search").val("half typed");
    rt.inplace_rerender(visible_key);
    assert.deepEqual(blueslip.get_test_logs("error"), []);
    $("#recent_view_search").val("");
});

test("rerender moves the keyboard focus with its conversation's row", ({override}) => {
    // The focus is kept as a row index, so a row inserted above the
    // focused one would leave the focus on the conversation above it.
    const view = show_recent_view_with_focused_row(override, [topic1, topic2], topic2);
    assert.equal(get_focused_topic(), topic2);

    const topics_after_rerender = [topic3, topic1, topic2];
    override(ListWidget, "filter_and_sort", () => {
        view.set_list(topics_after_rerender);
        return [];
    });
    override(ListWidget, "insert_rendered_row", () => {
        view.set_rows(topics_after_rerender);
    });
    rt.inplace_rerender(get_topic_key(stream1, topic3));
    assert.equal(get_focused_topic(), topic2);
});

test("rerender keeps the keyboard focus on its conversation when a row above it is removed", ({
    override,
}) => {
    // The focus is kept as a row index, so removing a row above the
    // focused one would leave the focus on the conversation below it.
    const view = show_recent_view_with_focused_row(override, [topic1, topic2, topic3], topic2);

    override(ListWidget, "filter_and_sort", () => {
        view.set_list([topic2, topic3]);
        view.set_rows([topic2, topic3]);
        return [conversation_for(topic1)];
    });
    rt.inplace_rerender(get_topic_key(stream1, topic1));
    assert.equal(get_focused_topic(), topic2);
});

test("rerender leaves the keyboard focus in place when its conversation moves", ({override}) => {
    // A rerender replaces the focused conversation's row, and the new row
    // can sort far from the old one, so the focus stays at its row index.
    const view = show_recent_view_with_focused_row(override, [topic1, topic2], topic2);

    const topics_after_rerender = [topic2, topic1];
    override(ListWidget, "filter_and_sort", () => {
        view.set_list(topics_after_rerender);
        return [];
    });
    override(ListWidget, "render_item", () => {
        view.replace_row(topic2);
        view.set_rows(topics_after_rerender);
    });
    rt.inplace_rerender(get_topic_key(stream1, topic2));
    assert.equal(get_focused_topic(), topic1);
});

test("rerender moves the keyboard focus up when the last row is removed", ({override}) => {
    const view = show_recent_view_with_focused_row(override, [topic1, topic2], topic2);

    // The filters now hide the focused conversation, so the widget's
    // resort removes its row.
    override(ListWidget, "filter_and_sort", () => {
        view.set_list([topic1]);
        view.set_rows([topic1]);
        return [conversation_for(topic2)];
    });
    rt.inplace_rerender(get_topic_key(stream1, topic2));
    assert.equal(get_focused_topic(), topic1);
});

test("rerender keeps the keyboard focus within the rows that are left", ({override}) => {
    // The focused conversation is rerendered along with one above it,
    // which the filters now hide. The focus stays at its row index,
    // which is past the last row once the row above is gone.
    const view = show_recent_view_with_focused_row(override, [topic1, topic2, topic3], topic3);

    override(ListWidget, "filter_and_sort", () => {
        view.set_list([topic2, topic3]);
        view.set_rows([topic2, topic3]);
        return [conversation_for(topic1)];
    });
    override(ListWidget, "render_item", () => {
        view.replace_row(topic3);
        view.set_rows([topic2, topic3]);
    });
    bulk_rerender([get_topic_key(stream1, topic1), get_topic_key(stream1, topic3)]);
    assert.equal(get_focused_topic(), topic3);
});

test("rerender leaves the keyboard focus in place when the view is redrawn", ({override}) => {
    // A redraw during the rerender, as when the view falls back from the
    // unread sort, replaces every row and can reorder them all.
    const view = show_recent_view_with_focused_row(override, [topic1, topic2, topic3], topic2);

    const topics_after_redraw = [topic2, topic3, topic1];
    override(ListWidget, "filter_and_sort", () => {
        for (const topic of topics_after_redraw) {
            view.replace_row(topic);
        }
        view.set_list(topics_after_redraw);
        view.set_rows(topics_after_redraw);
        return [];
    });
    override(ListWidget, "render_item", noop);
    rt.inplace_rerender(get_topic_key(stream1, topic1));
    assert.equal(get_focused_topic(), topic3);
});

test("basic assertions", ({mock_template}) => {
    rt.clear_for_tests();
    rt.set_filters_for_tests();

    mock_template("recent_view_table.hbs", false, () => "<recent-view-table-stub>");
    mock_template("recent_view_row.hbs", true, (_data, html) => {
        assert.ok(html.startsWith('<tr id="recent_conversation'));
        return "<recent-view-row-stub>";
    });

    stub_out_filter_buttons();
    recent_view_util.set_visible(true);
    rt.set_default_focus();
    rt.process_messages(messages);
    let all_topics = rt_data.get_conversations();

    // update a message
    generate_topic_data([[1, "topic-7", 1, all_visibility_policies.INHERIT]]);
    stub_out_filter_buttons();
    expected_data_to_replace_in_list_widget = [
        {
            last_msg_id: 11,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 10,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 9,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 7,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 5,
            participated: false,
            type: "stream",
        },
        {
            last_msg_id: 4,
            participated: false,
            type: "stream",
        },
        {
            last_msg_id: 3,
            participated: true,
            type: "stream",
        },
        {
            last_msg_id: 1,
            participated: true,
            type: "stream",
        },
    ];

    rt.process_messages([messages[9]]);
    // Check for expected lengths.
    // total 8 topics, 1 muted
    assert.deepEqual(all_topics.keys().toArray(), [
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-7",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-3",
        "1:topic-2",
        "1:topic-1",
    ]);

    // Process direct message
    rt_data.process_message({
        type: "private",
        to_user_ids: "6,7,8",
    });
    all_topics = rt_data.get_conversations();
    assert.deepEqual(all_topics.keys().toArray(), [
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-7",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-3",
        "1:topic-2",
        "1:topic-1",
        "6,7,8",
    ]);

    // participated
    verify_topic_data(all_topics, stream1, topic1, messages[0].id, true);

    // No message was sent by us.
    verify_topic_data(all_topics, stream1, topic3, messages[3].id, false);

    // Not participated
    verify_topic_data(all_topics, stream1, topic4, messages[4].id, false);

    // topic3 now participated
    rt_data.process_message({
        stream_id: stream1,
        id: (id += 1),
        topic: topic3,
        sender_id: sender1,
        type: "stream",
    });

    all_topics = rt_data.get_conversations();
    assert.deepEqual(all_topics.keys().toArray(), [
        "1:topic-3",
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-7",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-2",
        "1:topic-1",
        "6,7,8",
    ]);
    verify_topic_data(all_topics, stream1, topic3, id, true);

    // Send new message to topic7 (muted)
    // The topic will be hidden when displayed
    rt_data.process_message({
        stream_id: stream1,
        id: (id += 1),
        topic: topic7,
        sender_id: sender1,
        type: "stream",
    });

    all_topics = rt_data.get_conversations();
    assert.deepEqual(all_topics.keys().toArray(), [
        "1:topic-7",
        "1:topic-3",
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-2",
        "1:topic-1",
        "6,7,8",
    ]);
});

test("test_reify_local_echo_message", ({mock_template}) => {
    mock_template("recent_view_table.hbs", false, () => "<recent-view-table-stub>");
    mock_template("recent_view_row.hbs", false, () => "<recent-view-row-stub>");

    rt.clear_for_tests();
    rt.set_filters_for_tests();
    stub_out_filter_buttons();
    recent_view_util.set_visible(true);
    rt.process_messages(messages);

    rt_data.process_message({
        stream_id: stream1,
        id: 1000.01,
        topic: topic7,
        sender_id: sender1,
        type: "stream",
    });

    assert.equal(
        rt_data.reify_message_id_if_available({
            old_id: 1000.01,
            new_id: 1001,
        }),
        true,
    );

    rt_data.process_message({
        stream_id: stream1,
        id: 1001.01,
        topic: topic7,
        sender_id: sender1,
        type: "stream",
    });

    // A new message arrived in the same topic before we could reify the message_id
    rt_data.process_message({
        stream_id: stream1,
        id: 1003,
        topic: topic7,
        sender_id: sender1,
        type: "stream",
    });

    assert.equal(
        rt_data.reify_message_id_if_available({
            old_id: 1000.01,
            new_id: 1001,
        }),
        false,
    );
});

test("test_delete_messages", ({override}) => {
    recent_view_util.set_visible(false);
    rt.clear_for_tests();
    stub_out_filter_buttons();
    rt.process_messages(messages);

    // messages[0] was removed.
    let reduced_msgs = messages.slice(1);
    override(recent_view_messages_data, "all_messages_after_mute_filtering", () => reduced_msgs);

    let all_topics = rt_data.get_conversations();
    assert.deepEqual(all_topics.keys().toArray(), [
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-7",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-3",
        "1:topic-2",
        "1:topic-1",
    ]);
    rt.update_topics_of_deleted_message_ids([messages[0].id]);

    all_topics = rt_data.get_conversations();
    assert.deepEqual(all_topics.keys().toArray(), [
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-7",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-3",
        "1:topic-2",
    ]);

    // messages[0], messages[1] and message[2] were removed.
    reduced_msgs = messages.slice(3);

    rt.update_topics_of_deleted_message_ids([messages[1].id, messages[2].id]);

    all_topics = rt_data.get_conversations();
    assert.deepEqual(all_topics.keys().toArray(), [
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-7",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-3",
    ]);
    // test deleting a message which is not locally
    // stored, doesn't raise any errors.
    rt.update_topics_of_deleted_message_ids([-1]);
});

test("test_topic_edit", ({override}) => {
    override(recent_view_messages_data, "all_messages_after_mute_filtering", () => messages);
    recent_view_util.set_visible(false);

    // NOTE: This test should always run in the end as it modified the messages data.
    rt.clear_for_tests();
    stub_out_filter_buttons();
    rt.process_messages(messages);

    let all_topics = rt_data.get_conversations();
    assert.deepEqual(all_topics.keys().toArray(), [
        "6:topic-12",
        "6:topic-11",
        "6:topic-8",
        "4:topic-10",
        "1:topic-7",
        "1:topic-6",
        "1:topic-5",
        "1:topic-4",
        "1:topic-3",
        "1:topic-2",
        "1:topic-1",
    ]);

    // ---------------- test change topic ----------------
    verify_topic_data(all_topics, stream1, topic6, messages[8].id, true);
    assert.equal(all_topics.get(get_topic_key(stream1, topic8)), undefined);

    // change topic of topic6 to topic8
    messages[7].topic = topic8;
    messages[8].topic = topic8;
    rt.process_topic_edit(stream1, topic6, topic8);
    all_topics = rt_data.get_conversations();

    verify_topic_data(all_topics, stream1, topic8, messages[8].id, true);
    assert.equal(all_topics.get(get_topic_key(stream1, topic6)), undefined);

    // ---------------- test stream change ----------------
    verify_topic_data(all_topics, stream1, topic1, messages[0].id, true);
    assert.equal(all_topics.get(get_topic_key(stream2, topic1)), undefined);

    messages[0].stream_id = stream2;
    rt.process_topic_edit(stream1, topic1, topic1, stream2);
    all_topics = rt_data.get_conversations();

    assert.equal(all_topics.get(get_topic_key(stream1, topic1)), undefined);
    verify_topic_data(all_topics, stream2, topic1, messages[0].id, true);

    // ---------------- test stream & topic change ----------------
    verify_topic_data(all_topics, stream2, topic1, messages[0].id, true);
    assert.equal(all_topics.get(get_topic_key(stream3, topic9)), undefined);

    messages[0].stream_id = stream3;
    messages[0].topic = topic9;
    rt.process_topic_edit(stream2, topic1, topic9, stream3);
    all_topics = rt_data.get_conversations();

    assert.equal(all_topics.get(get_topic_key(stream2, topic1)), undefined);
    verify_topic_data(all_topics, stream3, topic9, messages[0].id, true);

    // Message was moved to a deleted stream, hence hidden regardless of filter.
    messages[0].stream_id = stream5;
    messages[0].topic = topic8;
    rt.process_topic_edit(stream3, topic9, topic8, stream5);
    all_topics = rt_data.get_conversations();
    assert.equal(rt.filters_should_hide_row(all_topics.get("5:topic-8")), true);
});

test("test_search", () => {
    rt.clear_for_tests();
    assert.equal(rt.topic_in_search_results("t", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("T", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("to", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("top", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("ToP", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("Topi", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("tOpi", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("toPic", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("Topic", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("topic", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("recent", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("RECENT", "general", "Recent topic"), true);

    // Match (by prefix) in any order of words.
    assert.equal(rt.topic_in_search_results("topic recent", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("o", "general", "Recent topic"), false);
    assert.equal(rt.topic_in_search_results("to recen", "general", "Recent topic"), true);
    assert.equal(rt.topic_in_search_results("ner opic", "general", "Recent topic"), false);
    assert.equal(rt.topic_in_search_results("pr pro", "general", "pro PRs"), true);
    assert.equal(rt.topic_in_search_results("pr pro pr pro", "general", "pro PRs"), false);
    assert.equal(rt.topic_in_search_results("co cows", "general", "one cow 2 cows"), true);
    assert.equal(rt.topic_in_search_results("cows cows", "general", "one cow 2 cows"), false);

    assert.equal(rt.topic_in_search_results("?", "general", "Recent topic"), false);

    // Test special character match
    assert.equal(rt.topic_in_search_results(".*+?^${}()[]\\", "general", "Recent topic"), false);
    assert.equal(rt.topic_in_search_results("?", "general", "?at-start"), true);

    assert.equal(rt.topic_in_search_results("?", "general", "?"), true);
    assert.equal(rt.topic_in_search_results("?", "general", "\\?"), false);

    assert.equal(rt.topic_in_search_results("\\", "general", "\\"), true);
    assert.equal(rt.topic_in_search_results("\\", "general", "\\\\"), true);

    // Test for empty string topic name.
    assert.equal(rt.topic_in_search_results("general chat", "Scotland", ""), true);
});

test("test_folder_filter", () => {
    const folder_id_a = 100;

    // Set up folder_id on stream subs for testing.
    // stream1 is in folder A, stream4 has no folder.
    // Message 1 (last_msg_id=1) is on stream1, message 11 (last_msg_id=11) is on stream4.
    const sub1 = sub_store.get(stream1);
    const sub4 = sub_store.get(stream4);
    sub1.folder_id = folder_id_a;
    sub4.folder_id = null;

    rt.clear_for_tests();
    rt.set_filters_for_tests();

    // Default "Any folder" filter — all rows visible.
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 1, participated: true, type: "stream"}),
        false,
    );
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 11, participated: true, type: "stream"}),
        false,
    );

    // Filter to folder A — stream1 visible, stream4 hidden.
    rt.set_folder_filter_for_tests(folder_id_a);
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 1, participated: true, type: "stream"}),
        false,
    );
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 11, participated: true, type: "stream"}),
        true,
    );

    // Folder filter hides DMs regardless.
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 15, participated: true, type: "private"}),
        true,
    );

    // "Uncategorized" filter — stream4 (no folder) visible, stream1 (folder A) hidden.
    rt.set_folder_filter_for_tests(-101);
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 1, participated: true, type: "stream"}),
        true,
    );
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 11, participated: true, type: "stream"}),
        false,
    );

    // "Uncategorized" filter also hides DMs.
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 15, participated: true, type: "private"}),
        true,
    );

    // Reset to "Any folder" — all visible again.
    rt.set_folder_filter_for_tests(-102);
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 1, participated: true, type: "stream"}),
        false,
    );
    assert.equal(
        rt.filters_should_hide_row({last_msg_id: 11, participated: true, type: "stream"}),
        false,
    );

    // Clean up folder_id modifications.
    delete sub1.folder_id;
    delete sub4.folder_id;
});