using ZB.MOM.WW.ScadaBridge.AuditLog.Site.Telemetry;

namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Site;

/// <summary>
/// Eager startup validation for <see cref="SiteAuditTelemetryOptions"/>
/// (arch-review 08 round 2 NF4). The drain-loop cadences are used as raw
/// scheduling delays in <c>SiteAuditTelemetryActor</c>; a zero interval spins
/// the drain and a zero batch drains nothing. Idle must back off at least as
/// far as busy (idle &lt; busy inverts the actor's intent).
/// </summary>
public class SiteAuditTelemetryOptionsValidatorTests
{
    [Fact]
    public void DefaultOptions_AreValid()
    {
        var validator = new SiteAuditTelemetryOptionsValidator();
        Assert.True(validator.Validate(null, new SiteAuditTelemetryOptions()).Succeeded);
    }

    [Fact]
    public void ZeroBatchSize_Fails()
    {
        var validator = new SiteAuditTelemetryOptionsValidator();
        var result = validator.Validate(null, new SiteAuditTelemetryOptions { BatchSize = 0 });
        Assert.False(result.Succeeded);
        Assert.Contains(result.Failures!,
            f => f.Contains(nameof(SiteAuditTelemetryOptions.BatchSize), StringComparison.Ordinal));
    }

    [Fact]
    public void ZeroBusyInterval_Fails()
    {
        var validator = new SiteAuditTelemetryOptionsValidator();
        var result = validator.Validate(null, new SiteAuditTelemetryOptions { BusyIntervalSeconds = 0 });
        Assert.False(result.Succeeded);
        Assert.Contains(result.Failures!,
            f => f.Contains(nameof(SiteAuditTelemetryOptions.BusyIntervalSeconds), StringComparison.Ordinal));
    }

    [Fact]
    public void ZeroIdleInterval_Fails()
    {
        var validator = new SiteAuditTelemetryOptionsValidator();
        var result = validator.Validate(null, new SiteAuditTelemetryOptions { IdleIntervalSeconds = 0 });
        Assert.False(result.Succeeded);
        Assert.Contains(result.Failures!,
            f => f.Contains(nameof(SiteAuditTelemetryOptions.IdleIntervalSeconds), StringComparison.Ordinal));
    }

    [Fact]
    public void IdleShorterThanBusy_Fails()
    {
        var validator = new SiteAuditTelemetryOptionsValidator();
        var result = validator.Validate(null,
            new SiteAuditTelemetryOptions { BusyIntervalSeconds = 30, IdleIntervalSeconds = 5 });
        Assert.False(result.Succeeded);
        Assert.Contains(result.Failures!,
            f => f.Contains(nameof(SiteAuditTelemetryOptions.IdleIntervalSeconds), StringComparison.Ordinal));
    }
}
