Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
topic_generator: Add tests for get_next_topic.
Aman Agrawalbaf7398ddb9a
diff --git a/web/tests/topic_generator.test.cjs b/web/tests/topic_generator.test.cjsindex c05cc73cbce..715bc1d5695 100644--- a/web/tests/topic_generator.test.cjs+++ b/web/tests/topic_generator.test.cjs@@ -5,11 +5,15 @@ const assert = require("node:assert/strict");const {mock_esm, zrequire} = require("./lib/namespace.cjs");const {run_test} = require("./lib/test.cjs");+const narrow_state = mock_esm("../src/narrow_state");const pm_conversations = mock_esm("../src/pm_conversations", {recent: {},});+const stream_data = mock_esm("../src/stream_data");const stream_list_sort = mock_esm("../src/stream_list_sort");+const stream_topic_history = mock_esm("../src/stream_topic_history");const unread = mock_esm("../src/unread");+const user_topics = mock_esm("../src/user_topics");const tg = zrequire("topic_generator");@@ -148,6 +152,160 @@ run_test("topics", () => {}});+const devel = 1;+const design = 2;+const muted_channel = 3;+const sorted_channels_info = [+ {channel_id: devel, is_collapsed: false},+ {channel_id: design, is_collapsed: true},+ {channel_id: muted_channel, is_collapsed: false},+];+const elsewhere = [undefined, undefined];++// Mocks what choosing the next topic reads, and returns setters for+// the unread topics per channel, the topic visibility policies, and+// the current narrow.+function mock_channels_and_topics(override) {+ // Not every test reads all of these.+ const mock = (module, name, f) => override(module, name, f, {unused: false});++ let unread_topics = new Map();+ let topic_policies = new Map();+ let curr_stream_id;+ let curr_topic;++ function topic_policy(stream_id, topic) {+ return topic_policies.get(`${stream_id}:${topic.toLowerCase()}`);+ }+ function is_unread(stream_id, topic) {+ return (unread_topics.get(stream_id) ?? []).includes(topic);+ }++ mock(stream_data, "is_muted", (stream_id) => stream_id === muted_channel);+ mock(+ user_topics,+ "is_topic_muted",+ (stream_id, topic) => topic_policy(stream_id, topic) === "muted",+ );+ mock(user_topics, "is_topic_unmuted_or_followed", (stream_id, topic) =>+ ["unmuted", "followed"].includes(topic_policy(stream_id, topic)),+ );+ mock(stream_topic_history, "get_recent_topic_names", (stream_id) => [+ "read topic",+ ...(unread_topics.get(stream_id) ?? []),+ "another read topic",+ ]);+ mock(unread, "get_msg_ids_for_topic", (stream_id, topic) =>+ is_unread(stream_id, topic) ? [`${stream_id}:${topic}`] : [],+ );+ mock(narrow_state, "stream_id", () => curr_stream_id);+ mock(narrow_state, "topic", () => curr_topic);+ mock(narrow_state, "filter", () => ({+ sorted_term_types: () => (curr_topic === undefined ? ["channel"] : ["channel", "topic"]),+ }));+ mock(+ narrow_state,+ "narrowed_by_topic_reply",+ () => curr_stream_id !== undefined && curr_topic !== undefined,+ );++ return {+ set_unread_topics(topics) {+ unread_topics = new Map(topics);+ },+ set_topic_policies(policies) {+ topic_policies = new Map(policies);+ },+ set_narrow(narrow) {+ [curr_stream_id, curr_topic] = narrow;+ },+ };+}++run_test("get_next_topic", ({override}) => {+ const {set_unread_topics, set_topic_policies, set_narrow} = mock_channels_and_topics(override);++ function assert_next_topic(narrow, expected) {+ tg.reset_topics_kept_unread_by_user();+ set_narrow(narrow);+ const only_followed_topics = false;+ assert.deepEqual(+ tg.get_next_topic(...narrow, only_followed_topics, sorted_channels_info),+ expected,+ );+ }++ assert_next_topic(elsewhere, undefined);+ assert_next_topic([devel, "lunch"], undefined);++ // An unread topic is next from anywhere but that topic itself.+ set_unread_topics([[devel, ["lunch"]]]);+ assert_next_topic(elsewhere, {stream_id: devel, topic: "lunch"});+ assert_next_topic([devel, undefined], {stream_id: devel, topic: "lunch"});+ assert_next_topic([devel, "read topic"], {stream_id: devel, topic: "lunch"});+ assert_next_topic([design, "lunch"], {stream_id: devel, topic: "lunch"});+ assert_next_topic([devel, "lunch"], undefined);++ set_unread_topics([[devel, ["lunch", "dinner"]]]);+ assert_next_topic([devel, "lunch"], {stream_id: devel, topic: "dinner"});++ // The topic with an empty name is a topic like any other.+ set_unread_topics([[devel, [""]]]);+ assert_next_topic([devel, undefined], {stream_id: devel, topic: ""});+ assert_next_topic([devel, ""], undefined);++ // A channel in a collapsed folder counts.+ set_unread_topics([[design, ["logo"]]]);+ assert_next_topic(elsewhere, {stream_id: design, topic: "logo"});+ assert_next_topic([devel, "lunch"], {stream_id: design, topic: "logo"});++ // A muted topic does not.+ set_topic_policies([[`${design}:logo`, "muted"]]);+ assert_next_topic(elsewhere, undefined);+ assert_next_topic([design, undefined], undefined);++ // In a muted channel, only unmuted and followed topics count.+ set_topic_policies([]);+ set_unread_topics([[muted_channel, ["noise"]]]);+ assert_next_topic(elsewhere, undefined);+ assert_next_topic([muted_channel, undefined], undefined);++ set_topic_policies([[`${muted_channel}:noise`, "unmuted"]]);+ assert_next_topic(elsewhere, {stream_id: muted_channel, topic: "noise"});+ assert_next_topic([muted_channel, undefined], {stream_id: muted_channel, topic: "noise"});+ assert_next_topic([muted_channel, "noise"], undefined);++ set_topic_policies([[`${muted_channel}:noise`, "followed"]]);+ assert_next_topic(elsewhere, {stream_id: muted_channel, topic: "noise"});++ // Each channel is judged by its own muting, wherever the user is.+ set_topic_policies([]);+ assert_next_topic([devel, "read topic"], undefined);+ set_unread_topics([[devel, ["lunch"]]]);+ assert_next_topic([muted_channel, "read topic"], {stream_id: devel, topic: "lunch"});++ // A channel that is not in the left sidebar does not count.+ set_unread_topics([[99, ["lunch"]]]);+ assert_next_topic(elsewhere, undefined);+});++run_test("get_next_topic skips a topic left unread once", ({override}) => {+ const {set_unread_topics, set_narrow} = mock_channels_and_topics(override);+ tg.reset_topics_kept_unread_by_user();+ const only_followed_topics = false;+ function get_next_topic(narrow) {+ set_narrow(narrow);+ return tg.get_next_topic(...narrow, only_followed_topics, sorted_channels_info);+ }++ set_unread_topics([[devel, ["lunch", "dinner"]]]);+ assert.deepEqual(get_next_topic([devel, "lunch"]), {stream_id: devel, topic: "dinner"});++ set_unread_topics([[devel, ["lunch"]]]);+ assert.equal(get_next_topic([devel, "read topic"]), undefined);+ assert.deepEqual(get_next_topic([devel, "read topic"]), {stream_id: devel, topic: "lunch"});+});+run_test("get_next_unread_pm_string", ({override}) => {override(pm_conversations.recent, "get_strings", () => ["1", "read", "2,3", "4", "unk"]);