using System.Collections.Concurrent; using System.Text.Json; using Akka.Actor; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging.Abstractions; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; using ZB.MOM.WW.OtOpcUa.Commons.Types; 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.OpcUaServer; using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa; using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.OpcUa; public sealed class OpcUaPublishActorRebuildTests : RuntimeActorTestBase { /// Tests that RebuildAddressSpace with dbFactory loads artifact, composes, and applies. [Fact] public void RebuildAddressSpace_with_dbFactory_loads_artifact_composes_and_applies() { var db = NewInMemoryDbFactory(); var sink = new RecordingSink(); var applier = new Phase7Applier(sink, NullLogger.Instance); SeedDeployment(db, equipmentIds: new[] { "eq-1", "eq-2" }, driverIds: new[] { "drv-1" }); var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests( sink: sink, dbFactory: db, applier: applier)); actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId())); AwaitAssert(() => { // Add path: Equipment + Driver + Alarm — but only Equipment/Alarm topology triggers // RebuildAddressSpace. With 2 new equipment we expect one Rebuild call. sink.RebuildCalls.ShouldBe(1); }, duration: TimeSpan.FromSeconds(2)); } /// Tests that rebuild with no artifact is idempotent no-op. [Fact] public void Rebuild_with_no_artifact_is_idempotent_no_op() { var db = NewInMemoryDbFactory(); var sink = new RecordingSink(); var applier = new Phase7Applier(sink, NullLogger.Instance); // No deployment seeded — LoadLatestArtifact returns empty blob. var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests( sink: sink, dbFactory: db, applier: applier)); actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId())); Thread.Sleep(200); sink.RebuildCalls.ShouldBe(0); } /// Tests that second rebuild with same artifact is empty plan no-op. [Fact] public void Second_rebuild_with_same_artifact_is_empty_plan_no_op() { var db = NewInMemoryDbFactory(); var sink = new RecordingSink(); var applier = new Phase7Applier(sink, NullLogger.Instance); SeedDeployment(db, equipmentIds: new[] { "eq-1" }, driverIds: Array.Empty()); var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests( sink: sink, dbFactory: db, applier: applier)); actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId())); AwaitAssert(() => sink.RebuildCalls.ShouldBe(1), duration: TimeSpan.FromSeconds(2)); actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId())); Thread.Sleep(200); // Same composition ⇒ plan IsEmpty ⇒ applier not called again. sink.RebuildCalls.ShouldBe(1); } /// Tests that rebuild without dbFactory falls back to raw sink rebuild. [Fact] public void Rebuild_without_dbFactory_falls_back_to_raw_sink_rebuild() { // Pre-#109 behavior: no dbFactory wired ⇒ RebuildAddressSpace calls _sink.RebuildAddressSpace // directly. The dev/Mac path before the full integration is bound. var sink = new RecordingSink(); var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(sink: sink)); actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId())); AwaitAssert(() => sink.RebuildCalls.ShouldBe(1), duration: TimeSpan.FromMilliseconds(500)); } private static void SeedDeployment( IDbContextFactory dbFactory, string[] equipmentIds, string[] driverIds) { var artifact = JsonSerializer.SerializeToUtf8Bytes(new { Equipment = equipmentIds.Select(id => new { EquipmentId = id, MachineCode = id.ToUpperInvariant(), UnsLineId = "line-1", Name = id, }).ToArray(), DriverInstances = driverIds.Select(id => new { DriverInstanceId = id, DriverType = "Modbus", Enabled = true, DriverConfig = "{}", }).ToArray(), ScriptedAlarms = Array.Empty(), }); using var ctx = dbFactory.CreateDbContext(); ctx.Deployments.Add(new Deployment { DeploymentId = Guid.NewGuid(), RevisionHash = new string('a', 64), Status = DeploymentStatus.Sealed, CreatedBy = "test", SealedAtUtc = DateTime.UtcNow, ArtifactBlob = artifact, }); ctx.SaveChanges(); } private sealed class RecordingSink : IOpcUaAddressSpaceSink { /// Gets the list of recorded sink calls. public ConcurrentQueue Calls { get; } = new(); /// Gets or sets the count of rebuild address space calls. public int RebuildCalls; /// Records a value write call. /// The OPC UA node ID. /// The value to write. /// The OPC UA quality code. /// The timestamp of the write. public void WriteValue(string nodeId, object? value, OpcUaQuality quality, DateTime ts) => Calls.Enqueue($"WV:{nodeId}"); /// Records an alarm state write call. /// The alarm node ID. /// Whether the alarm is active. /// Whether the alarm is acknowledged. /// The timestamp of the state change. public void WriteAlarmState(string alarmNodeId, bool active, bool acknowledged, DateTime ts) => Calls.Enqueue($"WA:{alarmNodeId}"); /// Records a folder ensure call. /// The folder node ID. /// The parent node ID, or null if this is a root folder. /// The display name of the folder. public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName) => Calls.Enqueue($"EF:{folderNodeId}"); /// Records a variable ensure call. /// The variable node ID. /// The parent folder node ID, or null if this is a root variable. /// The display name of the variable. /// The OPC UA built-in type name. public void EnsureVariable(string variableNodeId, string? parentFolderNodeId, string displayName, string dataType) => Calls.Enqueue($"EV:{variableNodeId}"); /// Records a rebuild address space call. public void RebuildAddressSpace() => Interlocked.Increment(ref RebuildCalls); } }