[UnitTests] Add more UTs (#139)
* + Tests for McpClientExtensions code * + code coverage for ClientOptions/Capabilities code --------- Co-authored-by: Esteban Solano <estebanso@microsoft.com> Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
parent
f652b1a03d
commit
c70dde3feb
1 changed files with 252 additions and 2 deletions
|
|
@ -1,9 +1,12 @@
|
|||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Protocol.Messages;
|
||||
using ModelContextProtocol.Protocol.Transport;
|
||||
using ModelContextProtocol.Protocol.Types;
|
||||
using ModelContextProtocol.Server;
|
||||
using ModelContextProtocol.Tests.Utils;
|
||||
using Moq;
|
||||
using System.IO.Pipelines;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
|
|
@ -40,6 +43,180 @@ public class McpClientExtensionsTests : LoggedTest
|
|||
_serverTask = server.RunAsync(cancellationToken: _cts.Token);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData(0.7f, 50)]
|
||||
[InlineData(1.0f, 100)]
|
||||
public async Task CreateSamplingHandler_ShouldHandleTextMessages(float? temperature, int? maxTokens)
|
||||
{
|
||||
// Arrange
|
||||
var mockChatClient = new Mock<IChatClient>();
|
||||
var requestParams = new CreateMessageRequestParams
|
||||
{
|
||||
Messages =
|
||||
[
|
||||
new SamplingMessage
|
||||
{
|
||||
Role = Role.User,
|
||||
Content = new Content { Type = "text", Text = "Hello" }
|
||||
}
|
||||
],
|
||||
Temperature = temperature,
|
||||
MaxTokens = maxTokens,
|
||||
Meta = new RequestParamsMetadata
|
||||
{
|
||||
ProgressToken = new ProgressToken(),
|
||||
}
|
||||
};
|
||||
|
||||
var cancellationToken = CancellationToken.None;
|
||||
var expectedResponse = new[] {
|
||||
new ChatResponseUpdate
|
||||
{
|
||||
ModelId = "test-model",
|
||||
FinishReason = ChatFinishReason.Stop,
|
||||
Role = ChatRole.Assistant,
|
||||
Contents =
|
||||
[
|
||||
new TextContent("Hello, World!") { RawRepresentation = "Hello, World!" }
|
||||
]
|
||||
}
|
||||
}.ToAsyncEnumerable();
|
||||
|
||||
mockChatClient
|
||||
.Setup(client => client.GetStreamingResponseAsync(It.IsAny<IEnumerable<ChatMessage>>(), It.IsAny<ChatOptions>(), cancellationToken))
|
||||
.Returns(expectedResponse);
|
||||
|
||||
var handler = McpClientExtensions.CreateSamplingHandler(mockChatClient.Object);
|
||||
|
||||
// Act
|
||||
var result = await handler(requestParams, Mock.Of<IProgress<ProgressNotificationValue>>(), cancellationToken);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("Hello, World!", result.Content.Text);
|
||||
Assert.Equal("test-model", result.Model);
|
||||
Assert.Equal("assistant", result.Role);
|
||||
Assert.Equal("endTurn", result.StopReason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateSamplingHandler_ShouldHandleImageMessages()
|
||||
{
|
||||
// Arrange
|
||||
var mockChatClient = new Mock<IChatClient>();
|
||||
var requestParams = new CreateMessageRequestParams
|
||||
{
|
||||
Messages =
|
||||
[
|
||||
new SamplingMessage
|
||||
{
|
||||
Role = Role.User,
|
||||
Content = new Content
|
||||
{
|
||||
Type = "image",
|
||||
MimeType = "image/png",
|
||||
Data = Convert.ToBase64String(new byte[] { 1, 2, 3 })
|
||||
}
|
||||
}
|
||||
],
|
||||
MaxTokens = 100
|
||||
};
|
||||
|
||||
const string expectedData = "SGVsbG8sIFdvcmxkIQ==";
|
||||
var cancellationToken = CancellationToken.None;
|
||||
var expectedResponse = new[] {
|
||||
new ChatResponseUpdate
|
||||
{
|
||||
ModelId = "test-model",
|
||||
FinishReason = ChatFinishReason.Stop,
|
||||
Role = ChatRole.Assistant,
|
||||
Contents =
|
||||
[
|
||||
new DataContent($"data:image/png;base64,{expectedData}") { RawRepresentation = "Hello, World!" }
|
||||
]
|
||||
}
|
||||
}.ToAsyncEnumerable();
|
||||
|
||||
mockChatClient
|
||||
.Setup(client => client.GetStreamingResponseAsync(It.IsAny<IEnumerable<ChatMessage>>(), It.IsAny<ChatOptions>(), cancellationToken))
|
||||
.Returns(expectedResponse);
|
||||
|
||||
var handler = McpClientExtensions.CreateSamplingHandler(mockChatClient.Object);
|
||||
|
||||
// Act
|
||||
var result = await handler(requestParams, Mock.Of<IProgress<ProgressNotificationValue>>(), cancellationToken);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(expectedData, result.Content.Data);
|
||||
Assert.Equal("test-model", result.Model);
|
||||
Assert.Equal("assistant", result.Role);
|
||||
Assert.Equal("endTurn", result.StopReason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateSamplingHandler_ShouldHandleResourceMessages()
|
||||
{
|
||||
// Arrange
|
||||
const string data = "SGVsbG8sIFdvcmxkIQ==";
|
||||
string content = $"data:application/octet-stream;base64,{data}";
|
||||
var mockChatClient = new Mock<IChatClient>();
|
||||
var resource = new BlobResourceContents
|
||||
{
|
||||
Blob = data,
|
||||
MimeType = "application/octet-stream",
|
||||
Uri = "data:application/octet-stream"
|
||||
};
|
||||
|
||||
var requestParams = new CreateMessageRequestParams
|
||||
{
|
||||
Messages =
|
||||
[
|
||||
new SamplingMessage
|
||||
{
|
||||
Role = Role.User,
|
||||
Content = new Content
|
||||
{
|
||||
Type = "resource",
|
||||
Resource = resource
|
||||
},
|
||||
}
|
||||
],
|
||||
MaxTokens = 100
|
||||
};
|
||||
|
||||
var cancellationToken = CancellationToken.None;
|
||||
var expectedResponse = new[] {
|
||||
new ChatResponseUpdate
|
||||
{
|
||||
ModelId = "test-model",
|
||||
FinishReason = ChatFinishReason.Stop,
|
||||
AuthorName = "bot",
|
||||
Role = ChatRole.Assistant,
|
||||
Contents =
|
||||
[
|
||||
resource.ToAIContent()
|
||||
]
|
||||
}
|
||||
}.ToAsyncEnumerable();
|
||||
|
||||
mockChatClient
|
||||
.Setup(client => client.GetStreamingResponseAsync(It.IsAny<IEnumerable<ChatMessage>>(), It.IsAny<ChatOptions>(), cancellationToken))
|
||||
.Returns(expectedResponse);
|
||||
|
||||
var handler = McpClientExtensions.CreateSamplingHandler(mockChatClient.Object);
|
||||
|
||||
// Act
|
||||
var result = await handler(requestParams, Mock.Of<IProgress<ProgressNotificationValue>>(), cancellationToken);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("test-model", result.Model);
|
||||
Assert.Equal(ChatRole.Assistant.ToString(), result.Role);
|
||||
Assert.Equal("endTurn", result.StopReason);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _cts.CancelAsync();
|
||||
|
|
|
|||
Loading…
Reference in a new issue