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,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
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",
|
|
|
|
};
|
|
|
|
const shiv = {
|
|
|
|
email: "shiv@zulip.com",
|
|
|
|
user_id: 106,
|
|
|
|
full_name: "Shiv",
|
|
|
|
};
|
|
|
|
const desdemona = {
|
|
|
|
email: "desdemona@zulip.com",
|
|
|
|
user_id: 107,
|
|
|
|
full_name: "Desdemona",
|
|
|
|
};
|
|
|
|
const lago = {
|
|
|
|
email: "lago@zulip.com",
|
|
|
|
user_id: 108,
|
|
|
|
full_name: "Lago",
|
|
|
|
};
|
|
|
|
const aaron = {
|
|
|
|
email: "aaron@zulip.com",
|
|
|
|
user_id: 109,
|
|
|
|
full_name: "Aaron",
|
|
|
|
};
|
|
|
|
const jai = {
|
|
|
|
email: "jai@zulip.com",
|
|
|
|
user_id: 110,
|
|
|
|
full_name: "Jai",
|
|
|
|
};
|
|
|
|
const shivam = {
|
|
|
|
email: "shivam@zulip.com",
|
|
|
|
user_id: 111,
|
|
|
|
full_name: "Shivam",
|
|
|
|
};
|
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);
|
|
|
|
people.add_active_user(shiv);
|
|
|
|
people.add_active_user(desdemona);
|
|
|
|
people.add_active_user(lago);
|
|
|
|
people.add_active_user(aaron);
|
|
|
|
people.add_active_user(jai);
|
|
|
|
people.add_active_user(shivam);
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-27 16:21:49 +02:00
|
|
|
function get_list_info(zoomed) {
|
|
|
|
return pm_list_data.get_list_info(zoomed);
|
|
|
|
}
|
|
|
|
|
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 = [
|
|
|
|
{
|
|
|
|
is_active: false,
|
|
|
|
is_group: false,
|
|
|
|
is_zero: false,
|
|
|
|
recipients: "Me Myself",
|
|
|
|
unread: 1,
|
2022-10-25 14:38:45 +02:00
|
|
|
url: "#narrow/pm-with/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,
|
|
|
|
url: "#narrow/pm-with/101,102-group",
|
|
|
|
user_circle_class: undefined,
|
|
|
|
is_group: true,
|
|
|
|
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,
|
2022-10-25 14:38:45 +02:00
|
|
|
url: "#narrow/pm-with/314-Outgoing-webhook",
|
2022-03-15 18:24:43 +01:00
|
|
|
status_emoji_info: undefined,
|
|
|
|
user_circle_class: "user_circle_green",
|
|
|
|
is_group: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
recipients: "Alice, Bob",
|
|
|
|
user_ids_string: "101,102",
|
|
|
|
unread: 1,
|
|
|
|
is_zero: false,
|
|
|
|
is_active: false,
|
|
|
|
url: "#narrow/pm-with/101,102-group",
|
|
|
|
user_circle_class: undefined,
|
|
|
|
status_emoji_info: undefined,
|
|
|
|
is_group: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
function set_filter_result(emails) {
|
2022-03-16 13:53:34 +01:00
|
|
|
const active_filter = {
|
2022-11-17 23:33:43 +01:00
|
|
|
operands(operand) {
|
2022-03-15 18:24:43 +01:00
|
|
|
assert.equal(operand, "pm-with");
|
|
|
|
return emails;
|
|
|
|
},
|
|
|
|
};
|
2022-03-16 13:53:34 +01:00
|
|
|
narrow_state.set_current_filter(active_filter);
|
2022-03-15 18:24:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set_filter_result([]);
|
|
|
|
assert.equal(pm_list_data.get_active_user_ids_string(), undefined);
|
|
|
|
|
|
|
|
set_filter_result(["bob@zulip.com,alice@zulip.com"]);
|
|
|
|
assert.equal(pm_list_data.get_active_user_ids_string(), "101,102");
|
|
|
|
});
|
|
|
|
|
2022-03-27 16:21:49 +02:00
|
|
|
test("get_list_info", ({override}) => {
|
|
|
|
let list_info;
|
|
|
|
assert.equal(narrow_state.filter(), undefined);
|
|
|
|
|
|
|
|
// Initialize an empty list to start.
|
|
|
|
const empty_list_info = get_list_info();
|
|
|
|
|
|
|
|
assert.deepEqual(empty_list_info, {
|
|
|
|
conversations_to_be_shown: [],
|
|
|
|
more_conversations_unread_count: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: We should just initialize a Filter object with `new
|
|
|
|
// Filter` rather than creating a mock.
|
|
|
|
function set_filter_result(emails) {
|
|
|
|
const active_filter = {
|
2022-11-17 23:33:43 +01:00
|
|
|
operands(operand) {
|
2022-03-27 16:21:49 +02:00
|
|
|
assert.equal(operand, "pm-with");
|
|
|
|
return emails;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
narrow_state.set_current_filter(active_filter);
|
|
|
|
}
|
|
|
|
set_filter_result([]);
|
|
|
|
assert.equal(pm_list_data.get_active_user_ids_string(), undefined);
|
|
|
|
|
|
|
|
// Mock to arrange that each user has exactly 1 unread.
|
2022-10-22 07:15:44 +02:00
|
|
|
const num_unread_for_user_ids_string = 1;
|
|
|
|
override(unread, "num_unread_for_user_ids_string", () => num_unread_for_user_ids_string);
|
2022-03-27 16:21:49 +02:00
|
|
|
|
|
|
|
// Initially, we append 2 conversations and check for the
|
|
|
|
// `conversations_to_be_shown` returned in list_info.
|
|
|
|
pm_conversations.recent.insert([alice.user_id, bob.user_id], 1);
|
|
|
|
pm_conversations.recent.insert([me.user_id], 2);
|
|
|
|
|
|
|
|
list_info = get_list_info(false);
|
|
|
|
const expected_list_info = [
|
|
|
|
{
|
|
|
|
is_active: false,
|
|
|
|
is_group: false,
|
|
|
|
is_zero: false,
|
|
|
|
recipients: "Me Myself",
|
|
|
|
status_emoji_info: {
|
|
|
|
emoji_code: 20,
|
|
|
|
},
|
|
|
|
unread: 1,
|
2022-10-25 14:38:45 +02:00
|
|
|
url: "#narrow/pm-with/103-Me-Myself",
|
2022-03-27 16:21:49 +02:00
|
|
|
user_circle_class: "user_circle_empty",
|
|
|
|
user_ids_string: "103",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
recipients: "Alice, Bob",
|
|
|
|
user_ids_string: "101,102",
|
|
|
|
unread: 1,
|
|
|
|
is_active: false,
|
|
|
|
url: "#narrow/pm-with/101,102-group",
|
|
|
|
user_circle_class: undefined,
|
|
|
|
status_emoji_info: undefined,
|
|
|
|
is_group: true,
|
|
|
|
is_zero: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(list_info, {
|
|
|
|
conversations_to_be_shown: expected_list_info,
|
|
|
|
more_conversations_unread_count: 0,
|
|
|
|
});
|
|
|
|
|
2023-01-02 20:50:23 +01:00
|
|
|
// Now, add additional conversations until we exceed
|
2022-03-27 16:21:49 +02:00
|
|
|
// `max_conversations_to_show_with_unreads`.
|
|
|
|
|
|
|
|
pm_conversations.recent.insert([zoe.user_id], 3);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id], 4);
|
|
|
|
pm_conversations.recent.insert([zoe.user_id, cardelio.user_id], 5);
|
|
|
|
pm_conversations.recent.insert([shiv.user_id], 6);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id, shiv.user_id], 7);
|
|
|
|
pm_conversations.recent.insert([desdemona.user_id], 8);
|
|
|
|
|
|
|
|
// We've now added a total of 8 conversations, which is the value
|
|
|
|
// of `max_conversations_to_show_with_unreads` so even now, the
|
|
|
|
// number of unreads in `more conversations` li-item should be 0.
|
|
|
|
list_info = get_list_info(false);
|
|
|
|
|
|
|
|
assert.deepEqual(list_info.conversations_to_be_shown.length, 8);
|
|
|
|
assert.deepEqual(list_info.more_conversations_unread_count, 0);
|
|
|
|
|
|
|
|
// Verify just the ordering of the conversations with unreads.
|
|
|
|
assert.deepEqual(
|
|
|
|
list_info.conversations_to_be_shown.map((conversation) => conversation.recipients),
|
|
|
|
[
|
|
|
|
"Desdemona",
|
|
|
|
"Cardelio, Shiv",
|
|
|
|
"Shiv",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Cardelio",
|
|
|
|
"Zoe",
|
|
|
|
"Me Myself",
|
|
|
|
"Alice, Bob",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-04-08 05:56:35 +02:00
|
|
|
// After adding two more conversations, there will be 10
|
2022-03-27 16:21:49 +02:00
|
|
|
// conversations, which exceeds
|
|
|
|
// `max_conversations_to_show_with_unreads`. Verify that the
|
2022-04-08 05:56:35 +02:00
|
|
|
// oldest conversations are not shown and their unreads are counted in
|
2022-03-27 16:21:49 +02:00
|
|
|
// more_conversations_unread_count.
|
|
|
|
|
|
|
|
pm_conversations.recent.insert([lago.user_id], 9);
|
|
|
|
pm_conversations.recent.insert([zoe.user_id, lago.user_id], 10);
|
|
|
|
list_info = get_list_info(false);
|
|
|
|
assert.deepEqual(list_info.conversations_to_be_shown.length, 8);
|
|
|
|
assert.deepEqual(list_info.more_conversations_unread_count, 2);
|
|
|
|
assert.deepEqual(
|
|
|
|
list_info.conversations_to_be_shown.map((conversation) => conversation.recipients),
|
|
|
|
[
|
|
|
|
"Lago, Zoe",
|
|
|
|
"Lago",
|
|
|
|
"Desdemona",
|
|
|
|
"Cardelio, Shiv",
|
|
|
|
"Shiv",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Cardelio",
|
|
|
|
"Zoe",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-04-08 05:56:35 +02:00
|
|
|
// If we are narrowed to an older conversation, then that one gets
|
|
|
|
// included in the list despite not being among the 8 most recent.
|
2022-03-27 16:21:49 +02:00
|
|
|
|
|
|
|
set_filter_result(["alice@zulip.com,bob@zulip.com"]);
|
|
|
|
list_info = get_list_info(false);
|
|
|
|
assert.deepEqual(list_info.conversations_to_be_shown.length, 9);
|
|
|
|
assert.deepEqual(list_info.more_conversations_unread_count, 1);
|
|
|
|
assert.deepEqual(
|
|
|
|
list_info.conversations_to_be_shown.map((conversation) => conversation.recipients),
|
|
|
|
[
|
|
|
|
"Lago, Zoe",
|
|
|
|
"Lago",
|
|
|
|
"Desdemona",
|
|
|
|
"Cardelio, Shiv",
|
|
|
|
"Shiv",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Cardelio",
|
|
|
|
"Zoe",
|
|
|
|
"Alice, Bob",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-04-08 05:56:35 +02:00
|
|
|
// Verify that if the list is zoomed, we'll include all 10
|
2022-03-27 16:21:49 +02:00
|
|
|
// conversations in the correct order.
|
|
|
|
|
|
|
|
list_info = get_list_info(true);
|
|
|
|
assert.deepEqual(list_info.conversations_to_be_shown.length, 10);
|
|
|
|
assert.deepEqual(
|
|
|
|
list_info.conversations_to_be_shown.map((conversation) => conversation.recipients),
|
|
|
|
[
|
|
|
|
"Lago, Zoe",
|
|
|
|
"Lago",
|
|
|
|
"Desdemona",
|
|
|
|
"Cardelio, Shiv",
|
|
|
|
"Shiv",
|
|
|
|
"Cardelio, Zoe",
|
|
|
|
"Cardelio",
|
|
|
|
"Zoe",
|
|
|
|
"Me Myself",
|
|
|
|
"Alice, Bob",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
// We now test some no unreads cases.
|
2022-10-22 07:15:44 +02:00
|
|
|
override(unread, "num_unread_for_user_ids_string", () => 0);
|
2022-03-27 16:21:49 +02:00
|
|
|
pm_conversations.clear_for_testing();
|
|
|
|
pm_conversations.recent.insert([alice.user_id], 1);
|
|
|
|
pm_conversations.recent.insert([bob.user_id], 2);
|
|
|
|
pm_conversations.recent.insert([me.user_id], 3);
|
|
|
|
pm_conversations.recent.insert([zoe.user_id], 4);
|
|
|
|
pm_conversations.recent.insert([cardelio.user_id], 5);
|
|
|
|
pm_conversations.recent.insert([shiv.user_id], 6);
|
|
|
|
pm_conversations.recent.insert([alice.user_id, bob.user_id], 7);
|
|
|
|
|
|
|
|
// We have 7 conversations in total, but only most recent 5 are visible.
|
|
|
|
list_info = get_list_info(false);
|
|
|
|
assert.deepEqual(list_info.conversations_to_be_shown.length, 5);
|
|
|
|
|
2022-04-08 05:56:35 +02:00
|
|
|
// If we set the oldest conversation as active, it will
|
|
|
|
// also be included in the list with the most recent 5.
|
2022-03-27 16:21:49 +02:00
|
|
|
|
|
|
|
set_filter_result(["alice@zulip.com"]);
|
|
|
|
assert.equal(pm_list_data.get_active_user_ids_string(), "101");
|
|
|
|
list_info = get_list_info(false);
|
|
|
|
assert.deepEqual(list_info.conversations_to_be_shown.length, 6);
|
|
|
|
assert.deepEqual(list_info.conversations_to_be_shown[5], {
|
|
|
|
recipients: "Alice",
|
|
|
|
user_ids_string: "101",
|
|
|
|
unread: 0,
|
|
|
|
is_zero: true,
|
|
|
|
is_active: true,
|
2022-10-25 14:38:45 +02:00
|
|
|
url: "#narrow/pm-with/101-Alice",
|
2022-03-27 16:21:49 +02:00
|
|
|
status_emoji_info: {emoji_code: 20},
|
|
|
|
user_circle_class: "user_circle_empty",
|
|
|
|
is_group: false,
|
|
|
|
});
|
|
|
|
assert.deepEqual(
|
|
|
|
list_info.conversations_to_be_shown.map((conversation) => conversation.recipients),
|
|
|
|
["Alice, Bob", "Shiv", "Cardelio", "Zoe", "Me Myself", "Alice"],
|
|
|
|
);
|
|
|
|
});
|