DEV: improves reliability of delete/restore/update specs (#24265)

This commit is contained in:
Joffrey JAFFEUX 2023-11-07 11:34:35 +01:00 committed by GitHub
parent a0b94dca16
commit 039d060832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 48 additions and 45 deletions

View File

@ -44,7 +44,7 @@ module Chat
step :process_direct_message_channel
end
step :publish_new_thread
step :publish_new_message_events
step :process
step :publish_user_tracking_state
class Contract
@ -175,17 +175,15 @@ module Chat
Chat::Publisher.publish_thread_created!(channel, reply, thread.id)
end
def publish_new_message_events(channel:, message_instance:, contract:, **)
staged_id = contract.staged_id
def process(channel:, message_instance:, contract:, **)
if contract.process_inline
Jobs::Chat::ProcessMessage.new.execute(
{ chat_message_id: message_instance.id, staged_id: staged_id },
{ chat_message_id: message_instance.id, staged_id: contract.staged_id },
)
else
Jobs.enqueue(
Jobs::Chat::ProcessMessage,
{ chat_message_id: message_instance.id, staged_id: staged_id },
{ chat_message_id: message_instance.id, staged_id: contract.staged_id },
)
end
end

View File

@ -26,7 +26,7 @@ export default class ChatMessage {
@tracked selected;
@tracked channel;
@tracked staged;
@tracked processed;
@tracked processed = true;
@tracked draftSaved;
@tracked draft;
@tracked createdAt;

View File

@ -51,21 +51,14 @@ RSpec.describe "Chat::Thread replies_count cache accuracy" do
# Lose the cache intentionally.
Chat::Thread.clear_caches!(thread.id)
message_to_destroy = thread.last_message
Chat::TrashMessage.call(
message_id: message_to_destroy.id,
channel_id: thread.channel_id,
guardian: guardian,
)
trash_message!(message_to_destroy, user: guardian.user)
expect(thread.replies_count_cache).to eq(5)
expect(thread.reload.replies_count).to eq(5)
# Lose the cache intentionally.
Chat::Thread.clear_caches!(thread.id)
Chat::RestoreMessage.call(
message_id: message_to_destroy.id,
channel_id: thread.channel_id,
guardian: guardian,
)
restore_message!(message_to_destroy, user: guardian.user)
expect(thread.replies_count_cache).to eq(6)
expect(thread.reload.replies_count).to eq(6)
end

View File

@ -510,7 +510,7 @@ describe Chat::Message do
it "keeps the same hashtags the user has permission to after rebake" do
group.add(chat_message.user)
update_message(
update_message!(
chat_message,
user: chat_message.user,
text:
@ -538,7 +538,7 @@ describe Chat::Message do
it "creates newly added mentions" do
existing_mention_ids = message.chat_mentions.pluck(:id)
update_message(
update_message!(
message,
user: message.user,
text: message.message + " @#{user3.username} @#{user4.username} ",
@ -552,14 +552,14 @@ describe Chat::Message do
it "drops removed mentions" do
# user 2 is not mentioned anymore
update_message(message, user: message.user, text: "Hey @#{user1.username}")
update_message!(message, user: message.user, text: "Hey @#{user1.username}")
expect(message.chat_mentions.pluck(:user_id)).to contain_exactly(user1.id)
end
it "changes nothing if passed mentions are identical to existing mentions" do
existing_mention_ids = message.chat_mentions.pluck(:id)
update_message(message, user: message.user, text: message.message)
update_message!(message, user: message.user, text: message.message)
expect(message.chat_mentions.pluck(:user_id)).to match_array(already_mentioned)
expect(message.chat_mentions.pluck(:id)).to include(*existing_mention_ids) # the mentions weren't recreated

View File

@ -68,7 +68,7 @@ module ChatSpecHelpers
)
end
def update_message(message, text: nil, user: Discourse.system_user, upload_ids: nil)
def update_message!(message, text: nil, user: Discourse.system_user, upload_ids: nil)
result =
Chat::UpdateMessage.call(
guardian: user.guardian,
@ -80,6 +80,28 @@ module ChatSpecHelpers
service_failed!(result) if result.failure?
result.message_instance
end
def trash_message!(message, user: Discourse.system_user)
result =
Chat::TrashMessage.call(
message_id: message.id,
channel_id: message.chat_channel_id,
guardian: user.guardian,
)
service_failed!(result) if result.failure?
result
end
def restore_message!(message, user: Discourse.system_user)
result =
Chat::RestoreMessage.call(
message_id: message.id,
channel_id: message.chat_channel_id,
guardian: user.guardian,
)
service_failed!(result) if result.failure?
result
end
end
RSpec.configure do |config|

View File

@ -120,7 +120,7 @@ RSpec.describe Chat::Api::ChannelThreadsController do
end
it "has preloaded chat mentions and users for the thread original message" do
update_message(
update_message!(
thread_1.original_message,
user: thread_1.original_message.user,
text: "@#{current_user.username} hello and @#{thread_2.original_message_user.username}!",

View File

@ -277,7 +277,7 @@ RSpec.describe "Chat channel", type: :system do
end
it "renders safe HTML like mentions (which are just links) in the reply-to" do
update_message(
update_message!(
message_2,
user: other_user,
text: "@#{other_user.username} <mark>not marked</mark>",

View File

@ -83,10 +83,10 @@ RSpec.describe "Deleted message", type: :system do
it "groups them" do
chat_page.visit_channel(channel_1)
channel_page.messages.delete(message_1)
channel_page.messages.delete(message_3)
channel_page.messages.delete(message_4)
channel_page.messages.delete(message_6)
trash_message!(message_1)
trash_message!(message_3)
trash_message!(message_4)
trash_message!(message_6)
expect(channel_page.messages).to have_deleted_messages(message_1, message_6)
expect(channel_page.messages).to have_deleted_message(message_4, count: 2)

View File

@ -124,7 +124,7 @@ describe "Thread indicator for chat messages", type: :system do
end
it "shows an excerpt of the last reply in the thread" do
update_message(
update_message!(
thread_1.original_message,
user: thread_1.original_message.user,
text: "test for excerpt",
@ -141,7 +141,7 @@ describe "Thread indicator for chat messages", type: :system do
new_user = Fabricate(:user)
chat_system_user_bootstrap(user: new_user, channel: channel)
original_last_reply = thread_1.last_message
update_message(original_last_reply, user: original_last_reply.user, text: "test for excerpt")
update_message!(original_last_reply, user: original_last_reply.user, text: "test for excerpt")
chat_page.visit_channel(channel)

View File

@ -96,7 +96,7 @@ module PageObjects
if args[:not_processed]
selector += ".-not-processed"
else
selector += ".-processed"
selector += ".-processed.-persisted"
end
selector += "[data-id=\"#{args[:id]}\"]" if args[:id]

View File

@ -99,7 +99,7 @@ RSpec.describe "Reply to message - channel - full page", type: :system do
it "renders safe HTML from the original message excerpt" do
other_user = Fabricate(:user)
update_message(
update_message!(
original_message,
user: current_user,
text: "@#{other_user.username} <mark>not marked</mark>",

View File

@ -90,7 +90,7 @@ describe "Thread list in side panel | full page", type: :system do
end
it "shows an excerpt of the original message of the thread", inline: true do
update_message(
update_message!(
thread_1.original_message,
user: thread_1.original_message.user,
text: "This is a long message for the excerpt",
@ -157,11 +157,7 @@ describe "Thread list in side panel | full page", type: :system do
expect(thread_list_page).to have_thread(thread_1)
Chat::TrashMessage.call(
message_id: thread_1.original_message.id,
channel_id: thread_1.channel.id,
guardian: other_user.guardian,
)
trash_message!(thread_1.original_message, user: other_user)
expect(thread_list_page).to have_no_thread(thread_1)
end

View File

@ -51,13 +51,7 @@ describe "Thread preview", type: :system do
context "when message has thread with deleted original message" do
fab!(:thread_1) { Fabricate(:chat_thread, channel: channel_1, original_message: message_1) }
before do
Chat::TrashMessage.call(
message_id: message_1.id,
channel_id: channel_1.id,
guardian: message_1.user.guardian,
)
end
before { trash_message!(message_1, user: message_1.user) }
it "shows preview" do
chat_page.visit_channel(channel_1)