Add more test
Some checks failed
SonarQube / Build and analyze (push) Failing after 2m59s
Unit Tests / Run Tests (push) Failing after 2m22s

This commit is contained in:
Leonid Pershin
2025-10-20 08:36:57 +03:00
parent c9eac74e35
commit e011bb667f
7 changed files with 2120 additions and 6 deletions

View File

@@ -0,0 +1,327 @@
using ChatBot.Models.Dto;
using ChatBot.Services.Interfaces;
using ChatBot.Tests.TestUtilities;
using FluentAssertions;
using Moq;
namespace ChatBot.Tests.Services.Interfaces;
public class IAIServiceTests : UnitTestBase
{
[Fact]
public void IAIService_ShouldHaveCorrectMethodSignatures()
{
// Arrange & Act
var interfaceType = typeof(IAIService);
var methods = interfaceType.GetMethods();
// Assert
methods.Should().HaveCount(2);
var generateChatCompletionMethod = methods.FirstOrDefault(m =>
m.Name == "GenerateChatCompletionAsync"
);
generateChatCompletionMethod.Should().NotBeNull();
generateChatCompletionMethod!.ReturnType.Should().Be(typeof(Task<string>));
generateChatCompletionMethod.GetParameters().Should().HaveCount(2);
generateChatCompletionMethod
.GetParameters()[0]
.ParameterType.Should()
.Be(typeof(List<ChatMessage>));
generateChatCompletionMethod
.GetParameters()[1]
.ParameterType.Should()
.Be(typeof(CancellationToken));
var generateChatCompletionWithCompressionMethod = methods.FirstOrDefault(m =>
m.Name == "GenerateChatCompletionWithCompressionAsync"
);
generateChatCompletionWithCompressionMethod.Should().NotBeNull();
generateChatCompletionWithCompressionMethod!.ReturnType.Should().Be(typeof(Task<string>));
generateChatCompletionWithCompressionMethod.GetParameters().Should().HaveCount(2);
generateChatCompletionWithCompressionMethod
.GetParameters()[0]
.ParameterType.Should()
.Be(typeof(List<ChatMessage>));
generateChatCompletionWithCompressionMethod
.GetParameters()[1]
.ParameterType.Should()
.Be(typeof(CancellationToken));
}
[Fact]
public void IAIService_ShouldBeImplementedByAIService()
{
// Arrange & Act
var aiServiceType = typeof(ChatBot.Services.AIService);
var interfaceType = typeof(IAIService);
// Assert
interfaceType.IsAssignableFrom(aiServiceType).Should().BeTrue();
}
[Fact]
public async Task IAIService_GenerateChatCompletionAsync_ShouldReturnString()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>
{
new() { Role = "user", Content = "Test message" },
};
var cancellationToken = CancellationToken.None;
var expectedResponse = "Test response";
mock.Setup(x =>
x.GenerateChatCompletionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionAsync(messages, cancellationToken);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(x => x.GenerateChatCompletionAsync(messages, cancellationToken), Times.Once);
}
[Fact]
public async Task IAIService_GenerateChatCompletionWithCompressionAsync_ShouldReturnString()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>
{
new() { Role = "user", Content = "Test message" },
};
var cancellationToken = CancellationToken.None;
var expectedResponse = "Test response with compression";
mock.Setup(x =>
x.GenerateChatCompletionWithCompressionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionWithCompressionAsync(
messages,
cancellationToken
);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(
x => x.GenerateChatCompletionWithCompressionAsync(messages, cancellationToken),
Times.Once
);
}
[Fact]
public async Task IAIService_GenerateChatCompletionAsync_ShouldHandleEmptyMessages()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>();
var cancellationToken = CancellationToken.None;
var expectedResponse = "Empty response";
mock.Setup(x =>
x.GenerateChatCompletionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionAsync(messages, cancellationToken);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(x => x.GenerateChatCompletionAsync(messages, cancellationToken), Times.Once);
}
[Fact]
public async Task IAIService_GenerateChatCompletionWithCompressionAsync_ShouldHandleEmptyMessages()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>();
var cancellationToken = CancellationToken.None;
var expectedResponse = "Empty response with compression";
mock.Setup(x =>
x.GenerateChatCompletionWithCompressionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionWithCompressionAsync(
messages,
cancellationToken
);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(
x => x.GenerateChatCompletionWithCompressionAsync(messages, cancellationToken),
Times.Once
);
}
[Fact]
public async Task IAIService_GenerateChatCompletionAsync_ShouldHandleCancellationToken()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>
{
new() { Role = "user", Content = "Test message" },
};
var cancellationToken = new CancellationToken(true); // Cancelled token
var expectedResponse = "Cancelled response";
mock.Setup(x =>
x.GenerateChatCompletionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionAsync(messages, cancellationToken);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(x => x.GenerateChatCompletionAsync(messages, cancellationToken), Times.Once);
}
[Fact]
public async Task IAIService_GenerateChatCompletionWithCompressionAsync_ShouldHandleCancellationToken()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>
{
new() { Role = "user", Content = "Test message" },
};
var cancellationToken = new CancellationToken(true); // Cancelled token
var expectedResponse = "Cancelled response with compression";
mock.Setup(x =>
x.GenerateChatCompletionWithCompressionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionWithCompressionAsync(
messages,
cancellationToken
);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(
x => x.GenerateChatCompletionWithCompressionAsync(messages, cancellationToken),
Times.Once
);
}
[Fact]
public async Task IAIService_GenerateChatCompletionAsync_ShouldHandleLargeMessageList()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>();
for (int i = 0; i < 100; i++)
{
messages.Add(new() { Role = "user", Content = $"Message {i}" });
}
var cancellationToken = CancellationToken.None;
var expectedResponse = "Large response";
mock.Setup(x =>
x.GenerateChatCompletionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionAsync(messages, cancellationToken);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(x => x.GenerateChatCompletionAsync(messages, cancellationToken), Times.Once);
}
[Fact]
public async Task IAIService_GenerateChatCompletionWithCompressionAsync_ShouldHandleLargeMessageList()
{
// Arrange
var mock = new Mock<IAIService>();
var messages = new List<ChatMessage>();
for (int i = 0; i < 100; i++)
{
messages.Add(new() { Role = "user", Content = $"Message {i}" });
}
var cancellationToken = CancellationToken.None;
var expectedResponse = "Large response with compression";
mock.Setup(x =>
x.GenerateChatCompletionWithCompressionAsync(
It.IsAny<List<ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync(expectedResponse);
// Act
var result = await mock.Object.GenerateChatCompletionWithCompressionAsync(
messages,
cancellationToken
);
// Assert
result.Should().Be(expectedResponse);
mock.Verify(
x => x.GenerateChatCompletionWithCompressionAsync(messages, cancellationToken),
Times.Once
);
}
[Fact]
public void IAIService_ShouldBePublicInterface()
{
// Arrange & Act
var interfaceType = typeof(IAIService);
// Assert
interfaceType.IsPublic.Should().BeTrue();
interfaceType.IsInterface.Should().BeTrue();
}
[Fact]
public void IAIService_ShouldHaveCorrectNamespace()
{
// Arrange & Act
var interfaceType = typeof(IAIService);
// Assert
interfaceType.Namespace.Should().Be("ChatBot.Services.Interfaces");
}
}