using NSubstitute; using SIGCM2.Application.Abstractions.Persistence; using SIGCM2.Application.Audit; using SIGCM2.Application.Medios.Deactivate; using SIGCM2.Domain.Entities; using SIGCM2.Domain.Exceptions; namespace SIGCM2.Application.Tests.Medios.Deactivate; public class DeactivateMedioCommandHandlerTests { private readonly IMedioRepository _repo = Substitute.For(); private readonly IAuditLogger _audit = Substitute.For(); private readonly DeactivateMedioCommandHandler _handler; private static Medio MakeMedio(int id = 1, bool activo = true) => new(id, "COD" + id, "Nombre", TipoMedio.Diario, null, activo, DateTime.UtcNow, null); public DeactivateMedioCommandHandlerTests() { _handler = new DeactivateMedioCommandHandler(_repo, _audit, TimeProvider.System); } // ── not found → throws ────────────────────────────────────────────────── [Fact] public async Task Handle_NotFound_ThrowsMedioNotFoundException() { _repo.GetByIdAsync(999, Arg.Any()).Returns((Medio?)null); await Assert.ThrowsAsync( () => _handler.Handle(new DeactivateMedioCommand(999))); } // ── idempotent ─────────────────────────────────────────────────────────── [Fact] public async Task Handle_AlreadyInactive_IsIdempotentAndDoesNotCallUpdateAsync() { _repo.GetByIdAsync(1, Arg.Any()).Returns(MakeMedio(1, false)); await _handler.Handle(new DeactivateMedioCommand(1)); await _repo.DidNotReceive().UpdateAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task Handle_AlreadyInactive_DoesNotWriteAuditEvent() { _repo.GetByIdAsync(1, Arg.Any()).Returns(MakeMedio(1, false)); await _handler.Handle(new DeactivateMedioCommand(1)); await _audit.DidNotReceive().LogAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } // ── happy path ─────────────────────────────────────────────────────────── [Fact] public async Task Handle_ActiveMedio_CallsUpdateAsyncWithInactiveEntity() { _repo.GetByIdAsync(1, Arg.Any()).Returns(MakeMedio(1, true)); await _handler.Handle(new DeactivateMedioCommand(1)); await _repo.Received(1).UpdateAsync( Arg.Is(m => !m.Activo), Arg.Any()); } [Fact] public async Task Handle_ActiveMedio_WritesAuditWithDeactivateAction() { _repo.GetByIdAsync(1, Arg.Any()).Returns(MakeMedio(1, true)); await _handler.Handle(new DeactivateMedioCommand(1)); await _audit.Received(1).LogAsync( action: "medio.deactivate", targetType: "Medio", targetId: "1", metadata: Arg.Any(), ct: Arg.Any()); } }