2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-06 19:05:17 +01:00
|
|
|
const {mock_module, 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-02-01 04:46:54 +01:00
|
|
|
let next_id = 0;
|
2020-05-01 08:29:08 +02:00
|
|
|
const messages = [];
|
|
|
|
|
2021-03-06 19:05:17 +01:00
|
|
|
mock_module("message_store", {
|
2020-08-07 09:16:25 +02:00
|
|
|
get: (msg_id) => messages[msg_id - 1],
|
2020-05-01 08:29:08 +02:00
|
|
|
});
|
2021-02-28 21:31:33 +01:00
|
|
|
const message_list = {
|
2020-08-07 09:16:25 +02:00
|
|
|
all: {
|
|
|
|
all_messages() {
|
|
|
|
return messages;
|
|
|
|
},
|
|
|
|
},
|
2021-02-28 21:31:33 +01:00
|
|
|
};
|
|
|
|
|
2021-03-06 19:05:17 +01:00
|
|
|
mock_module("message_list", message_list);
|
2020-12-01 23:21:38 +01:00
|
|
|
const rs = zrequire("recent_senders");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2020-08-07 09:16:25 +02:00
|
|
|
zrequire("message_util.js");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2021-02-28 18:48:44 +01:00
|
|
|
run_test("process_message_for_senders", (override) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream1 = 1;
|
|
|
|
const stream2 = 2;
|
|
|
|
const stream3 = 3;
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic1 = "topic-1";
|
|
|
|
const topic2 = "topic-2";
|
|
|
|
const topic3 = "topic-3";
|
2020-05-01 08:29:08 +02:00
|
|
|
const topic4 = "topic-4";
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sender1 = 1;
|
|
|
|
const sender2 = 2;
|
|
|
|
const sender3 = 3;
|
2020-05-01 08:29:08 +02:00
|
|
|
const stream4 = 4;
|
|
|
|
const stream5 = 5;
|
2017-06-01 07:42:57 +02:00
|
|
|
|
|
|
|
// New stream
|
2019-11-02 00:06:25 +01:00
|
|
|
const message1 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender1,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const message2 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream2,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender2,
|
|
|
|
};
|
2020-05-01 08:29:08 +02:00
|
|
|
messages.push(message1, message2);
|
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
rs.process_message_for_senders(message1);
|
|
|
|
rs.process_message_for_senders(message2);
|
|
|
|
|
2017-08-29 16:57:47 +02:00
|
|
|
// Users have posted in only one of the streams
|
2017-06-01 07:42:57 +02:00
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-06-01 07:42:57 +02:00
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream2, topic1) > 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-08-29 16:57:47 +02:00
|
|
|
|
|
|
|
// Users haven't posted in this stream, return zero
|
2017-07-28 19:39:44 +02:00
|
|
|
assert.equal(
|
2017-08-29 16:57:47 +02:00
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream3, undefined) === 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-06-01 07:42:57 +02:00
|
|
|
|
|
|
|
// New topic
|
2019-11-02 00:06:25 +01:00
|
|
|
const message3 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic2,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender3,
|
|
|
|
};
|
2020-05-01 08:29:08 +02:00
|
|
|
messages.push(message3);
|
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
rs.process_message_for_senders(message3);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender3}, {user_id: sender2}, stream1, topic2) < 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-06-01 07:42:57 +02:00
|
|
|
|
|
|
|
// New sender
|
2019-11-02 00:06:25 +01:00
|
|
|
const message4 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender2,
|
|
|
|
};
|
2020-05-01 08:29:08 +02:00
|
|
|
messages.push(message4);
|
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
rs.process_message_for_senders(message4);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) > 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-06-01 07:42:57 +02:00
|
|
|
|
|
|
|
// More recent message
|
2019-11-02 00:06:25 +01:00
|
|
|
const message5 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender1,
|
|
|
|
};
|
2020-05-01 08:29:08 +02:00
|
|
|
messages.push(message5);
|
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
rs.process_message_for_senders(message5);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-06-03 21:14:16 +02:00
|
|
|
|
2017-08-29 16:57:47 +02:00
|
|
|
// Same stream, but different topics
|
2019-11-02 00:06:25 +01:00
|
|
|
const message6 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-08-29 16:57:47 +02:00
|
|
|
stream_id: stream3,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-08-29 16:57:47 +02:00
|
|
|
sender_id: sender1,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const message7 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-08-29 16:57:47 +02:00
|
|
|
stream_id: stream3,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic2,
|
2017-08-29 16:57:47 +02:00
|
|
|
sender_id: sender2,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const message8 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2017-08-29 16:57:47 +02:00
|
|
|
stream_id: stream3,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic3,
|
2017-08-29 16:57:47 +02:00
|
|
|
sender_id: sender3,
|
|
|
|
};
|
2020-05-01 08:29:08 +02:00
|
|
|
messages.push(message6, message7, message8);
|
2017-08-29 16:57:47 +02:00
|
|
|
|
|
|
|
rs.process_message_for_senders(message6);
|
|
|
|
rs.process_message_for_senders(message7);
|
|
|
|
rs.process_message_for_senders(message8);
|
|
|
|
|
|
|
|
// topic3 has a message in it, but sender1 nor sender2 have participated, so sort by stream
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream3, topic3) > 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-08-29 16:57:47 +02:00
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender2}, {user_id: sender1}, stream3, topic3) < 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.compare_by_recency({}, {}, (next_id += 1), ""), 0);
|
2020-05-01 08:29:08 +02:00
|
|
|
|
|
|
|
// new message in topic2
|
|
|
|
const message9 = {
|
2020-07-23 05:52:37 +02:00
|
|
|
type: "stream",
|
2020-05-01 08:29:08 +02:00
|
|
|
stream_id: stream3,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (next_id += 1),
|
2020-05-01 08:29:08 +02:00
|
|
|
topic: topic2,
|
|
|
|
sender_id: sender3,
|
|
|
|
};
|
|
|
|
messages.push(message9);
|
|
|
|
|
|
|
|
rs.process_message_for_senders(message9);
|
|
|
|
|
|
|
|
// Test topic change
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream3, topic3).toString(), "3");
|
|
|
|
assert.equal(rs.get_topic_recent_senders(stream3, topic2).toString(), "2,3");
|
2020-05-01 08:29:08 +02:00
|
|
|
|
|
|
|
// message7's topic was changed by user
|
|
|
|
messages[6].topic = topic3;
|
|
|
|
|
|
|
|
rs.process_topic_edit(stream3, topic2, topic3);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream3, topic3).toString(), "2,3");
|
|
|
|
assert.equal(rs.get_topic_recent_senders(stream3, topic2).toString(), "3");
|
2020-05-01 08:29:08 +02:00
|
|
|
|
|
|
|
// Test stream change
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream3, topic3).toString(), "2,3");
|
|
|
|
assert.equal(rs.get_topic_recent_senders(stream4, topic3).toString(), "");
|
2020-05-01 08:29:08 +02:00
|
|
|
// stream of topic3 was changed to stream4.
|
|
|
|
messages[6].stream_id = stream4; // message7's topic is topic3
|
|
|
|
messages[7].stream_id = stream4;
|
|
|
|
rs.process_topic_edit(stream3, topic3, topic3, stream4);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream3, topic3).toString(), "");
|
|
|
|
assert.equal(rs.get_topic_recent_senders(stream4, topic3).toString(), "2,3");
|
2020-05-01 08:29:08 +02:00
|
|
|
|
|
|
|
// Test stream & topic change
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream4, topic3).toString(), "2,3");
|
|
|
|
assert.equal(rs.get_topic_recent_senders(stream5, topic4).toString(), "");
|
2020-05-01 08:29:08 +02:00
|
|
|
// stream of topic3 was changed to stream5 and topic was changed to topic4.
|
|
|
|
messages[6].stream_id = stream5;
|
|
|
|
messages[7].stream_id = stream5;
|
|
|
|
messages[6].topic = topic4;
|
|
|
|
messages[7].topic = topic4;
|
|
|
|
rs.process_topic_edit(stream4, topic3, topic4, stream5);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream4, topic3).toString(), "");
|
|
|
|
assert.equal(rs.get_topic_recent_senders(stream5, topic4).toString(), "2,3");
|
2020-07-15 09:36:03 +02:00
|
|
|
|
2021-02-28 18:48:44 +01:00
|
|
|
// messages[0] (message1) and messages[4] (message5) were removed.
|
|
|
|
const reduced_msgs = [...messages.slice(1, 4), ...messages.slice(5)];
|
|
|
|
override(message_list.all, "all_messages", () => reduced_msgs);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream1, topic1).toString(), "2,1");
|
2020-07-15 09:36:03 +02:00
|
|
|
// delete message1 and message5 sent by sender1
|
2020-08-06 21:00:28 +02:00
|
|
|
rs.update_topics_of_deleted_message_ids([message1.id, message5.id]);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(rs.get_topic_recent_senders(stream1, topic1).toString(), "2");
|
2020-08-07 09:16:25 +02:00
|
|
|
|
|
|
|
// deleting an old message which isn't locally stored.
|
|
|
|
// We are just testing that it doesn't raise an error;
|
|
|
|
// no changes should take place in this case.
|
|
|
|
rs.update_topics_of_deleted_message_ids([-1]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|