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
|
|
|
|
2020-08-29 04:08:14 +02:00
|
|
|
const events = require("./lib/events");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {make_stub} = require("./lib/stub");
|
2023-12-14 23:51:33 +01:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2023-02-22 23:04:10 +01:00
|
|
|
const blueslip = require("./lib/zblueslip");
|
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
|
|
|
|
2024-03-15 21:51:16 +01:00
|
|
|
const compose_recipient = mock_esm("../src/compose_recipient");
|
2023-02-22 23:04:10 +01:00
|
|
|
const message_lists = mock_esm("../src/message_lists");
|
|
|
|
const narrow_state = mock_esm("../src/narrow_state");
|
|
|
|
const overlays = mock_esm("../src/overlays");
|
|
|
|
const settings_org = mock_esm("../src/settings_org");
|
|
|
|
const settings_streams = mock_esm("../src/settings_streams");
|
|
|
|
const stream_events = mock_esm("../src/stream_events");
|
|
|
|
const stream_list = mock_esm("../src/stream_list");
|
|
|
|
const stream_settings_ui = mock_esm("../src/stream_settings_ui");
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current = {};
|
2021-02-28 21:31:02 +01:00
|
|
|
|
2023-05-07 14:45:04 +02:00
|
|
|
const compose_state = zrequire("compose_state");
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const peer_data = zrequire("peer_data");
|
|
|
|
const people = zrequire("people");
|
2021-02-10 04:53:22 +01:00
|
|
|
const server_events_dispatch = zrequire("server_events_dispatch");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_realm} = zrequire("state_data");
|
2022-07-10 01:06:33 +02:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const sub_store = zrequire("sub_store");
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2024-10-09 22:44:13 +02:00
|
|
|
const realm = {};
|
|
|
|
set_realm(realm);
|
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
people.add_active_user(test_user);
|
|
|
|
|
2021-02-11 16:36:42 +01:00
|
|
|
const me = {
|
|
|
|
email: "me@zulip.com",
|
|
|
|
full_name: "Me Myself",
|
|
|
|
user_id: 101,
|
|
|
|
};
|
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
2020-06-12 22:27:49 +02:00
|
|
|
const dispatch = server_events_dispatch.dispatch_normal_event;
|
|
|
|
|
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2021-03-10 13:39:24 +01:00
|
|
|
stream_data.clear_subscriptions();
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:38:37 +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,
|
|
|
|
});
|
|
|
|
|
2021-02-13 03:46:14 +01:00
|
|
|
const subscription_stub = make_stub();
|
|
|
|
override(stream_events, "mark_subscribed", subscription_stub.f);
|
|
|
|
dispatch(event);
|
|
|
|
assert.equal(subscription_stub.num_calls, 1);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +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
|
|
|
});
|
|
|
|
|
2022-10-11 14:20:27 +02:00
|
|
|
const stream_stub = make_stub();
|
|
|
|
override(stream_events, "process_subscriber_update", stream_stub.f);
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2020-06-12 23:05:06 +02:00
|
|
|
dispatch(event);
|
2022-10-11 14:20:27 +02:00
|
|
|
assert.equal(stream_stub.num_calls, 1);
|
2020-06-12 22:27:49 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));
|
2021-01-22 18:40:00 +01:00
|
|
|
|
2020-06-12 22:27:49 +02:00
|
|
|
event = event_fixtures.subscription__peer_remove;
|
2020-06-12 23:05:06 +02:00
|
|
|
dispatch(event);
|
2022-10-11 14:20:27 +02:00
|
|
|
assert.equal(stream_stub.num_calls, 2);
|
2021-01-22 18:40:00 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +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);
|
|
|
|
|
2021-02-13 03:46:14 +01:00
|
|
|
const stub = make_stub();
|
|
|
|
override(stream_events, "mark_unsubscribed", stub.f);
|
|
|
|
dispatch(event);
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
const args = stub.get_args("sub");
|
|
|
|
assert.deepEqual(args.sub, sub);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("update", ({override}) => {
|
2020-06-12 22:27:49 +02:00
|
|
|
const event = event_fixtures.subscription__update;
|
2021-02-13 03:46:14 +01:00
|
|
|
|
|
|
|
const stub = make_stub();
|
|
|
|
override(stream_events, "update_property", stub.f);
|
|
|
|
dispatch(event);
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
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);
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
|
2021-03-16 23:55:01 +01:00
|
|
|
test("add error handling", () => {
|
2020-06-12 22:27:49 +02:00
|
|
|
// test blueslip errors/warns
|
|
|
|
const event = event_fixtures.subscription__add;
|
2021-02-13 03:46:14 +01:00
|
|
|
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "Subscribing to unknown stream");
|
2021-02-13 03:46:14 +01:00
|
|
|
dispatch(event);
|
2021-03-16 23:55:01 +01:00
|
|
|
blueslip.reset();
|
2020-06-12 22:27:49 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("peer event error handling (bad stream_ids/user_ids)", ({override}) => {
|
2023-12-14 23:51:33 +01:00
|
|
|
override(stream_events, "process_subscriber_update", noop);
|
2020-10-22 14:14:02 +02:00
|
|
|
|
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");
|
2023-07-12 07:22:30 +02:00
|
|
|
blueslip.expect("warn", "We have untracked user_ids");
|
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");
|
2023-07-12 07:22:30 +02:00
|
|
|
blueslip.expect("warn", "We have untracked user_ids");
|
2020-06-12 16:54:01 +02:00
|
|
|
dispatch(remove_event);
|
|
|
|
});
|
2021-02-11 16:36:42 +01:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("stream update", ({override}) => {
|
2021-02-11 16:36:42 +01:00
|
|
|
const event = event_fixtures.stream__update;
|
|
|
|
|
2021-02-13 03:46:14 +01:00
|
|
|
const stub = make_stub();
|
|
|
|
override(stream_events, "update_property", stub.f);
|
|
|
|
override(settings_streams, "update_default_streams_table", noop);
|
2022-08-15 21:32:10 +02:00
|
|
|
override(stream_list, "update_subscribe_to_more_streams_link", noop);
|
2021-02-13 03:46:14 +01:00
|
|
|
dispatch(event);
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
const args = stub.get_args("stream_id", "property", "value");
|
|
|
|
assert.equal(args.stream_id, event.stream_id);
|
|
|
|
assert.equal(args.property, event.property);
|
|
|
|
assert.equal(args.value, event.value);
|
2021-02-11 16:36:42 +01:00
|
|
|
});
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test("stream create", ({override}) => {
|
|
|
|
assert.equal(sub_store.get(101), undefined);
|
|
|
|
assert.equal(sub_store.get(102), undefined);
|
|
|
|
|
2021-02-11 16:36:42 +01:00
|
|
|
const event = event_fixtures.stream__create;
|
2021-02-13 03:46:14 +01:00
|
|
|
|
2021-07-09 15:51:31 +02:00
|
|
|
override(stream_settings_ui, "add_sub_to_table", noop);
|
2022-08-15 21:32:10 +02:00
|
|
|
override(stream_list, "update_subscribe_to_more_streams_link", noop);
|
2021-02-13 03:46:14 +01:00
|
|
|
override(overlays, "streams_open", () => true);
|
|
|
|
dispatch(event);
|
2022-07-10 01:06:33 +02:00
|
|
|
|
|
|
|
assert.deepEqual(sub_store.get(101).name, "devel");
|
|
|
|
assert.deepEqual(sub_store.get(102).name, "test");
|
2021-02-11 16:36:42 +01:00
|
|
|
});
|
|
|
|
|
2023-05-07 14:45:04 +02:00
|
|
|
test("stream delete (normal)", ({override}) => {
|
2021-02-11 16:36:42 +01:00
|
|
|
const event = event_fixtures.stream__delete;
|
|
|
|
|
|
|
|
for (const stream of event.streams) {
|
|
|
|
stream_data.add_sub(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_data.subscribe_myself(event.streams[0]);
|
|
|
|
|
|
|
|
override(settings_streams, "update_default_streams_table", noop);
|
|
|
|
|
|
|
|
narrow_state.is_for_stream_id = () => true;
|
|
|
|
|
|
|
|
let bookend_updates = 0;
|
2021-03-30 02:21:21 +02:00
|
|
|
override(message_lists.current, "update_trailing_bookend", () => {
|
2021-02-11 16:36:42 +01:00
|
|
|
bookend_updates += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
const removed_stream_ids = [];
|
|
|
|
|
2021-07-09 15:51:31 +02:00
|
|
|
override(stream_settings_ui, "remove_stream", (stream_id) => {
|
2021-02-11 16:36:42 +01:00
|
|
|
removed_stream_ids.push(stream_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
let removed_sidebar_rows = 0;
|
|
|
|
override(stream_list, "remove_sidebar_row", () => {
|
|
|
|
removed_sidebar_rows += 1;
|
|
|
|
});
|
2022-08-15 21:32:10 +02:00
|
|
|
override(stream_list, "update_subscribe_to_more_streams_link", noop);
|
2021-02-11 16:36:42 +01:00
|
|
|
|
|
|
|
dispatch(event);
|
|
|
|
|
|
|
|
assert.deepEqual(removed_stream_ids, [event.streams[0].stream_id, event.streams[1].stream_id]);
|
|
|
|
|
|
|
|
// We should possibly be able to make a single call to
|
|
|
|
// update_trailing_bookend, but we currently do it for each stream.
|
|
|
|
assert.equal(bookend_updates, 2);
|
|
|
|
|
|
|
|
assert.equal(removed_sidebar_rows, 1);
|
|
|
|
});
|
|
|
|
|
2023-05-07 14:45:04 +02:00
|
|
|
test("stream delete (special streams)", ({override}) => {
|
2021-02-11 16:36:42 +01:00
|
|
|
const event = event_fixtures.stream__delete;
|
|
|
|
|
|
|
|
for (const stream of event.streams) {
|
|
|
|
stream_data.add_sub(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanity check data
|
|
|
|
assert.equal(event.streams.length, 2);
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_new_stream_announcements_stream_id", event.streams[0].stream_id);
|
|
|
|
override(realm, "realm_signup_announcements_stream_id", event.streams[1].stream_id);
|
|
|
|
override(realm, "realm_zulip_update_announcements_stream_id", event.streams[0].stream_id);
|
2021-02-11 16:36:42 +01:00
|
|
|
|
2021-07-09 15:51:31 +02:00
|
|
|
override(stream_settings_ui, "remove_stream", noop);
|
2021-02-11 16:36:42 +01:00
|
|
|
override(settings_org, "sync_realm_settings", noop);
|
|
|
|
override(settings_streams, "update_default_streams_table", noop);
|
2021-03-30 02:21:21 +02:00
|
|
|
override(message_lists.current, "update_trailing_bookend", noop);
|
2021-02-11 16:36:42 +01:00
|
|
|
override(stream_list, "remove_sidebar_row", noop);
|
2022-08-15 21:32:10 +02:00
|
|
|
override(stream_list, "update_subscribe_to_more_streams_link", noop);
|
2021-02-11 16:36:42 +01:00
|
|
|
|
|
|
|
dispatch(event);
|
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
assert.equal(realm.realm_new_stream_announcements_stream_id, -1);
|
2024-02-07 17:11:43 +01:00
|
|
|
assert.equal(realm.realm_signup_announcements_stream_id, -1);
|
2024-01-26 14:45:37 +01:00
|
|
|
assert.equal(realm.realm_zulip_update_announcements_stream_id, -1);
|
2021-02-11 16:36:42 +01:00
|
|
|
});
|
2023-05-07 14:45:04 +02:00
|
|
|
|
2024-03-15 21:51:16 +01:00
|
|
|
test("stream delete (stream is selected in compose)", ({override}) => {
|
|
|
|
override(compose_recipient, "on_compose_select_recipient_update", noop);
|
2023-05-07 14:45:04 +02:00
|
|
|
|
|
|
|
const event = event_fixtures.stream__delete;
|
|
|
|
|
|
|
|
for (const stream of event.streams) {
|
|
|
|
stream_data.add_sub(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_data.subscribe_myself(event.streams[0]);
|
2023-06-27 01:40:25 +02:00
|
|
|
compose_state.set_stream_id(event.streams[0].stream_id);
|
2023-05-07 14:45:04 +02:00
|
|
|
|
|
|
|
override(settings_streams, "update_default_streams_table", noop);
|
|
|
|
|
|
|
|
narrow_state.is_for_stream_id = () => true;
|
|
|
|
|
|
|
|
let bookend_updates = 0;
|
|
|
|
override(message_lists.current, "update_trailing_bookend", () => {
|
|
|
|
bookend_updates += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
const removed_stream_ids = [];
|
|
|
|
|
|
|
|
override(stream_settings_ui, "remove_stream", (stream_id) => {
|
|
|
|
removed_stream_ids.push(stream_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
let removed_sidebar_rows = 0;
|
|
|
|
override(stream_list, "remove_sidebar_row", () => {
|
|
|
|
removed_sidebar_rows += 1;
|
|
|
|
});
|
|
|
|
override(stream_list, "update_subscribe_to_more_streams_link", noop);
|
|
|
|
|
|
|
|
dispatch(event);
|
|
|
|
|
|
|
|
assert.equal(compose_state.stream_name(), "");
|
|
|
|
assert.deepEqual(removed_stream_ids, [event.streams[0].stream_id, event.streams[1].stream_id]);
|
|
|
|
|
|
|
|
// We should possibly be able to make a single call to
|
|
|
|
// update_trailing_bookend, but we currently do it for each stream.
|
|
|
|
assert.equal(bookend_updates, 2);
|
|
|
|
|
|
|
|
assert.equal(removed_sidebar_rows, 1);
|
|
|
|
});
|