|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "mcp/client/transports/stdio" |
| 5 | +require "json" |
| 6 | + |
| 7 | +module MCP |
| 8 | + class Client |
| 9 | + module Transports |
| 10 | + class StdioTest < ActiveSupport::TestCase |
| 11 | + setup do |
| 12 | + @transport = Stdio.new(timeout: 1) |
| 13 | + end |
| 14 | + |
| 15 | + test "initializes with default timeout" do |
| 16 | + transport = Stdio.new |
| 17 | + assert_equal 30, transport.timeout |
| 18 | + end |
| 19 | + |
| 20 | + test "initializes with custom timeout" do |
| 21 | + transport = Stdio.new(timeout: 10) |
| 22 | + assert_equal 10, transport.timeout |
| 23 | + end |
| 24 | + |
| 25 | + test "sends request and receives response successfully" do |
| 26 | + request = { |
| 27 | + jsonrpc: "2.0", |
| 28 | + method: "ping", |
| 29 | + id: 1, |
| 30 | + } |
| 31 | + |
| 32 | + response = { |
| 33 | + jsonrpc: "2.0", |
| 34 | + id: 1, |
| 35 | + result: {}, |
| 36 | + } |
| 37 | + |
| 38 | + # Mock stdin and stdout |
| 39 | + mock_stdin = StringIO.new(JSON.generate(response) + "\n") |
| 40 | + mock_stdout = StringIO.new |
| 41 | + |
| 42 | + @transport.instance_variable_set(:@stdin, mock_stdin) |
| 43 | + @transport.instance_variable_set(:@stdout, mock_stdout) |
| 44 | + |
| 45 | + result = @transport.send_request(request) |
| 46 | + |
| 47 | + # Verify request was sent to stdout |
| 48 | + assert_equal JSON.generate(request) + "\n", mock_stdout.string |
| 49 | + |
| 50 | + # Verify response was parsed correctly |
| 51 | + assert_equal response, result |
| 52 | + end |
| 53 | + |
| 54 | + test "raises error on timeout" do |
| 55 | + request = { jsonrpc: "2.0", method: "ping", id: 1 } |
| 56 | + |
| 57 | + # Mock stdin that never returns data |
| 58 | + mock_stdin = StringIO.new("") |
| 59 | + mock_stdout = StringIO.new |
| 60 | + |
| 61 | + @transport.instance_variable_set(:@stdin, mock_stdin) |
| 62 | + @transport.instance_variable_set(:@stdout, mock_stdout) |
| 63 | + |
| 64 | + error = assert_raises(Stdio::StdioError) do |
| 65 | + @transport.send_request(request) |
| 66 | + end |
| 67 | + assert_match(/No response received within 1 seconds/, error.message) |
| 68 | + end |
| 69 | + |
| 70 | + test "raises error on invalid JSON response" do |
| 71 | + request = { jsonrpc: "2.0", method: "ping", id: 1 } |
| 72 | + |
| 73 | + # Mock stdin with invalid JSON |
| 74 | + mock_stdin = StringIO.new("invalid json\n") |
| 75 | + mock_stdout = StringIO.new |
| 76 | + |
| 77 | + @transport.instance_variable_set(:@stdin, mock_stdin) |
| 78 | + @transport.instance_variable_set(:@stdout, mock_stdout) |
| 79 | + |
| 80 | + error = assert_raises(Stdio::StdioError) do |
| 81 | + @transport.send_request(request) |
| 82 | + end |
| 83 | + assert_match(/Invalid JSON response/, error.message) |
| 84 | + assert_instance_of JSON::ParserError, error.original_error |
| 85 | + end |
| 86 | + |
| 87 | + test "raises error on broken pipe" do |
| 88 | + request = { jsonrpc: "2.0", method: "ping", id: 1 } |
| 89 | + |
| 90 | + mock_stdout = StringIO.new |
| 91 | + @transport.instance_variable_set(:@stdout, mock_stdout) |
| 92 | + |
| 93 | + # Mock puts to raise EPIPE |
| 94 | + mock_stdout.define_singleton_method(:puts) do |*args| |
| 95 | + raise Errno::EPIPE.new("Broken pipe") |
| 96 | + end |
| 97 | + |
| 98 | + error = assert_raises(Stdio::StdioError) do |
| 99 | + @transport.send_request(request) |
| 100 | + end |
| 101 | + assert_match(/Broken pipe/, error.message) |
| 102 | + assert_instance_of Errno::EPIPE, error.original_error |
| 103 | + end |
| 104 | + |
| 105 | + test "raises error on IO error" do |
| 106 | + request = { jsonrpc: "2.0", method: "ping", id: 1 } |
| 107 | + |
| 108 | + mock_stdout = StringIO.new |
| 109 | + @transport.instance_variable_set(:@stdout, mock_stdout) |
| 110 | + |
| 111 | + # Mock puts to raise IOError |
| 112 | + mock_stdout.define_singleton_method(:puts) do |*args| |
| 113 | + raise IOError.new("IO error") |
| 114 | + end |
| 115 | + |
| 116 | + error = assert_raises(Stdio::StdioError) do |
| 117 | + @transport.send_request(request) |
| 118 | + end |
| 119 | + assert_match(/IO error/, error.message) |
| 120 | + assert_instance_of IOError, error.original_error |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments