namespace ZB.MOM.WW.ScadaBridge.HealthMonitoring.Tests;

/// <summary>
/// Bundle C (M5-T7) regression coverage. The canonical audit redactor
/// (<see cref="ZB.MOM.WW.ScadaBridge.AuditLog.Redaction.ScadaBridgeAuditRedactor"/>) increments
/// <see cref="ZB.MOM.WW.ScadaBridge.AuditLog.Payload.IAuditRedactionFailureCounter"/> every time
/// a header/body/SQL-param redactor stage throws and the redactor has to
/// over-redact the field with the <c>&lt;redacted: redactor error&gt;</c> marker.
/// Bundle C bridges that counter into the Site Health Monitoring report payload
/// as <c>AuditRedactionFailure</c> so a misconfigured / catastrophic regex
/// surfaces on /monitoring/health rather than disappearing into a NoOp sink.
/// Mirrors the Bundle G <c>SiteAuditWriteFailures</c> metric shape — same
/// per-interval increment-and-reset semantics, same defaults-to-zero
/// contract.
/// </summary>
public class AuditRedactionFailureMetricTests
{
    private readonly SiteHealthCollector _collector = new();

    [Fact]
    public void Increment_Three_Times_Counter_Reports_3()
    {
        _collector.IncrementAuditRedactionFailure();
        _collector.IncrementAuditRedactionFailure();
        _collector.IncrementAuditRedactionFailure();

        var report = _collector.CollectReport("site-1");

        Assert.Equal(3, report.AuditRedactionFailure);
    }

    [Fact]
    public void Report_Payload_Includes_AuditRedactionFailure_AsZeroByDefault()
    {
        var report = _collector.CollectReport("site-1");

        Assert.Equal(0, report.AuditRedactionFailure);
    }

    /// <summary>
    /// Mirrors the existing per-interval reset semantics for ScriptErrorCount /
    /// AlarmEvaluationErrorCount / DeadLetterCount / SiteAuditWriteFailures —
    /// AuditRedactionFailure is an interval count, not a running total.
    /// </summary>
    [Fact]
    public void CollectReport_Resets_AuditRedactionFailure()
    {
        _collector.IncrementAuditRedactionFailure();
        _collector.IncrementAuditRedactionFailure();

        var first = _collector.CollectReport("site-1");
        Assert.Equal(2, first.AuditRedactionFailure);

        var second = _collector.CollectReport("site-1");
        Assert.Equal(0, second.AuditRedactionFailure);
    }
}
