2022-03-15 18:24:43 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2022-03-15 18:24:43 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const unread = mock_esm("../src/unread");
|
2022-03-15 18:24:43 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/user_status", {
|
2022-03-15 18:24:43 +01:00
|
|
|
get_status_emoji: () => ({
|
|
|
|
emoji_code: 20,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
const {Filter} = zrequire("filter");
|
2022-03-16 13:53:34 +01:00
|
|
|
const narrow_state = zrequire("narrow_state");
|
2022-03-15 18:24:43 +01:00
|
|
|
const people = zrequire("people");
|
|
|
|
const pm_conversations = zrequire("pm_conversations");
|
|
|
|
const pm_list_data = zrequire("pm_list_data");
|
|
|
|
|
|
|
|
const alice = {
|
|
|
|
email: "alice@zulip.com",
|
|
|
|
user_id: 101,
|
|
|
|
full_name: "Alice",
|
|
|
|
};
|
|
|
|
const bob = {
|
|
|
|
email: "bob@zulip.com",
|
|
|
|
user_id: 102,
|
|
|
|
full_name: "Bob",
|
|
|
|
};
|
|
|
|
const me = {
|
|
|
|
email: "me@zulip.com",
|
|
|
|
user_id: 103,
|
|
|
|
full_name: "Me Myself",
|
|
|
|
};
|
2022-03-27 16:21:49 +02:00
|
|
|
const zoe = {
|
|
|
|
email: "zoe@zulip.com",
|
|
|
|
user_id: 104,
|
|
|
|
full_name: "Zoe",
|
|
|
|
};
|
|
|
|
const cardelio = {
|
|
|
|
email: "cardelio@zulip.com",
|
|
|
|
user_id: 105,
|
|
|
|
full_name: "Cardelio",
|
|
|
|
};
|
2023-02-10 20:03:06 +01:00
|
|
|
const iago = {
|
|
|
|
email: "iago@zulip.com",
|
|
|
|
user_id: 106,
|
|
|
|
full_name: "Iago",
|
|
|
|
};
|
2022-03-15 18:24:43 +01:00
|
|
|
const bot_test = {
|
|
|
|
email: "outgoingwebhook@zulip.com",
|
|
|
|
user_id: 314,
|
|
|
|
full_name: "Outgoing webhook",
|
|
|
|
is_admin: false,
|
|
|
|
is_bot: true,
|
|
|
|
};
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(bob);
|
|
|
|
people.add_active_user(me);
|
2022-03-27 16:21:49 +02:00
|
|
|
people.add_active_user(zoe);
|
|
|
|
people.add_active_user(cardelio);
|
2023-02-10 20:03:06 +01:00
|
|
|
people.add_active_user(iago);
|
2022-03-15 18:24:43 +01:00
|
|
|
people.add_active_user(bot_test);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2022-03-16 13:53:34 +01:00
|
|
|
narrow_state.reset_current_filter();
|
2022-03-15 18:24:43 +01:00
|
|
|
pm_conversations.clear_for_testing();
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2022-03-15 18:24:43 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
function set_pm_with_filter(emails) {
|
2023-04-11 21:04:33 +02:00
|
|
|
const active_filter = new Filter([{operator: "dm", operand: emails}]);
|
2023-02-10 19:29:43 +01:00
|
|
|
narrow_state.set_current_filter(active_filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_list_info(list, length, more_unread, recipients_array) {
|
|
|
|
assert.deepEqual(list.conversations_to_be_shown.length, length);
|
|
|
|
assert.deepEqual(list.more_conversations_unread_count, more_unread);
|
|
|
|
assert.deepEqual(
|
|
|
|
list.conversations_to_be_shown.map((conversation) => conversation.recipients),
|
|
|
|
recipients_array,
|
|
|
|
);
|
2022-03-27 16:21:49 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 00:29:10 +02:00
|
|
|
test("get_conversations", ({override}) => {
|
2022-03-15 19:06:27 +01:00
|
|
|
pm_conversations.recent.insert([alice.user_id, bob.user_id], 1);
|
|
|
|
pm_conversations.recent.insert([me.user_id], 2);
|
2022-10-22 07:15:44 +02:00
|
|
|
let num_unread_for_user_ids_string = 1;
|
|
|
|
override(unread, "num_unread_for_user_ids_string", () => num_unread_for_user_ids_string);
|
2022-03-15 18:24:43 +01:00
|
|
|
|
2022-03-16 13:53:34 +01:00
|
|
|
assert.equal(narrow_state.filter(), undefined);
|
2022-03-15 18:24:43 +01:00
|
|
|
|
|
|
|
const expected_data = [
|
|
|
|
{
|
2023-04-11 14:43:49 +02:00
|
|
|
is_bot: false,
|
2022-03-15 18:24:43 +01:00
|
|
|
is_active: false,
|
|
|
|
is_group: false,
|
|
|
|
is_zero: false,
|
|
|
|
recipients: "Me Myself",
|
|
|
|
unread: 1,
|
2023-04-11 21:04:33 +02:00
|
|
|
url: "#narrow/dm/103-Me-Myself",
|
2022-03-15 18:24:43 +01:00
|
|
|
user_circle_class: "user_circle_empty",
|
|
|
|
user_ids_string: "103",
|
|
|
|
status_emoji_info: {
|
|
|
|
emoji_code: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
recipients: "Alice, Bob",
|
|
|
|
user_ids_string: "101,102",
|
|
|
|
unread: 1,
|
|
|
|
is_zero: false,
|
|
|
|
is_active: false,
|
2023-04-11 21:04:33 +02:00
|
|
|
url: "#narrow/dm/101,102-group",
|
2022-03-15 18:24:43 +01:00
|
|
|
user_circle_class: undefined,
|
|
|
|
is_group: true,
|
2023-04-11 14:43:49 +02:00
|
|
|
is_bot: false,
|
2022-03-15 18:24:43 +01:00
|
|
|
status_emoji_info: undefined,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-04-07 00:29:10 +02:00
|
|
|
let pm_data = pm_list_data.get_conversations();
|
2022-03-15 18:24:43 +01:00
|
|
|
assert.deepEqual(pm_data, expected_data);
|
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
num_unread_for_user_ids_string = 0;
|
2022-03-15 18:24:43 +01:00
|
|
|
|
2022-04-07 00:29:10 +02:00
|
|
|
pm_data = pm_list_data.get_conversations();
|
2022-03-15 18:24:43 +01:00
|
|
|
expected_data[0].unread = 0;
|
|
|
|
expected_data[0].is_zero = true;
|
|
|
|
expected_data[1].unread = 0;
|
|
|
|
expected_data[1].is_zero = true;
|
|
|
|
assert.deepEqual(pm_data, expected_data);
|
|
|
|
|
2022-04-07 00:29:10 +02:00
|
|
|
pm_data = pm_list_data.get_conversations();
|
2022-03-15 18:24:43 +01:00
|
|
|
assert.deepEqual(pm_data, expected_data);
|
|
|
|
});
|
|
|
|
|
2022-04-07 00:29:10 +02:00
|
|
|
test("get_conversations bot", ({override}) => {
|
2022-03-15 19:06:27 +01:00
|
|
|
pm_conversations.recent.insert([alice.user_id, bob.user_id], 1);
|
|
|
|
pm_conversations.recent.insert([bot_test.user_id], 2);
|
2022-03-15 18:24:43 +01:00
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
override(unread, "num_unread_for_user_ids_string", () => 1);
|
2022-03-15 18:24:43 +01:00
|
|
|
|
2022-03-16 13:53:34 +01:00
|
|
|
assert.equal(narrow_state.filter(), undefined);
|
2022-03-15 18:24:43 +01:00
|
|
|
|
|
|
|
const expected_data = [
|
|
|
|
{
|
|
|
|
recipients: "Outgoing webhook",
|
|
|
|
user_ids_string: "314",
|
|
|
|
unread: 1,
|
|
|
|
is_zero: false,
|
|
|
|
is_active: false,
|
2023-04-11 21:04:33 +02:00
|
|
|
url: "#narrow/dm/314-Outgoing-webhook",
|
2022-03-15 18:24:43 +01:00
|
|
|
status_emoji_info: undefined,
|
2023-04-11 14:43:49 +02:00
|
|
|
user_circle_class: "user_circle_empty",
|
2022-03-15 18:24:43 +01:00
|
|
|
is_group: false,
|
2023-04-11 14:43:49 +02:00
|
|
|
is_bot: true,
|
2022-03-15 18:24:43 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
recipients: "Alice, Bob",
|
|
|
|
user_ids_string: "101,102",
|
|
|
|
unread: 1,
|
|
|
|
is_zero: false,
|
|
|
|
is_active: false,
|
2023-04-11 21:04:33 +02:00
|
|
|
url: "#narrow/dm/101,102-group",
|
2022-03-15 18:24:43 +01:00
|
|
|
user_circle_class: undefined,
|
|
|
|
status_emoji_info: undefined,
|
|
|
|
is_group: true,
|
2023-04-11 14:43:49 +02:00
|
|
|
is_bot: false,
|
2022-03-15 18:24:43 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-04-07 00:29:10 +02:00
|
|
|
const pm_data = pm_list_data.get_conversations();
|
2022-03-15 18:24:43 +01:00
|
|
|
assert.deepEqual(pm_data, expected_data);
|
|
|
|
});
|
|
|
|
|
2022-03-16 13:53:34 +01:00
|
|
|
test("get_active_user_ids_string", () => {
|
2022-03-15 18:24:43 +01:00
|
|
|
assert.equal(pm_list_data.get_active_user_ids_string(), undefined);
|
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
const stream_filter = new Filter([{operator: "stream", operand: "test"}]);
|
|
|
|
narrow_state.set_current_filter(stream_filter);
|
2022-03-15 18:24:43 +01:00
|
|
|
assert.equal(pm_list_data.get_active_user_ids_string(), undefined);
|
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
set_pm_with_filter("bob@zulip.com,alice@zulip.com");
|
2022-03-15 18:24:43 +01:00
|
|
|
assert.equal(pm_list_data.get_active_user_ids_string(), "101,102");
|
|
|
|
});
|
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
test("get_list_info_unread_messages", ({override}) => {
|
2022-03-27 16:21:49 +02:00
|
|
|
let list_info;
|
|
|
|
assert.equal(narrow_state.filter(), undefined);
|
|
|
|
|
|
|
|
// Initialize an empty list to start.
|
2023-02-10 19:29:43 +01:00
|
|
|
list_info = pm_list_data.get_list_info(false);
|
|
|
|
check_list_info(list_info, 0, 0, []);
|
2022-03-27 16:21:49 +02:00
|
|
|
|
|
|
|
// Mock to arrange that each user has exactly 1 unread.
|
2023-02-10 19:29:43 +01:00
|
|
|
override(unread, "num_unread_for_user_ids_string", () => 1);
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
// Initially, append 2 conversations and check for the
|
2022-03-27 16:21:49 +02:00
|
|
|
// `conversations_to_be_shown` returned in list_info.
|
2023-02-10 19:29:43 +01:00
|
|
|
pm_conversations.recent.insert([alice.user_id], 1);
|
2022-03-27 16:21:49 +02:00
|
|
|
pm_conversations.recent.insert([me.user_id], 2);
|
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
list_info = pm_list_data.get_list_info(false);
|
|
|
|
check_list_info(list_info, 2, 0, ["Me Myself", "Alice"]);
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
// Visible conversations are limited to value of
|
2022-03-27 16:21:49 +02:00
|
|
|
// `max_conversations_to_show_with_unreads`.
|
2023-02-10 19:29:43 +01:00
|
|
|
// Verify that the oldest conversations are not shown and
|
|
|
|
// their unreads are counted in more_conversations_unread_count.
|
|
|
|
pm_conversations.recent.insert([bob.user_id], 3);
|
|
|
|
pm_conversations.recent.insert([alice.user_id, bob.user_id], 4);
|
|
|
|
pm_conversations.recent.insert([zoe.user_id], 5);
|
|
|
|
pm_conversations.recent.insert([zoe.user_id, bob.user_id], 6);
|
|
|
|
pm_conversations.recent.insert([zoe.user_id, alice.user_id], 7);
|
|
|
|
pm_conversations.recent.insert([zoe.user_id, bob.user_id, alice.user_id], 8);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id, zoe.user_id], 9);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id, bob.user_id], 10);
|
2023-02-10 20:03:06 +01:00
|
|
|
pm_conversations.recent.insert([cardelio.user_id, alice.user_id], 11);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id, zoe.user_id, bob.user_id], 12);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id, zoe.user_id, alice.user_id], 13);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id, bob.user_id, alice.user_id], 14);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id, bob.user_id, alice.user_id, zoe.user_id], 15);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id], 16);
|
|
|
|
pm_conversations.recent.insert([iago.user_id], 17);
|
2023-02-10 19:29:43 +01:00
|
|
|
|
|
|
|
list_info = pm_list_data.get_list_info(false);
|
2023-02-10 20:03:06 +01:00
|
|
|
check_list_info(list_info, 15, 2, [
|
|
|
|
"Iago",
|
|
|
|
"Cardelio",
|
|
|
|
"Alice, Bob, Cardelio, Zoe",
|
|
|
|
"Alice, Bob, Cardelio",
|
|
|
|
"Alice, Cardelio, Zoe",
|
|
|
|
"Bob, Cardelio, Zoe",
|
|
|
|
"Alice, Cardelio",
|
2023-02-10 19:29:43 +01:00
|
|
|
"Bob, Cardelio",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Alice, Bob, Zoe",
|
|
|
|
"Alice, Zoe",
|
|
|
|
"Bob, Zoe",
|
|
|
|
"Zoe",
|
|
|
|
"Alice, Bob",
|
|
|
|
"Bob",
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Narrowing to private messages with Alice adds older
|
|
|
|
// one-on-one conversation with her to the list and one
|
|
|
|
// unread is removed from more_conversations_unread_count.
|
|
|
|
set_pm_with_filter("alice@zulip.com");
|
|
|
|
list_info = pm_list_data.get_list_info(false);
|
2023-02-10 20:03:06 +01:00
|
|
|
check_list_info(list_info, 16, 1, [
|
|
|
|
"Iago",
|
|
|
|
"Cardelio",
|
|
|
|
"Alice, Bob, Cardelio, Zoe",
|
|
|
|
"Alice, Bob, Cardelio",
|
|
|
|
"Alice, Cardelio, Zoe",
|
|
|
|
"Bob, Cardelio, Zoe",
|
|
|
|
"Alice, Cardelio",
|
2023-02-10 19:29:43 +01:00
|
|
|
"Bob, Cardelio",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Alice, Bob, Zoe",
|
|
|
|
"Alice, Zoe",
|
|
|
|
"Bob, Zoe",
|
|
|
|
"Zoe",
|
|
|
|
"Alice, Bob",
|
|
|
|
"Bob",
|
|
|
|
"Alice",
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Zooming will show all conversations and there will
|
|
|
|
// be no unreads in more_conversations_unread_count.
|
|
|
|
list_info = pm_list_data.get_list_info(true);
|
2023-02-10 20:03:06 +01:00
|
|
|
check_list_info(list_info, 17, 0, [
|
|
|
|
"Iago",
|
|
|
|
"Cardelio",
|
|
|
|
"Alice, Bob, Cardelio, Zoe",
|
|
|
|
"Alice, Bob, Cardelio",
|
|
|
|
"Alice, Cardelio, Zoe",
|
|
|
|
"Bob, Cardelio, Zoe",
|
|
|
|
"Alice, Cardelio",
|
2023-02-10 19:29:43 +01:00
|
|
|
"Bob, Cardelio",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Alice, Bob, Zoe",
|
|
|
|
"Alice, Zoe",
|
|
|
|
"Bob, Zoe",
|
|
|
|
"Zoe",
|
|
|
|
"Alice, Bob",
|
|
|
|
"Bob",
|
|
|
|
"Me Myself",
|
|
|
|
"Alice",
|
|
|
|
]);
|
|
|
|
});
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
test("get_list_info_no_unread_messages", ({override}) => {
|
|
|
|
let list_info;
|
2022-10-22 07:15:44 +02:00
|
|
|
override(unread, "num_unread_for_user_ids_string", () => 0);
|
2023-02-10 19:29:43 +01:00
|
|
|
|
2022-03-27 16:21:49 +02:00
|
|
|
pm_conversations.recent.insert([alice.user_id], 1);
|
2023-02-10 19:29:43 +01:00
|
|
|
pm_conversations.recent.insert([me.user_id], 2);
|
|
|
|
pm_conversations.recent.insert([bob.user_id], 3);
|
2022-03-27 16:21:49 +02:00
|
|
|
pm_conversations.recent.insert([zoe.user_id], 4);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id], 5);
|
2023-02-10 19:29:43 +01:00
|
|
|
pm_conversations.recent.insert([zoe.user_id, cardelio.user_id], 6);
|
2022-03-27 16:21:49 +02:00
|
|
|
pm_conversations.recent.insert([alice.user_id, bob.user_id], 7);
|
2023-02-10 20:03:06 +01:00
|
|
|
pm_conversations.recent.insert([zoe.user_id, bob.user_id], 8);
|
|
|
|
pm_conversations.recent.insert([alice.user_id, cardelio.user_id], 9);
|
|
|
|
pm_conversations.recent.insert([bob.user_id, cardelio.user_id], 10);
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2023-02-10 19:29:43 +01:00
|
|
|
// Visible conversations are limited to value of
|
|
|
|
// `max_conversations_to_show`.
|
|
|
|
list_info = pm_list_data.get_list_info(false);
|
2023-02-10 20:03:06 +01:00
|
|
|
check_list_info(list_info, 8, 0, [
|
|
|
|
"Bob, Cardelio",
|
|
|
|
"Alice, Cardelio",
|
|
|
|
"Bob, Zoe",
|
|
|
|
"Alice, Bob",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Cardelio",
|
|
|
|
"Zoe",
|
|
|
|
"Bob",
|
|
|
|
]);
|
2023-02-10 19:29:43 +01:00
|
|
|
|
|
|
|
// Narrowing to private messages with Alice adds older
|
|
|
|
// one-on-one conversation with her to the list.
|
|
|
|
set_pm_with_filter("alice@zulip.com");
|
|
|
|
list_info = pm_list_data.get_list_info(false);
|
2023-02-10 20:03:06 +01:00
|
|
|
check_list_info(list_info, 9, 0, [
|
|
|
|
"Bob, Cardelio",
|
|
|
|
"Alice, Cardelio",
|
|
|
|
"Bob, Zoe",
|
2023-02-10 19:29:43 +01:00
|
|
|
"Alice, Bob",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Cardelio",
|
|
|
|
"Zoe",
|
|
|
|
"Bob",
|
|
|
|
"Alice",
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Zooming will show all conversations.
|
|
|
|
list_info = pm_list_data.get_list_info(true);
|
2023-02-10 20:03:06 +01:00
|
|
|
check_list_info(list_info, 10, 0, [
|
|
|
|
"Bob, Cardelio",
|
|
|
|
"Alice, Cardelio",
|
|
|
|
"Bob, Zoe",
|
2023-02-10 19:29:43 +01:00
|
|
|
"Alice, Bob",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Cardelio",
|
|
|
|
"Zoe",
|
|
|
|
"Bob",
|
|
|
|
"Me Myself",
|
|
|
|
"Alice",
|
|
|
|
]);
|
2022-03-27 16:21:49 +02:00
|
|
|
});
|