2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-01-27 15:54:06 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:06:06 +01:00
|
|
|
const {make_stub, with_stub} = require("../zjsunit/stub");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-08-29 04:08:14 +02:00
|
|
|
const events = require("./lib/events");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2020-06-12 22:27:49 +02:00
|
|
|
const event_fixtures = events.fixtures;
|
2020-06-12 23:05:06 +02:00
|
|
|
const test_user = events.test_user;
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2020-07-26 14:06:28 +02:00
|
|
|
set_global("compose_fade", {});
|
|
|
|
set_global("stream_events", {});
|
|
|
|
set_global("subs", {});
|
|
|
|
|
2021-01-12 21:38:01 +01:00
|
|
|
const peer_data = zrequire("peer_data");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-01-12 21:38:01 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const server_events_dispatch = zrequire("server_events_dispatch");
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
people.add_active_user(test_user);
|
|
|
|
|
2020-06-12 22:27:49 +02:00
|
|
|
const dispatch = server_events_dispatch.dispatch_normal_event;
|
|
|
|
|
|
|
|
function test(label, f) {
|
2020-06-12 23:05:06 +02:00
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
|
2020-07-26 16:25:43 +02:00
|
|
|
run_test(label, (override) => {
|
|
|
|
f(override);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test("add", (override) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
const event = event_fixtures.subscription__add;
|
2020-06-12 23:05:06 +02:00
|
|
|
|
|
|
|
const sub = event.subscriptions[0];
|
|
|
|
const stream_id = sub.stream_id;
|
|
|
|
|
|
|
|
stream_data.add_sub({
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-06-12 23:05:06 +02:00
|
|
|
name: sub.name,
|
|
|
|
});
|
|
|
|
|
2020-12-01 00:06:06 +01:00
|
|
|
with_stub((subscription_stub) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
override("stream_events.mark_subscribed", subscription_stub.f);
|
2020-06-12 23:05:06 +02:00
|
|
|
dispatch(event);
|
2020-07-15 01:29:15 +02:00
|
|
|
const args = subscription_stub.get_args("sub", "subscribers");
|
2020-06-12 23:05:06 +02:00
|
|
|
assert.deepEqual(args.sub.stream_id, stream_id);
|
|
|
|
assert.deepEqual(args.subscribers, event.subscriptions[0].subscribers);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test("peer add/remove", (override) => {
|
2020-06-12 16:54:01 +02:00
|
|
|
let event = event_fixtures.subscription__peer_add;
|
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
stream_data.add_sub({
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "devel",
|
2020-10-22 14:14:02 +02:00
|
|
|
stream_id: event.stream_ids[0],
|
2020-06-12 23:05:06 +02:00
|
|
|
});
|
|
|
|
|
2020-12-01 00:06:06 +01:00
|
|
|
const subs_stub = make_stub();
|
2020-07-15 01:29:15 +02:00
|
|
|
override("subs.update_subscribers_ui", subs_stub.f);
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2020-12-01 00:06:06 +01:00
|
|
|
const compose_fade_stub = make_stub();
|
2020-07-15 01:29:15 +02:00
|
|
|
override("compose_fade.update_faded_users", compose_fade_stub.f);
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
dispatch(event);
|
2020-06-12 22:27:49 +02:00
|
|
|
assert.equal(compose_fade_stub.num_calls, 1);
|
2020-06-20 19:58:38 +02:00
|
|
|
assert.equal(subs_stub.num_calls, 1);
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2021-01-22 18:40:00 +01:00
|
|
|
assert(peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));
|
|
|
|
|
2020-06-12 22:27:49 +02:00
|
|
|
event = event_fixtures.subscription__peer_remove;
|
2020-06-12 23:05:06 +02:00
|
|
|
dispatch(event);
|
2020-06-12 22:27:49 +02:00
|
|
|
assert.equal(compose_fade_stub.num_calls, 2);
|
2020-06-20 19:58:38 +02:00
|
|
|
assert.equal(subs_stub.num_calls, 2);
|
2021-01-22 18:40:00 +01:00
|
|
|
|
|
|
|
assert(!peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test("remove", (override) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
const event = event_fixtures.subscription__remove;
|
2020-06-12 23:05:06 +02:00
|
|
|
const event_sub = event.subscriptions[0];
|
|
|
|
const stream_id = event_sub.stream_id;
|
|
|
|
|
|
|
|
const sub = {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-06-12 23:05:06 +02:00
|
|
|
name: event_sub.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
|
2020-12-01 00:06:06 +01:00
|
|
|
with_stub((stub) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
override("stream_events.mark_unsubscribed", stub.f);
|
2020-06-12 22:27:49 +02:00
|
|
|
dispatch(event);
|
2020-07-15 01:29:15 +02:00
|
|
|
const args = stub.get_args("sub");
|
2020-06-12 23:05:06 +02:00
|
|
|
assert.deepEqual(args.sub, sub);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test("update", (override) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
const event = event_fixtures.subscription__update;
|
2020-12-01 00:06:06 +01:00
|
|
|
with_stub((stub) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
override("stream_events.update_property", stub.f);
|
2020-06-12 22:27:49 +02:00
|
|
|
dispatch(event);
|
2020-07-15 01:29:15 +02:00
|
|
|
const args = stub.get_args("stream_id", "property", "value");
|
2020-06-12 22:27:49 +02:00
|
|
|
assert.deepEqual(args.stream_id, event.stream_id);
|
|
|
|
assert.deepEqual(args.property, event.property);
|
|
|
|
assert.deepEqual(args.value, event.value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test("add error handling", (override) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
// test blueslip errors/warns
|
|
|
|
const event = event_fixtures.subscription__add;
|
2020-12-01 00:06:06 +01:00
|
|
|
with_stub((stub) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
override("blueslip.error", stub.f);
|
2020-06-12 22:27:49 +02:00
|
|
|
dispatch(event);
|
2020-07-25 22:33:03 +02:00
|
|
|
assert.deepEqual(stub.get_args("param").param, "Subscribing to unknown stream with ID 101");
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-27 15:54:06 +01:00
|
|
|
test("peer event error handling (bad stream_ids/user_ids)", (override) => {
|
2020-10-22 14:14:02 +02:00
|
|
|
override("compose_fade.update_faded_users", () => {});
|
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
const add_event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "subscription",
|
|
|
|
op: "peer_add",
|
2021-01-27 15:54:06 +01:00
|
|
|
stream_ids: [8888, 9999],
|
|
|
|
user_ids: [3333, 4444],
|
2020-06-12 16:54:01 +02:00
|
|
|
};
|
|
|
|
|
2021-01-27 15:54:06 +01:00
|
|
|
blueslip.expect("warn", "We have untracked stream_ids: 8888,9999");
|
|
|
|
blueslip.expect("warn", "We have untracked user_ids: 3333,4444");
|
2020-06-12 16:54:01 +02:00
|
|
|
dispatch(add_event);
|
|
|
|
blueslip.reset();
|
|
|
|
|
|
|
|
const remove_event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "subscription",
|
|
|
|
op: "peer_remove",
|
2021-01-27 15:54:06 +01:00
|
|
|
stream_ids: [8888, 9999],
|
|
|
|
user_ids: [3333, 4444],
|
2020-06-12 16:54:01 +02:00
|
|
|
};
|
|
|
|
|
2021-01-27 15:54:06 +01:00
|
|
|
blueslip.expect("warn", "We have untracked stream_ids: 8888,9999");
|
|
|
|
blueslip.expect("warn", "We have untracked user_ids: 3333,4444");
|
2020-06-12 16:54:01 +02:00
|
|
|
dispatch(remove_event);
|
|
|
|
});
|