Files
sure/test/controllers/api/v1/messages_controller_test.rb
Michal Tajchert ccd6a53071 fix(chat): eager pending AssistantMessage to fix Turbo subscribe race (#1657) (#1658)
* fix(chat): persist eager pending assistant message to fix subscribe race

When the LLM replies in ~1-2s the assistant message broadcast could
fire before the client's Turbo stream subscription was established,
leaving the UI stuck on the thinking indicator while the response was
already persisted.

Create the AssistantMessage as `pending` synchronously in
`Chat#ask_assistant_later`, so it is rendered server-side on the chat
show page with a "Thinking ..." inline placeholder. The worker then
finds and updates the existing row via `append_text!`, which flips the
status to `complete` and broadcasts updates against a DOM id that is
already in the page — no race possible. On error, the placeholder is
destroyed if no content streamed, otherwise demoted to `failed`.

Replaces the standalone thinking indicator partial and the
`Assistant::Broadcastable` thinking helpers, both now redundant.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(chat): bind each assistant job to its specific pending placeholder

Addressing review feedback on #1658:

1. The pending placeholder lookup based on `last pending` was racy —
   back-to-back user messages would let one job fill another job's
   placeholder. Pass the placeholder through the job arguments
   (`AssistantResponseJob.perform_later(user_message, pending)`) so
   each turn is bound to its own row.

2. In `Assistant::External#respond_to`, the configured/authorized
   guards raise before the local was bound, leaving rescue cleanup
   with `nil` and the placeholder visible forever. Bind the parameter
   first so cleanup can destroy it on the misconfigured path.

The kwarg defaults to nil so the API#retry path
(`AssistantResponseJob.perform_later(new_message)`) and the model-level
test calls continue to work — they fall back to an in-memory new
message, restoring the original test count assertions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(chat): i18n the pending assistant placeholder string

Move the hardcoded "Thinking ..." indicator into the locale file per
CLAUDE.md i18n guidelines. With i18n.fallbacks enabled, non-en locales
fall back to English until translated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Add thinking label translations

* Fix chat pending assistant expectations

* Fix external assistant pending test lookup

* Scope chat stream targets per chat

* Update message broadcast target tests

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 20:33:29 +02:00

112 lines
3.2 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class Api::V1::MessagesControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:family_admin)
@user.update!(ai_enabled: true)
@oauth_app = Doorkeeper::Application.create!(
name: "Test API App",
redirect_uri: "https://example.com/callback",
scopes: "read write read_write"
)
@write_token = Doorkeeper::AccessToken.create!(
application: @oauth_app,
resource_owner_id: @user.id,
scopes: "read_write"
)
@chat = chats(:one)
end
test "should require authentication" do
post "/api/v1/chats/#{@chat.id}/messages"
assert_response :unauthorized
end
test "should require AI to be enabled" do
@user.update!(ai_enabled: false)
post "/api/v1/chats/#{@chat.id}/messages",
params: { content: "Hello" },
headers: bearer_auth_header(@write_token)
assert_response :forbidden
end
test "should create message with write scope" do
assert_difference "UserMessage.count" do
post "/api/v1/chats/#{@chat.id}/messages",
params: { content: "Test message", model: "gpt-4" },
headers: bearer_auth_header(@write_token)
end
assert_response :created
response_body = JSON.parse(response.body)
assert_equal "Test message", response_body["content"]
assert_equal "user_message", response_body["type"]
assert_equal "pending", response_body["ai_response_status"]
end
test "should enqueue assistant response job" do
assert_enqueued_with(job: AssistantResponseJob) do
post "/api/v1/chats/#{@chat.id}/messages",
params: { content: "Test message" },
headers: bearer_auth_header(@write_token)
end
end
test "should retry last assistant message" do
skip "Retry functionality needs debugging"
# Create an assistant message to retry
assistant_message = @chat.messages.create!(
type: "AssistantMessage",
content: "Previous response",
ai_model: "gpt-4"
)
assert_enqueued_with(job: AssistantResponseJob) do
post "/api/v1/chats/#{@chat.id}/messages/retry",
headers: bearer_auth_header(@write_token)
end
assert_response :accepted
response_body = JSON.parse(response.body)
assert response_body["message_id"].present?
end
test "should not retry if no assistant message exists" do
# Remove all assistant messages
@chat.messages.where(type: "AssistantMessage").destroy_all
post "/api/v1/chats/#{@chat.id}/messages/retry.json",
headers: bearer_auth_header(@write_token)
assert_response :unprocessable_entity
response_body = JSON.parse(response.body)
assert_equal "No assistant message to retry", response_body["error"]
end
test "should not access messages in other user's chat" do
other_user = users(:family_member)
other_user.update!(family: families(:empty))
other_chat = chats(:two)
other_chat.update!(user: other_user)
post "/api/v1/chats/#{other_chat.id}/messages",
params: { content: "Test" },
headers: bearer_auth_header(@write_token)
assert_response :not_found
end
private
def bearer_auth_header(token)
{ "Authorization" => "Bearer #{token.token}" }
end
end