Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.StoreAndForward.Tests/StoreAndForwardSiteEventTests.cs
T
Joseph Doherty f2efeb37b7 refactor(sf,site): both stores take ILocalDb instead of a connection string
Tasks 5 and 6 of the Phase 2 plan, committed together because their test
fallout is entangled — several fixtures construct both stores.

StoreAndForwardStorage and SiteStorageService now take ILocalDb. Connections
come from ILocalDb.CreateConnection(), which hands out an already-open,
pragma-configured connection carrying the zb_hlc_next() UDF the capture triggers
call; a raw connection would lack the UDF and every write to a replicated table
would fail closed. Deleted with the connection strings: S&F's
EnsureDatabaseDirectoryExists and its per-open busy_timeout pragma, and the site
service's BusyTimeoutFloorSeconds normalization — LocalDb owns all of it now.

DI: AddSiteRuntime's string overload is gone (nothing left to supply), so the
Host calls the no-arg form. ScadaBridge:Database:SiteDbPath and
StoreAndForwardOptions.SqliteDbPath survive only as the migrator's source
locations in Tasks 8/9.

Two things the plan did not anticipate, both worth reading:

1. FOUND A REAL LATENT DEFECT, from Phase 1, now fixed. The plan assumed
   directory creation simply moved to LocalDb along with file ownership. It did
   not: the LocalDb library never creates the parent directory, and
   SqliteLocalDb opens the file eagerly in its constructor — so a missing
   directory is a hard boot failure ("SQLite Error 14: unable to open database
   file"), not a degraded start. The default site config points at the RELATIVE
   path ./data/site-localdb.db, so any site node without a pre-existing data/
   directory fails to boot. The docker rig escapes only because its volume mount
   happens to create /app/data — a coincidence that would have hidden this until
   a bare-metal or fresh deployment. This has been latent since Phase 1 made
   LocalDb:Path required; deleting S&F's EnsureDatabaseDirectoryExists here
   would have widened it. Re-established the guarantee at the layer that now
   owns the path (SiteLocalDbDirectory.Ensure, called before AddZbLocalDb) and
   pinned it with SiteLocalDbDirectoryTests. Non-vacuity is not assumed: two
   tests written against the wrong assumption failed with exactly this
   SQLite Error 14 before the fix existed.

2. Test fallout was ~7x the plan's estimate. The plan named "fixtures" in one
   project; the constructor change actually reaches 40 files across 7 test
   projects, and most used Mode=Memory;Cache=Shared — which LocalDb has no
   equivalent for, so every one had to move to a real temp file. Rather than
   copy the Phase 1 TestLocalDb fixture into 7 projects, added a shared
   tests/ZB.MOM.WW.ScadaBridge.TestSupport library (not a test project) so the
   WAL-sidecar cleanup and the "real, not stubbed" rationale live in one place.

Retargeted rather than deleted, in both directions: the S&F WAL test now asserts
against the LocalDb-backed store (WAL genuinely is LocalDb's job), while the
directory-creation test moved to Host.Tests (that guarantee is NOT LocalDb's).
SiteStorageServiceTests.Initialize_EnablesWalJournalMode got the same treatment.
DeploymentManagerMediumFindingsTests induced a persistence failure via an
unopenable path, which no longer reaches the assertion since the fixture now
throws first; it induces the same failure shape via an uninitialized store.

Verified: full solution build 0 warnings; SiteRuntime 532, Host 318,
AuditLog 355, ExternalSystemGateway 142, HealthMonitoring 97,
StoreAndForward 153 — 1597 passed, 0 failed.

Claude-Session: https://claude.ai/code/session_01BL2Vu1ESDQ9SCN4gVKkdts
2026-07-20 03:05:45 -04:00

171 lines
7.2 KiB
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.SiteEventLogging;
using ZB.MOM.WW.ScadaBridge.TestSupport;
namespace ZB.MOM.WW.ScadaBridge.StoreAndForward.Tests;
/// <summary>
/// M1.7: the StoreAndForwardService emits site operational events for its own
/// buffer/park activity — <c>store_and_forward</c> for cached-call categories
/// (ExternalSystem / CachedDbWrite) and <c>notification</c> for the site's
/// notification forward-to-central path. Emission rides the existing
/// <c>OnActivity</c> hook and is best-effort (a failing logger never affects
/// delivery bookkeeping).
/// </summary>
public class StoreAndForwardSiteEventTests : IAsyncLifetime, IDisposable
{
private sealed record Entry(string EventType, string Severity, string Source, string Message);
private sealed class FakeSiteEventLogger : ISiteEventLogger
{
private readonly ConcurrentQueue<Entry> _entries = new();
public IReadOnlyList<Entry> Entries => _entries.ToArray();
public IReadOnlyList<Entry> OfType(string t) => _entries.Where(e => e.EventType == t).ToArray();
public Task LogEventAsync(string eventType, string severity, string? instanceId,
string source, string message, string? details = null)
{
_entries.Enqueue(new Entry(eventType, severity, source, message));
return Task.CompletedTask;
}
public long FailedWriteCount => 0;
}
private readonly TestLocalDb _localDb;
private readonly StoreAndForwardStorage _storage;
private readonly StoreAndForwardOptions _options;
private readonly FakeSiteEventLogger _siteLog = new();
private readonly StoreAndForwardService _service;
public StoreAndForwardSiteEventTests()
{
_localDb = TestLocalDb.CreateTemp("SiteEvt");
_storage = new StoreAndForwardStorage(_localDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
_options = new StoreAndForwardOptions
{
DefaultRetryInterval = TimeSpan.Zero,
DefaultMaxRetries = 1,
RetryTimerInterval = TimeSpan.FromMinutes(10)
};
_service = new StoreAndForwardService(
_storage, _options, NullLogger<StoreAndForwardService>.Instance,
replication: null, cachedCallObserver: null, siteId: "site-a",
siteEventLogger: _siteLog);
}
public async Task InitializeAsync() => await _storage.InitializeAsync();
public Task DisposeAsync() => Task.CompletedTask;
public void Dispose()
{
var path = _localDb.Path;
_localDb.Dispose();
TestLocalDb.DeleteFiles(path);
}
[Fact]
public async Task BufferForRetry_ExternalSystem_EmitsStoreAndForwardSiteEvent()
{
_service.RegisterDeliveryHandler(StoreAndForwardCategory.ExternalSystem,
_ => throw new HttpRequestException("transient"));
await _service.EnqueueAsync(StoreAndForwardCategory.ExternalSystem, "api.example.com", """{}""", "Pump1");
var rows = _siteLog.OfType("store_and_forward");
Assert.Contains(rows, r => r.Severity == "Warning" &&
r.Source == "StoreAndForwardService" &&
r.Message.Contains("queued", StringComparison.OrdinalIgnoreCase));
// The cached-call categories must NOT surface as notification events.
Assert.Empty(_siteLog.OfType("notification"));
}
[Fact]
public async Task ForwardFailure_Notification_EmitsNotificationSiteEvent()
{
// The site's notification role is forward-to-central. When the immediate
// forward to central throws (central unreachable), the notification is
// buffered for retry — a forward FAILURE, which the spec says to log as a
// `notification` site event (filling the in-transit blind spot).
_service.RegisterDeliveryHandler(StoreAndForwardCategory.Notification,
_ => throw new HttpRequestException("central unreachable"));
await _service.EnqueueAsync(StoreAndForwardCategory.Notification, "list-a", """{}""", "Pump1");
var rows = _siteLog.OfType("notification");
Assert.Contains(rows, r => r.Severity == "Warning" &&
r.Source == "StoreAndForwardService" &&
r.Message.Contains("queued", StringComparison.OrdinalIgnoreCase));
// A notification forward-failure is not a store_and_forward (cached-call) event.
Assert.Empty(_siteLog.OfType("store_and_forward"));
}
[Fact]
public async Task RoutineEnqueue_Notification_DoesNotEmitSiteEvent()
{
// Spec: routine enqueue / forward-success on the notification path are
// deliberately NOT logged — central's Notifications table is the audit
// record of record. A successful immediate forward emits no site event.
_service.RegisterDeliveryHandler(StoreAndForwardCategory.Notification,
_ => Task.FromResult(true));
await _service.EnqueueAsync(StoreAndForwardCategory.Notification, "list-a", """{}""", "Pump1");
Assert.Empty(_siteLog.OfType("notification"));
}
[Fact]
public async Task Park_Notification_EmitsErrorNotificationSiteEvent()
{
// A long-buffered notification that exhausts retries is parked — the spec
// logs this as a `notification` event (Error severity).
_service.RegisterDeliveryHandler(StoreAndForwardCategory.Notification,
_ => throw new HttpRequestException("central unreachable"));
await _service.EnqueueAsync(
StoreAndForwardCategory.Notification, "list-a", """{}""", "Pump1",
attemptImmediateDelivery: false, maxRetries: 1);
await _service.RetryPendingMessagesAsync();
var rows = _siteLog.OfType("notification");
Assert.Contains(rows, r => r.Severity == "Error" &&
r.Message.Contains("parked", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task Park_ExternalSystem_EmitsErrorStoreAndForwardSiteEvent()
{
// MaxRetries = 1 → the first sweep retry parks the message.
_service.RegisterDeliveryHandler(StoreAndForwardCategory.ExternalSystem,
_ => throw new HttpRequestException("transient"));
await _service.EnqueueAsync(
StoreAndForwardCategory.ExternalSystem, "api.example.com", """{}""", "Pump1",
attemptImmediateDelivery: false, maxRetries: 1);
await _service.RetryPendingMessagesAsync();
var rows = _siteLog.OfType("store_and_forward");
Assert.Contains(rows, r => r.Severity == "Error" &&
r.Message.Contains("parked", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task DeliveredImmediately_DoesNotEmitSiteEvent()
{
// A successful immediate delivery is the normal hot path — it is not a
// store-and-forward buffering event, so no operational event is logged.
_service.RegisterDeliveryHandler(StoreAndForwardCategory.ExternalSystem,
_ => Task.FromResult(true));
await _service.EnqueueAsync(StoreAndForwardCategory.ExternalSystem, "api", """{}""", "Pump1");
Assert.Empty(_siteLog.OfType("store_and_forward"));
Assert.Empty(_siteLog.OfType("notification"));
}
}