2020-06-12 22:27:49 +02:00
|
|
|
|
|
|
|
const events = require('./lib/events.js');
|
|
|
|
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
|
|
|
|
|
|
|
const noop = function () {};
|
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('stream_data');
|
2020-06-12 22:27:49 +02:00
|
|
|
zrequire('server_events_dispatch');
|
|
|
|
|
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-06-12 22:27:49 +02:00
|
|
|
run_test(label, () => {
|
|
|
|
global.with_overrides(f);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test('add', (override) => {
|
|
|
|
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({
|
|
|
|
stream_id: stream_id,
|
|
|
|
name: sub.name,
|
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
global.with_stub((subscription_stub) => {
|
2020-06-12 23:05:06 +02:00
|
|
|
override('stream_events.mark_subscribed', subscription_stub.f);
|
|
|
|
dispatch(event);
|
|
|
|
const args = subscription_stub.get_args('sub', 'subscribers');
|
|
|
|
assert.deepEqual(args.sub.stream_id, stream_id);
|
|
|
|
assert.deepEqual(args.subscribers, event.subscriptions[0].subscribers);
|
2020-06-12 22:27:49 +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({
|
|
|
|
name: 'devel',
|
2020-06-12 16:54:01 +02:00
|
|
|
stream_id: event.stream_id,
|
2020-06-12 23:05:06 +02:00
|
|
|
});
|
|
|
|
|
2020-06-20 19:58:38 +02:00
|
|
|
const subs_stub = global.make_stub();
|
|
|
|
override('subs.rerender_subscriptions_settings', subs_stub.f);
|
2020-06-12 22:27:49 +02:00
|
|
|
|
|
|
|
const compose_fade_stub = global.make_stub();
|
|
|
|
override('compose_fade.update_faded_users', compose_fade_stub.f);
|
|
|
|
|
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
|
|
|
|
|
|
|
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);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('remove', (override) => {
|
|
|
|
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 = {
|
|
|
|
stream_id: stream_id,
|
|
|
|
name: event_sub.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
global.with_stub((stub) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
override('stream_events.mark_unsubscribed', stub.f);
|
|
|
|
dispatch(event);
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('update', (override) => {
|
|
|
|
const event = event_fixtures.subscription__update;
|
2020-07-02 01:45:54 +02:00
|
|
|
global.with_stub((stub) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
override('stream_events.update_property', stub.f);
|
|
|
|
dispatch(event);
|
|
|
|
const args = stub.get_args('stream_id', 'property', 'value');
|
|
|
|
assert.deepEqual(args.stream_id, event.stream_id);
|
|
|
|
assert.deepEqual(args.property, event.property);
|
|
|
|
assert.deepEqual(args.value, event.value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('add error handling', (override) => {
|
|
|
|
// test blueslip errors/warns
|
|
|
|
const event = event_fixtures.subscription__add;
|
2020-07-02 01:45:54 +02:00
|
|
|
global.with_stub((stub) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
override('blueslip.error', stub.f);
|
|
|
|
dispatch(event);
|
|
|
|
assert.deepEqual(stub.get_args('param').param, 'Subscribing to unknown stream with ID 42');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2020-06-12 16:54:01 +02:00
|
|
|
test('peer event error handling (bad stream_ids)', (override) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
override('compose_fade.update_faded_users', noop);
|
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
const add_event = {
|
|
|
|
type: 'subscription',
|
|
|
|
op: 'peer_add',
|
2020-06-12 16:54:01 +02:00
|
|
|
stream_id: 99999,
|
|
|
|
};
|
|
|
|
|
|
|
|
blueslip.expect('warn', 'Cannot find stream for peer_add: 99999');
|
|
|
|
dispatch(add_event);
|
|
|
|
blueslip.reset();
|
|
|
|
|
|
|
|
const remove_event = {
|
|
|
|
type: 'subscription',
|
|
|
|
op: 'peer_remove',
|
|
|
|
stream_id: 99999,
|
|
|
|
};
|
|
|
|
|
|
|
|
blueslip.expect('warn', 'Cannot find stream for peer_remove: 99999');
|
|
|
|
dispatch(remove_event);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('peer event error handling (add_subscriber)', (override) => {
|
|
|
|
stream_data.add_sub({
|
|
|
|
name: 'devel',
|
|
|
|
stream_id: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
override('stream_data.add_subscriber', () => false);
|
|
|
|
|
|
|
|
const add_event = {
|
|
|
|
type: 'subscription',
|
|
|
|
op: 'peer_add',
|
|
|
|
stream_id: 1,
|
|
|
|
user_id: 99999, // id is irrelevant
|
2020-06-12 23:05:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
blueslip.expect('warn', 'Cannot process peer_add event');
|
|
|
|
dispatch(add_event);
|
|
|
|
blueslip.reset();
|
|
|
|
|
2020-06-12 16:54:01 +02:00
|
|
|
override('stream_data.remove_subscriber', () => false);
|
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
const remove_event = {
|
|
|
|
type: 'subscription',
|
|
|
|
op: 'peer_remove',
|
2020-06-12 16:54:01 +02:00
|
|
|
stream_id: 1,
|
|
|
|
user_id: 99999, // id is irrelevant
|
2020-06-12 23:05:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
blueslip.expect('warn', 'Cannot process peer_remove event.');
|
|
|
|
dispatch(remove_event);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
|