Files
sure/test/models/chat_test.rb
Guillem Arias 714cf0bbb4 fix(ai): address PR review on Anthropic provider foundation
Surface fixes raised by Codex + CodeRabbit on PR 1/5:

- Provider::Anthropic#chat_response now accepts (and ignores) a
  `messages:` kwarg. Assistant::Responder passes both `messages:`
  (OpenAI-shape) and `conversation_history:` (raw Message records) for
  cross-provider parity, so the previous signature raised
  ArgumentError on the first chat turn through the Anthropic provider.
- Provider::Anthropic#supports_model? bypasses the `claude` prefix
  gate when a custom base_url is configured, mirroring the OpenAI
  provider. Bedrock-shaped IDs like
  `anthropic.claude-sonnet-4-5-20250929-v1:0` and
  `claude-opus-4@20250514` are otherwise rejected by
  Assistant::Provided#get_model_provider and the chat dies.
- Setting.anthropic_access_token is now in
  EncryptedSettingFields::ENCRYPTED_FIELDS so the Anthropic API key
  is encrypted at rest like every other provider secret. Previously
  plaintext while siblings (openai_access_token, twelve_data_api_key,
  external_assistant_token) were ciphertext.
- Chat.default_model falls back to whichever provider is actually
  configured. Previously, with LLM_PROVIDER=anthropic but no
  Anthropic credentials, the default model resolved to a Claude ID
  that no registered provider supported, so chats failed even when
  OpenAI was fully configured. Adds Provider::{Anthropic,Openai}#configured?
  class methods for the readable callsite.
- Provider::Anthropic.effective_model uses
  `ENV["ANTHROPIC_MODEL"].presence || Setting.anthropic_model` so the
  Setting lookup is only performed when the env var is absent — the
  previous `ENV.fetch(KEY, default)` evaluated the default arg
  eagerly on every call.
- Provider::Anthropic::ChatConfig#anthropic_input_schema strips both
  `:strict` and `"strict"` keys so JSON-decoded schemas with string
  keys cannot leak the OpenAI-only flag through to Anthropic.

Test coverage added: supports_model? bypass on custom endpoints,
chat_response messages: kwarg compatibility, default_model fallback
in the three credential combinations, configured? against ENV +
Setting, strict-flag stripping for both key types, and a
`Setting.expects(:anthropic_model).never` assertion proving the
ENV-precedence test now exercises the lazy path.

All 4365 tests pass (1 pre-existing libvips env error unrelated).
2026-05-25 19:49:25 +02:00

150 lines
4.8 KiB
Ruby

require "test_helper"
class ChatTest < ActiveSupport::TestCase
setup do
@user = users(:family_admin)
@assistant = mock
end
test "user sees all messages in debug mode" do
chat = chats(:one)
with_env_overrides AI_DEBUG_MODE: "true" do
assert_equal chat.messages.count, chat.conversation_messages.count
end
end
test "user sees assistant and user messages in normal mode" do
chat = chats(:one)
assert_equal 3, chat.conversation_messages.count
end
test "uses chat-scoped stream targets" do
first_chat = chats(:one)
second_chat = chats(:two)
assert_not_equal "messages", first_chat.messages_target
assert_not_equal "chat-error", first_chat.error_target
assert_not_equal first_chat.messages_target, second_chat.messages_target
assert_not_equal first_chat.error_target, second_chat.error_target
end
test "creates with initial message" do
prompt = "Test prompt"
assert_difference "@user.chats.count", 1 do
chat = @user.chats.start!(prompt, model: "gpt-4.1")
assert_equal 2, chat.messages.count
assert_equal 1, chat.messages.where(type: "UserMessage").count
assert_equal 1, chat.messages.where(type: "AssistantMessage", status: "pending").count
end
end
test "creates with default model when model is nil" do
prompt = "Test prompt"
assert_difference "@user.chats.count", 1 do
chat = @user.chats.start!(prompt, model: nil)
assert_equal 2, chat.messages.count
assert_equal Provider::Openai::DEFAULT_MODEL, chat.messages.find_by!(type: "UserMessage").ai_model
end
end
test "creates with default model when model is empty string" do
prompt = "Test prompt"
assert_difference "@user.chats.count", 1 do
chat = @user.chats.start!(prompt, model: "")
assert_equal 2, chat.messages.count
assert_equal Provider::Openai::DEFAULT_MODEL, chat.messages.find_by!(type: "UserMessage").ai_model
end
end
test "default_model returns claude when LLM_PROVIDER=anthropic and Anthropic is configured" do
Provider::Anthropic.stubs(:configured?).returns(true)
Setting.stubs(:llm_provider).returns("anthropic")
assert_equal Provider::Anthropic::DEFAULT_MODEL, Chat.default_model
end
test "default_model falls back to OpenAI when Anthropic is preferred but unconfigured" do
Provider::Anthropic.stubs(:configured?).returns(false)
Provider::Openai.stubs(:configured?).returns(true)
Setting.stubs(:llm_provider).returns("anthropic")
assert_equal Provider::Openai::DEFAULT_MODEL, Chat.default_model
end
test "default_model uses Anthropic when OpenAI is unconfigured" do
Provider::Anthropic.stubs(:configured?).returns(true)
Provider::Openai.stubs(:configured?).returns(false)
Setting.stubs(:llm_provider).returns("openai")
assert_equal Provider::Anthropic::DEFAULT_MODEL, Chat.default_model
end
test "creates with configured model when OPENAI_MODEL env is set" do
prompt = "Test prompt"
with_env_overrides OPENAI_MODEL: "custom-model" do
chat = @user.chats.start!(prompt, model: "")
assert_equal "custom-model", chat.messages.find_by!(type: "UserMessage").ai_model
end
end
test "returns nil presentable error message when no error is stored" do
chat = chats(:one)
chat.update!(error: nil)
assert_nil chat.presentable_error_message
end
test "surfaces a friendly rate limit error" do
chat = chats(:one)
chat.add_error(StandardError.new("OpenAI API error 429: rate limit exceeded"))
assert_equal I18n.t("chat.errors.rate_limited"), chat.presentable_error_message
assert_match "429", chat.technical_error_message
end
test "surfaces a friendly temporary provider error" do
chat = chats(:one)
chat.add_error(StandardError.new("OpenAI API error 503: service unavailable"))
assert_equal I18n.t("chat.errors.temporarily_unavailable"), chat.presentable_error_message
assert_match "503", chat.technical_error_message
end
test "surfaces a friendly auth configuration error" do
chat = chats(:one)
chat.add_error(StandardError.new("OpenAI API error: invalid api key"))
assert_equal I18n.t("chat.errors.misconfigured"), chat.presentable_error_message
assert_match "invalid api key", chat.technical_error_message
end
test "surfaces a friendly default error for unrecognized errors" do
chat = chats(:one)
chat.add_error(StandardError.new("something totally unknown happened"))
assert_equal I18n.t("chat.errors.default"), chat.presentable_error_message
end
test "falls back to a friendly message for legacy serialized errors" do
chat = chats(:one)
chat.update!(error: "OpenAI API error 429: rate limit exceeded".to_json)
assert_equal I18n.t("chat.errors.rate_limited"), chat.presentable_error_message
assert_equal "OpenAI API error 429: rate limit exceeded", chat.technical_error_message
end
end