|
118 | 118 | end |
119 | 119 | end |
120 | 120 |
|
| 121 | + describe "#establish_connection" do |
| 122 | + let(:client_streamable_http) { instance_double(RubyLLM::MCP::Client) } |
| 123 | + let(:client_stdio) { instance_double(RubyLLM::MCP::Client) } |
| 124 | + let(:clients) { [client_streamable_http, client_stdio] } |
| 125 | + |
| 126 | + before do |
| 127 | + allow(RubyLLM::MCP).to receive(:clients).and_return(clients) |
| 128 | + allow(client_streamable_http).to receive(:start) |
| 129 | + allow(client_stdio).to receive(:start) |
| 130 | + allow(client_streamable_http).to receive(:alive?).and_return(true) |
| 131 | + allow(client_stdio).to receive(:alive?).and_return(true) |
| 132 | + allow(client_streamable_http).to receive(:stop) |
| 133 | + allow(client_stdio).to receive(:stop) |
| 134 | + end |
| 135 | + |
| 136 | + it "starts all clients" do |
| 137 | + RubyLLM::MCP.establish_connection |
| 138 | + |
| 139 | + expect(client_streamable_http).to have_received(:start) |
| 140 | + expect(client_stdio).to have_received(:start) |
| 141 | + end |
| 142 | + |
| 143 | + it "returns clients when no block given" do |
| 144 | + result = RubyLLM::MCP.establish_connection |
| 145 | + |
| 146 | + expect(result).to eq(clients) |
| 147 | + end |
| 148 | + |
| 149 | + context "when block is given" do |
| 150 | + it "yields clients to the block" do |
| 151 | + yielded_clients = nil |
| 152 | + RubyLLM::MCP.establish_connection do |c| |
| 153 | + yielded_clients = c |
| 154 | + end |
| 155 | + |
| 156 | + expect(yielded_clients).to eq(clients) |
| 157 | + end |
| 158 | + |
| 159 | + it "calls close_connection after the block executes" do |
| 160 | + RubyLLM::MCP.establish_connection { |_c| "test" } |
| 161 | + |
| 162 | + expect(client_streamable_http).to have_received(:stop) |
| 163 | + expect(client_stdio).to have_received(:stop) |
| 164 | + end |
| 165 | + |
| 166 | + it "calls close_connection even if block raises an exception" do |
| 167 | + expect do |
| 168 | + RubyLLM::MCP.establish_connection { |_c| raise "test error" } |
| 169 | + end.to raise_error("test error") |
| 170 | + |
| 171 | + expect(client_streamable_http).to have_received(:stop) |
| 172 | + expect(client_stdio).to have_received(:stop) |
| 173 | + end |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | + describe "#close_connection" do |
| 178 | + let(:alive_client) { instance_double(RubyLLM::MCP::Client) } |
| 179 | + let(:dead_client) { instance_double(RubyLLM::MCP::Client) } |
| 180 | + let(:clients) { [alive_client, dead_client] } |
| 181 | + |
| 182 | + before do |
| 183 | + allow(RubyLLM::MCP).to receive(:clients).and_return(clients) |
| 184 | + allow(alive_client).to receive(:alive?).and_return(true) |
| 185 | + allow(dead_client).to receive(:alive?).and_return(false) |
| 186 | + allow(alive_client).to receive(:stop) |
| 187 | + allow(dead_client).to receive(:stop) |
| 188 | + end |
| 189 | + |
| 190 | + it "stops all alive clients" do |
| 191 | + RubyLLM::MCP.close_connection |
| 192 | + |
| 193 | + expect(alive_client).to have_received(:stop) |
| 194 | + end |
| 195 | + |
| 196 | + it "does not stop clients that are not alive" do |
| 197 | + RubyLLM::MCP.close_connection |
| 198 | + |
| 199 | + expect(dead_client).not_to have_received(:stop) |
| 200 | + end |
| 201 | + |
| 202 | + it "checks alive status of all clients" do |
| 203 | + RubyLLM::MCP.close_connection |
| 204 | + |
| 205 | + expect(alive_client).to have_received(:alive?) |
| 206 | + expect(dead_client).to have_received(:alive?) |
| 207 | + end |
| 208 | + end |
| 209 | + |
121 | 210 | describe "#tools" do |
122 | 211 | let(:add) { instance_double(RubyLLM::MCP::Tool, name: "add") } |
123 | 212 | let(:sub) { instance_double(RubyLLM::MCP::Tool, name: "sub") } |
|
0 commit comments