Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
node tests: Clear more data for typeahead_helper.
Steve Howell15d99f87fb7a
diff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.jsindex 25ef35a1da1..d9f758ee6d7 100644--- a/frontend_tests/node_tests/typeahead_helper.js+++ b/frontend_tests/node_tests/typeahead_helper.js@@ -106,7 +106,18 @@ stream_data.create_streams([{name: "Linux", color: "red", stream_id: 2},]);-run_test("sort_streams", (override) => {+function test(label, f) {+ run_test(label, (override) => {+ pm_conversations.clear_for_testing();+ recent_senders.clear_for_testing();+ peer_data.clear_for_testing();+ people.clear_recipient_counts_for_testing();+ page_params.is_admin = false;+ f(override);+ });+}++test("sort_streams", (override) => {let test_streams = [{stream_id: 101,@@ -244,7 +255,7 @@ run_test("sort_streams", (override) => {assert.deepEqual(test_streams[5].name, "Mew"); // Unsubscribed and no match});-run_test("sort_languages", () => {+test("sort_languages", () => {Object.assign(pygments_data, {langs: {python: {priority: 40},@@ -298,17 +309,7 @@ function get_typeahead_result(query, current_stream, current_topic) {return result.map((person) => person.email);}-run_test("sort_recipients", () => {- pm_conversations.clear_for_testing();- recent_senders.clear_for_testing();- peer_data.clear_for_testing();- a_bot.pm_recipient_count = 0;- a_user.pm_recipient_count = 0;- b_user_1.pm_recipient_count = 0;- b_user_2.pm_recipient_count = 0;- b_user_3.pm_recipient_count = 0;- b_bot.pm_recipient_count = 0;-+test("sort_recipients", () => {const dev_sub = stream_data.get_sub("Dev");const linux_sub = stream_data.get_sub("Linux");@@ -406,7 +407,7 @@ run_test("sort_recipients", () => {]);});-run_test("sort_recipients all mention", () => {+test("sort_recipients all mention", () => {const all_obj = ct.broadcast_mentions()[0];assert.equal(all_obj.email, "all");assert.equal(all_obj.is_broadcast, true);@@ -417,39 +418,67 @@ run_test("sort_recipients all mention", () => {const results = th.sort_recipients(test_objs, "a", "Linux", "Linux Topic");- assertSameEmails(results, [all_obj, a_user, a_bot, zman, b_user_3, b_bot, b_user_1, b_user_2]);+ assertSameEmails(results, [all_obj, a_bot, a_user, b_user_1, b_user_2, b_user_3, b_bot, zman]);});-run_test("sort_recipients pm counts", () => {+test("sort_recipients pm counts", () => {// Test sort_recipients with pm counts- a_bot.pm_recipient_count = 50;- a_user.pm_recipient_count = 2;- b_user_1.pm_recipient_count = 32;- b_user_2.pm_recipient_count = 42;- b_user_3.pm_recipient_count = 0;- b_bot.pm_recipient_count = 1;+ people.set_recipient_count_for_testing(a_bot.user_id, 50);+ people.set_recipient_count_for_testing(a_user.user_id, 2);+ people.set_recipient_count_for_testing(b_user_1.user_id, 32);+ people.set_recipient_count_for_testing(b_user_2.user_id, 42);+ people.set_recipient_count_for_testing(b_user_3.user_id, 0);+ people.set_recipient_count_for_testing(b_bot.user_id, 1);++ assert.deepEqual(get_typeahead_result("b"), [+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ ]);++ // Now prioritize stream membership over pm counts.+ const linux_sub = stream_data.get_sub("Linux");+ peer_data.add_subscriber(linux_sub.stream_id, b_user_3.user_id);assert.deepEqual(get_typeahead_result("b", "Linux", "Linux Topic"), [- "[email protected]",- "[email protected]",- "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",]);++ function compare() {+ throw new Error("We do not expect to need a tiebreaker here.");+ }++ // get some line coverage+ assert.equal(+ th.compare_people_for_relevance(b_user_1, b_user_3, compare, linux_sub.stream_id),+ 1,+ );+ assert.equal(+ th.compare_people_for_relevance(b_user_3, b_user_1, compare, linux_sub.stream_id),+ -1,+ );});-run_test("sort_recipients dup bots", () => {+test("sort_recipients dup bots", () => {const dup_objects = matches.concat([a_bot]);const recipients = th.sort_recipients(dup_objects, "b", "", "");const recipients_email = recipients.map((person) => person.email);const expected = [- "[email protected]",- "[email protected]",- "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",@@ -458,7 +487,7 @@ run_test("sort_recipients dup bots", () => {assert.deepEqual(recipients_email, expected);});-run_test("sort_recipients dup alls", () => {+test("sort_recipients dup alls", () => {const all_obj = ct.broadcast_mentions()[0];// full_name starts with same character but emails are 'all'@@ -470,7 +499,7 @@ run_test("sort_recipients dup alls", () => {assertSameEmails(recipients, expected);});-run_test("sort_recipients subscribers", () => {+test("sort_recipients subscribers", () => {// b_user_2 is a subscriber and b_user_1 is not.const small_matches = [b_user_2, b_user_1];const recipients = th.sort_recipients(small_matches, "b", "Dev", "Dev Topic");@@ -479,7 +508,7 @@ run_test("sort_recipients subscribers", () => {assert.deepEqual(recipients_email, expected);});-run_test("sort_recipients pm partners", () => {+test("sort_recipients pm partners", () => {// b_user_3 is a pm partner and b_user_2 is not and// both are not subscribered to the stream Linux.const small_matches = [b_user_3, b_user_2];@@ -489,7 +518,7 @@ run_test("sort_recipients pm partners", () => {assert.deepEqual(recipients_email, expected);});-run_test("sort broadcast mentions", () => {+test("sort broadcast mentions", () => {// test the normal case, which is that the// broadcast mentions are already sorted (we// actually had a bug where the sort would@@ -516,7 +545,7 @@ run_test("sort broadcast mentions", () => {);});-run_test("test compare directly", () => {+test("test compare directly", () => {// This is important for ensuring test coverage.// We don't technically need it now, but our test// coverage is subject to the whims of how JS sorts.@@ -527,7 +556,7 @@ run_test("test compare directly", () => {assert.equal(th.compare_people_for_relevance(zman, all_obj), 1);});-run_test("highlight_with_escaping", () => {+test("highlight_with_escaping", () => {function highlight(query, item) {const regex = th.build_highlight_regex(query);return th.highlight_with_escaping_and_regex(regex, item);@@ -552,9 +581,8 @@ run_test("highlight_with_escaping", () => {assert.equal(result, expected);});-run_test("render_person when emails hidden", () => {+test("render_person when emails hidden", () => {// Test render_person with regular person, under hidden email visibility case- page_params.is_admin = false;let rendered = false;stub_templates((template_name, args) => {assert.equal(template_name, "typeahead_list_item");@@ -568,7 +596,7 @@ run_test("render_person when emails hidden", () => {assert(rendered);});-run_test("render_person", () => {+test("render_person", () => {// Test render_person with regular personpage_params.is_admin = true;let rendered = false;@@ -584,7 +612,7 @@ run_test("render_person", () => {assert(rendered);});-run_test("render_person special_item_text", () => {+test("render_person special_item_text", () => {let rendered = false;// Test render_person with special_item_text person@@ -609,7 +637,8 @@ run_test("render_person special_item_text", () => {assert(rendered);});-run_test("clear_rendered_person", () => {+test("clear_rendered_person", () => {+ page_params.is_admin = true;th.clear_rendered_person(b_bot.user_id);let rendered = false;@@ -636,7 +665,7 @@ run_test("clear_rendered_person", () => {assert(rendered);});-run_test("render_stream", () => {+test("render_stream", () => {// Test render_stream with short descriptionlet rendered = false;const stream = {@@ -657,7 +686,7 @@ run_test("render_stream", () => {assert(rendered);});-run_test("render_stream w/long description", () => {+test("render_stream w/long description", () => {// Test render_stream with long descriptionlet rendered = false;const stream = {@@ -679,7 +708,7 @@ run_test("render_stream w/long description", () => {assert(rendered);});-run_test("clear_rendered_stream", () => {+test("clear_rendered_stream", () => {let rendered = false;const stream = {description: "This is a description.",@@ -708,7 +737,7 @@ run_test("clear_rendered_stream", () => {assert(rendered);});-run_test("render_emoji", () => {+test("render_emoji", () => {// Test render_emoji with normal emoji.let rendered = false;let test_emoji = {@@ -759,7 +788,7 @@ run_test("render_emoji", () => {assert(rendered);});-run_test("sort_slash_commands", () => {+test("sort_slash_commands", () => {const slash_commands = [{name: "my"},{name: "poll"},@@ -778,26 +807,26 @@ run_test("sort_slash_commands", () => {]);});-run_test("sort_recipientbox_typeahead", () => {+test("sort_recipientbox_typeahead", () => {let recipients = th.sort_recipientbox_typeahead("b, a", matches, ""); // search "a"let recipients_email = recipients.map((person) => person.email);assert.deepEqual(recipients_email, ["[email protected]", // matches "a""[email protected]", // matches "a"- "[email protected]",+ "[email protected]",+ "[email protected]",- "[email protected]",- "[email protected]",+ "[email protected]",]);recipients = th.sort_recipientbox_typeahead("b, a, b", matches, ""); // search "b"recipients_email = recipients.map((person) => person.email);assert.deepEqual(recipients_email, [- "[email protected]",- "[email protected]",- "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",diff --git a/static/js/people.js b/static/js/people.jsindex edb5bd95569..f3269c02017 100644--- a/static/js/people.js+++ b/static/js/people.js@@ -857,6 +857,14 @@ export function incr_recipient_count(user_id) {pm_recipient_count_dict.set(user_id, old_count + 1);}+export function clear_recipient_counts_for_testing() {+ pm_recipient_count_dict.clear();+}++export function set_recipient_count_for_testing(user_id, count) {+ pm_recipient_count_dict.set(user_id, count);+}+export function get_message_people() {/*message_people are roughly the people who have