using Akka.Actor; using Microsoft.EntityFrameworkCore; using Xunit; using ZB.MOM.WW.OtOpcUa.Configuration; using ZB.MOM.WW.OtOpcUa.Configuration.Entities; using ZB.MOM.WW.OtOpcUa.Configuration.Enums; using ZB.MOM.WW.OtOpcUa.ControlPlane.Fleet; using ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Harness; namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests; /// /// Proves the reconciler is actually wired to something — subscribes to membership, reads the DB, /// and emits. covers the comparison logic, which a /// dormant actor would leave perfectly correct and completely inert. /// public sealed class ClusterNodeAddressReconcilerActorTests : ControlPlaneActorTestBase { /// /// An enabled row with no matching driver member is warned about. The harness ActorSystem /// joins as admin with no driver members, so the seeded row is unmatched by /// construction. /// [Fact] public void Enabled_row_with_no_matching_member_is_logged() { var dbFactory = NewInMemoryDbFactory(); SeedNode(dbFactory, "site-a-1:4053", enabled: true); EventFilter.Warning(contains: "site-a-1:4053").ExpectOne(TimeSpan.FromSeconds(10), () => Sys.ActorOf(ClusterNodeAddressReconcilerActor.Props(dbFactory))); } /// /// The finding set is logged once, not once per sweep. A check that reprints the same warning /// every five minutes trains operators to filter it out, which costs more than it catches — /// so this runs a deliberately fast sweep and asserts the count stays at one. /// [Fact] public void Unchanged_findings_are_not_repeated_on_every_sweep() { var dbFactory = NewInMemoryDbFactory(); SeedNode(dbFactory, "site-a-1:4053", enabled: true); // ~6 sweeps inside the window; without the change-detection guard this logs 6 times. EventFilter.Warning(contains: "site-a-1:4053").Expect(1, TimeSpan.FromSeconds(3), () => { Sys.ActorOf(ClusterNodeAddressReconcilerActor.Props( dbFactory, sweepInterval: TimeSpan.FromMilliseconds(400))); // Hold the filter open past several sweeps rather than returning immediately. ExpectNoMsg(TimeSpan.FromSeconds(2.5)); }); } /// /// A disabled row is silence — the maintenance hatch must not trade a failed deployment for a /// permanent warning. Positive control for the test above: same setup, one flag flipped. /// [Fact] public void Disabled_row_produces_no_warning() { var dbFactory = NewInMemoryDbFactory(); SeedNode(dbFactory, "site-a-1:4053", enabled: false); EventFilter.Warning(contains: "site-a-1:4053").Expect(0, TimeSpan.FromSeconds(2), () => { Sys.ActorOf(ClusterNodeAddressReconcilerActor.Props( dbFactory, sweepInterval: TimeSpan.FromMilliseconds(400))); ExpectNoMsg(TimeSpan.FromSeconds(1.5)); }); } private static void SeedNode( IDbContextFactory dbFactory, string nodeId, bool enabled) { using var db = dbFactory.CreateDbContext(); db.ServerClusters.Add(new ServerCluster { ClusterId = "SITE-A", Name = "Site A", Enterprise = "zb", Site = "site-a", NodeCount = 2, RedundancyMode = RedundancyMode.Warm, CreatedBy = "test", }); db.ClusterNodes.Add(new ClusterNode { NodeId = nodeId, ClusterId = "SITE-A", Host = nodeId.Split(':')[0], ApplicationUri = $"urn:OtOpcUa:{nodeId}", Enabled = enabled, CreatedBy = "test", }); db.SaveChanges(); } }