Skip to content

A platform for community discussion. Free, open, simple.

6.7 KB
Raw Blame
import { getOwner } from "@ember/owner";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import {
  logIn,
  updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
import ChatMessageInteractor from "discourse/plugins/chat/discourse/lib/chat-message-interactor";
import ChatFabricators from "discourse/plugins/chat/discourse/lib/fabricators";

module("Unit | chat-message-interactor", function (hooks) {
  setupTest(hooks);

  hooks.beforeEach(function () {
    this.currentUser = logIn(getOwner(this));
    this.chatFabricators = new ChatFabricators(getOwner(this));
    const message = this.chatFabricators.message();
    this.messageInteractor = new ChatMessageInteractor(getOwner(this), message);
    this.emojiStore = getOwner(this).lookup("service:emoji-store");
    this.siteSettings = getOwner(this).lookup("service:site-settings");
  });

  test("emojiReactions with no option uses site default", function (assert) {
    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1", "heart", "tada"]
    );
  });

  test("emojiReactions empty when no frequent or site defaults", function (assert) {
    this.siteSettings.default_emoji_reactions = "";

    assert.deepEqual(this.messageInteractor.emojiReactions, []);
  });

  test("emojiReactions with user option frequent falls back to site defaults", function (assert) {
    updateCurrentUser({
      user_option: {
        chat_quick_reaction_type: "frequent",
      },
    });

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1", "heart", "tada"]
    );
  });

  test("emojiReactions with diversity set applies to site defaults", function (assert) {
    updateCurrentUser({
      user_option: {
        chat_quick_reaction_type: "frequent",
      },
    });

    this.emojiStore.diversity = 2;

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1:t2", "heart", "tada"]
    );
  });

  test("emojiReactions with top 3 frequent", function (assert) {
    this.emojiStore.trackEmojiForContext("eyes", "chat");
    this.emojiStore.trackEmojiForContext("camera", "chat");
    this.emojiStore.trackEmojiForContext("butterfly", "chat");
    this.emojiStore.trackEmojiForContext("butterfly", "chat");
    this.emojiStore.trackEmojiForContext("laptop", "chat");

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["butterfly", "laptop", "camera"]
    );
  });

  test("emojiReactions with 1 frequent falls back to system", function (assert) {
    this.emojiStore.trackEmojiForContext("butterfly", "chat");

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["butterfly", "+1", "heart"]
    );
  });

  test("emojiReactions uses custom user option", function (assert) {
    updateCurrentUser({
      user_option: {
        chat_quick_reaction_type: "custom",
        chat_quick_reactions_custom: "grin|fearful|angry",
      },
    });

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["grin", "fearful", "angry"]
    );
  });

  test("emojiReactions does not use custom if set to frequent", function (assert) {
    updateCurrentUser({
      user_option: {
        chat_quick_reaction_type: "frequent",
        chat_quick_reactions_custom: "grin|fearful|angry",
      },
    });

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1", "heart", "tada"]
    );
  });

  test("emojiReactions avoids duplicates from frequent and site", function (assert) {
    this.emojiStore.trackEmojiForContext("+1", "chat");

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1", "heart", "tada"]
    );
  });

  test("emojiReactions avoids duplicates from custom + frequent + site", function (assert) {
    updateCurrentUser({
      user_option: {
        chat_quick_reaction_type: "custom",
        chat_quick_reactions_custom: "+1|+1|+1",
      },
    });
    this.emojiStore.trackEmojiForContext("+1", "chat");
    this.emojiStore.trackEmojiForContext("butterfly", "chat");

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1", "butterfly", "heart"]
    );
  });

  test("emojiReactions keeps its order once read", function (assert) {
    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1", "heart", "tada"]
    );

    this.emojiStore.trackEmojiForContext("butterfly", "chat");

    assert.deepEqual(
      this.messageInteractor.emojiReactions.map((r) => r.emoji),
      ["+1", "heart", "tada"],
      "reacting does not reorder the controls under the user"
    );
  });

  test("emojiReactions keeps the identity of the models it did not change", function (assert) {
    const before = this.messageInteractor.emojiReactions;

    this.messageInteractor.message.react(
      "+1",
      "add",
      this.currentUser,
      this.currentUser.id
    );

    const after = this.messageInteractor.emojiReactions;

    assert.notStrictEqual(
      after[0],
      before[0],
      "the reacted emoji gets a model"
    );
    assert.strictEqual(after[1], before[1], "heart is the same model");
    assert.strictEqual(after[2], before[2], "tada is the same model");
  });

  test("canRestoreMessage allows moderators to restore deleted messages", function (assert) {
    updateCurrentUser({ admin: false, moderator: false, staff: false });

    const otherUser = this.chatFabricators.coreFabricators.user();
    const channel = this.chatFabricators.channel({
      meta: { can_moderate: true },
    });
    const message = this.chatFabricators.message({
      channel,
      user: otherUser,
      deleted_at: new Date(),
      deleted_by_id: otherUser.id,
    });

    const interactor = new ChatMessageInteractor(getOwner(this), message);

    assert.true(interactor.canRestoreMessage);
    assert.true(
      interactor.secondaryActions.some(({ id }) => id === "restore"),
      "restore action is visible to moderators"
    );
  });

  test("canRestoreMessage disallows regular users from restoring moderator-deleted messages", function (assert) {
    updateCurrentUser({ admin: false, moderator: false, staff: false });

    const channel = this.chatFabricators.channel({
      meta: { can_moderate: false },
    });
    const message = this.chatFabricators.message({
      channel,
      user: this.currentUser,
      deleted_at: new Date(),
      deleted_by_id: this.currentUser.id + 1,
    });

    const interactor = new ChatMessageInteractor(getOwner(this), message);

    assert.false(interactor.canRestoreMessage);
    assert.false(
      interactor.secondaryActions.some(({ id }) => id === "restore"),
      "restore action stays hidden for non-moderators"
    );
  });
});