DEV: Add specs to ChannelSerializer and StructuredChannelSerializer (#20872)

This adds specs to the mentioned serializers to catch regressions
with MessageBus last_ids and to ensure the correct ones are being
returned and passed down to the ChannelSerializer.

Followup to d8ad5c3
This commit is contained in:
Martin Brennan 2023-03-30 12:16:32 +10:00 committed by GitHub
parent 49e7e639cc
commit f8ff97b2ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 172 additions and 5 deletions

View File

@ -58,11 +58,13 @@ module Chat
chat_message_bus_last_ids[Chat::Publisher::CHANNEL_ARCHIVE_STATUS_MESSAGE_BUS_CHANNEL],
}
if id =
chat_message_bus_last_ids[
Chat::Publisher.user_tracking_state_message_bus_channel(scope.user.id)
]
last_ids[:user_tracking_state] = id
if !scope.anonymous?
user_tracking_state_last_id =
chat_message_bus_last_ids[
Chat::Publisher.user_tracking_state_message_bus_channel(scope.user.id)
]
last_ids[:user_tracking_state] = user_tracking_state_last_id if user_tracking_state_last_id
end
{ message_bus_last_ids: last_ids }

View File

@ -47,4 +47,70 @@ describe Chat::ChannelSerializer do
end
end
end
describe "#meta" do
context "for category channels" do
fab!(:chat_channel) { Fabricate(:chat_channel) }
it "has the required message_bus_last_ids keys and calls MessageBus" do
MessageBus.expects(:last_id).with(Chat::Publisher.root_message_bus_channel(chat_channel.id))
MessageBus.expects(:last_id).with(
Chat::Publisher.new_messages_message_bus_channel(chat_channel.id),
)
MessageBus.expects(:last_id).with(
Chat::Publisher.new_mentions_message_bus_channel(chat_channel.id),
)
MessageBus.expects(:last_id).with(
Chat::Publisher.kick_users_message_bus_channel(chat_channel.id),
)
expect(subject.as_json.dig(:meta, :message_bus_last_ids).keys).to eq(
%i[channel_message_bus_last_id new_messages new_mentions kick],
)
end
it "gets the kick_message_bus_last_id" do
MessageBus.expects(:last_id).at_least_once
MessageBus.expects(:last_id).with(
Chat::Publisher.kick_users_message_bus_channel(chat_channel.id),
)
expect(subject.as_json[:meta][:message_bus_last_ids].key?(:kick)).to eq(true)
end
it "does not call MessageBus for last_id if all the last IDs are already passed in" do
MessageBus.expects(:last_id).never
described_class.new(
chat_channel,
scope: guardian,
root: nil,
channel_message_bus_last_id: 1,
new_messages_message_bus_last_id: 1,
new_mentions_message_bus_last_id: 1,
kick_message_bus_last_id: 1,
).as_json
end
end
context "for direct message channels" do
fab!(:chat_channel) { Fabricate(:direct_message_channel) }
it "has the required message_bus_last_ids keys and calls MessageBus" do
MessageBus.expects(:last_id).with(Chat::Publisher.root_message_bus_channel(chat_channel.id))
MessageBus.expects(:last_id).with(
Chat::Publisher.new_messages_message_bus_channel(chat_channel.id),
)
MessageBus.expects(:last_id).with(
Chat::Publisher.new_mentions_message_bus_channel(chat_channel.id),
)
expect(subject.as_json.dig(:meta, :message_bus_last_ids).keys).to eq(
%i[channel_message_bus_last_id new_messages new_mentions],
)
end
it "does not get the kick_message_bus_last_id" do
MessageBus.expects(:last_id).at_least_once
MessageBus.expects(:last_id).never
expect(subject.as_json[:meta][:message_bus_last_ids].key?(:kick)).to eq(false)
end
end
end
end

View File

@ -104,4 +104,103 @@ RSpec.describe Chat::StructuredChannelSerializer do
],
).to eq(nil)
end
describe "#meta" do
context "when user is anonymous" do
it "does not query MessageBus for the user_tracking_state_message_bus_channel last_id" do
Chat::Publisher.expects(:user_tracking_state_message_bus_channel).never
json = described_class.new(fetch_data, scope: Guardian.new).as_json
expect(
json.dig(:structured_channel, :meta, :message_bus_last_ids).key?(:user_tracking_state),
).to eq(false)
end
end
context "when user is not anonymous" do
it "has the required message_bus_last_ids" do
expect(
described_class
.new(fetch_data, scope: guardian)
.as_json
.dig(:structured_channel, :meta, :message_bus_last_ids)
.keys,
).to eq(
%i[
channel_metadata
channel_edits
channel_status
new_channel
archive_status
user_tracking_state
],
)
end
it "calls MessageBus.last_ids with all the required channels for each public and DM chat chat channel" do
MessageBus
.expects(:last_ids)
.with(
Chat::Publisher::CHANNEL_METADATA_MESSAGE_BUS_CHANNEL,
Chat::Publisher::CHANNEL_EDITS_MESSAGE_BUS_CHANNEL,
Chat::Publisher::CHANNEL_STATUS_MESSAGE_BUS_CHANNEL,
Chat::Publisher::NEW_CHANNEL_MESSAGE_BUS_CHANNEL,
Chat::Publisher::CHANNEL_ARCHIVE_STATUS_MESSAGE_BUS_CHANNEL,
Chat::Publisher.user_tracking_state_message_bus_channel(user1.id),
Chat::Publisher.new_messages_message_bus_channel(channel1.id),
Chat::Publisher.new_mentions_message_bus_channel(channel1.id),
Chat::Publisher.kick_users_message_bus_channel(channel1.id),
Chat::Publisher.root_message_bus_channel(channel1.id),
Chat::Publisher.new_messages_message_bus_channel(channel2.id),
Chat::Publisher.new_mentions_message_bus_channel(channel2.id),
Chat::Publisher.kick_users_message_bus_channel(channel2.id),
Chat::Publisher.root_message_bus_channel(channel2.id),
Chat::Publisher.new_messages_message_bus_channel(channel3.id),
Chat::Publisher.new_mentions_message_bus_channel(channel3.id),
Chat::Publisher.root_message_bus_channel(channel3.id),
Chat::Publisher.new_messages_message_bus_channel(channel4.id),
Chat::Publisher.new_mentions_message_bus_channel(channel4.id),
Chat::Publisher.root_message_bus_channel(channel4.id),
)
.returns({})
described_class.new(fetch_data, scope: guardian).as_json
end
it "passes down the required message_bus ids for category channels to Chat::ChannelSerializer" do
data = fetch_data
Chat::ChannelSerializer.expects(:new).at_least_once
Chat::ChannelSerializer
.expects(:new)
.with(
channel1,
root: nil,
scope: guardian,
membership: membership1,
new_messages_message_bus_last_id: 0,
new_mentions_message_bus_last_id: 0,
kick_message_bus_last_id: 0,
channel_message_bus_last_id: 0,
)
.once
described_class.new(data, scope: guardian).as_json
end
it "passes down the required message_bus ids for direct message channels to Chat::ChannelSerializer" do
data = fetch_data
Chat::ChannelSerializer.expects(:new).at_least_once
Chat::ChannelSerializer
.expects(:new)
.with(
channel3,
root: nil,
scope: guardian,
membership: membership3,
new_messages_message_bus_last_id: 0,
new_mentions_message_bus_last_id: 0,
channel_message_bus_last_id: 0,
)
.once
described_class.new(data, scope: guardian).as_json
end
end
end
end