svrnty-mcp-client/tests/Svrnty.MCP.Client.Core.Tests/Infrastructure/StdioServerConnectionTests.cs
Svrnty 97880406dc refactor: rename OpenHarbor.MCP to Svrnty.MCP across all libraries
- Renamed all directories: OpenHarbor.MCP.* → Svrnty.MCP.*
- Updated all namespaces in 179 C# files
- Renamed 20 .csproj files and 3 .sln files
- Updated 193 documentation references
- Updated 33 references in main CODEX codebase
- Updated Codex.sln with new paths
- Build verified: 0 errors

Preparing for extraction to standalone repositories.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-22 21:04:17 -04:00

171 lines
4.3 KiB
C#

using Svrnty.MCP.Client.Core.Abstractions;
using Svrnty.MCP.Client.Core.Exceptions;
using Svrnty.MCP.Client.Core.Models;
using Svrnty.MCP.Client.Infrastructure;
using Xunit;
namespace Svrnty.MCP.Client.Core.Tests.Infrastructure;
/// <summary>
/// Unit tests for StdioServerConnection following TDD approach.
/// Tests connection lifecycle, tool discovery, and tool execution.
/// </summary>
public class StdioServerConnectionTests
{
[Fact]
public void Constructor_WithValidConfig_CreatesConnection()
{
// Arrange
var config = new McpServerConfig
{
Name = "test-server",
Transport = new StdioTransportConfig
{
Type = "Stdio",
Command = "dotnet",
Args = new[] { "run" }
}
};
// Act
var connection = new StdioServerConnection(config);
// Assert
Assert.Equal("test-server", connection.ServerName);
Assert.False(connection.IsConnected);
}
[Fact]
public void ServerName_ReturnsConfiguredName()
{
// Arrange
var config = new McpServerConfig
{
Name = "my-server",
Transport = new StdioTransportConfig
{
Type = "Stdio",
Command = "node",
Args = new[] { "server.js" }
}
};
var connection = new StdioServerConnection(config);
// Act
var serverName = connection.ServerName;
// Assert
Assert.Equal("my-server", serverName);
}
[Fact]
public void IsConnected_BeforeConnect_ReturnsFalse()
{
// Arrange
var config = CreateTestConfig();
var connection = new StdioServerConnection(config);
// Act
var isConnected = connection.IsConnected;
// Assert
Assert.False(isConnected);
}
[Fact]
public async Task ConnectAsync_WithValidConfig_SetsIsConnectedTrue()
{
// Arrange
var config = CreateTestConfig();
var connection = new StdioServerConnection(config);
// Act
await connection.ConnectAsync();
// Assert
Assert.True(connection.IsConnected);
}
[Fact]
public async Task DisconnectAsync_AfterConnect_SetsIsConnectedFalse()
{
// Arrange
var config = CreateTestConfig();
var connection = new StdioServerConnection(config);
await connection.ConnectAsync();
// Act
await connection.DisconnectAsync();
// Assert
Assert.False(connection.IsConnected);
}
[Fact]
public async Task ListToolsAsync_WithoutConnection_ThrowsException()
{
// Arrange
var config = CreateTestConfig();
var connection = new StdioServerConnection(config);
// Act & Assert
await Assert.ThrowsAsync<McpConnectionException>(
async () => await connection.ListToolsAsync()
);
}
[Fact]
public async Task CallToolAsync_WithoutConnection_ThrowsException()
{
// Arrange
var config = CreateTestConfig();
var connection = new StdioServerConnection(config);
// Act & Assert
await Assert.ThrowsAsync<McpConnectionException>(
async () => await connection.CallToolAsync("test_tool")
);
}
[Fact]
public async Task PingAsync_WhenConnected_DoesNotThrow()
{
// Arrange
var config = CreateTestConfig();
var connection = new StdioServerConnection(config);
await connection.ConnectAsync();
// Act & Assert - should not throw
await connection.PingAsync();
}
[Fact]
public async Task DisposeAsync_AfterConnect_DisconnectsCleanly()
{
// Arrange
var config = CreateTestConfig();
var connection = new StdioServerConnection(config);
await connection.ConnectAsync();
// Act
await connection.DisposeAsync();
// Assert
Assert.False(connection.IsConnected);
}
private static McpServerConfig CreateTestConfig()
{
return new McpServerConfig
{
Name = "test-server",
Transport = new StdioTransportConfig
{
Type = "Stdio",
Command = "echo",
Args = new[] { "test" }
}
};
}
}