|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module MCP |
| 6 | + class Tool |
| 7 | + class ResponseTest < ActiveSupport::TestCase |
| 8 | + test "#initialize with content" do |
| 9 | + content = [{ |
| 10 | + type: "text", |
| 11 | + text: "Unauthorized", |
| 12 | + }] |
| 13 | + response = Response.new(content) |
| 14 | + |
| 15 | + assert_equal content, response.content |
| 16 | + refute response.error? |
| 17 | + end |
| 18 | + |
| 19 | + test "#initialize with content and error set to true" do |
| 20 | + content = [{ |
| 21 | + type: "text", |
| 22 | + text: "Unauthorized", |
| 23 | + }] |
| 24 | + response = Response.new(content, error: true) |
| 25 | + |
| 26 | + assert_equal content, response.content |
| 27 | + assert response.error? |
| 28 | + end |
| 29 | + |
| 30 | + test "#initialize with content and error explicitly set to false" do |
| 31 | + content = [{ |
| 32 | + type: "text", |
| 33 | + text: "Unauthorized", |
| 34 | + }] |
| 35 | + response = Response.new(content, error: false) |
| 36 | + |
| 37 | + assert_equal content, response.content |
| 38 | + refute response.error? |
| 39 | + end |
| 40 | + |
| 41 | + test "#error? for a standard response" do |
| 42 | + response = Response.new(nil, error: false) |
| 43 | + refute response.error? |
| 44 | + end |
| 45 | + |
| 46 | + test "#error? for an error response" do |
| 47 | + response = Response.new(nil, error: true) |
| 48 | + assert response.error? |
| 49 | + end |
| 50 | + |
| 51 | + test "#to_h for a standard response" do |
| 52 | + content = [{ |
| 53 | + type: "text", |
| 54 | + text: "Unauthorized", |
| 55 | + }] |
| 56 | + response = Response.new(content) |
| 57 | + actual = response.to_h |
| 58 | + |
| 59 | + assert_equal [:content, :isError].sort, actual.keys.sort |
| 60 | + assert_equal content, actual[:content] |
| 61 | + refute actual[:isError] |
| 62 | + end |
| 63 | + |
| 64 | + test "#to_h for an error response" do |
| 65 | + content = [{ |
| 66 | + type: "text", |
| 67 | + text: "Unauthorized", |
| 68 | + }] |
| 69 | + response = Response.new(content, error: true) |
| 70 | + actual = response.to_h |
| 71 | + assert_equal [:content, :isError].sort, actual.keys.sort |
| 72 | + assert_equal content, actual[:content] |
| 73 | + assert actual[:isError] |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments