Wednesday, July 15, 2026 4:00:49 AM
> mode_spec.rb
# frozen_string_literal: true

describe ESM::Command::Community::Mode, category: "command" do
  include_context "command" do
    let(:user_is_owner) { true }
  end

  include_examples "validate_command", requires_registration: false

  describe "#execute" do
    context "when player mode is enabled" do
      before do
        community.update(player_mode_enabled: true)
      end

      it "disables player mode" do
        execution_args = {
          channel_type: :dm,
          arguments: {community_id: community.community_id}
        }

        execute!(**execution_args)

        response = ESM.discord_bot.test_outbox.first.content
        expect(response).not_to be_nil

        community.reload
        expect(community.player_mode_enabled?).to be(false)

        expect(response.description).to eq(I18n.t("commands.mode.disabled", community_name: community.name))
        expect(response.color).to eq(ESM::Color::Toast::GREEN)
      end
    end

    context "when player mode is disabled" do
      before do
        community.update(player_mode_enabled: false)
      end

      it "enables player mode" do
        execution_args = {
          channel_type: :dm,
          arguments: {community_id: community.community_id}
        }

        execute!(**execution_args)

        response = ESM.discord_bot.test_outbox.first.content
        expect(response).not_to be_nil
        community.reload

        expect(community.player_mode_enabled?).to be(true)

        expect(response.description).to eq(I18n.t("commands.mode.enabled", community_name: community.name))
        expect(response.color).to eq(ESM::Color::Toast::GREEN)
      end

      context "when there are servers" do
        before do
          # Create a server so there is one
          create(:server, community_id: community.id)
        end

        it "raises an exception" do
          execution_args = {
            channel_type: :dm,
            arguments: {community_id: community.community_id}
          }

          expect { execute!(**execution_args) }.to raise_error do |error|
            embed = error.data
            expect(embed.description).to match(/in order to enable player mode you must remove any servers you've registered via my/i)
          end
        end
      end
    end

    context "when the user is not the owner" do
      it "raises an exception" do
        non_owner = create(:user)

        execution_args = {
          user: non_owner,
          channel_type: :dm,
          arguments: {community_id: community.community_id}
        }

        expect { execute!(**execution_args) }.to raise_error do |error|
          embed = error.data
          expect(embed.description).to match(/only the owner of this community has access to this command/i)
        end
      end
    end
  end
end
All opinions represented herein are my own
- © 2024 - 2026 itsthedevman
- build 43aa0d7