discourse/plugins/chat/spec/system/chat_message_interaction_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

59 lines
1.5 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe "Interacting with a message", type: :system do
fab!(:current_user, :user)
fab!(:channel_1, :chat_channel)
fab!(:message_1) do
Fabricate(
:chat_message,
user: Discourse.system_user,
chat_channel: channel_1,
blocks: [
{
type: "actions",
elements: [
{ value: "foo value", type: "button", text: { type: "plain_text", text: "Click Me" } },
],
},
],
)
end
let(:chat_page) { PageObjects::Pages::Chat.new }
let(:chat_channel_page) { PageObjects::Pages::ChatChannel.new }
before do
chat_system_bootstrap
channel_1.add(current_user)
sign_in(current_user)
end
it "creates an interaction" do
action_id = nil
blk =
Proc.new do |interaction|
action_id = interaction.action["action_id"]
Chat::CreateMessage.call(
params: {
message: "#{action_id}: #{interaction.action["value"]}",
chat_channel_id: channel_1.id,
},
guardian: current_user.guardian,
)
end
chat_page.visit_channel(channel_1)
begin
DiscourseEvent.on(:chat_message_interaction, &blk)
find(".block__button").click
try_until_success(reason: "Relies on an Ember timer causing a delay") do
expect(action_id).to_not be_nil
expect(chat_channel_page.messages).to have_text(action_id)
end
ensure
DiscourseEvent.off(:chat_message_interaction, &blk)
end
end
end