using NSubstitute; using SIGCM2.Application.Abstractions.Persistence; using SIGCM2.Application.Audit; using SIGCM2.Application.PuntosDeVenta.Deactivate; using SIGCM2.Domain.Entities; using SIGCM2.Domain.Exceptions; namespace SIGCM2.Application.Tests.PuntosDeVenta.Deactivate; public class DeactivatePuntoDeVentaCommandHandlerTests { private readonly IPuntoDeVentaRepository _repo = Substitute.For(); private readonly IMedioRepository _medioRepo = Substitute.For(); private readonly IAuditLogger _audit = Substitute.For(); private readonly DeactivatePuntoDeVentaCommandHandler _handler; private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = true) => new(id, medioId, 1, "PdV Test", null, activo, DateTime.UtcNow, null); private static Medio MakeMedio(int id = 5, bool activo = true) => new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null); public DeactivatePuntoDeVentaCommandHandlerTests() { _handler = new DeactivatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit); _medioRepo.GetByIdAsync(Arg.Any(), Arg.Any()).Returns(MakeMedio(5, true)); } [Fact] public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException() { _repo.GetByIdAsync(999, Arg.Any()).Returns((PuntoDeVenta?)null); await Assert.ThrowsAsync( () => _handler.Handle(new DeactivatePuntoDeVentaCommand(999))); } [Fact] public async Task Handle_AlreadyInactive_IsIdempotentAndDoesNotCallUpdateAsync() { _repo.GetByIdAsync(10, Arg.Any()).Returns(MakePdv(10, activo: false)); await _handler.Handle(new DeactivatePuntoDeVentaCommand(10)); await _repo.DidNotReceive().UpdateAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task Handle_AlreadyInactive_DoesNotWriteAuditEvent() { _repo.GetByIdAsync(10, Arg.Any()).Returns(MakePdv(10, activo: false)); await _handler.Handle(new DeactivatePuntoDeVentaCommand(10)); await _audit.DidNotReceive().LogAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] public async Task Handle_ActivePdv_CallsUpdateAsyncWithInactiveEntity() { _repo.GetByIdAsync(10, Arg.Any()).Returns(MakePdv(10, activo: true)); await _handler.Handle(new DeactivatePuntoDeVentaCommand(10)); await _repo.Received(1).UpdateAsync( Arg.Is(p => !p.Activo), Arg.Any()); } [Fact] public async Task Handle_ActivePdv_WritesAuditWithDeactivateAction() { _repo.GetByIdAsync(10, Arg.Any()).Returns(MakePdv(10, activo: true)); await _handler.Handle(new DeactivatePuntoDeVentaCommand(10)); await _audit.Received(1).LogAsync( action: "punto_de_venta.deactivate", targetType: "PuntoDeVenta", targetId: "10", metadata: Arg.Any(), ct: Arg.Any()); } }