Files
sure/test/system/chats_test.rb
Serge L 7f3b12107b fix: resolve flaky chats system test race condition (#1375)
Wait for the chat to fully load after click before triggering a page
refresh, ensuring last_viewed_chat is persisted server-side.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 08:54:25 +02:00

81 lines
1.9 KiB
Ruby

require "application_system_test_case"
class ChatsTest < ApplicationSystemTestCase
setup do
@user = users(:family_admin)
login_as(@user)
end
test "sidebar shows consent if ai is disabled for user" do
@user.update!(ai_enabled: false)
visit root_path
within "#chat-container" do
assert_selector "h3", text: "Enable AI Chats"
end
end
test "sidebar shows index when enabled and chats are empty" do
with_env_overrides OPENAI_ACCESS_TOKEN: "test-token" do
@user.update!(ai_enabled: true)
@user.chats.destroy_all
visit root_url
within "#chat-container" do
assert_selector "h1", text: "Chats"
end
end
end
test "sidebar shows last viewed chat" do
with_env_overrides OPENAI_ACCESS_TOKEN: "test-token" do
@user.update!(ai_enabled: true)
chat_title = @user.chats.first.title
visit root_url
click_on chat_title
# Wait for the chat to actually load before refreshing
within "#chat-container" do
assert_selector "h1", text: chat_title
end
# Page refresh
visit root_url
# After page refresh, we're still on the last chat we were viewing
within "#chat-container" do
assert_selector "h1", text: chat_title
end
end
end
test "create chat and navigate chats sidebar" do
with_env_overrides OPENAI_ACCESS_TOKEN: "test-token" do
@user.chats.destroy_all
visit root_url
Chat.any_instance.expects(:ask_assistant_later).once
within "#chat-form" do
fill_in "chat[content]", with: "Can you help with my finances?"
find("button[type='submit']").click
end
assert_text "Can you help with my finances?"
find("#chat-nav-back").click
assert_selector "h1", text: "Chats"
click_on @user.chats.reload.first.title
assert_text "Can you help with my finances?"
end
end
end