diff --git a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs index ca97570..ba2a6a2 100644 --- a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs +++ b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs @@ -1032,4 +1032,70 @@ public class AnthropicApiClientTests : IntegrationTest result.Value.CancelInitiatedAt.Should().Be(DateTimeOffset.Parse("2024-08-20T18:37:24.100435Z")); result.Value.ResultsUrl.Should().Be("https://api.anthropic.com/v1/messages/batches/msgbatch_013Zva2CMHLNnXjNJJKqJ2EF/results"); } + + [Fact] + public async Task CreateMessageBatchAsync_WhenCalledAndErrorReturned_ItShouldHandleError() + { + _mockHttpMessageHandler + .WhenCreateMessageBatchRequest() + .Respond( + HttpStatusCode.BadRequest, + "application/json", + @"{ + ""type"": ""error"", + ""error"": { + ""type"": ""invalid_request_error"", + ""message"": ""messages: roles must alternate between user and assistant, but found multiple user roles in a row"" + } + }" + ); + + var request = new MessageBatchRequest([new("custom_id", new())]); + + var result = await Client.CreateMessageBatchAsync(request); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().BeOfType(); + result.Error.Error.Should().BeOfType(); + } + + [Fact] + public async Task CreateMessageBatchAsync_WhenCalledRequestFailsAndErrorCanNotBeDeserialized_ItShouldReturnUnknownError() + { + _mockHttpMessageHandler + .WhenCreateMessageBatchRequest() + .Respond( + HttpStatusCode.BadRequest, + "application/json", + @"{}" + ); + + var request = new MessageBatchRequest([new("custom_id", new())]); + + var result = await Client.CreateMessageBatchAsync(request); + + result.IsSuccess.Should().BeFalse(); + result.Error.Should().BeOfType(); + result.Error.Error.Should().BeOfType(); + } + + [Fact] + public async Task CreateMessageBatchAsync_WhenCalledRequestSucceedsAndCanNotDeserializeResponse_ItShouldReturnEmptyResponse() + { + _mockHttpMessageHandler + .WhenCreateMessageBatchRequest() + .Respond( + HttpStatusCode.OK, + "application/json", + @"{}" + ); + + var request = new MessageBatchRequest([new("custom_id", new())]); + + var result = await Client.CreateMessageBatchAsync(request); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().BeOfType(); + result.Value.Should().BeEquivalentTo(new MessageBatchResponse()); + } } \ No newline at end of file