2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-08-18 19:53:22 +02:00
|
|
|
const pm_conversations = zrequire("pm_conversations");
|
|
|
|
pm_conversations.recent = {};
|
2017-05-24 02:16:47 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("muting");
|
|
|
|
zrequire("unread");
|
|
|
|
zrequire("stream_data");
|
|
|
|
zrequire("stream_topic_history");
|
|
|
|
zrequire("stream_sort");
|
|
|
|
const tg = zrequire("topic_generator");
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
function is_even(i) {
|
|
|
|
return i % 2 === 0;
|
|
|
|
}
|
|
|
|
function is_odd(i) {
|
|
|
|
return i % 2 === 1;
|
|
|
|
}
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basics", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let gen = tg.list_generator([10, 20, 30]);
|
2017-04-06 05:40:40 +02:00
|
|
|
assert.equal(gen.next(), 10);
|
|
|
|
assert.equal(gen.next(), 20);
|
|
|
|
assert.equal(gen.next(), 30);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const gen1 = tg.list_generator([100, 200]);
|
|
|
|
const gen2 = tg.list_generator([300, 400]);
|
|
|
|
const outers = [gen1, gen2];
|
2017-04-06 05:40:40 +02:00
|
|
|
gen = tg.chain(outers);
|
|
|
|
assert.equal(gen.next(), 100);
|
|
|
|
assert.equal(gen.next(), 200);
|
|
|
|
assert.equal(gen.next(), 300);
|
|
|
|
assert.equal(gen.next(), 400);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
gen = tg.wrap([5, 15, 25, 35], 25);
|
|
|
|
assert.equal(gen.next(), 25);
|
|
|
|
assert.equal(gen.next(), 35);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
assert.equal(gen.next(), 15);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
gen = tg.wrap_exclude([5, 15, 25, 35], 25);
|
|
|
|
assert.equal(gen.next(), 35);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
assert.equal(gen.next(), 15);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
gen = tg.wrap([5, 15, 25, 35], undefined);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
|
|
|
|
gen = tg.wrap_exclude([5, 15, 25, 35], undefined);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
|
|
|
|
gen = tg.wrap([5, 15, 25, 35], 17);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
|
|
|
|
gen = tg.wrap([], 42);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let ints = tg.list_generator([1, 2, 3, 4, 5]);
|
2017-04-06 05:40:40 +02:00
|
|
|
gen = tg.filter(ints, is_even);
|
|
|
|
assert.equal(gen.next(), 2);
|
|
|
|
assert.equal(gen.next(), 4);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
ints = tg.list_generator([]);
|
|
|
|
gen = tg.filter(ints, is_even);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
2017-04-21 17:26:40 +02:00
|
|
|
ints = tg.list_generator([10, 20, 30]);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
function mult10(x) {
|
|
|
|
return x * 10;
|
|
|
|
}
|
2017-04-21 17:26:40 +02:00
|
|
|
|
|
|
|
gen = tg.map(ints, mult10);
|
|
|
|
assert.equal(gen.next(), 100);
|
|
|
|
assert.equal(gen.next(), 200);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("reverse", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let gen = tg.reverse_list_generator([10, 20, 30]);
|
2017-08-16 22:37:05 +02:00
|
|
|
assert.equal(gen.next(), 30);
|
|
|
|
assert.equal(gen.next(), 20);
|
|
|
|
assert.equal(gen.next(), 10);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
// If second parameter is not in the list, we just traverse the list
|
|
|
|
// in reverse.
|
|
|
|
gen = tg.reverse_wrap_exclude([10, 20, 30]);
|
|
|
|
assert.equal(gen.next(), 30);
|
|
|
|
assert.equal(gen.next(), 20);
|
|
|
|
assert.equal(gen.next(), 10);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
gen = tg.reverse_wrap_exclude([10, 20, 30], "whatever");
|
2017-08-16 22:37:05 +02:00
|
|
|
assert.equal(gen.next(), 30);
|
|
|
|
assert.equal(gen.next(), 20);
|
|
|
|
assert.equal(gen.next(), 10);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
// Witness the mostly typical cycling behavior.
|
|
|
|
gen = tg.reverse_wrap_exclude([5, 10, 20, 30], 20);
|
|
|
|
assert.equal(gen.next(), 10);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
assert.equal(gen.next(), 30);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
gen = tg.reverse_wrap_exclude([5, 10, 20, 30], 5);
|
|
|
|
assert.equal(gen.next(), 30);
|
|
|
|
assert.equal(gen.next(), 20);
|
|
|
|
assert.equal(gen.next(), 10);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
gen = tg.reverse_wrap_exclude([5, 10, 20, 30], 30);
|
|
|
|
assert.equal(gen.next(), 20);
|
|
|
|
assert.equal(gen.next(), 10);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
// Test small lists.
|
|
|
|
gen = tg.reverse_wrap_exclude([], 5);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
gen = tg.reverse_wrap_exclude([5], 5);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
gen = tg.reverse_wrap_exclude([5], 10);
|
|
|
|
assert.equal(gen.next(), 5);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-08-16 22:37:05 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("fchain", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const mults = function (n) {
|
|
|
|
let ret = 0;
|
2017-04-06 05:40:40 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
next() {
|
2017-04-06 05:40:40 +02:00
|
|
|
ret += n;
|
2018-06-06 18:19:09 +02:00
|
|
|
return ret <= 100 ? ret : undefined;
|
2017-04-06 05:40:40 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let ints = tg.list_generator([29, 43]);
|
|
|
|
let gen = tg.fchain(ints, mults);
|
2017-04-06 05:40:40 +02:00
|
|
|
assert.equal(gen.next(), 29);
|
|
|
|
assert.equal(gen.next(), 58);
|
|
|
|
assert.equal(gen.next(), 87);
|
|
|
|
assert.equal(gen.next(), 43);
|
|
|
|
assert.equal(gen.next(), 86);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
|
|
|
ints = tg.wrap([33, 34, 37], 37);
|
|
|
|
ints = tg.filter(ints, is_odd);
|
|
|
|
gen = tg.fchain(ints, mults);
|
|
|
|
assert.equal(gen.next(), 37);
|
|
|
|
assert.equal(gen.next(), 74);
|
|
|
|
assert.equal(gen.next(), 33);
|
|
|
|
assert.equal(gen.next(), 66);
|
|
|
|
assert.equal(gen.next(), 99);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
assert.equal(gen.next(), undefined);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const undef = function () {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-05-24 02:16:47 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Invalid generator returned.");
|
2017-05-24 02:16:47 +02:00
|
|
|
ints = tg.list_generator([29, 43]);
|
|
|
|
gen = tg.fchain(ints, undef);
|
|
|
|
gen.next();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("streams", () => {
|
2017-08-16 19:06:07 +02:00
|
|
|
function assert_next_stream(curr_stream, expected) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const actual = tg.get_next_stream(curr_stream);
|
2017-08-16 19:06:07 +02:00
|
|
|
assert.equal(actual, expected);
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
stream_sort.get_streams = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
return ["announce", "muted", "devel", "test here"];
|
2017-08-16 19:06:07 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert_next_stream(undefined, "announce");
|
|
|
|
assert_next_stream("NOT THERE", "announce");
|
2017-08-16 19:06:07 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert_next_stream("announce", "muted");
|
|
|
|
assert_next_stream("test here", "announce");
|
2017-08-16 19:06:07 +02:00
|
|
|
|
|
|
|
function assert_prev_stream(curr_stream, expected) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const actual = tg.get_prev_stream(curr_stream);
|
2017-08-16 19:06:07 +02:00
|
|
|
assert.equal(actual, expected);
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert_prev_stream(undefined, "test here");
|
|
|
|
assert_prev_stream("test here", "devel");
|
|
|
|
assert_prev_stream("announce", "test here");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("topics", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const streams = [1, 2, 3, 4];
|
2020-02-12 08:22:43 +01:00
|
|
|
const topics = new Map([
|
2020-07-15 01:29:15 +02:00
|
|
|
[1, ["read", "read", "1a", "1b", "read", "1c"]],
|
2020-02-12 08:22:43 +01:00
|
|
|
[2, []],
|
2020-07-15 01:29:15 +02:00
|
|
|
[3, ["3a", "read", "read", "3b", "read"]],
|
|
|
|
[4, ["4a"]],
|
2020-02-12 08:22:43 +01:00
|
|
|
]);
|
2017-04-06 05:40:40 +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
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(next_topic(1, "1a"), {stream: 1, topic: "1b"});
|
|
|
|
assert.deepEqual(next_topic(1, undefined), {stream: 1, topic: "1a"});
|
|
|
|
assert.deepEqual(next_topic(2, "bogus"), {stream: 3, topic: "3a"});
|
|
|
|
assert.deepEqual(next_topic(3, "3b"), {stream: 3, topic: "3a"});
|
|
|
|
assert.deepEqual(next_topic(4, "4a"), {stream: 1, topic: "1a"});
|
|
|
|
assert.deepEqual(next_topic(undefined, undefined), {stream: 1, topic: "1a"});
|
2017-04-21 20:36:12 +02:00
|
|
|
|
|
|
|
// Now test the deeper function that is wired up to
|
|
|
|
// real functions stream_data/stream_sort/unread.
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
stream_sort.get_streams = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
return ["announce", "muted", "devel", "test here"];
|
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;
|
2017-04-21 20:36:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id_dct = {
|
2017-07-24 15:15:28 +02:00
|
|
|
muted: muted_stream_id,
|
|
|
|
devel: devel_stream_id,
|
|
|
|
};
|
2017-05-13 19:26:54 +02:00
|
|
|
|
2020-03-22 18:40:05 +01:00
|
|
|
stream_topic_history.get_recent_topic_names = function (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:
|
|
|
|
return ["ms-topic1", "ms-topic2"];
|
|
|
|
case devel_stream_id:
|
|
|
|
return ["muted", "python"];
|
2017-05-13 19:26:54 +02:00
|
|
|
}
|
|
|
|
|
2017-07-24 15:15:28 +02:00
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
stream_data.get_stream_id = function (stream_name) {
|
2017-07-24 15:15:28 +02:00
|
|
|
return stream_id_dct[stream_name];
|
2017-05-13 19:26:54 +02:00
|
|
|
};
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
stream_data.is_stream_muted_by_name = function (stream_name) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return stream_name === "muted";
|
2017-05-17 16:58:06 +02:00
|
|
|
};
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
unread.topic_has_any_unread = function (stream_id) {
|
2020-02-08 04:04:36 +01:00
|
|
|
return [devel_stream_id, muted_stream_id].includes(stream_id);
|
2017-05-17 16:33:41 +02:00
|
|
|
};
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
muting.is_topic_muted = function (stream_name, topic) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return topic === "muted";
|
2017-04-21 20:36:12 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let next_item = tg.get_next_topic("announce", "whatever");
|
2017-04-21 20:36:12 +02:00
|
|
|
assert.deepEqual(next_item, {
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "devel",
|
|
|
|
topic: "python",
|
2017-04-21 20:36:12 +02:00
|
|
|
});
|
2017-08-17 10:27:47 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
next_item = tg.get_next_topic("muted", undefined);
|
2017-08-17 10:27:47 +02:00
|
|
|
assert.deepEqual(next_item, {
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "muted",
|
|
|
|
topic: "ms-topic1",
|
2017-08-17 10:27:47 +02:00
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-02-09 21:38:53 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_next_unread_pm_string", () => {
|
2018-02-09 21:38:53 +01:00
|
|
|
pm_conversations.recent.get_strings = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
return ["1", "read", "2,3", "4", "unk"];
|
2018-02-09 21:38:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
unread.num_unread_for_person = function (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
|
|
|
|
};
|
|
|
|
|
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");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|