using Akka.Actor; using Akka.TestKit.Xunit2; using NSubstitute; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol; using ZB.MOM.WW.ScadaBridge.Commons.Messages.DataConnection; using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums; using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Actors; using ZB.MOM.WW.ScadaBridge.HealthMonitoring; namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Actors; /// /// Review 2026-10-21 (lane A M2): a data connection name becomes its DataConnectionActor's /// name. The old sanitiser kept non-ASCII letters and '$' (both refused by Akka) and mapped /// "PLC A" and "PLC-A" to the same name; either made Context.ActorOf throw on the /// manager's own thread, restarting it and dropping every live connection. /// [Collection("DataConnectionManagerActor")] public class ConnectionActorNameTests : TestKit { private readonly IDataConnectionFactory _factory = Substitute.For(); private readonly ISiteHealthCollector _health = Substitute.For(); private readonly DataConnectionOptions _options = new() { ReconnectInterval = TimeSpan.FromMilliseconds(100), TagResolutionRetryInterval = TimeSpan.FromMilliseconds(200) }; /// Creates the test kit with a factory that hands out connected mock adapters. public ConnectionActorNameTests() { _factory.Create(Arg.Any(), Arg.Any>()).Returns(_ => { var adapter = Substitute.For(); adapter.ConnectAsync(Arg.Any>(), Arg.Any()) .Returns(Task.CompletedTask); adapter.Status.Returns(ConnectionHealth.Connected); adapter.SubscribeAsync(Arg.Any(), Arg.Any(), Arg.Any()) .Returns("sub-1"); return adapter; }); } /// Every sanitised name is a legal Akka path element, including non-ASCII and '$' input. [Theory] [InlineData("PLC A")] [InlineData("Ofen Süd")] [InlineData("$PLC")] [InlineData("名前")] [InlineData("a/b#c[d]%e")] [InlineData("")] public void Sanitize_AlwaysYieldsAValidActorName(string connectionName) { Assert.True(ActorPath.IsValidPathElement(ConnectionActorNames.Sanitize(connectionName))); } private IActorRef CreateManager() => Sys.ActorOf(Props.Create(() => new DataConnectionManagerActor(_factory, _options, _health))); private SubscribeTagsResponse Subscribe(IActorRef manager, string connection) { manager.Tell(new SubscribeTagsRequest( Guid.NewGuid().ToString("N"), "inst1", connection, ["tag1"], DateTimeOffset.UtcNow)); return ExpectMsg(TimeSpan.FromSeconds(10)); } /// A non-ASCII connection name is created instead of crashing the manager. [Fact] public void NonAsciiConnectionName_IsCreated() { var manager = CreateManager(); manager.Tell(new CreateConnectionCommand("Ofen Süd", "OpcUa", new Dictionary(), null, 3)); var response = Subscribe(manager, "Ofen Süd"); Assert.DoesNotContain("Unknown connection", response.ErrorMessage ?? ""); } /// /// Two names that sanitise alike: the second fails alone; the first — and the manager — /// keep working. /// [Fact] public void CollidingSanitisedNames_FailOnlyTheLaterConnection() { var manager = CreateManager(); manager.Tell(new CreateConnectionCommand("PLC A", "OpcUa", new Dictionary(), null, 3)); manager.Tell(new CreateConnectionCommand("PLC-A", "OpcUa", new Dictionary(), null, 3)); Assert.Contains("Unknown connection", Subscribe(manager, "PLC-A").ErrorMessage); Assert.DoesNotContain("Unknown connection", Subscribe(manager, "PLC A").ErrorMessage ?? ""); } }