discourse/plugins/chat/spec/system/send_message_spec.rb
Alan Guo Xiang Tan 55b05c921b
DEV: Add client settled checks for system tests (#35230)
This change seeks to improve the reliability of our system tests by
resolving the lack of consistency in the state of the client side
application between steps in a system test. This is achieved by patching
various action methods in `Capybara::Playwright::Node` and
`Capybara::Playwright::Browser` so that the methods execute an async
JavaScript function on the client side that waits for the client side
application to reach a settled state. A settled state is currently
defined as:

1. No inflight ajax requests. (_messageBus and presence requests are
excluded_)
2. 2 event cycles of the Javascript event loop has happened for for all
"click", "input", "mousedown", "keydown", "focusin", "focusout",
"touchstart", "change", "resize", "scroll" DOM events that fired.

For debugging purposes, a `--debug-client-settled` CLI flag has been
added to `bin/rspec`. When used, detailed debugging information will be
printed to the browser's console as well as to `stdout` of the
`bin/rspec` process.

This change was inspired by
https://evilmartians.com/chronicles/flaky-tests-be-gone-long-lasting-relief-chronic-ci-retry-irritation
and the https://github.com/makandra/capybara-lockstep rubygem.
2025-10-10 11:03:18 +08:00

128 lines
3.7 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe "Send message", type: :system do
fab!(:user_1, :admin)
fab!(:user_2, :admin)
let(:chat_page) { PageObjects::Pages::Chat.new }
let(:channel_page) { PageObjects::Pages::ChatChannel.new }
before do
# simpler user search without having to worry about user search data
SiteSetting.enable_names = false
chat_system_bootstrap
end
context "with direct message channels" do
context "when users are not following the channel" do
fab!(:channel_1) { Fabricate(:direct_message_channel, users: [user_1, user_2]) }
before do
channel_1.remove(user_1)
channel_1.remove(user_2)
end
it "shows correct state" do
sign_in(user_1)
visit("/")
expect(chat_page.sidebar).to have_no_direct_message_channel(channel_1)
using_session(:user_2) do
sign_in(user_2)
visit("/")
expect(chat_page.sidebar).to have_no_direct_message_channel(channel_1)
end
chat_page.open_new_message
chat_page.message_creator.filter(user_2.username)
chat_page.message_creator.click_row(user_2)
expect(chat_page.sidebar).to have_direct_message_channel(channel_1)
using_session(:user_2) do
expect(chat_page.sidebar).to have_no_direct_message_channel(channel_1)
end
channel_page.send_message
expect(chat_page.sidebar).to have_direct_message_channel(channel_1)
using_session(:user_2) do
expect(chat_page.sidebar).to have_direct_message_channel(channel_1, mention: true)
end
end
end
context "when users are following the channel" do
fab!(:channel_1) { Fabricate(:direct_message_channel, users: [user_1, user_2]) }
it "shows correct state" do
sign_in(user_1)
visit("/")
expect(chat_page.sidebar).to have_direct_message_channel(channel_1)
using_session(:user_2) do
sign_in(user_2)
visit("/")
expect(chat_page.sidebar).to have_direct_message_channel(channel_1)
end
chat_page.open_new_message
chat_page.message_creator.filter(user_2.username)
chat_page.message_creator.click_row(user_2)
expect(chat_page.sidebar).to have_direct_message_channel(channel_1)
using_session(:user_2) do
expect(chat_page.sidebar).to have_direct_message_channel(channel_1)
end
channel_page.send_message
expect(chat_page.sidebar).to have_direct_message_channel(channel_1)
using_session(:user_2) do
expect(chat_page.sidebar).to have_direct_message_channel(channel_1, mention: true)
end
end
end
end
context "when sending message from drawer" do
let(:drawer_page) { PageObjects::Pages::ChatDrawer.new }
let(:topic_page) { PageObjects::Pages::Topic.new }
fab!(:post_1, :post)
fab!(:post_2) { Fabricate(:post, topic: post_1.topic) }
fab!(:channel_1, :chat_channel)
before do
sign_in(user_1)
channel_1.add(user_1)
Jobs.run_immediately!
end
it "has topic context" do
tested_context = {}
blk = Proc.new { |message, channel, user, context| tested_context = context }
begin
DiscourseEvent.on(:chat_message_created, &blk)
topic_page.visit_topic(post_1.topic)
chat_page.open_from_header
drawer_page.open_channel(channel_1)
channel_page.send_message
expect(tested_context.dig(:context, :post_ids)).to eq([post_1.id, post_2.id])
expect(tested_context.dig(:context, :topic_id)).to eq(post_1.topic_id)
ensure
DiscourseEvent.off(:chat_message_created, &blk)
end
end
end
end