Files
sure/test/models/provider/anthropic/chat_parser_test.rb
Guillem Arias 17e9e0d937 test(ai): require "ostruct" explicitly in Anthropic provider tests
OpenStruct is moving out of Ruby's default load path (warning in 3.4+,
removed in 3.5+). Tests work today because ActiveSupport transitively
loads it, but that's incidental. Match the existing convention in
test/controllers/settings/hostings_controller_test.rb which explicitly
requires ostruct for the same reason.
2026-05-27 10:08:31 +02:00

86 lines
2.5 KiB
Ruby

require "test_helper"
require "ostruct"
class Provider::Anthropic::ChatParserTest < ActiveSupport::TestCase
test "parses text-only message into ChatResponse with single output_text" do
raw = build_message(
id: "msg_1",
model: "claude-sonnet-4-6",
content: [
OpenStruct.new(type: :text, text: "Hello"),
OpenStruct.new(type: :text, text: "world")
]
)
parsed = Provider::Anthropic::ChatParser.new(raw).parsed
assert_equal "msg_1", parsed.id
assert_equal "claude-sonnet-4-6", parsed.model
assert_equal 1, parsed.messages.size
assert_equal "Hello\nworld", parsed.messages.first.output_text
assert_empty parsed.function_requests
end
test "parses tool_use blocks into ChatFunctionRequest" do
raw = build_message(
id: "msg_2",
model: "claude-sonnet-4-6",
content: [
OpenStruct.new(
type: :tool_use,
id: "toolu_abc",
name: "get_transactions",
input: { "page" => 1, "order" => "asc" }
)
]
)
parsed = Provider::Anthropic::ChatParser.new(raw).parsed
assert_empty parsed.messages
assert_equal 1, parsed.function_requests.size
req = parsed.function_requests.first
assert_equal "toolu_abc", req.id
assert_equal "toolu_abc", req.call_id
assert_equal "get_transactions", req.function_name
assert_equal({ "page" => 1, "order" => "asc" }.to_json, req.function_args)
end
test "parses mixed content blocks" do
raw = build_message(
id: "msg_3",
model: "claude-sonnet-4-6",
content: [
OpenStruct.new(type: :text, text: "Looking up your transactions..."),
OpenStruct.new(type: :tool_use, id: "toolu_42", name: "get_transactions", input: {})
]
)
parsed = Provider::Anthropic::ChatParser.new(raw).parsed
assert_equal 1, parsed.messages.size
assert_equal "Looking up your transactions...", parsed.messages.first.output_text
assert_equal 1, parsed.function_requests.size
assert_equal "toolu_42", parsed.function_requests.first.call_id
end
test "accepts hash-shaped content blocks" do
raw = OpenStruct.new(
id: "msg_4",
model: "claude-sonnet-4-6",
content: [
{ type: :text, text: "from hash" }
]
)
parsed = Provider::Anthropic::ChatParser.new(raw).parsed
assert_equal "from hash", parsed.messages.first.output_text
end
private
def build_message(id:, model:, content:)
OpenStruct.new(id: id, model: model, content: content)
end
end