2024-09-06 22:25:25 +08:00
|
|
|
import { getOwner } from "@ember/owner";
|
|
|
|
import { module, test } from "qunit";
|
|
|
|
import sinon from "sinon";
|
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
|
|
|
import chatAudioInitializer from "discourse/plugins/chat/discourse/initializers/chat-audio";
|
|
|
|
|
|
|
|
module("Discourse Chat | Unit | chat-audio", function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
hooks.beforeEach(function () {
|
|
|
|
const chatAudioManager = getOwner(this).lookup(
|
|
|
|
"service:chat-audio-manager"
|
|
|
|
);
|
|
|
|
|
|
|
|
this.chat = getOwner(this).lookup("service:chat");
|
|
|
|
sinon.stub(this.chat, "userCanChat").value(true);
|
|
|
|
|
|
|
|
this.siteSettings = getOwner(this).lookup("service:site-settings");
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
|
2024-10-29 17:15:53 +08:00
|
|
|
this.currentUser.chat_sound = "ding";
|
2024-09-06 22:25:25 +08:00
|
|
|
this.currentUser.user_option.has_chat_enabled = true;
|
|
|
|
this.currentUser.user_option.chat_header_indicator_preference = "all_new";
|
|
|
|
|
|
|
|
withPluginApi("0.12.1", async (api) => {
|
|
|
|
this.stub = sinon.spy(api, "registerDesktopNotificationHandler");
|
|
|
|
chatAudioInitializer.initialize(getOwner(this));
|
|
|
|
|
2024-10-29 17:15:53 +08:00
|
|
|
// stub the service worker response
|
|
|
|
sinon
|
|
|
|
.stub(chatAudioInitializer, "canPlaySound")
|
|
|
|
.returns(Promise.resolve(true));
|
|
|
|
|
2024-09-06 22:25:25 +08:00
|
|
|
this.notificationHandler = this.stub.getCall(0).callback;
|
|
|
|
this.playStub = sinon.stub(chatAudioManager, "play");
|
|
|
|
|
|
|
|
this.handleNotification = (data = {}) => {
|
|
|
|
if (!data.notification_type) {
|
|
|
|
data.notification_type = 30;
|
|
|
|
}
|
|
|
|
this.notificationHandler(data, this.siteSettings, this.currentUser);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("registers desktop notification handler", function (assert) {
|
|
|
|
assert.true(this.stub.calledOnce);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("plays chat sound", async function (assert) {
|
2024-10-29 17:15:53 +08:00
|
|
|
await this.handleNotification();
|
2024-09-06 22:25:25 +08:00
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.calledOnce);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("skips chat sound for user in DND mode", async function (assert) {
|
2024-09-06 22:25:25 +08:00
|
|
|
this.currentUser.isInDoNotDisturb = () => true;
|
2024-10-29 17:15:53 +08:00
|
|
|
await this.handleNotification();
|
2024-09-06 22:25:25 +08:00
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.notCalled);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("skips chat sound for user with no chat sound set", async function (assert) {
|
2024-10-29 17:15:53 +08:00
|
|
|
this.currentUser.chat_sound = null;
|
|
|
|
await this.handleNotification();
|
2024-09-06 22:25:25 +08:00
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.notCalled);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("plays a chat sound for mentions", async function (assert) {
|
2024-09-06 22:25:25 +08:00
|
|
|
this.currentUser.user_option.chat_header_indicator_preference =
|
|
|
|
"only_mentions";
|
|
|
|
|
2024-10-29 17:15:53 +08:00
|
|
|
await this.handleNotification({ notification_type: 29 });
|
2024-09-06 22:25:25 +08:00
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.calledOnce);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("skips chat sound for non-mentions", async function (assert) {
|
2024-09-06 22:25:25 +08:00
|
|
|
this.currentUser.user_option.chat_header_indicator_preference =
|
|
|
|
"only_mentions";
|
|
|
|
|
2024-10-29 17:15:53 +08:00
|
|
|
await this.handleNotification();
|
2024-09-06 22:25:25 +08:00
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.notCalled);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("plays a chat sound for DMs", async function (assert) {
|
2024-09-06 22:25:25 +08:00
|
|
|
this.currentUser.user_option.chat_header_indicator_preference =
|
|
|
|
"dm_and_mentions";
|
|
|
|
|
2024-10-29 17:15:53 +08:00
|
|
|
await this.handleNotification({ is_direct_message_channel: true });
|
2024-09-06 22:25:25 +08:00
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.calledOnce);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("skips chat sound for non-DM messages", async function (assert) {
|
2024-09-06 22:25:25 +08:00
|
|
|
this.currentUser.user_option.chat_header_indicator_preference =
|
|
|
|
"dm_and_mentions";
|
|
|
|
|
2024-10-29 17:15:53 +08:00
|
|
|
await this.handleNotification({ is_direct_message_channel: false });
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.notCalled);
|
2024-10-29 17:15:53 +08:00
|
|
|
});
|
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
test("skips chat sound when service worker returns false", async function (assert) {
|
2024-10-29 17:15:53 +08:00
|
|
|
chatAudioInitializer.canPlaySound.returns(Promise.resolve(false));
|
|
|
|
await this.handleNotification();
|
2024-09-06 22:25:25 +08:00
|
|
|
|
2024-11-28 18:22:27 +08:00
|
|
|
assert.true(this.playStub.notCalled);
|
2024-09-06 22:25:25 +08:00
|
|
|
});
|
|
|
|
});
|