csharp-sdk/tests/McpDotNet.Extensions.AI.Tests/AIFunctionUtilitiesTests.cs
Toni Wenzel 709b2e0a74
Fix some code smells (#39)
* Fix some code smells

* Fix build

* Fix come issues

* Revert some changes
2025-03-10 18:03:47 +01:00

95 lines
No EOL
3.4 KiB
C#

using System.Text.Json;
using McpDotNet.Protocol.Types;
namespace McpDotNet.Extensions.AI.Tests;
public class AIFunctionUtilitiesTests
{
[Fact]
public void MapToJsonElement_WithRequiredProperties_CreatesCorrectSchema()
{
// Arrange
var tool = new Tool()
{
Name = "calculator",
Description = "A simple calculator",
InputSchema = new JsonSchema
{
Properties = new Dictionary<string, JsonSchemaProperty>
{
["a"] = new JsonSchemaProperty { Type = "number" },
["b"] = new JsonSchemaProperty { Type = "number" }
},
Required = ["a", "b"]
}
};
// Act
var result = AIFunctionUtilities.MapToJsonElement(tool);
// Assert
var resultObj = JsonSerializer.Deserialize<JsonElement>(result.GetRawText());
Assert.Equal("object", resultObj.GetProperty("type").GetString());
Assert.Equal("calculator", resultObj.GetProperty("title").GetString());
Assert.Equal("A simple calculator", resultObj.GetProperty("description").GetString());
// Add more detailed property assertions
}
[Fact]
public void MapToJsonElement_WithOptionalProperties_CreatesCorrectSchema()
{
// Arrange
var tool = new Tool()
{
Name = "format",
Description = "Text formatter",
InputSchema = new JsonSchema
{
Properties = new Dictionary<string, JsonSchemaProperty>
{
["text"] = new JsonSchemaProperty { Type = "string" },
["style"] = new JsonSchemaProperty { Type = "string" }
},
Required = ["text"] // only text is required
}
};
// Act
var result = AIFunctionUtilities.MapToJsonElement(tool);
// Assert
var resultObj = JsonSerializer.Deserialize<JsonElement>(result.GetRawText());
Assert.Equal("object", resultObj.GetProperty("type").GetString());
Assert.Equal("format", resultObj.GetProperty("title").GetString());
Assert.Equal("Text formatter", resultObj.GetProperty("description").GetString());
Assert.Contains("text", resultObj.GetProperty("required").EnumerateArray().Select(x => x.GetString()));
Assert.DoesNotContain("style", resultObj.GetProperty("required").EnumerateArray().Select(x => x.GetString()));
}
[Fact]
public void MapToJsonElement_WithNullProperties_HandlesGracefully()
{
// Arrange
var tool = new Tool()
{
Name = "minimal",
Description = "Minimal tool",
InputSchema = new JsonSchema
{
Properties = null,
Required = null
}
};
// Act
var result = AIFunctionUtilities.MapToJsonElement(tool);
// Assert
var resultObj = JsonSerializer.Deserialize<JsonElement>(result.GetRawText());
Assert.Equal("object", resultObj.GetProperty("type").GetString());
Assert.Equal("minimal", resultObj.GetProperty("title").GetString());
Assert.Equal("Minimal tool", resultObj.GetProperty("description").GetString());
Assert.Equal("{}", resultObj.GetProperty("properties").GetRawText());
Assert.Empty(resultObj.GetProperty("required").EnumerateArray());
}
}