2021-05-05 19:54:26 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2021-05-05 19:54:26 +02:00
|
|
|
|
|
|
|
const {addDays} = require("date-fns");
|
|
|
|
|
2023-08-04 23:40:48 +02:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {run_test} = require("./lib/test");
|
2024-02-13 02:08:24 +01:00
|
|
|
const {current_user, page_params, realm} = require("./lib/zpage_params");
|
2021-05-05 19:54:26 +02:00
|
|
|
|
2020-12-24 06:10:37 +01:00
|
|
|
page_params.is_spectator = false;
|
|
|
|
|
2023-10-07 00:15:22 +02:00
|
|
|
const desktop_notifications = mock_esm("../src/desktop_notifications");
|
2023-08-04 23:40:48 +02:00
|
|
|
const util = mock_esm("../src/util");
|
2023-08-11 19:46:58 +02:00
|
|
|
const timerender = mock_esm("../src/timerender");
|
2023-08-04 23:40:48 +02:00
|
|
|
|
2021-05-05 19:54:26 +02:00
|
|
|
const {localstorage} = zrequire("localstorage");
|
2021-05-17 10:01:02 +02:00
|
|
|
const navbar_alerts = zrequire("navbar_alerts");
|
2021-05-05 19:54:26 +02:00
|
|
|
|
|
|
|
function test(label, f) {
|
2023-08-04 23:40:48 +02:00
|
|
|
run_test(label, (helpers) => {
|
2022-04-10 02:16:59 +02:00
|
|
|
window.localStorage.clear();
|
2023-08-04 23:40:48 +02:00
|
|
|
f(helpers);
|
2021-05-05 19:54:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-04 23:40:48 +02:00
|
|
|
test("allow_notification_alert", ({disallow, override}) => {
|
2021-05-07 11:45:19 +02:00
|
|
|
const ls = localstorage();
|
|
|
|
|
|
|
|
// Show alert.
|
|
|
|
assert.equal(ls.get("dontAskForNotifications"), undefined);
|
2023-08-04 23:40:48 +02:00
|
|
|
override(util, "is_mobile", () => false);
|
2023-10-07 00:15:22 +02:00
|
|
|
override(desktop_notifications, "granted_desktop_notifications_permission", () => false);
|
|
|
|
override(desktop_notifications, "permission_state", () => "granted");
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), true);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if the user said to never show alert on this computer again.
|
|
|
|
ls.set("dontAskForNotifications", true);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if device is mobile.
|
|
|
|
ls.set("dontAskForNotifications", undefined);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), true);
|
2023-08-04 23:40:48 +02:00
|
|
|
override(util, "is_mobile", () => true);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
2021-05-18 14:44:05 +02:00
|
|
|
// Avoid showing if notification permission is denied.
|
2023-08-04 23:40:48 +02:00
|
|
|
override(util, "is_mobile", () => false);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), true);
|
2023-10-07 00:15:22 +02:00
|
|
|
override(desktop_notifications, "permission_state", () => "denied");
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if notification is already granted.
|
2023-10-07 00:15:22 +02:00
|
|
|
disallow(desktop_notifications, "permission_state");
|
|
|
|
override(desktop_notifications, "granted_desktop_notifications_permission", () => "granted");
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2020-12-24 06:10:37 +01:00
|
|
|
|
|
|
|
// Don't ask for permission to spectator.
|
2023-08-04 23:40:48 +02:00
|
|
|
disallow(util, "is_mobile");
|
2023-10-07 00:15:22 +02:00
|
|
|
disallow(desktop_notifications, "granted_desktop_notifications_permission");
|
|
|
|
disallow(desktop_notifications, "permission_state");
|
2020-12-24 06:10:37 +01:00
|
|
|
page_params.is_spectator = true;
|
|
|
|
assert.equal(navbar_alerts.should_show_notifications(ls), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
});
|
|
|
|
|
2023-08-11 19:46:58 +02:00
|
|
|
test("profile_incomplete_alert", ({override}) => {
|
|
|
|
// Don't test time related conditions
|
|
|
|
override(timerender, "should_display_profile_incomplete_alert", () => true);
|
|
|
|
|
2021-05-07 11:45:19 +02:00
|
|
|
// Show alert.
|
2024-10-09 21:21:41 +02:00
|
|
|
override(current_user, "is_admin", true);
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_description", "Organization imported from Slack!");
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), true);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if the user is not admin.
|
2024-10-09 21:21:41 +02:00
|
|
|
override(current_user, "is_admin", false);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
|
|
|
|
// Avoid showing if the realm description is already updated.
|
2024-10-09 21:21:41 +02:00
|
|
|
override(current_user, "is_admin", true);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), true);
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_description", "Organization description already set!");
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.check_profile_incomplete(), false);
|
2021-05-07 11:45:19 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("server_upgrade_alert hide_duration_expired", ({override}) => {
|
2021-05-05 19:54:26 +02:00
|
|
|
const ls = localstorage();
|
2023-04-07 23:13:00 +02:00
|
|
|
const start_time = 1620327447050; // Thursday 06/5/2021 07:02:27 AM (UTC+0)
|
2021-05-05 19:54:26 +02:00
|
|
|
|
|
|
|
override(Date, "now", () => start_time);
|
|
|
|
assert.equal(ls.get("lastUpgradeNagDismissalTime"), undefined);
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), true);
|
|
|
|
navbar_alerts.dismiss_upgrade_nag(ls);
|
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), false);
|
2021-05-05 19:54:26 +02:00
|
|
|
|
2023-04-07 23:13:00 +02:00
|
|
|
override(Date, "now", () => addDays(start_time, 8).getTime()); // Friday 14/5/2021 07:02:27 AM (UTC+0)
|
2021-05-17 10:01:02 +02:00
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), true);
|
|
|
|
navbar_alerts.dismiss_upgrade_nag(ls);
|
|
|
|
assert.equal(navbar_alerts.should_show_server_upgrade_notification(ls), false);
|
2021-05-05 19:54:26 +02:00
|
|
|
});
|
2021-09-06 21:25:46 +02:00
|
|
|
|
2022-08-03 22:13:25 +02:00
|
|
|
test("demo_organization_days_remaining", ({override}) => {
|
2021-09-06 21:25:46 +02:00
|
|
|
const start_time = new Date(1620327447050); // Thursday 06/5/2021 07:02:27 AM (UTC+0)
|
|
|
|
|
|
|
|
const high_priority_deadline = addDays(start_time, 5);
|
2024-10-09 22:20:06 +02:00
|
|
|
override(
|
|
|
|
realm,
|
|
|
|
"demo_organization_scheduled_deletion_date",
|
|
|
|
Math.trunc(high_priority_deadline / 1000),
|
|
|
|
);
|
2021-09-06 21:25:46 +02:00
|
|
|
override(Date, "now", () => start_time);
|
|
|
|
assert.equal(navbar_alerts.get_demo_organization_deadline_days_remaining(), 5);
|
|
|
|
|
|
|
|
const low_priority_deadline = addDays(start_time, 10);
|
2024-10-09 22:20:06 +02:00
|
|
|
override(
|
|
|
|
realm,
|
|
|
|
"demo_organization_scheduled_deletion_date",
|
|
|
|
Math.trunc(low_priority_deadline / 1000),
|
|
|
|
);
|
2021-09-06 21:25:46 +02:00
|
|
|
override(Date, "now", () => start_time);
|
|
|
|
assert.equal(navbar_alerts.get_demo_organization_deadline_days_remaining(), 10);
|
|
|
|
});
|