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

/// <summary>
/// Bundle G (M2-T11) regression coverage. The site-side Audit Log writer chain
/// (FallbackAuditWriter) increments <see cref="IAuditWriteFailureCounter"/>
/// every time the primary SQLite writer throws. Bundle G bridges that counter
/// into the Site Health Monitoring report payload as <c>SiteAuditWriteFailures</c>
/// so a sustained audit-write outage surfaces on /monitoring/health rather than
/// disappearing into a NoOp sink.
/// </summary>
public class SiteAuditWriteFailuresMetricTests
{
    private readonly SiteHealthCollector _collector = new();

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

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

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

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

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

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

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

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