2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-08-04 23:40:48 +02:00
|
|
|
const pm_conversations = mock_esm("../src/pm_conversations", {
|
|
|
|
recent: {},
|
|
|
|
});
|
2023-02-22 23:04:10 +01:00
|
|
|
const stream_data = mock_esm("../src/stream_data");
|
2023-08-04 23:40:48 +02:00
|
|
|
const stream_list_sort = mock_esm("../src/stream_list_sort");
|
2023-02-22 23:04:10 +01:00
|
|
|
const stream_topic_history = mock_esm("../src/stream_topic_history");
|
|
|
|
const unread = mock_esm("../src/unread");
|
2023-08-04 23:40:48 +02:00
|
|
|
const user_topics = mock_esm("../src/user_topics");
|
2022-07-10 01:06:33 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const tg = zrequire("topic_generator");
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2023-08-04 23:40:48 +02:00
|
|
|
run_test("streams", ({override}) => {
|
2024-08-04 07:09:35 +02:00
|
|
|
function assert_next_stream(curr_stream_id, expected) {
|
|
|
|
const actual = tg.get_next_stream(curr_stream_id);
|
2017-08-16 19:06:07 +02:00
|
|
|
assert.equal(actual, expected);
|
|
|
|
}
|
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
override(stream_list_sort, "get_stream_ids", () => [1, 2, 3, 4]);
|
2017-08-16 19:06:07 +02:00
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
assert_next_stream(undefined, 1);
|
2017-08-16 19:06:07 +02:00
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
assert_next_stream(1, 2);
|
|
|
|
assert_next_stream(4, 1);
|
2017-08-16 19:06:07 +02:00
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
function assert_prev_stream(curr_stream_id, expected) {
|
|
|
|
const actual = tg.get_prev_stream(curr_stream_id);
|
2017-08-16 19:06:07 +02:00
|
|
|
assert.equal(actual, expected);
|
|
|
|
}
|
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
assert_prev_stream(undefined, 4);
|
|
|
|
assert_prev_stream(4, 3);
|
|
|
|
assert_prev_stream(1, 4);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("topics", ({override}) => {
|
2024-08-04 07:09:35 +02:00
|
|
|
const streams = [1, 2, 3, 4];
|
2020-02-12 08:22:43 +01:00
|
|
|
const topics = new Map([
|
2024-08-04 07:09:35 +02:00
|
|
|
[1, ["read", "read", "1a", "1b", "read", "1c"]],
|
|
|
|
[2, []],
|
|
|
|
[3, ["3a", "read", "read", "3b", "read"]],
|
|
|
|
[4, ["4a"]],
|
2020-02-12 08:22:43 +01:00
|
|
|
]);
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2023-06-29 21:59:08 +02:00
|
|
|
function has_unread_messages(_stream, topic) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return topic !== "read";
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_topics(stream) {
|
2020-02-12 08:22:43 +01:00
|
|
|
return topics.get(stream);
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function next_topic(curr_stream, curr_topic) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return tg.next_topic(streams, get_topics, has_unread_messages, curr_stream, curr_topic);
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
assert.deepEqual(next_topic(1, "1a"), {stream_id: 1, topic: "1b"});
|
|
|
|
assert.deepEqual(next_topic(1, undefined), {stream_id: 1, topic: "1a"});
|
|
|
|
assert.deepEqual(next_topic(2, "bogus"), {stream_id: 3, topic: "3a"});
|
|
|
|
assert.deepEqual(next_topic(3, "3b"), {stream_id: 3, topic: "3a"});
|
|
|
|
assert.deepEqual(next_topic(4, "4a"), {stream_id: 1, topic: "1a"});
|
|
|
|
assert.deepEqual(next_topic(undefined, undefined), {stream_id: 1, topic: "1a"});
|
2017-04-21 20:36:12 +02:00
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
assert.deepEqual(
|
2024-08-04 07:09:35 +02:00
|
|
|
tg.next_topic(streams, get_topics, () => false, 1, "1a"),
|
2021-01-23 02:07:22 +01:00
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
|
2017-04-21 20:36:12 +02:00
|
|
|
// Now test the deeper function that is wired up to
|
2023-04-24 07:37:18 +02:00
|
|
|
// real functions stream_data/stream_list_sort/unread.
|
2017-04-21 20:36:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const muted_stream_id = 400;
|
|
|
|
const devel_stream_id = 401;
|
2024-08-04 07:09:35 +02:00
|
|
|
const announce_stream_id = 402;
|
|
|
|
const test_here_stream_id = 403;
|
|
|
|
override(stream_list_sort, "get_stream_ids", () => [
|
|
|
|
announce_stream_id,
|
|
|
|
muted_stream_id,
|
|
|
|
devel_stream_id,
|
|
|
|
test_here_stream_id,
|
|
|
|
]);
|
2017-05-13 19:26:54 +02:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
override(stream_topic_history, "get_recent_topic_names", (stream_id) => {
|
2017-07-24 15:15:28 +02:00
|
|
|
switch (stream_id) {
|
2020-07-15 00:34:28 +02:00
|
|
|
case muted_stream_id:
|
2023-11-29 08:39:32 +01:00
|
|
|
return ["ms-topic1", "ms-topic2", "unmuted", "followed-muted"];
|
2020-07-15 00:34:28 +02:00
|
|
|
case devel_stream_id:
|
2023-11-29 08:39:32 +01:00
|
|
|
return ["muted", "python", "followed-devel"];
|
2017-05-13 19:26:54 +02:00
|
|
|
}
|
|
|
|
|
2017-07-24 15:15:28 +02:00
|
|
|
return [];
|
2021-02-28 00:54:32 +01:00
|
|
|
});
|
2017-07-24 15:15:28 +02:00
|
|
|
|
2024-08-03 21:15:23 +02:00
|
|
|
override(stream_data, "is_muted", (stream_id) => stream_id === muted_stream_id);
|
2017-05-17 16:58:06 +02:00
|
|
|
|
2023-11-29 08:39:32 +01:00
|
|
|
let topic_has_unreads = new Set([
|
|
|
|
"unmuted",
|
|
|
|
"followed-muted",
|
|
|
|
"muted",
|
|
|
|
"python",
|
|
|
|
"followed-devel",
|
|
|
|
]);
|
|
|
|
function mark_topic_as_read(topic) {
|
|
|
|
topic_has_unreads.delete(topic);
|
|
|
|
}
|
|
|
|
override(unread, "topic_has_any_unread", (_stream_id, topic) => topic_has_unreads.has(topic));
|
2017-05-17 16:33:41 +02:00
|
|
|
|
2023-06-29 21:59:08 +02:00
|
|
|
override(user_topics, "is_topic_muted", (_stream_name, topic) => topic === "muted");
|
2017-04-21 20:36:12 +02:00
|
|
|
|
2023-11-29 08:39:32 +01:00
|
|
|
override(
|
|
|
|
user_topics,
|
|
|
|
"is_topic_unmuted_or_followed",
|
|
|
|
(_stream_name, topic) =>
|
|
|
|
topic === "unmuted" || topic === "followed-muted" || topic === "followed-devel",
|
|
|
|
);
|
|
|
|
|
|
|
|
override(
|
|
|
|
user_topics,
|
|
|
|
"is_topic_followed",
|
|
|
|
(_stream_name, topic) => topic === "followed-muted" || topic === "followed-devel",
|
|
|
|
);
|
2023-11-01 12:59:58 +01:00
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
let next_item = tg.get_next_topic(announce_stream_id, "whatever");
|
2023-11-29 08:39:32 +01:00
|
|
|
assert.deepEqual(next_item, {
|
2024-08-04 07:09:35 +02:00
|
|
|
stream_id: muted_stream_id,
|
2023-11-29 08:39:32 +01:00
|
|
|
topic: "unmuted",
|
|
|
|
});
|
|
|
|
mark_topic_as_read("unmuted");
|
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
next_item = tg.get_next_topic(muted_stream_id, "unmuted");
|
2023-11-29 08:39:32 +01:00
|
|
|
assert.deepEqual(next_item, {
|
2024-08-04 07:09:35 +02:00
|
|
|
stream_id: muted_stream_id,
|
2023-11-29 08:39:32 +01:00
|
|
|
topic: "followed-muted",
|
|
|
|
});
|
|
|
|
mark_topic_as_read("followed-muted");
|
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
next_item = tg.get_next_topic(muted_stream_id, "followed-muted");
|
2017-04-21 20:36:12 +02:00
|
|
|
assert.deepEqual(next_item, {
|
2024-08-04 07:09:35 +02:00
|
|
|
stream_id: devel_stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "python",
|
2017-04-21 20:36:12 +02:00
|
|
|
});
|
2023-11-29 08:39:32 +01:00
|
|
|
mark_topic_as_read("python");
|
2017-08-17 10:27:47 +02:00
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
next_item = tg.get_next_topic(devel_stream_id, "python");
|
2023-11-01 12:59:58 +01:00
|
|
|
assert.deepEqual(next_item, {
|
2024-08-04 07:09:35 +02:00
|
|
|
stream_id: devel_stream_id,
|
2023-11-29 08:39:32 +01:00
|
|
|
topic: "followed-devel",
|
2023-11-01 12:59:58 +01:00
|
|
|
});
|
2023-11-29 08:39:32 +01:00
|
|
|
mark_topic_as_read("followed-devel");
|
2023-11-01 12:59:58 +01:00
|
|
|
|
2023-11-29 08:39:32 +01:00
|
|
|
// Mark topics as unread again
|
|
|
|
topic_has_unreads = new Set(["unmuted", "followed-muted", "muted", "python", "followed-devel"]);
|
2023-11-29 08:17:11 +01:00
|
|
|
// Shift + N takes the user to next unread followed topic,
|
|
|
|
// even if the stream is muted.
|
2024-08-04 07:09:35 +02:00
|
|
|
next_item = tg.get_next_topic(announce_stream_id, "whatever", true);
|
2023-11-29 08:17:11 +01:00
|
|
|
assert.deepEqual(next_item, {
|
2024-08-04 07:09:35 +02:00
|
|
|
stream_id: muted_stream_id,
|
2023-11-29 08:39:32 +01:00
|
|
|
topic: "followed-muted",
|
2023-11-29 08:17:11 +01:00
|
|
|
});
|
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
next_item = tg.get_next_topic(muted_stream_id, "whatever", true);
|
2023-11-01 12:59:58 +01:00
|
|
|
assert.deepEqual(next_item, {
|
2024-08-04 07:09:35 +02:00
|
|
|
stream_id: muted_stream_id,
|
2023-11-29 08:39:32 +01:00
|
|
|
topic: "followed-muted",
|
2023-11-01 12:59:58 +01:00
|
|
|
});
|
|
|
|
|
2024-08-04 07:09:35 +02:00
|
|
|
next_item = tg.get_next_topic(muted_stream_id, undefined);
|
2017-08-17 10:27:47 +02:00
|
|
|
assert.deepEqual(next_item, {
|
2024-08-04 07:09:35 +02:00
|
|
|
stream_id: muted_stream_id,
|
2023-11-29 08:39:32 +01:00
|
|
|
topic: "unmuted",
|
2017-08-17 10:27:47 +02:00
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-02-09 21:38:53 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("get_next_unread_pm_string", ({override}) => {
|
2023-08-04 23:40:48 +02:00
|
|
|
override(pm_conversations.recent, "get_strings", () => ["1", "read", "2,3", "4", "unk"]);
|
2018-02-09 21:38:53 +01:00
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
override(unread, "num_unread_for_user_ids_string", (user_ids_string) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (user_ids_string === "unk") {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-09 21:38:53 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (user_ids_string === "read") {
|
2018-02-09 21:38:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 5; // random non-zero value
|
2021-02-28 21:30:38 +01:00
|
|
|
});
|
2018-02-09 21:38:53 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(tg.get_next_unread_pm_string(), "1");
|
|
|
|
assert.equal(tg.get_next_unread_pm_string("4"), "1");
|
|
|
|
assert.equal(tg.get_next_unread_pm_string("unk"), "1");
|
|
|
|
assert.equal(tg.get_next_unread_pm_string("4"), "1");
|
|
|
|
assert.equal(tg.get_next_unread_pm_string("1"), "2,3");
|
|
|
|
assert.equal(tg.get_next_unread_pm_string("read"), "2,3");
|
|
|
|
assert.equal(tg.get_next_unread_pm_string("2,3"), "4");
|
2021-01-23 02:07:22 +01:00
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
override(unread, "num_unread_for_user_ids_string", () => 0);
|
2021-01-23 02:07:22 +01:00
|
|
|
|
|
|
|
assert.equal(tg.get_next_unread_pm_string("2,3"), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|