|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module MCP |
| 6 | + class Resource |
| 7 | + class ContentsTest < ActiveSupport::TestCase |
| 8 | + test "Contents#to_h returns hash with mimeType" do |
| 9 | + contents = Resource::Contents.new( |
| 10 | + uri: "test://example", |
| 11 | + mime_type: "text/plain", |
| 12 | + ) |
| 13 | + |
| 14 | + result = contents.to_h |
| 15 | + |
| 16 | + assert_equal "test://example", result[:uri] |
| 17 | + assert_equal "text/plain", result[:mimeType] |
| 18 | + refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'" |
| 19 | + end |
| 20 | + |
| 21 | + test "Contents#to_h omits mimeType when nil" do |
| 22 | + contents = Resource::Contents.new(uri: "test://example") |
| 23 | + |
| 24 | + result = contents.to_h |
| 25 | + |
| 26 | + assert_equal({ uri: "test://example" }, result) |
| 27 | + refute result.key?(:mimeType) |
| 28 | + end |
| 29 | + |
| 30 | + test "TextContents#to_h returns hash with text and mimeType" do |
| 31 | + text_contents = Resource::TextContents.new( |
| 32 | + uri: "test://text", |
| 33 | + mime_type: "text/plain", |
| 34 | + text: "Hello, world!", |
| 35 | + ) |
| 36 | + |
| 37 | + result = text_contents.to_h |
| 38 | + |
| 39 | + assert_equal "test://text", result[:uri] |
| 40 | + assert_equal "text/plain", result[:mimeType] |
| 41 | + assert_equal "Hello, world!", result[:text] |
| 42 | + refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'" |
| 43 | + end |
| 44 | + |
| 45 | + test "BlobContents#to_h returns hash with blob and mimeType" do |
| 46 | + blob_contents = Resource::BlobContents.new( |
| 47 | + uri: "test://binary", |
| 48 | + mime_type: "image/png", |
| 49 | + data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", |
| 50 | + ) |
| 51 | + |
| 52 | + result = blob_contents.to_h |
| 53 | + |
| 54 | + assert_equal "test://binary", result[:uri] |
| 55 | + assert_equal "image/png", result[:mimeType] |
| 56 | + assert_equal "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", result[:blob] |
| 57 | + refute result.key?(:data), "Should use 'blob' not 'data' per MCP specification" |
| 58 | + refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'" |
| 59 | + end |
| 60 | + |
| 61 | + test "BlobContents#to_h omits mimeType when nil" do |
| 62 | + blob_contents = Resource::BlobContents.new( |
| 63 | + uri: "test://binary", |
| 64 | + mime_type: nil, |
| 65 | + data: "base64data", |
| 66 | + ) |
| 67 | + |
| 68 | + result = blob_contents.to_h |
| 69 | + |
| 70 | + assert_equal({ uri: "test://binary", blob: "base64data" }, result) |
| 71 | + refute result.key?(:mimeType) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | +end |
0 commit comments