using Microsoft.Extensions.Options;
using ZB.MOM.WW.ScadaBridge.KpiHistory;
using ZB.MOM.WW.ScadaBridge.NotificationOutbox;
using ZB.MOM.WW.ScadaBridge.SiteCallAudit;
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
///
/// is the
/// boot-time guardrail for the "rate-fold interval triple" —
/// ,
/// , and
/// . The sample sources now rescale
/// their Rate-classified samples onto SampleInterval, so the hourly rollup
/// fold's correctness no longer depends on this equality — but the
/// recorded/charted sample no longer equals the live dashboard tile unless it
/// holds, so this validator still fails the host at boot on any divergence,
/// naming all three configuration keys.
///
public class KpiRateFoldIntervalCrossValidatorTests
{
private static ValidateOptionsResult Validate(
KpiHistoryOptions kpiHistoryOptions,
NotificationOutboxOptions notificationOutboxOptions,
SiteCallAuditOptions siteCallAuditOptions)
{
var validator = new KpiRateFoldIntervalCrossValidator(
Options.Create(notificationOutboxOptions),
Options.Create(siteCallAuditOptions));
return validator.Validate(Options.DefaultName, kpiHistoryOptions);
}
/// Verifies that the validator accepts all three values being equal at the documented defaults.
[Fact]
public void AllThreeEqual_AtDocumentedDefaults_IsValid()
{
var result = Validate(
new KpiHistoryOptions(), new NotificationOutboxOptions(), new SiteCallAuditOptions());
Assert.True(result.Succeeded, result.FailureMessage);
}
/// Verifies that the validator accepts all three values being equal at a common non-default value.
[Fact]
public void AllThreeEqual_AtANonDefaultCommonValue_IsValid()
{
var common = TimeSpan.FromMinutes(5);
var result = Validate(
new KpiHistoryOptions { SampleInterval = common },
new NotificationOutboxOptions { DeliveredKpiWindow = common },
new SiteCallAuditOptions { KpiInterval = common });
Assert.True(result.Succeeded, result.FailureMessage);
}
/// Verifies that the validator fails and names all three keys when the delivered KPI window diverges.
[Fact]
public void DeliveredKpiWindowDiverges_FailsAndNamesAllThreeKeys()
{
var result = Validate(
new KpiHistoryOptions { SampleInterval = TimeSpan.FromMinutes(1) },
new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromMinutes(15) },
new SiteCallAuditOptions { KpiInterval = TimeSpan.FromMinutes(1) });
Assert.True(result.Failed);
Assert.Contains("ScadaBridge:KpiHistory:SampleInterval", result.FailureMessage);
Assert.Contains("ScadaBridge:NotificationOutbox:DeliveredKpiWindow", result.FailureMessage);
Assert.Contains("ScadaBridge:SiteCallAudit:KpiInterval", result.FailureMessage);
}
/// Verifies that the validator fails and names all three keys when the KPI interval diverges.
[Fact]
public void KpiIntervalDiverges_FailsAndNamesAllThreeKeys()
{
var result = Validate(
new KpiHistoryOptions { SampleInterval = TimeSpan.FromMinutes(1) },
new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromMinutes(1) },
new SiteCallAuditOptions { KpiInterval = TimeSpan.FromMinutes(20) });
Assert.True(result.Failed);
Assert.Contains("ScadaBridge:KpiHistory:SampleInterval", result.FailureMessage);
Assert.Contains("ScadaBridge:NotificationOutbox:DeliveredKpiWindow", result.FailureMessage);
Assert.Contains("ScadaBridge:SiteCallAudit:KpiInterval", result.FailureMessage);
}
/// Verifies that the validator fails when both the delivered KPI window and the KPI interval diverge.
[Fact]
public void BothDeliveredKpiWindowAndKpiIntervalDiverge_Fails()
{
var result = Validate(
new KpiHistoryOptions { SampleInterval = TimeSpan.FromMinutes(1) },
new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromMinutes(15) },
new SiteCallAuditOptions { KpiInterval = TimeSpan.FromMinutes(20) });
Assert.True(result.Failed);
}
}