2018-12-23 16:01:30 +01:00
|
|
|
zrequire('util');
|
2019-11-02 00:06:25 +01:00
|
|
|
const rs = zrequire('recent_senders');
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('process_message_for_senders', () => {
|
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";
|
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;
|
2017-06-01 07:42:57 +02:00
|
|
|
|
|
|
|
// New stream
|
2019-11-02 00:06:25 +01:00
|
|
|
const message1 = {
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
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 = {
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream2,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender2,
|
|
|
|
};
|
|
|
|
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,
|
|
|
|
true);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream2, topic1) > 0,
|
|
|
|
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,
|
2017-07-28 19:39:44 +02:00
|
|
|
true);
|
2017-06-01 07:42:57 +02:00
|
|
|
|
|
|
|
// New topic
|
2019-11-02 00:06:25 +01:00
|
|
|
const message3 = {
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic2,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender3,
|
|
|
|
};
|
|
|
|
rs.process_message_for_senders(message3);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender3}, {user_id: sender2}, stream1, topic2) < 0,
|
|
|
|
true);
|
|
|
|
|
|
|
|
// New sender
|
2019-11-02 00:06:25 +01:00
|
|
|
const message4 = {
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender2,
|
|
|
|
};
|
|
|
|
rs.process_message_for_senders(message4);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) > 0,
|
|
|
|
true);
|
|
|
|
|
|
|
|
// More recent message
|
2019-11-02 00:06:25 +01:00
|
|
|
const message5 = {
|
2017-06-01 07:42:57 +02:00
|
|
|
stream_id: stream1,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic1,
|
2017-06-01 07:42:57 +02:00
|
|
|
sender_id: sender1,
|
|
|
|
};
|
|
|
|
rs.process_message_for_senders(message5);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
|
|
|
|
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 = {
|
2017-08-29 16:57:47 +02:00
|
|
|
stream_id: stream3,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
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 = {
|
2017-08-29 16:57:47 +02:00
|
|
|
stream_id: stream3,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
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 = {
|
2017-08-29 16:57:47 +02:00
|
|
|
stream_id: stream3,
|
2018-02-08 21:08:24 +01:00
|
|
|
id: _.uniqueId(),
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: topic3,
|
2017-08-29 16:57:47 +02:00
|
|
|
sender_id: sender3,
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
true);
|
|
|
|
assert.equal(
|
|
|
|
rs.compare_by_recency({user_id: sender2}, {user_id: sender1}, stream3, topic3) < 0,
|
|
|
|
true);
|
|
|
|
|
2017-06-03 21:14:16 +02:00
|
|
|
assert.equal(rs.compare_by_recency({}, {}, _.uniqueId(), ''), 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|