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

55 lines
1.9 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe "Update last read", type: :system do
fab!(:current_user, :user)
fab!(:channel_1, :chat_channel)
fab!(:messages) { Fabricate.times(15, :chat_message, chat_channel: channel_1) }
let(:chat_page) { PageObjects::Pages::Chat.new }
let(:channel_page) { PageObjects::Pages::ChatChannel.new }
let(:membership) { Chat::ChannelMembershipManager.new(channel_1).find_for_user(current_user) }
before do
chat_system_bootstrap
channel_1.add(current_user)
membership.update!(last_read_message_id: messages.last.id)
sign_in(current_user)
end
context "when the full message is visible" do
it "marks it as read" do
last_message = Fabricate(:chat_message, chat_channel: channel_1)
chat_page.visit_channel(channel_1)
try_until_success(
reason: "Relies on Ember timer to fire off request to update last_read_message_id",
) { expect(membership.reload.last_read_message_id).to eq(last_message.id) }
end
end
context "when receiving a messages" do
it "marks it as read" do
chat_page.visit_channel(channel_1)
last_message = Fabricate(:chat_message, chat_channel: channel_1, use_service: true)
try_until_success(
reason: "Relies on Ember timer to fire off request to update last_read_message_id",
) { expect(membership.reload.last_read_message_id).to eq(last_message.id) }
end
end
context "when user had not previous last read" do
before { membership.update!(last_read_message_id: nil) }
it "marks last message as read" do
last_message = Fabricate(:chat_message, chat_channel: channel_1)
chat_page.visit_channel(channel_1)
try_until_success(
reason: "Relies on Ember timer to fire off request to update last_read_message_id",
) { expect(membership.reload.last_read_message_id).to eq(last_message.id) }
end
end
end