discourse/plugins/chat/spec/models/chat_channel_spec.rb
Martin Brennan 22a55ef0ce
DEV: Add messages_count to ChatChannel table (#19295)
This commit adds the messages_count column for ChatChannel messages,
which is the number of not-deleted messages in the channel.

This is not updated every time a message is created or deleted in a
channel, so it should not be displayed in the UI.
It is updated eventually via Jobs::ChatPeriodicalUpdates, which
will have additional functions in future after being introduced
here.

Also update these counts for existing channels in a post migration.
2022-12-06 08:40:46 +10:00

87 lines
3.0 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ChatChannel do
fab!(:category_channel1) { Fabricate(:category_channel) }
fab!(:dm_channel1) { Fabricate(:direct_message_channel) }
describe "#relative_url" do
context "when the slug is nil" do
it "uses a - instead" do
category_channel1.slug = nil
expect(category_channel1.relative_url).to eq("/chat/channel/#{category_channel1.id}/-")
end
end
context "when the slug is not nil" do
before { category_channel1.update!(slug: "some-cool-channel") }
it "includes the slug for the channel" do
expect(category_channel1.relative_url).to eq(
"/chat/channel/#{category_channel1.id}/some-cool-channel",
)
end
end
end
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
describe "#allow_channel_wide_mentions" do
it "defaults to true" do
expect(category_channel1.allow_channel_wide_mentions).to be(true)
end
it "cant be nullified" do
expect { category_channel1.update!(allow_channel_wide_mentions: nil) }.to raise_error(
ActiveRecord::NotNullViolation,
)
end
end
end