Files
2026-03-11 13:21:47 -05:00

85 lines
2.7 KiB
C#

using System;
using System.Threading.Tasks;
using Moq;
using Xunit;
using LCU.Api.Internal.MaidBotApi.Exceptions;
using LCU.Api.Internal.MaidBotApi.Services;
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.Api.Internal.MaidBotLib.Repositories;
using LCU.Library.Internal.Exceptions;
namespace LCU.Api.Internal.MaidBotTests.ServiceTests;
public class DebugEventLogServiceTest
{
private readonly DebugEventLogService _debugEventLogService;
private readonly Mock<IDebugEventLogRepository> _mockDebugEventLogRepository;
public DebugEventLogServiceTest()
{
var mockUnitOfWork = new Mock<IUnitOfWork>();
_mockDebugEventLogRepository = new Mock<IDebugEventLogRepository>();
_debugEventLogService = new DebugEventLogService(mockUnitOfWork.Object, _mockDebugEventLogRepository.Object);
}
[Fact]
public async Task SaveAsync_Success()
{
// Arrange
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(It.IsAny<DebugEventLog>())).ReturnsAsync(new DebugEventLog());
// Act
var result = await _debugEventLogService.SaveAsync(new DebugEventLog());
// Assert
Assert.NotNull(result);
}
[Fact]
public async Task SaveAsync_Exception_Throws_ServerError()
{
// Arrange
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(It.IsAny<DebugEventLog>())).ThrowsAsync(new Exception());
// Act / Assert
await Assert.ThrowsAsync<ServiceException>(async () =>
await _debugEventLogService.SaveAsync(new DebugEventLog()));
}
[Fact]
public async Task DebugEventLog_Verify_AddAsyncCalled_Once()
{
// Arrange
var testEventLog = new EventLog()
{
DebugData = "Test Debug Data"
};
var testDebugEventLog = new DebugEventLog()
{
RawLog = "Test Debug Data"
};
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(testDebugEventLog)).ReturnsAsync(testDebugEventLog);
//Act
await _debugEventLogService.DebugEventLog(testEventLog);
//Assert
_mockDebugEventLogRepository.Verify(x => x.AddAsync(It.IsAny<DebugEventLog>()), Times.Once);
}
[Fact]
public async Task DebugEventLog_Verify_AddAsyncCalled_Never()
{
// Arrange
var testEventLog = new EventLog() { };
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(It.IsAny<DebugEventLog>())).ReturnsAsync(new DebugEventLog());
var testDebugEventLog = new DebugEventLog();
//Act
await _debugEventLogService.DebugEventLog(testEventLog);
//Assert
_mockDebugEventLogRepository.Verify(x => x.AddAsync(testDebugEventLog), Times.Never);
}
}