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

View File

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

View File

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

View File

@ -510,7 +510,7 @@ describe Chat::Message do
it "keeps the same hashtags the user has permission to after rebake" do it "keeps the same hashtags the user has permission to after rebake" do
group.add(chat_message.user) group.add(chat_message.user)
update_message( update_message!(
chat_message, chat_message,
user: chat_message.user, user: chat_message.user,
text: text:
@ -538,7 +538,7 @@ describe Chat::Message do
it "creates newly added mentions" do it "creates newly added mentions" do
existing_mention_ids = message.chat_mentions.pluck(:id) existing_mention_ids = message.chat_mentions.pluck(:id)
update_message( update_message!(
message, message,
user: message.user, user: message.user,
text: message.message + " @#{user3.username} @#{user4.username} ", text: message.message + " @#{user3.username} @#{user4.username} ",
@ -552,14 +552,14 @@ describe Chat::Message do
it "drops removed mentions" do it "drops removed mentions" do
# user 2 is not mentioned anymore # 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) expect(message.chat_mentions.pluck(:user_id)).to contain_exactly(user1.id)
end end
it "changes nothing if passed mentions are identical to existing mentions" do it "changes nothing if passed mentions are identical to existing mentions" do
existing_mention_ids = message.chat_mentions.pluck(:id) 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(:user_id)).to match_array(already_mentioned)
expect(message.chat_mentions.pluck(:id)).to include(*existing_mention_ids) # the mentions weren't recreated 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 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 = result =
Chat::UpdateMessage.call( Chat::UpdateMessage.call(
guardian: user.guardian, guardian: user.guardian,
@ -80,6 +80,28 @@ module ChatSpecHelpers
service_failed!(result) if result.failure? service_failed!(result) if result.failure?
result.message_instance result.message_instance
end 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 end
RSpec.configure do |config| RSpec.configure do |config|

View File

@ -120,7 +120,7 @@ RSpec.describe Chat::Api::ChannelThreadsController do
end end
it "has preloaded chat mentions and users for the thread original message" do it "has preloaded chat mentions and users for the thread original message" do
update_message( update_message!(
thread_1.original_message, thread_1.original_message,
user: thread_1.original_message.user, user: thread_1.original_message.user,
text: "@#{current_user.username} hello and @#{thread_2.original_message_user.username}!", 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 end
it "renders safe HTML like mentions (which are just links) in the reply-to" do it "renders safe HTML like mentions (which are just links) in the reply-to" do
update_message( update_message!(
message_2, message_2,
user: other_user, user: other_user,
text: "@#{other_user.username} <mark>not marked</mark>", 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 it "groups them" do
chat_page.visit_channel(channel_1) chat_page.visit_channel(channel_1)
channel_page.messages.delete(message_1) trash_message!(message_1)
channel_page.messages.delete(message_3) trash_message!(message_3)
channel_page.messages.delete(message_4) trash_message!(message_4)
channel_page.messages.delete(message_6) trash_message!(message_6)
expect(channel_page.messages).to have_deleted_messages(message_1, 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) 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 end
it "shows an excerpt of the last reply in the thread" do it "shows an excerpt of the last reply in the thread" do
update_message( update_message!(
thread_1.original_message, thread_1.original_message,
user: thread_1.original_message.user, user: thread_1.original_message.user,
text: "test for excerpt", text: "test for excerpt",
@ -141,7 +141,7 @@ describe "Thread indicator for chat messages", type: :system do
new_user = Fabricate(:user) new_user = Fabricate(:user)
chat_system_user_bootstrap(user: new_user, channel: channel) chat_system_user_bootstrap(user: new_user, channel: channel)
original_last_reply = thread_1.last_message 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) chat_page.visit_channel(channel)

View File

@ -96,7 +96,7 @@ module PageObjects
if args[:not_processed] if args[:not_processed]
selector += ".-not-processed" selector += ".-not-processed"
else else
selector += ".-processed" selector += ".-processed.-persisted"
end end
selector += "[data-id=\"#{args[:id]}\"]" if args[:id] 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 it "renders safe HTML from the original message excerpt" do
other_user = Fabricate(:user) other_user = Fabricate(:user)
update_message( update_message!(
original_message, original_message,
user: current_user, user: current_user,
text: "@#{other_user.username} <mark>not marked</mark>", 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 end
it "shows an excerpt of the original message of the thread", inline: true do it "shows an excerpt of the original message of the thread", inline: true do
update_message( update_message!(
thread_1.original_message, thread_1.original_message,
user: thread_1.original_message.user, user: thread_1.original_message.user,
text: "This is a long message for the excerpt", 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) expect(thread_list_page).to have_thread(thread_1)
Chat::TrashMessage.call( trash_message!(thread_1.original_message, user: other_user)
message_id: thread_1.original_message.id,
channel_id: thread_1.channel.id,
guardian: other_user.guardian,
)
expect(thread_list_page).to have_no_thread(thread_1) expect(thread_list_page).to have_no_thread(thread_1)
end end

View File

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