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>(); generateChatCompletionMethod.GetParameters().Should().HaveCount(2); generateChatCompletionMethod .GetParameters()[0] .ParameterType.Should() .Be>(); generateChatCompletionMethod .GetParameters()[1] .ParameterType.Should() .Be(); var generateChatCompletionWithCompressionMethod = methods.FirstOrDefault(m => m.Name == "GenerateChatCompletionWithCompressionAsync" ); generateChatCompletionWithCompressionMethod.Should().NotBeNull(); generateChatCompletionWithCompressionMethod!.ReturnType.Should().Be>(); generateChatCompletionWithCompressionMethod.GetParameters().Should().HaveCount(2); generateChatCompletionWithCompressionMethod .GetParameters()[0] .ParameterType.Should() .Be>(); generateChatCompletionWithCompressionMethod .GetParameters()[1] .ParameterType.Should() .Be(); } [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(); var messages = new List { new() { Role = "user", Content = "Test message" }, }; var cancellationToken = CancellationToken.None; var expectedResponse = "Test response"; mock.Setup(x => x.GenerateChatCompletionAsync( It.IsAny>(), It.IsAny() ) ) .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(); var messages = new List { new() { Role = "user", Content = "Test message" }, }; var cancellationToken = CancellationToken.None; var expectedResponse = "Test response with compression"; mock.Setup(x => x.GenerateChatCompletionWithCompressionAsync( It.IsAny>(), It.IsAny() ) ) .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(); var messages = new List(); var cancellationToken = CancellationToken.None; var expectedResponse = "Empty response"; mock.Setup(x => x.GenerateChatCompletionAsync( It.IsAny>(), It.IsAny() ) ) .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(); var messages = new List(); var cancellationToken = CancellationToken.None; var expectedResponse = "Empty response with compression"; mock.Setup(x => x.GenerateChatCompletionWithCompressionAsync( It.IsAny>(), It.IsAny() ) ) .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(); var messages = new List { new() { Role = "user", Content = "Test message" }, }; var cancellationToken = new CancellationToken(true); // Cancelled token var expectedResponse = "Cancelled response"; mock.Setup(x => x.GenerateChatCompletionAsync( It.IsAny>(), It.IsAny() ) ) .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(); var messages = new List { 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>(), It.IsAny() ) ) .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(); var messages = new List(); 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>(), It.IsAny() ) ) .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(); var messages = new List(); 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>(), It.IsAny() ) ) .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"); } }