From a06632e2509d4ac627a9f1e7b0893980ffd6d4b0 Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Thu, 12 Dec 2024 15:05:55 +0000 Subject: [PATCH] Create GenerateCommitMessageServiceTests.cs --- .../GenerateCommitMessageServiceTests.cs | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs diff --git a/Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs b/Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs new file mode 100644 index 0000000..a6d87ac --- /dev/null +++ b/Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs @@ -0,0 +1,144 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Text.Json; +using System.Text.RegularExpressions; +using AiCommitMessage.Options; +using AiCommitMessage.Services; +using AiCommitMessage.Utility; +using FluentAssertions; +using NSubstitute; +using OpenAI; +using OpenAI.Chat; +using Xunit; + +namespace AiCommitMessage.Tests; + +public class GenerateCommitMessageServiceTests +{ + private readonly ChatClient _mockChatClient; + private readonly GenerateCommitMessageService _service; + + public GenerateCommitMessageServiceTests() + { + // Mock the ChatClient using NSubstitute + _mockChatClient = Substitute.For(null, null, null); + + // Initialize the service + _service = new GenerateCommitMessageService(); + } + + [Fact] + public void GenerateCommitMessage_Should_ThrowException_When_BothBranchAndDiffAreEmpty() + { + // Arrange + var options = new GenerateCommitMessageOptions + { + Branch = string.Empty, + Diff = string.Empty, + Message = "Test message" + }; + + // Act + Action act = () => _service.GenerateCommitMessage(options); + + // Assert + act.Should().Throw() + .WithMessage("Unable to generate commit message: Both branch and diff are empty."); + } + + [Fact] + public void GenerateCommitMessage_Should_ReturnMessage_When_MergeConflictResolutionDetected() + { + // Arrange + var options = new GenerateCommitMessageOptions + { + Branch = "feature/test", + Diff = "Some diff", + Message = "Merge branch 'feature/test' into main" + }; + + // Act + var result = _service.GenerateCommitMessage(options); + + // Assert + result.Should().Be("Merge branch 'feature/test' into main"); + } + + [Fact] + public void GenerateCommitMessage_Should_IncludeBranchAndDiff_When_Provided() + { + // Arrange + var options = new GenerateCommitMessageOptions + { + Branch = "feature/test", + Diff = "Added new feature", + Message = "Initial commit" + }; + + _mockChatClient.CompleteChat(Arg.Any(), Arg.Any()) + .Returns(new ChatCompletionResult + { + Value = new ChatCompletion + { + Content = new[] { new ChatMessage { Text = "Generated commit message" } } + } + }); + + // Act + var result = _service.GenerateCommitMessage(options); + + // Assert + result.Should().Contain("Branch: feature/test"); + result.Should().Contain("Original message: Initial commit"); + result.Should().Contain("Git Diff: Added new feature"); + } + + [Fact] + public void GenerateCommitMessage_Should_DebugOutputToFile_When_DebugIsEnabled() + { + // Arrange + var options = new GenerateCommitMessageOptions + { + Branch = "feature/test", + Diff = "Some diff", + Message = "Initial commit", + Debug = true + }; + + var chatCompletionResult = new ChatCompletionResult + { + Value = new ChatCompletion + { + Content = new[] { new ChatMessage { Text = "Generated commit message" } } + } + }; + + _mockChatClient.CompleteChat(Arg.Any(), Arg.Any()) + .Returns(chatCompletionResult); + + // Act + var result = _service.GenerateCommitMessage(options); + + // Assert + result.Should().Be("Generated commit message"); + var debugFileContent = File.ReadAllText("debug.json"); + debugFileContent.Should().Be(JsonSerializer.Serialize(chatCompletionResult)); + } + + [Fact] + public void GetGitProvider_Should_ReturnCorrectProvider_When_OriginUrlMatches() + { + // Arrange + var process = Substitute.For(); + process.StandardOutput.ReadToEnd().Returns("https://github.com/example/repo.git"); + + // Act + var result = typeof(GenerateCommitMessageService) + .GetMethod("GetGitProvider", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static) + .Invoke(null, null); + + // Assert + result.Should().Be(GitProvider.GitHub); + } +}