diff --git a/tests/Orbit.Application.Tests/Commands/Habits/LogHabitCommandHandlerTests.cs b/tests/Orbit.Application.Tests/Commands/Habits/LogHabitCommandHandlerTests.cs index 6ceb7153..9f753953 100644 --- a/tests/Orbit.Application.Tests/Commands/Habits/LogHabitCommandHandlerTests.cs +++ b/tests/Orbit.Application.Tests/Commands/Habits/LogHabitCommandHandlerTests.cs @@ -28,6 +28,7 @@ public class LogHabitCommandHandlerTests private readonly IUserStreakService _userStreakService = Substitute.For(); private readonly IGamificationService _gamificationService = Substitute.For(); private readonly IChallengeProgressService _challengeProgressService = Substitute.For(); + private readonly IPayGateService _payGate = Substitute.For(); private readonly IUnitOfWork _unitOfWork = Substitute.For(); private readonly MemoryCache _cache = new MemoryCache(new MemoryCacheOptions()); private readonly MediatR.IMediator _mediator = Substitute.For(); @@ -40,7 +41,13 @@ public class LogHabitCommandHandlerTests public LogHabitCommandHandlerTests() { var repos = new LogHabitRepositories(_habitRepo, _habitLogRepo, _goalRepo, _userRepo); - var services = new LogHabitServices(_userDateService, _userStreakService, _gamificationService, _challengeProgressService, _mediator); + var services = new LogHabitServices( + _userDateService, + _userStreakService, + _gamificationService, + _challengeProgressService, + _mediator, + _payGate); _handler = new LogHabitCommandHandler( repos, services, _unitOfWork, _cache, _logger); @@ -157,6 +164,34 @@ public async Task Handle_AlreadyLogged_TogglesUnlog() await _unitOfWork.Received(2).SaveChangesAsync(Arg.Any()); } + [Fact] + public async Task Handle_CompletedOneTimeTask_PayGateFailureRejectsWithoutChangingState() + { + var habit = Habit.Create(new HabitCreateParams( + UserId, "Finished task", null, null, DueDate: Today)).Value; + habit.Log(Today).IsSuccess.Should().BeTrue(); + var existingLog = habit.Logs.Should().ContainSingle().Which; + _habitRepo.FindOneTrackedAsync( + Arg.Any>>(), + Arg.Any, IQueryable>?>(), + Arg.Any()) + .Returns(habit); + _payGate.CanCreateHabits(UserId, 1, Arg.Any()) + .Returns(Result.PayGateFailure("Habit limit reached")); + + var result = await _handler.Handle( + new LogHabitCommand(UserId, habit.Id), + CancellationToken.None); + + result.IsFailure.Should().BeTrue(); + result.ErrorCode.Should().Be(Result.PayGateErrorCode); + habit.IsCompleted.Should().BeTrue(); + habit.Logs.Should().ContainSingle().Which.Should().BeSameAs(existingLog); + existingLog.IsDeleted.Should().BeFalse(); + await _payGate.Received(1).CanCreateHabits(UserId, 1, Arg.Any()); + await _unitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + [Fact] public async Task Handle_HabitNotFound_ReturnsFailure() {