2022-11-09 08:28:31 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe ChatChannel do
|
2022-12-06 06:40:46 +08:00
|
|
|
fab!(:category_channel1) { Fabricate(:category_channel) }
|
|
|
|
fab!(:dm_channel1) { Fabricate(:direct_message_channel) }
|
2022-11-09 08:28:31 +08:00
|
|
|
|
|
|
|
describe "#relative_url" do
|
|
|
|
context "when the slug is nil" do
|
|
|
|
it "uses a - instead" do
|
2022-12-06 06:40:46 +08:00
|
|
|
category_channel1.slug = nil
|
|
|
|
expect(category_channel1.relative_url).to eq("/chat/channel/#{category_channel1.id}/-")
|
2022-11-09 08:28:31 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the slug is not nil" do
|
2022-12-06 06:40:46 +08:00
|
|
|
before { category_channel1.update!(slug: "some-cool-channel") }
|
2022-11-09 08:28:31 +08:00
|
|
|
|
|
|
|
it "includes the slug for the channel" do
|
2022-12-06 06:40:46 +08:00
|
|
|
expect(category_channel1.relative_url).to eq(
|
|
|
|
"/chat/channel/#{category_channel1.id}/some-cool-channel",
|
2022-12-06 00:03:51 +08:00
|
|
|
)
|
2022-11-09 08:28:31 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-06 00:03:51 +08:00
|
|
|
|
2022-12-06 06:40:46 +08:00
|
|
|
describe ".ensure_consistency!" do
|
|
|
|
fab!(:category_channel2) { Fabricate(:category_channel) }
|
|
|
|
fab!(:category_channel3) { Fabricate(:category_channel) }
|
|
|
|
fab!(:category_channel4) { Fabricate(:category_channel) }
|
|
|
|
fab!(:dm_channel2) { Fabricate(:direct_message_channel) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel1)
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel1)
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel1)
|
|
|
|
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel2)
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel2)
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel2)
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel2)
|
|
|
|
|
|
|
|
Fabricate(:chat_message, chat_channel: category_channel3)
|
|
|
|
|
|
|
|
Fabricate(:chat_message, chat_channel: dm_channel2)
|
|
|
|
Fabricate(:chat_message, chat_channel: dm_channel2)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "updating messages_count for all channels" do
|
|
|
|
it "counts correctly" do
|
|
|
|
described_class.ensure_consistency!
|
|
|
|
expect(category_channel1.reload.messages_count).to eq(3)
|
|
|
|
expect(category_channel2.reload.messages_count).to eq(4)
|
|
|
|
expect(category_channel3.reload.messages_count).to eq(1)
|
|
|
|
expect(category_channel4.reload.messages_count).to eq(0)
|
|
|
|
expect(dm_channel1.reload.messages_count).to eq(0)
|
|
|
|
expect(dm_channel2.reload.messages_count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not count deleted messages" do
|
|
|
|
category_channel3.chat_messages.last.trash!
|
|
|
|
described_class.ensure_consistency!
|
|
|
|
expect(category_channel3.reload.messages_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not update deleted channels" do
|
|
|
|
described_class.ensure_consistency!
|
|
|
|
category_channel3.chat_messages.last.trash!
|
|
|
|
category_channel3.trash!
|
|
|
|
described_class.ensure_consistency!
|
|
|
|
expect(category_channel3.reload.messages_count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-06 00:03:51 +08:00
|
|
|
describe "#allow_channel_wide_mentions" do
|
|
|
|
it "defaults to true" do
|
2022-12-06 06:40:46 +08:00
|
|
|
expect(category_channel1.allow_channel_wide_mentions).to be(true)
|
2022-12-06 00:03:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cant be nullified" do
|
2022-12-06 06:40:46 +08:00
|
|
|
expect { category_channel1.update!(allow_channel_wide_mentions: nil) }.to raise_error(
|
2022-12-06 00:03:51 +08:00
|
|
|
ActiveRecord::NotNullViolation,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-11-09 08:28:31 +08:00
|
|
|
end
|