mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 14:12:10 +08:00

We are not waiting for asynchronous operations to complete before executing our assertions. ### Reviewer notes Example test failure: https://github.com/discourse/discourse/actions/runs/14100970402/job/39496976787 ``` Failure/Error: measurement = Benchmark.measure { example.run } expected `Chat::Channel.count` to have changed by 1, but was changed by 0 [Screenshot Image]: /__w/discourse/discourse/tmp/capybara/failures_r_spec_example_groups_chat_new_message_from_params_with_multiple_users_creates_a_dm_channel_when_none_exists_626.png ~~~~~~~ JS LOGS ~~~~~~~ ~~~~~ END JS LOGS ~~~~~ ./plugins/chat/spec/system/chat_new_message_spec.rb:57:in `block (3 levels) in <main>' ./spec/rails_helper.rb:619:in `block (3 levels) in <top (required)>' /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/benchmark-0.4.0/lib/benchmark.rb:304:in `measure' ./spec/rails_helper.rb:619:in `block (2 levels) in <top (required)>' ./spec/rails_helper.rb:580:in `block (3 levels) in <top (required)>' /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/timeout-0.4.3/lib/timeout.rb:185:in `block in timeout' /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/timeout-0.4.3/lib/timeout.rb:192:in `timeout' ./spec/rails_helper.rb:570:in `block (2 levels) in <top (required)>' ./spec/rails_helper.rb:527:in `block (2 levels) in <top (required)>' /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/webmock-3.25.1/lib/webmock/rspec.rb:39:in `block (2 levels) in <top (required)>' ```
81 lines
2.5 KiB
Ruby
81 lines
2.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe "Chat New Message from params", type: :system do
|
|
fab!(:current_user) { Fabricate(:user) }
|
|
fab!(:user_1) { Fabricate(:user) }
|
|
fab!(:user_2) { Fabricate(:user) }
|
|
fab!(:public_channel) { Fabricate(: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) { Fabricate(: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])
|
|
|
|
try_until_success { 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
|