fix(messages): handle chat not found during message creation (#2896)

* fix(messages): handle chat not found during message creation

* test(messages): cover missing chat race

---------

Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
This commit is contained in:
sentry[bot]
2026-08-05 00:57:43 +02:00
committed by GitHub
co-authored by sentry[bot] <39604003+sentry[bot]@users.noreply.github.com> sure-admin
parent 705878807b
commit 521946b9d6
3 changed files with 13 additions and 0 deletions
@@ -12,6 +12,15 @@ class MessagesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to chat_path(@chat, thinking: true)
end
test "redirects to chats when message save races with chat deletion" do
UserMessage.any_instance.stubs(:save).raises(ActiveRecord::InvalidForeignKey)
post chat_messages_url(@chat), params: { message: { content: "Hello", ai_model: "gpt-4.1" } }
assert_redirected_to chats_path
assert_equal I18n.t("messages.create.chat_not_found"), flash[:alert]
end
test "cannot create a message if AI is disabled" do
@user.update!(ai_enabled: false)