2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
const {set_global, zrequire} = require("./lib/namespace.js");
|
|
|
|
const {run_test} = require("./lib/test.js");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
const muted_users = zrequire("muted_users");
|
2021-02-10 04:53:22 +01:00
|
|
|
const typing_data = zrequire("typing_data");
|
2017-03-21 17:45:10 +01:00
|
|
|
|
2021-03-15 16:17:37 +01:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-03-15 16:17:37 +01:00
|
|
|
typing_data.clear_for_testing();
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.set_muted_users([]);
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-03-15 16:17:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("basics", () => {
|
2017-03-21 17:45:10 +01:00
|
|
|
// The typing_data needs to be robust with lists of
|
2023-02-28 07:39:07 +01:00
|
|
|
// user ids being in arbitrary sorting order. So all
|
2017-03-21 17:45:10 +01:00
|
|
|
// the apparent randomness in these tests has a purpose.
|
2021-01-04 16:21:54 +01:00
|
|
|
|
|
|
|
const stream_id = 1;
|
|
|
|
const topic = "typing notifications";
|
|
|
|
const topic_typing_key = typing_data.get_topic_key(stream_id, topic);
|
|
|
|
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 15);
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([15, 10, 5]), [15]);
|
|
|
|
|
2021-01-04 16:21:54 +01:00
|
|
|
typing_data.add_typist(topic_typing_key, 12);
|
|
|
|
assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [12]);
|
|
|
|
|
2017-03-21 17:45:10 +01:00
|
|
|
// test that you can add twice
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 15);
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
// add another id to our first group
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 10);
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([10, 15, 5]), [10, 15]);
|
|
|
|
|
2021-01-04 16:21:54 +01:00
|
|
|
typing_data.add_typist(topic_typing_key, 12);
|
|
|
|
|
|
|
|
// add another typist to our stream/topic
|
|
|
|
typing_data.add_typist(topic_typing_key, 13);
|
|
|
|
assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [12, 13]);
|
|
|
|
|
2017-03-21 17:45:10 +01:00
|
|
|
// start adding to a new group
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([7, 15]), 7);
|
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([7, 15]), 15);
|
2017-03-21 17:45:10 +01:00
|
|
|
|
2021-03-23 18:13:08 +01:00
|
|
|
// test get_all_direct_message_typists
|
|
|
|
assert.deepEqual(typing_data.get_all_direct_message_typists(), [7, 10, 15]);
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
// test basic removal
|
2021-04-05 21:10:11 +02:00
|
|
|
assert.ok(
|
|
|
|
typing_data.remove_typist(typing_data.get_direct_message_conversation_key([15, 7]), 7),
|
|
|
|
);
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);
|
2021-01-04 16:21:54 +01:00
|
|
|
assert.ok(typing_data.remove_typist(topic_typing_key, 12));
|
|
|
|
assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [13]);
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
// test removing an id that is not there
|
2021-04-05 21:10:11 +02:00
|
|
|
assert.ok(
|
|
|
|
!typing_data.remove_typist(typing_data.get_direct_message_conversation_key([15, 7]), 7),
|
|
|
|
);
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);
|
2021-03-23 18:13:08 +01:00
|
|
|
assert.deepEqual(typing_data.get_all_direct_message_typists(), [10, 15]);
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
// remove user from one group, but "15" will still be among
|
|
|
|
// "all typists"
|
2021-04-05 21:10:11 +02:00
|
|
|
assert.ok(
|
|
|
|
typing_data.remove_typist(typing_data.get_direct_message_conversation_key([15, 7]), 15),
|
|
|
|
);
|
2021-03-23 18:13:08 +01:00
|
|
|
assert.deepEqual(typing_data.get_all_direct_message_typists(), [10, 15]);
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
// now remove from the other group
|
2021-04-05 21:10:11 +02:00
|
|
|
assert.ok(
|
|
|
|
typing_data.remove_typist(typing_data.get_direct_message_conversation_key([5, 15, 10]), 15),
|
|
|
|
);
|
2021-03-23 18:13:08 +01:00
|
|
|
assert.deepEqual(typing_data.get_all_direct_message_typists(), [10]);
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
// test duplicate ids in a groups
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([20, 40, 20]), 20);
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([20, 40]), [20]);
|
2024-04-12 19:30:29 +02:00
|
|
|
|
|
|
|
// test clearing out typing data
|
|
|
|
typing_data.clear_typing_data();
|
|
|
|
assert.deepEqual(typing_data.get_group_typists(), []);
|
|
|
|
assert.deepEqual(typing_data.get_all_direct_message_typists(), []);
|
|
|
|
assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), []);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-03-22 16:20:16 +01:00
|
|
|
|
2021-05-05 05:38:24 +02:00
|
|
|
test("muted_typists_excluded", () => {
|
2021-05-23 08:25:39 +02:00
|
|
|
const stream_id = 1;
|
|
|
|
const topic = "typing notifications";
|
|
|
|
const topic_typing_key = typing_data.get_topic_key(stream_id, topic);
|
|
|
|
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 5);
|
|
|
|
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 10);
|
2021-05-23 08:25:39 +02:00
|
|
|
typing_data.add_typist(topic_typing_key, 7);
|
|
|
|
typing_data.add_typist(topic_typing_key, 12);
|
2021-05-05 05:38:24 +02:00
|
|
|
|
|
|
|
// Nobody is muted.
|
|
|
|
assert.deepEqual(typing_data.get_group_typists([5, 10, 15]), [5, 10]);
|
2021-03-23 18:13:08 +01:00
|
|
|
assert.deepEqual(typing_data.get_all_direct_message_typists(), [5, 10]);
|
2021-05-23 08:25:39 +02:00
|
|
|
assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [7, 12]);
|
2021-05-05 05:38:24 +02:00
|
|
|
|
|
|
|
// Mute a user, and test that the get_* functions exclude that user.
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(10);
|
2021-05-23 08:25:39 +02:00
|
|
|
muted_users.add_muted_user(7);
|
2021-05-05 05:38:24 +02:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([5, 10, 15]), [5]);
|
2021-03-23 18:13:08 +01:00
|
|
|
assert.deepEqual(typing_data.get_all_direct_message_typists(), [5]);
|
2021-05-23 08:25:39 +02:00
|
|
|
assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [12]);
|
2021-05-05 05:38:24 +02:00
|
|
|
});
|
|
|
|
|
2021-03-15 16:17:37 +01:00
|
|
|
test("timers", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const events = {};
|
2017-03-22 16:20:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const stub_timer_id = "timer_id_stub";
|
2019-11-02 00:06:25 +01:00
|
|
|
const stub_group = [5, 10, 15];
|
|
|
|
const stub_delay = 99;
|
2020-07-15 01:29:15 +02:00
|
|
|
const stub_f = "function";
|
2021-01-04 16:21:54 +01:00
|
|
|
const stub_stream_id = 1;
|
|
|
|
const stub_topic = "typing notifications";
|
|
|
|
const topic_typing_key = typing_data.get_topic_key(stub_stream_id, stub_topic);
|
2017-03-22 16:20:16 +01:00
|
|
|
|
|
|
|
function set_timeout(f, delay) {
|
|
|
|
assert.equal(delay, stub_delay);
|
|
|
|
events.f = f;
|
|
|
|
events.timer_set = true;
|
|
|
|
return stub_timer_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear_timeout(timer) {
|
|
|
|
assert.equal(timer, stub_timer_id);
|
|
|
|
events.timer_cleared = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset_events() {
|
|
|
|
events.f = undefined;
|
|
|
|
events.timer_cleared = false;
|
|
|
|
events.timer_set = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function kickstart() {
|
|
|
|
reset_events();
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.kickstart_inbound_timer(
|
|
|
|
typing_data.get_direct_message_conversation_key(stub_group),
|
|
|
|
stub_delay,
|
|
|
|
stub_f,
|
|
|
|
);
|
2017-03-22 16:20:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
reset_events();
|
2021-04-05 21:10:11 +02:00
|
|
|
typing_data.clear_inbound_timer(
|
|
|
|
typing_data.get_direct_message_conversation_key(stub_group),
|
|
|
|
);
|
2017-03-22 16:20:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 16:21:54 +01:00
|
|
|
function streams_kickstart() {
|
|
|
|
reset_events();
|
|
|
|
typing_data.kickstart_inbound_timer(topic_typing_key, stub_delay, stub_f);
|
|
|
|
}
|
|
|
|
|
|
|
|
function streams_clear() {
|
|
|
|
reset_events();
|
|
|
|
typing_data.clear_inbound_timer(topic_typing_key);
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
set_global("setTimeout", set_timeout);
|
|
|
|
set_global("clearTimeout", clear_timeout);
|
2017-03-22 16:20:16 +01:00
|
|
|
|
|
|
|
// first time, we set
|
|
|
|
kickstart();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: stub_f,
|
|
|
|
timer_cleared: false,
|
|
|
|
timer_set: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// second time we clear and set
|
|
|
|
kickstart();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: stub_f,
|
|
|
|
timer_cleared: true,
|
|
|
|
timer_set: true,
|
|
|
|
});
|
|
|
|
|
2024-04-12 19:30:29 +02:00
|
|
|
// clearing out typing data
|
|
|
|
kickstart();
|
|
|
|
typing_data.clear_typing_data();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: stub_f,
|
|
|
|
timer_cleared: true,
|
|
|
|
timer_set: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
kickstart();
|
2017-03-22 16:20:16 +01:00
|
|
|
// first time clearing, we clear
|
|
|
|
clear();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: undefined,
|
|
|
|
timer_cleared: true,
|
|
|
|
timer_set: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// second time clearing, we noop
|
|
|
|
clear();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: undefined,
|
|
|
|
timer_cleared: false,
|
|
|
|
timer_set: false,
|
|
|
|
});
|
2021-01-04 16:21:54 +01:00
|
|
|
|
|
|
|
// first time, we set
|
|
|
|
streams_kickstart();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: stub_f,
|
|
|
|
timer_cleared: false,
|
|
|
|
timer_set: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// second time we clear and set
|
|
|
|
streams_kickstart();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: stub_f,
|
|
|
|
timer_cleared: true,
|
|
|
|
timer_set: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// first time clearing, we clear
|
|
|
|
streams_clear();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: undefined,
|
|
|
|
timer_cleared: true,
|
|
|
|
timer_set: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// second time clearing, we noop
|
|
|
|
streams_clear();
|
|
|
|
assert.deepEqual(events, {
|
|
|
|
f: undefined,
|
|
|
|
timer_cleared: false,
|
|
|
|
timer_set: false,
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|