From ff649cb3d19afa8c8dd4b539f06014461ff7c187 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 1 Jul 2024 21:29:23 -0500 Subject: [PATCH] tests: add test cases --- .../Unit/Models/ToolCallTests.cs | 82 ++++++++++++++++++- .../Unit/Utils/JsonSchemaGeneratorTestData.cs | 46 ++++++++++- 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/tests/AnthropicClient.Tests/Unit/Models/ToolCallTests.cs b/tests/AnthropicClient.Tests/Unit/Models/ToolCallTests.cs index 4afe175..3d6e10a 100644 --- a/tests/AnthropicClient.Tests/Unit/Models/ToolCallTests.cs +++ b/tests/AnthropicClient.Tests/Unit/Models/ToolCallTests.cs @@ -1,6 +1,8 @@ +using System.Text.Json.Nodes; + namespace AnthropicClient.Tests.Unit.Models; -public class ToolCallTests +public class ToolCallTests : SerializationTest { [Fact] public async Task InvokeAsync_WhenCalledToolHasNoParamsIsNotAwaitableAndToolCallIsSuccessful_ItShouldReturnSuccessResult() @@ -146,4 +148,82 @@ public class ToolCallTests result.Value.Should().Be("42"); result.Error.Should().BeNull(); } + + [Fact] + public async Task InvokeAsync_WhenCalledToolHasDefaultValueAndToolUseHasNotInput_ItShouldReturnSuccessResult() + { + var func = (int i = 42) => i.ToString(); + var anthropicFunction = new AnthropicFunction(func.Method, func.Target); + var tool = new Tool("tool", "description", anthropicFunction); + + var toolCall = new ToolCall(tool, new ToolUseContent()); + + var result = await toolCall.InvokeAsync(); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be("42"); + result.Error.Should().BeNull(); + } + + [Fact] + public async Task InvokeAsync_WhenCalledToolHasNoDefaultValueAndNoInput_ItShouldReturnFailureResult() + { + var func = (int i) => i.ToString(); + var anthropicFunction = new AnthropicFunction(func.Method, func.Target); + var tool = new Tool("tool", "description", anthropicFunction); + + var toolCall = new ToolCall(tool, new ToolUseContent()); + + var result = await toolCall.InvokeAsync(); + + result.IsSuccess.Should().BeFalse(); + result.Value.Should().BeNull(); + result.Error.Should().NotBeNull(); + } + + [Fact] + public async Task InvokeAsync_WhenCalledAndToolHasEnumParameter_ItShouldReturnSuccessResult() + { + var func = (DayOfWeek day) => day.ToString(); + var anthropicFunction = new AnthropicFunction(func.Method, func.Target); + var tool = new Tool("tool", "description", anthropicFunction); + + var input = new Dictionary { { "day", DayOfWeek.Monday } }; + var toolCall = new ToolCall(tool, new ToolUseContent { Input = input }); + + var result = await toolCall.InvokeAsync(); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be("Monday"); + result.Error.Should().BeNull(); + } + + [Fact] + public async Task InvokeAsync_WhenCalledAndToolHasComplexParameterType_ItShouldReturnSuccessResult() + { + var func = (Person person) => person.Name; + var anthropicFunction = new AnthropicFunction(func.Method, func.Target); + var tool = new Tool("tool", "description", anthropicFunction); + + var personJson = @"{ + ""person"": { + ""Name"": ""John"" + } + }"; + + var input = Deserialize>(personJson); + + var toolCall = new ToolCall(tool, new ToolUseContent { Input = input! }); + + var result = await toolCall.InvokeAsync(); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be("John"); + result.Error.Should().BeNull(); + } +} + +class Person +{ + public string Name { get; set; } = string.Empty; } \ No newline at end of file diff --git a/tests/AnthropicClient.Tests/Unit/Utils/JsonSchemaGeneratorTestData.cs b/tests/AnthropicClient.Tests/Unit/Utils/JsonSchemaGeneratorTestData.cs index 60aee8f..791b1a6 100644 --- a/tests/AnthropicClient.Tests/Unit/Utils/JsonSchemaGeneratorTestData.cs +++ b/tests/AnthropicClient.Tests/Unit/Utils/JsonSchemaGeneratorTestData.cs @@ -823,7 +823,51 @@ public class JsonSchemaGeneratorTestData : IEnumerable "family", }, } - }; + }; + + // string parameter type with custom name + yield return new object[] + { + Tool.CreateFromFunction(TestToolName,TestToolDescription,([FunctionParameter("Person's Name", "Name", true)]string name) => name), + new JsonObject() + { + ["type"] = "object", + ["properties"] = new JsonObject() + { + ["Name"] = new JsonObject() + { + ["type"] = "string", + ["description"] = "Person's Name" + } + }, + ["required"] = new JsonArray() + { + "Name" + }, + } + }; + + // string parameter with custom description + yield return new object[] + { + Tool.CreateFromFunction(TestToolName,TestToolDescription,([FunctionParameter("The name of the person.", required: true)]string name) => name), + new JsonObject() + { + ["type"] = "object", + ["properties"] = new JsonObject() + { + ["name"] = new JsonObject() + { + ["type"] = "string", + ["description"] = "The name of the person." + } + }, + ["required"] = new JsonArray() + { + "name" + }, + } + }; } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();