2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, 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
|
|
|
|
2021-05-05 05:38:24 +02:00
|
|
|
const muting = zrequire("muting");
|
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) {
|
|
|
|
run_test(label, (override) => {
|
|
|
|
typing_data.clear_for_testing();
|
2021-05-05 05:38:24 +02:00
|
|
|
muting.set_muted_users([]);
|
2021-03-15 16:17:37 +01:00
|
|
|
f(override);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("basics", () => {
|
2017-03-21 17:45:10 +01:00
|
|
|
// The typing_data needs to be robust with lists of
|
|
|
|
// user ids being in arbitrary sorting order and
|
|
|
|
// possibly in string form instead of integer. So all
|
|
|
|
// the apparent randomness in these tests has a purpose.
|
|
|
|
typing_data.add_typist([5, 10, 15], 15);
|
|
|
|
assert.deepEqual(typing_data.get_group_typists([15, 10, 5]), [15]);
|
|
|
|
|
|
|
|
// test that you can add twice
|
|
|
|
typing_data.add_typist([5, 10, 15], 15);
|
|
|
|
|
|
|
|
// add another id to our first group
|
|
|
|
typing_data.add_typist([5, 10, 15], "10");
|
|
|
|
assert.deepEqual(typing_data.get_group_typists([10, 15, 5]), [10, 15]);
|
|
|
|
|
|
|
|
// start adding to a new group
|
|
|
|
typing_data.add_typist([7, 15], 7);
|
|
|
|
typing_data.add_typist([7, "15"], 15);
|
|
|
|
|
|
|
|
// test get_all_typists
|
|
|
|
assert.deepEqual(typing_data.get_all_typists(), [7, 10, 15]);
|
|
|
|
|
|
|
|
// test basic removal
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(typing_data.remove_typist([15, 7], "7"));
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);
|
|
|
|
|
|
|
|
// test removing an id that is not there
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!typing_data.remove_typist([15, 7], 7));
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);
|
|
|
|
assert.deepEqual(typing_data.get_all_typists(), [10, 15]);
|
|
|
|
|
|
|
|
// remove user from one group, but "15" will still be among
|
|
|
|
// "all typists"
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(typing_data.remove_typist(["15", 7], "15"));
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_all_typists(), [10, 15]);
|
|
|
|
|
|
|
|
// now remove from the other group
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(typing_data.remove_typist([5, 15, 10], 15));
|
2017-03-21 17:45:10 +01:00
|
|
|
assert.deepEqual(typing_data.get_all_typists(), [10]);
|
|
|
|
|
|
|
|
// test duplicate ids in a groups
|
|
|
|
typing_data.add_typist([20, 40, 20], 20);
|
|
|
|
assert.deepEqual(typing_data.get_group_typists([20, 40]), [20]);
|
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", () => {
|
|
|
|
typing_data.add_typist([5, 10, 15], 5);
|
|
|
|
typing_data.add_typist([5, 10, 15], 10);
|
|
|
|
|
|
|
|
// Nobody is muted.
|
|
|
|
assert.deepEqual(typing_data.get_group_typists([5, 10, 15]), [5, 10]);
|
|
|
|
assert.deepEqual(typing_data.get_all_typists(), [5, 10]);
|
|
|
|
|
|
|
|
// Mute a user, and test that the get_* functions exclude that user.
|
|
|
|
muting.add_muted_user(10);
|
|
|
|
assert.deepEqual(typing_data.get_group_typists([5, 10, 15]), [5]);
|
|
|
|
assert.deepEqual(typing_data.get_all_typists(), [5]);
|
|
|
|
});
|
|
|
|
|
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";
|
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();
|
|
|
|
typing_data.kickstart_inbound_timer(stub_group, stub_delay, stub_f);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
reset_events();
|
|
|
|
typing_data.clear_inbound_timer(stub_group);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|