Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

activity tests: Improve actual coverage.

Steve Howellf2c8a5583ee3
diff --git a/web/src/activity_ui.ts b/web/src/activity_ui.ts
index 2fd48c2b0e4..8a2007ed7a6 100644
--- a/web/src/activity_ui.ts
+++ b/web/src/activity_ui.ts
@@ -29,6 +29,10 @@ export let user_filter: UserSearch | undefined;
// Function initialized from `ui_init` to avoid importing narrow.js and causing circular imports.
let narrow_by_email: (email: string) => void;
+export function get_narrow_by_email_function_for_test_code(): (email: string) => void {
+ return narrow_by_email;
+}
+
function get_pm_list_item(user_id: string): JQuery | undefined {
return buddy_list.find_li({
key: Number.parseInt(user_id, 10),
@@ -289,7 +293,7 @@ function keydown_enter_key(): void {
popovers.hide_all();
}
-export function set_cursor_and_filter(): void {
+export let set_cursor_and_filter = (): void => {
user_cursor = new ListCursor({
list: buddy_list,
highlight_class: "highlighted_user",
@@ -329,6 +333,10 @@ export function set_cursor_and_filter(): void {
},
},
});
+};
+
+export function rewire_set_cursor_and_filter(value: typeof set_cursor_and_filter): void {
+ set_cursor_and_filter = value;
}
export function initiate_search(): void {
diff --git a/web/tests/activity.test.cjs b/web/tests/activity.test.cjs
index 730fcf8ab50..b59752ce0f8 100644
--- a/web/tests/activity.test.cjs
+++ b/web/tests/activity.test.cjs
@@ -17,17 +17,12 @@ const blueslip = require("./lib/zblueslip.cjs");
const $ = require("./lib/zjquery.cjs");
const {page_params} = require("./lib/zpage_params.cjs");
-const $window_stub = $.create("window-stub");
-set_global("to_$", () => $window_stub);
-$(window).idle = noop;
-
const _document = {
hasFocus() {
return true;
},
};
-const channel = mock_esm("../src/channel");
const electron_bridge = mock_esm("../src/electron_bridge");
const keydown_util = mock_esm("../src/keydown_util", {handle() {}});
const padded_widget = mock_esm("../src/padded_widget");
@@ -36,7 +31,6 @@ const popovers = mock_esm("../src/popovers");
const settings_data = mock_esm("../src/settings_data");
const sidebar_ui = mock_esm("../src/sidebar_ui");
const scroll_util = mock_esm("../src/scroll_util");
-const watchdog = mock_esm("../src/watchdog");
set_global("document", _document);
@@ -761,97 +755,6 @@ test("update_presence_info", ({override, override_rewire}) => {
assert.equal(presence.presence_info.get(inaccessible_user_id), undefined);
});
-test("initialize", ({override, override_rewire}) => {
- override(document, "to_$", () => $("document-stub"));
- override(pm_list, "update_private_messages", noop);
- override(watchdog, "check_for_unsuspend", noop);
- override(buddy_list, "fill_screen_with_content", noop);
- override_rewire(activity_ui, "update_presence_indicators", noop);
-
- let payload;
- override(channel, "post", (arg) => {
- if (payload === undefined) {
- // This "if" block is added such that we can execute "success"
- // function when want_redraw is true.
- payload = arg;
- }
- });
-
- function clear() {
- $.clear_all_elements();
- buddy_list.$users_matching_view_list = $("#buddy-list-users-matching-view");
- buddy_list.$users_matching_view_list.append = noop;
- buddy_list.$other_users_list = $("#buddy-list-other-users");
- buddy_list.$other_users_list.append = noop;
- stub_buddy_list_elements();
- clear_buddy_list(buddy_list);
- page_params.presences = {};
- }
-
- clear();
-
- let scroll_handler_started;
- buddy_list.start_scroll_handler = () => {
- scroll_handler_started = true;
- };
-
- activity.mark_client_idle();
-
- $(window).off("focus");
-
- let set_timeout_function_called = false;
- set_global("setTimeout", (func) => {
- if (set_timeout_function_called) {
- // This conditional is needed to avoid indefinite calls.
- return;
- }
- set_timeout_function_called = true;
- func();
- });
-
- activity.initialize();
- activity_ui.initialize({narrow_by_email() {}});
- payload.success({
- presences: {},
- msg: "",
- result: "success",
- server_timestamp: 0,
- presence_last_update_id: -1,
- });
- $(window).trigger("focus");
- clear();
-
- assert.ok(scroll_handler_started);
- assert.ok(!activity.new_user_input);
- assert.equal(activity.compute_active_status(), "active");
-
- $(window).idle = (params) => {
- params.onIdle();
- };
- payload = undefined;
- set_timeout_function_called = false;
-
- $(window).off("focus");
- activity.initialize();
- activity_ui.initialize({narrow_by_email() {}});
- payload.success({
- presences: {},
- msg: "",
- result: "success",
- server_timestamp: 0,
- presence_last_update_id: -1,
- });
-
- assert.ok(!activity.new_user_input);
- assert.equal(activity.compute_active_status(), "idle");
-
- // Exercise the mousemove handler, which just
- // sets a flag.
- $(document).get_on_handler("mousemove")();
-
- clear();
-});
-
test("electron_bridge", ({override_rewire}) => {
override_rewire(activity, "send_presence_to_server", noop);
@@ -891,11 +794,6 @@ test("electron_bridge", ({override_rewire}) => {
assert.ok(activity.received_new_messages);
});
-test("test_send_or_receive_no_presence_for_spectator", () => {
- page_params.is_spectator = true;
- activity.send_presence_to_server();
-});
-
test("check_should_redraw_new_user", ({override}) => {
presence.presence_info.set(9999, {status: "active"});
diff --git a/web/tests/activity_init.test.cjs b/web/tests/activity_init.test.cjs
new file mode 100644
index 00000000000..94a4733b76d
--- /dev/null
+++ b/web/tests/activity_init.test.cjs
@@ -0,0 +1,105 @@
+"use strict";
+
+const assert = require("node:assert/strict");
+
+const {mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");
+const {run_test} = require("./lib/test.cjs");
+const $ = require("./lib/zjquery.cjs");
+
+const _document = {
+ hasFocus() {
+ return true;
+ },
+};
+set_global("document", _document);
+
+mock_esm("../src/electron_bridge", {
+ electron_bridge: {},
+});
+
+const activity = zrequire("activity");
+
+run_test("activity.initialize", ({override}) => {
+ /*
+ VERSION THAT WAS ORIGINALLY TESTED:
+ export function initialize(): void {
+ $(document).on("mousemove", () => {
+ set_new_user_input(true);
+ });
+
+ $(window).on("focus", mark_client_active);
+ $(window).idle({
+ idle: DEFAULT_IDLE_TIMEOUT_MS,
+ onIdle: mark_client_idle,
+ onActive: mark_client_active,
+ keepTracking: true,
+ });
+ }
+ */
+ const $document_stub = $("document-stub");
+ const $window_stub = $("window-stub");
+
+ $document_stub.on = (event, callback) => {
+ assert.equal(event, "mousemove");
+ callback(); // should be set_new_user_input(true)
+ };
+
+ $window_stub.on = (event, callback) => {
+ assert.equal(event, "focus");
+ assert.equal(callback, activity.mark_client_active);
+ };
+
+ $window_stub.idle = (info) => {
+ assert.deepEqual(info, {
+ idle: 300000,
+ keepTracking: true,
+ onActive: activity.mark_client_active,
+ onIdle: activity.mark_client_idle,
+ });
+ };
+
+ override(window, "to_$", () => $window_stub);
+ override(document, "to_$", () => $document_stub);
+
+ activity.set_new_user_input(false);
+ assert.equal(activity.new_user_input, false);
+
+ activity.initialize();
+
+ assert.equal(activity.new_user_input, true);
+});
+
+run_test("mark_client_active and mark_client_idle", ({override_rewire}) => {
+ // See the activity.initialize test above to verify
+ // how these helper functions are reliably wired to
+ // onActive and onIdle handlers on the browser window
+ // object.
+ let server_calls = 0;
+ override_rewire(activity, "send_presence_to_server", () => {
+ server_calls += 1;
+ });
+
+ // Use this to put us into an effective idle state, but then
+ // after this, the code does whatever mark_client_active and
+ // mark_client_idle tell it to do.
+ activity.clear_for_testing();
+ assert.equal(activity.compute_active_status(), "idle");
+
+ // This is a pretty simple API.
+
+ activity.mark_client_active();
+ assert.equal(activity.compute_active_status(), "active");
+ assert.equal(server_calls, 1);
+
+ activity.mark_client_idle();
+ assert.equal(activity.compute_active_status(), "idle");
+ assert.equal(server_calls, 1);
+
+ activity.mark_client_active();
+ assert.equal(activity.compute_active_status(), "active");
+ assert.equal(server_calls, 2);
+
+ activity.mark_client_idle();
+ assert.equal(activity.compute_active_status(), "idle");
+ assert.equal(server_calls, 2);
+});
diff --git a/web/tests/activity_post.test.cjs b/web/tests/activity_post.test.cjs
new file mode 100644
index 00000000000..a0cb131a624
--- /dev/null
+++ b/web/tests/activity_post.test.cjs
@@ -0,0 +1,100 @@
+"use strict";
+
+const assert = require("node:assert/strict");
+
+const {mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");
+const {noop, run_test} = require("./lib/test.cjs");
+const {page_params} = require("./lib/zpage_params.cjs");
+
+mock_esm("../src/electron_bridge");
+mock_esm("../src/watchdog", {
+ check_for_unsuspend: noop,
+});
+
+const channel = mock_esm("../src/channel");
+const presence = mock_esm("../src/presence");
+
+set_global("document", {hasFocus: () => true});
+
+const activity = zrequire("activity");
+
+run_test("send_presence_to_server (spectator)", ({override}) => {
+ override(page_params, "is_spectator", true);
+ activity.send_presence_to_server();
+});
+
+run_test("send_presence_to_server (no redraw)", ({override}) => {
+ let success_function;
+
+ assert.equal(activity.new_user_input, true);
+
+ override(channel, "post", ({url, data, success}) => {
+ success_function = success;
+ assert.equal(url, "/json/users/me/presence");
+ assert.deepEqual(data, {
+ status: "active",
+ ping_only: true,
+ new_user_input: true,
+ last_update_id: 42,
+ });
+ });
+
+ override(presence, "presence_last_update_id", 42);
+ activity.send_presence_to_server();
+
+ const response = {
+ presences: {},
+ msg: "",
+ result: "success",
+ server_timestamp: 0,
+ presence_last_update_id: -1,
+ };
+
+ success_function(response);
+ assert.equal(activity.new_user_input, false);
+});
+
+run_test("send_presence_to_server (redraw)", ({override}) => {
+ let success_function;
+
+ activity.set_new_user_input(true);
+ assert.equal(activity.new_user_input, true);
+
+ override(channel, "post", ({url, data, success}) => {
+ success_function = success;
+ assert.equal(url, "/json/users/me/presence");
+ assert.deepEqual(data, {
+ status: "active",
+ ping_only: false,
+ new_user_input: true,
+ last_update_id: 77,
+ });
+ });
+
+ let num_redraw_callbacks = 0;
+
+ function redraw() {
+ num_redraw_callbacks += 1;
+ }
+
+ override(presence, "presence_last_update_id", 77);
+ activity.send_presence_to_server(redraw);
+
+ const response = {
+ presences: {},
+ msg: "",
+ result: "success",
+ server_timestamp: 9999,
+ presence_last_update_id: 78,
+ };
+
+ override(presence, "set_info", (presences, server_timestamp, presence_last_update_id) => {
+ assert.deepEqual(presences, {});
+ assert.deepEqual(server_timestamp, 9999);
+ assert.deepEqual(presence_last_update_id, 78);
+ });
+
+ success_function(response);
+ assert.equal(activity.new_user_input, false);
+ assert.equal(num_redraw_callbacks, 1);
+});
diff --git a/web/tests/activity_ui.test.cjs b/web/tests/activity_ui.test.cjs
new file mode 100644
index 00000000000..e243d86cd08
--- /dev/null
+++ b/web/tests/activity_ui.test.cjs
@@ -0,0 +1,91 @@
+"use strict";
+
+const assert = require("node:assert/strict");
+
+const {make_realm} = require("./lib/example_realm.cjs");
+const {mock_esm, zrequire} = require("./lib/namespace.cjs");
+const {noop, run_test} = require("./lib/test.cjs");
+
+const activity = mock_esm("../src/activity");
+const scroll_util = mock_esm("../src/scroll_util");
+const pm_list = mock_esm("../src/pm_list");
+const util = mock_esm("../src/util");
+
+const state_data = zrequire("state_data");
+const activity_ui = zrequire("activity_ui");
+
+run_test("redraw", ({override, override_rewire}) => {
+ override_rewire(activity_ui, "build_user_sidebar", noop);
+ activity_ui.set_cursor_and_filter();
+ override(pm_list, "update_private_messages", noop);
+ override_rewire(activity_ui, "update_presence_indicators", noop);
+
+ activity_ui.redraw();
+});
+
+run_test("initialize", ({override, override_rewire}) => {
+ const realm = make_realm();
+ state_data.set_realm(realm);
+
+ override(realm, "server_presence_ping_interval_seconds", 42);
+
+ override_rewire(activity_ui, "set_cursor_and_filter", noop);
+ override_rewire(activity_ui, "build_user_sidebar", noop);
+
+ override(scroll_util, "get_scroll_element", (selector) => {
+ assert.equal(selector.selector, "#buddy_list_wrapper");
+ return {
+ on(action, callback) {
+ assert.equal(action, "scroll");
+ // We don't test the actual logic of the scroll
+ // handler here. We did our job to set it up.
+ assert.ok(callback !== undefined);
+ },
+ };
+ });
+
+ let expected_redraw;
+ let num_presence_calls = 0;
+
+ override(activity, "send_presence_to_server", (redraw) => {
+ num_presence_calls += 1;
+ assert.equal(redraw, expected_redraw);
+ });
+
+ let f_to_call_periodically;
+
+ override(util, "call_function_periodically", (f, delay) => {
+ f_to_call_periodically = f;
+ assert.equal(delay, 42000);
+ });
+
+ let my_email = "";
+
+ function narrow_by_email(email) {
+ my_email = email;
+ }
+
+ activity_ui.initialize({narrow_by_email});
+
+ assert.equal(my_email, "");
+ activity_ui.get_narrow_by_email_function_for_test_code()("[email protected]");
+ assert.equal(my_email, "[email protected]");
+
+ // Simulate the subsequent calls to
+ // activity.send_presence_to_server.
+ expected_redraw = activity_ui.redraw;
+
+ assert.equal(num_presence_calls, 1);
+ f_to_call_periodically();
+ f_to_call_periodically();
+ f_to_call_periodically();
+ f_to_call_periodically();
+ f_to_call_periodically();
+ assert.equal(num_presence_calls, 6);
+ f_to_call_periodically();
+ f_to_call_periodically();
+ f_to_call_periodically();
+ f_to_call_periodically();
+ f_to_call_periodically();
+ assert.equal(num_presence_calls, 11);
+});
diff --git a/web/tests/util.test.cjs b/web/tests/util.test.cjs
index 8c7c11ae6b2..250f597508d 100644
--- a/web/tests/util.test.cjs
+++ b/web/tests/util.test.cjs
@@ -606,6 +606,29 @@ run_test("sha256_hash", async ({override}) => {
assert.equal(hash, expected_hash);
});
+run_test("call_function_periodically", () => {
+ let num_set_timeout_calls = 0;
+ let num_callback_calls = 0;
+
+ set_global("setTimeout", (callbacK_function, delay) => {
+ assert.equal(delay, 42);
+
+ num_set_timeout_calls += 1;
+ if (num_set_timeout_calls === 100) {
+ return;
+ }
+ callbacK_function();
+ });
+
+ function callback_func() {
+ num_callback_calls += 1;
+ }
+
+ util.call_function_periodically(callback_func, 42);
+ assert.equal(num_set_timeout_calls, 100);
+ assert.equal(num_callback_calls, 99);
+});
+
run_test("unique_array_insert", () => {
const array = [{a: "foo", b: "bar"}];
util.unique_array_insert(array, {c: "beep", d: "boop"});