Skip to content

Commit

Permalink
Create GenerateCommitMessageServiceTests.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
guibranco authored Dec 12, 2024
1 parent 1f407ff commit a06632e
Showing 1 changed file with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ChatClient>(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<InvalidOperationException>()
.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<SystemChatMessage>(), Arg.Any<UserChatMessage>())
.Returns(new ChatCompletionResult

Check failure on line 80 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 80 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 80 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 80 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)
{
Value = new ChatCompletion

Check failure on line 82 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

'ChatCompletion' does not contain a constructor that takes 0 arguments

Check failure on line 82 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

'ChatCompletion' does not contain a constructor that takes 0 arguments

Check failure on line 82 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

'ChatCompletion' does not contain a constructor that takes 0 arguments

Check failure on line 82 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'ChatCompletion' does not contain a constructor that takes 0 arguments
{
Content = new[] { new ChatMessage { Text = "Generated commit message" } }

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

'ChatMessage' does not contain a definition for 'Text'

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

'ChatMessage' does not contain a definition for 'Text'

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

'ChatMessage' does not contain a definition for 'Text'

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 84 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'ChatMessage' does not contain a definition for 'Text'
}
});

// 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

Check failure on line 109 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 109 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 109 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 109 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The type or namespace name 'ChatCompletionResult' could not be found (are you missing a using directive or an assembly reference?)
{
Value = new ChatCompletion

Check failure on line 111 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

'ChatCompletion' does not contain a constructor that takes 0 arguments

Check failure on line 111 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

'ChatCompletion' does not contain a constructor that takes 0 arguments

Check failure on line 111 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

'ChatCompletion' does not contain a constructor that takes 0 arguments

Check failure on line 111 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'ChatCompletion' does not contain a constructor that takes 0 arguments
{
Content = new[] { new ChatMessage { Text = "Generated commit message" } }

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

'ChatMessage' does not contain a definition for 'Text'

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

'ChatMessage' does not contain a definition for 'Text'

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

'ChatMessage' does not contain a definition for 'Text'

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Property or indexer 'ChatCompletion.Content' cannot be assigned to -- it is read only

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'ChatMessage' does not contain a constructor that takes 0 arguments

Check failure on line 113 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'ChatMessage' does not contain a definition for 'Text'
}
};

_mockChatClient.CompleteChat(Arg.Any<SystemChatMessage>(), Arg.Any<UserChatMessage>())
.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>();
process.StandardOutput.ReadToEnd().Returns("https://github.com/example/repo.git");

// Act
var result = typeof(GenerateCommitMessageService)

Check warning on line 137 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

Dereference of a possibly null reference.

Check warning on line 137 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build

Dereference of a possibly null reference.

Check warning on line 137 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Dereference of a possibly null reference.

Check warning on line 137 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Dereference of a possibly null reference.

Check warning on line 137 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Dereference of a possibly null reference.

Check warning on line 137 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 137 in Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.
.GetMethod("GetGitProvider", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
.Invoke(null, null);

// Assert
result.Should().Be(GitProvider.GitHub);
}
}

0 comments on commit a06632e

Please sign in to comment.