using FluentAssertions;
using Microsoft.Extensions.Time.Testing;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Pricing.ChargeableChars.Reactivate;
using SIGCM2.Domain.Pricing.ChargeableChars;
using SIGCM2.Domain.Pricing.Exceptions;
namespace SIGCM2.Application.Tests.Pricing.ChargeableChars;
///
/// PRC-001 — ReactivateChargeableCharConfigCommandHandler tests.
/// Strict TDD — RED written before implementation.
/// Covers: happy path, audit emission, audit fail-closed, repo exception propagation.
///
public class ReactivateChargeableCharConfigHandlerTests
{
private readonly FakeTimeProvider _time = new(new DateTimeOffset(2026, 4, 20, 12, 0, 0, TimeSpan.Zero));
private readonly IChargeableCharConfigRepository _repo = Substitute.For();
private readonly IAuditLogger _audit = Substitute.For();
private readonly ReactivateChargeableCharConfigCommandHandler _handler;
private static readonly DateOnly Today = new(2026, 4, 20);
private static ChargeableCharConfig ClosedConfig() =>
ChargeableCharConfig.Rehydrate(
id: 1L, productTypeId: null, symbol: "$", category: ChargeableCharCategories.Currency,
price: 1.5m, validFrom: new DateOnly(2026, 1, 1), validTo: new DateOnly(2026, 4, 19), isActive: false);
private static ChargeableCharConfig ActiveConfig() =>
ChargeableCharConfig.Rehydrate(
id: 1L, productTypeId: null, symbol: "$", category: ChargeableCharCategories.Currency,
price: 1.5m, validFrom: new DateOnly(2026, 1, 1), validTo: null, isActive: true);
public ReactivateChargeableCharConfigHandlerTests()
{
_repo.ReactivateAsync(1L, Arg.Any())
.Returns(ActiveConfig());
_handler = new ReactivateChargeableCharConfigCommandHandler(_repo, _audit, _time);
}
private static ReactivateChargeableCharConfigCommand ValidCmd() => new(Id: 1L);
// ── Happy path ──────────────────────────────────────────────────────────────
[Fact]
public async Task Handle_HappyPath_ReturnsResponse()
{
var result = await _handler.Handle(ValidCmd());
result.Should().NotBeNull();
result.Id.Should().Be(1L);
result.Symbol.Should().Be("$");
result.IsActive.Should().BeTrue();
}
[Fact]
public async Task Handle_HappyPath_CallsReactivateAsync()
{
await _handler.Handle(ValidCmd());
await _repo.Received(1).ReactivateAsync(1L, Arg.Any());
}
[Fact]
public async Task Handle_HappyPath_EmitsAuditReactivate()
{
await _handler.Handle(ValidCmd());
await _audit.Received(1).LogAsync(
action: "tasacion.chargeable_char.reactivate",
targetType: "ChargeableCharConfig",
targetId: "1",
metadata: Arg.Any