using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Driver.FOCAS; namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests; /// /// CONV-4 — must resolve an equipment-tag reference (raw /// TagConfig JSON) to its OWN device host so per-host breaker keys are correct for equipment /// tags. An address-less equipment ref (FOCAS coalesces a missing deviceHostAddress to /// "") must fall back rather than key an empty host. /// [Trait("Category", "Unit")] public sealed class FocasResolveHostTests { private const string DeviceA = "focas://10.0.0.5:8193"; private const string DeviceB = "focas://10.0.0.6:8193"; // FOCAS's equipment-ref resolver validates against the initialized Devices map, so — unlike // the other drivers — the driver must be initialized (as it always is when ResolveHost runs at // runtime) before an equipment ref resolves. Probe off / no connect: init just parses devices. private static async Task NewDriverAsync() { var drv = new FocasDriver( new FocasDriverOptions { Devices = [new FocasDeviceOptions(DeviceA), new FocasDeviceOptions(DeviceB)], Probe = new FocasProbeOptions { Enabled = false }, }, "focas-1", new FakeFocasClientFactory()); await drv.InitializeAsync("{}", CancellationToken.None); return drv; } /// An equipment-tag ref naming device B resolves to device B's host, not the first device. [Fact] public async Task ResolveHost_EquipmentTagRef_ReturnsItsOwnDeviceHost() { var drv = await NewDriverAsync(); var json = $$"""{"deviceHostAddress":"{{DeviceB}}","address":"R100","dataType":"Byte"}"""; drv.ResolveHost(json).ShouldBe(DeviceB); } /// An equipment ref with no deviceHostAddress falls back to the first device, not an empty host. [Fact] public async Task ResolveHost_EquipmentTagRef_WithoutDeviceHost_FallsBack() { var drv = await NewDriverAsync(); var json = """{"address":"R100","dataType":"Byte"}"""; drv.ResolveHost(json).ShouldBe(DeviceA); } /// An unresolvable ref keeps the current first-device fallback. [Fact] public async Task ResolveHost_UnknownRef_KeepsCurrentFallback() { var drv = await NewDriverAsync(); drv.ResolveHost("totally-unknown").ShouldBe(DeviceA); } }