Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests/NotificationOutboxOptionsValidatorTests.cs
T
Joseph Doherty abbbcd1a8d fix(kpihistory)+fix(notificationoutbox)+fix(sitecallaudit)+fix(host): P5-5 — rate-fold interval triple made self-consistent + cross-validated
Defect: the hourly KPI rollup fold (KpiHistoryRepository.FoldHourlyRollupsAsync)
sums every Rate-classified sample recorded in an hour, correct only if each
sample is a non-overlapping delta covering exactly one recorder tick
(KpiHistoryOptions.SampleInterval). That invariant was an unenforced silent
assumption across three independently-owned config keys — SampleInterval,
NotificationOutboxOptions.DeliveredKpiWindow, SiteCallAuditOptions.KpiInterval —
so widening DeliveredKpiWindow to e.g. 15 min would silently over-count the
rollup ~15x.

Robust fix: NotificationOutboxKpiSampleSource and SiteCallAuditKpiSampleSource
now rescale their Rate-classified samples (deliveredLastInterval,
failedLastInterval) by SampleInterval / (DeliveredKpiWindow or KpiInterval)
before recording, so the rollup's per-hour Sum is correct regardless of how
the other two keys are tuned — the live dashboard tile's own cutoff/window is
untouched. Both sources gain a project reference to KpiHistory to read
SampleInterval; no cycle (KpiHistory references only Commons).

Eager cross-validation (defense-in-depth): KpiRateFoldIntervalCrossValidator
(Host, the one composition root referencing all three component projects)
fails boot naming all three config keys when they diverge, protecting the
separate (still-real) invariant that the recorded/charted KPI-history sample
equals the live tile at the same instant — which the rescale trades away
whenever the scale factor isn't 1. Rides KpiHistoryOptions' existing
ValidateOnStart trigger.

Also: NotificationOutboxOptions had no registered validator at all before this
(the gap this task's "whichever options class owns DeliveredKpiWindow" file
scope pointed at) — added NotificationOutboxOptionsValidator mirroring
SiteCallAuditOptionsValidator's positive-duration/count convention, wired with
ValidateOnStart.

Tests: new rescale tests in both sample-source suites prove the fold is
numerically correct at a non-default SampleInterval (30/40 counted over a
15/20-minute window correctly divides down to a 1-minute-tick delta); new
KpiRateFoldIntervalCrossValidatorTests prove divergent config fails with all
three keys named; new NotificationOutboxOptionsValidatorTests. Every new test
proven to fail against the unfixed computation first (temporarily
short-circuited the scale factor / cross-check to the pre-fix behavior,
confirmed failure, restored) — not via git stash (repo-wide, shared across
concurrent worktrees this session) but via file-copy swap.

Full suites green: Host.Tests (527), ConfigurationDatabase.Tests (401),
KpiHistory.Tests (54), NotificationOutbox.Tests (163),
SiteCallAudit.Tests (67).
2026-08-19 16:41:04 -04:00

93 lines
3.5 KiB
C#

using Microsoft.Extensions.Options;
namespace ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests;
/// <summary>
/// P5-5 (arch-review Low): <see cref="NotificationOutboxOptions"/> had no
/// registered validator before this — the dispatch/purge/KPI cadences and the
/// delivered-throughput KPI window (<see cref="NotificationOutboxOptions.DeliveredKpiWindow"/>)
/// could be zero/negative with no boot-time signal, which would spin an Akka
/// scheduler or — because <c>NotificationOutboxKpiSampleSource</c> divides by
/// <c>DeliveredKpiWindow</c> to rescale onto <c>KpiHistoryOptions.SampleInterval</c>
/// — divide by zero. <see cref="NotificationOutboxOptions.MaxParallelDeliveries"/>
/// is validated on its BASE property, not the clamped
/// <see cref="NotificationOutboxOptions.ResolvedMaxParallelDeliveries"/>, so a
/// misconfigured non-positive value fails fast instead of silently clamping to 1.
/// </summary>
public class NotificationOutboxOptionsValidatorTests
{
private static ValidateOptionsResult Validate(NotificationOutboxOptions options) =>
new NotificationOutboxOptionsValidator().Validate(Options.DefaultName, options);
[Fact]
public void DefaultOptions_AreValid()
{
var result = Validate(new NotificationOutboxOptions());
Assert.True(result.Succeeded, result.FailureMessage);
}
[Fact]
public void ZeroDispatchInterval_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { DispatchInterval = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("DispatchInterval", result.FailureMessage);
}
[Fact]
public void ZeroDispatchBatchSize_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { DispatchBatchSize = 0 });
Assert.True(result.Failed);
Assert.Contains("DispatchBatchSize", result.FailureMessage);
}
[Fact]
public void ZeroStuckAgeThreshold_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { StuckAgeThreshold = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("StuckAgeThreshold", result.FailureMessage);
}
[Fact]
public void ZeroTerminalRetention_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { TerminalRetention = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("TerminalRetention", result.FailureMessage);
}
[Fact]
public void ZeroPurgeInterval_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { PurgeInterval = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("PurgeInterval", result.FailureMessage);
}
[Fact]
public void ZeroDeliveredKpiWindow_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("DeliveredKpiWindow", result.FailureMessage);
}
[Fact]
public void NegativeDeliveredKpiWindow_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromSeconds(-1) });
Assert.True(result.Failed);
Assert.Contains("DeliveredKpiWindow", result.FailureMessage);
}
[Fact]
public void ZeroMaxParallelDeliveries_IsRejected()
{
var result = Validate(new NotificationOutboxOptions { MaxParallelDeliveries = 0 });
Assert.True(result.Failed);
Assert.Contains("MaxParallelDeliveries", result.FailureMessage);
}
}