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

80 lines
2.4 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe "Chat New Message from params", type: :system do
fab!(:current_user, :user)
fab!(:user_1, :user)
fab!(:user_2, :user)
fab!(:public_channel, :chat_channel)
fab!(:user_1_channel) { Fabricate(:direct_message_channel, users: [current_user, user_1]) }
let(:chat_page) { PageObjects::Pages::Chat.new }
before do
chat_system_bootstrap
public_channel.add(current_user)
sign_in(current_user)
end
def group_slug(users)
users.pluck(:username).permutation.map { |u| u.join("-") }.join("|")
end
context "with a single user" do
it "redirects to existing chat channel" do
chat_page.visit_new_message(user_1)
expect(page).to have_current_path("/chat/c/#{user_1.username}/#{user_1_channel.id}")
end
it "creates a dm channel and redirects if none exists" do
chat_page.visit_new_message(user_2)
expect(page).to have_css(".chat-channel-name__label", text: user_2.username)
expect(page).to have_current_path("/chat/c/#{user_2.username}/#{Chat::Channel.last.id}")
end
it "redirects to chat channel if recipients param is missing" do
visit("/chat/new-message")
expect(page).to have_no_current_path("/chat/new-message")
end
end
context "with multiple users" do
fab!(:group_dm) do
Fabricate(:direct_message_channel, users: [current_user, user_1, user_2], group: true)
end
fab!(:user_3, :user)
it "loads existing dm channel when one exists" do
expect { chat_page.visit_new_message([user_1, user_2]) }.not_to change { Chat::Channel.count }
expect(page).to have_current_path(
%r{/chat/c/(#{group_slug([user_1, user_2])})/#{group_dm.id}},
)
end
it "creates a dm channel when none exists" do
original_channel_count = Chat::Channel.count
chat_page.visit_new_message([user_1, user_3])
expect(Chat::Channel.count - original_channel_count).to eq(1)
expect(page).to have_current_path(
%r{/chat/c/#{group_slug([user_1, user_3])}/#{Chat::Channel.last.id}},
)
end
context "when user has chat disabled" do
before { user_3.user_option.update!(chat_enabled: false) }
it "loads channel without the chat disabled user" do
expect { chat_page.visit_new_message([user_1, user_3]) }.not_to change {
Chat::Channel.count
}
expect(page).to have_current_path("/chat/c/#{user_1.username}/#{user_1_channel.id}")
end
end
end
end