Skip to content

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

4.3 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe "Archive channel" do
  fab!(:channel_1, :chat_channel)

  let(:chat) { PageObjects::Pages::Chat.new }
  let(:channel) { PageObjects::Pages::ChatChannel.new }

  before do
    SiteSetting.navigation_menu = "sidebar"
    chat_system_bootstrap
    sign_in(current_user)
  end

  context "when archiving is disabled" do
    context "when admin user" do
      fab!(:current_user, :admin)

      before { sign_in(current_user) }

      it "doesn’t allow to archive a channel" do
        chat.visit_channel_settings(channel_1)

        expect(page).to have_no_content(I18n.t("js.chat.channel_settings.archive_channel"))
      end
    end
  end

  context "when archiving is enabled" do
    before { SiteSetting.chat_allow_archiving_channels = true }

    context "when regular user" do
      fab!(:current_user, :user)

      before { sign_in(current_user) }

      it "doesn’t allow to archive a channel" do
        chat.visit_channel_settings(channel_1)

        expect(page).to have_no_content(I18n.t("js.chat.channel_settings.archive_channel"))
      end
    end

    context "when admin user" do
      fab!(:current_user, :admin)

      before { sign_in(current_user) }

      it "allows to archive a channel" do
        chat.visit_channel_settings(channel_1)

        expect(page).to have_content(I18n.t("js.chat.channel_settings.archive_channel"))
      end

      context "when archiving" do
        it "archives the channel and creates a topic" do
          SiteSetting.tagging_enabled = true
          tag = Fabricate(:tag, name: "archived")
          Jobs.run_immediately!

          chat.visit_channel_settings(channel_1)
          click_button(I18n.t("js.chat.channel_settings.archive_channel"))
          find("#split-topic-name").fill_in(with: "An interesting topic for cats")

          tag_chooser =
            PageObjects::Components::SelectKit.new(".chat-to-topic-selector .tag-chooser")
          tag_chooser.expand
          tag_chooser.search(tag.name)
          tag_chooser.select_row_by_name(tag.name)
          tag_chooser.collapse

          click_button(I18n.t("js.chat.channel_archive.title"))

          expect(page).to have_css(".chat-channel-archive-status", wait: 15)

          try_until_success do
            archive = Chat::ChannelArchive.find_by(chat_channel: channel_1)
            expect(archive&.destination_tags).to eq([tag.name])
          end
        end

        context "when archived channels had unreads" do
          let(:other_user) { Fabricate(:user) }

          before do
            channel_1.add(current_user)
            channel_1.add(other_user)
          end

          it "clears unread indicators" do
            Jobs.run_immediately!

            Fabricate(
              :chat_message,
              chat_channel: channel_1,
              user: other_user,
              message: "this is fine @#{current_user.username}",
              use_service: true,
            )

            visit("/")
            expect(page.find(".chat-channel-unread-indicator")).to have_content(1)

            chat.visit_channel_settings(channel_1)
            click_button(I18n.t("js.chat.channel_settings.archive_channel"))
            find("#split-topic-name").fill_in(with: "An interesting topic for cats")
            click_button(I18n.t("js.chat.channel_archive.title"))

            expect(page).to have_no_css(".chat-channel-unread-indicator")
          end
        end
      end

      context "when archiving failed" do
        before { channel_1.update!(status: :read_only) }

        fab!(:archive) do
          Chat::ChannelArchive.create!(
            chat_channel: channel_1,
            archived_by: current_user,
            destination_topic_title: "This will be the archive topic",
            destination_category_id: channel_1.chatable_id,
            total_messages: 2,
            archived_messages: 1,
            archive_error: "Something went wrong",
          )
        end

        it "can be retried" do
          chat.visit_channel(channel_1)
          click_button(I18n.t("js.chat.channel_archive.retry"))

          Jobs::Chat::ChannelArchive.new.execute(chat_channel_archive_id: archive.id)

          archive_link = find(".chat-channel-archive-status a")
          expect(archive_link[:href]).to end_with("/t/-/#{archive.reload.destination_topic_id}")
        end
      end
    end
  end
end