ruby-sdk/test/mcp/tool/response_test.rb
2026-07-23 03:11:48 +08:00

147 lines
4.7 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
module MCP
class Tool
class ResponseTest < ActiveSupport::TestCase
test "#initialize with content" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content)
assert_equal content, response.content
refute response.error?
end
test "#content_provided? distinguishes empty content from omitted or nil content" do
assert Response.new([]).content_provided?
refute Response.new.content_provided?
refute Response.new(nil).content_provided?
end
test "#initialize with content and error set to true" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: true)
assert_equal content, response.content
assert response.error?
end
test "#initialize with content and error explicitly set to false" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: false)
assert_equal content, response.content
refute response.error?
end
test "#initialize with content and structuredContent" do
content = [{
type: "text",
text: "{\"code\":401,\"message\":\"Unauthorized\"}",
}]
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(content, structured_content: structured_content)
assert_equal content, response.content
assert_equal structured_content, response.structured_content
refute response.error?
end
test "#error? for a standard response" do
response = Response.new(nil, error: false)
refute response.error?
end
test "#error? for an error response" do
response = Response.new(nil, error: true)
assert response.error?
end
test "#to_h for a standard response" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content)
actual = response.to_h
assert_equal [:content, :isError], actual.keys
assert_equal content, actual[:content]
refute actual[:isError]
end
test "#to_h for an error response" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: true)
actual = response.to_h
assert_equal [:content, :isError], actual.keys
assert_equal content, actual[:content]
assert actual[:isError]
end
test "#to_h for a standard response with content and structured content" do
content = [{
type: "text",
text: "{\"code\":401,\"message\":\"Unauthorized\"}",
}]
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(content, structured_content: structured_content)
actual = response.to_h
assert_equal [:content, :isError, :structuredContent], actual.keys
assert_equal content, actual[:content]
assert_equal structured_content, actual[:structuredContent]
refute actual[:isError]
end
test "#to_h for a standard response with nil content and structured content" do
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(nil, structured_content: structured_content)
actual = response.to_h
assert_equal [:content, :isError, :structuredContent], actual.keys
assert_equal([], actual[:content])
assert_equal structured_content, actual[:structuredContent]
refute actual[:isError]
end
test "#to_h for a standard response with structured content only" do
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(structured_content: structured_content)
actual = response.to_h
assert_equal [:content, :isError, :structuredContent], actual.keys
assert_equal([], actual[:content])
assert_equal structured_content, actual[:structuredContent]
refute actual[:isError]
end
test "#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
response = Response.new([{ type: "text", text: "ok" }], meta: meta)
actual = response.to_h
assert_equal meta, actual[:_meta]
end
test "#to_h omits _meta when nil" do
response = Response.new([{ type: "text", text: "ok" }])
refute response.to_h.key?(:_meta)
end
end
end
end