Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests/ModbusUnitIdResolveHostTests.cs
Joseph Doherty aafb9d4929 v3(b1-modbus): RawPath identity seam — RawTagEntry-driven Modbus driver
Wave-B EXEMPLAR. Retire the pre-declared/blob-parse tag model; the driver now
builds its RawPath -> definition table from the artifact's RawTagEntry set via a
pure mapper.

- Contracts: rename ModbusEquipmentTagParser -> ModbusTagDefinitionFactory;
  TryParse(reference) -> FromTagConfig(tagConfig, rawPath) (def.Name = rawPath,
  no leading-brace heuristic); add inverse ToTagConfig(def) serializer; keep all
  strict-enum reads + guards; also read stringByteOrder/deadband/coalesceProhibited
  so a RawTagEntry round-trips the full authored def. Inspect() unchanged.
- Options: Tags(IReadOnlyList<ModbusTagDefinition>) -> RawTags(IReadOnlyList<RawTagEntry>).
- Driver: _tagsByName -> _tagsByRawPath (Ordinal); resolver byRawPath-only; build
  the table from RawTags at Initialize threading entry.WriteIdempotent onto each def;
  Discover/Teardown updated.
- Factory: remove ModbusTagDto/BuildTag/ValidateStringLength + ConfigDto.Tags; bind
  RawTags from driver-config JSON.
- CLI ModbusCommandBase.BuildOptions: serialise typed defs -> RawTagEntry via ToTagConfig.
- Tests: migrate Tags= -> RawTags via ModbusRawTags.Entries helper; parser tests ->
  FromTagConfig(rawPath); factory String-length/enum tests re-seated on the mapper seam.
- Propagated rename to ControlPlane EquipmentTagConfigInspector (outside Modbus projects).

Modbus.Tests 315/315, Addressing.Tests 161/161, Cli.Tests 72/72 green.
Docker-gated Driver.Modbus.IntegrationTests left untouched (won't compile against
the new API; expected).
2026-07-15 19:50:10 -04:00

77 lines
3.3 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
/// <summary>
/// CONV-4 (Modbus leg) — Modbus keys per-host resilience per SLAVE UNIT
/// (<c>host:port/unitN</c>). An authored raw tag carries its own <c>unitId</c> in its TagConfig so
/// multi-unit tags key their own breaker; <see cref="ModbusDriver.ResolveHost"/> resolves a RawPath
/// through the driver's authored RawPath → definition table. Under v3 the reference is always a
/// RawPath — an unauthored RawPath falls back to the driver-level host.
/// </summary>
[Trait("Category", "Unit")]
public sealed class ModbusUnitIdResolveHostTests
{
private sealed class NoopTransport : IModbusTransport
{
public Task ConnectAsync(CancellationToken ct) => Task.CompletedTask;
public Task<byte[]> SendAsync(byte unitId, byte[] pdu, CancellationToken ct)
=> Task.FromResult(new byte[] { 0x03, 0x00 });
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}
private static async Task<ModbusDriver> NewDriverAsync(params RawTagEntry[] rawTags)
{
var drv = new ModbusDriver(
new ModbusDriverOptions
{
Host = "10.0.0.1", Port = 502, UnitId = 1, RawTags = rawTags,
Probe = new ModbusProbeOptions { Enabled = false },
},
"modbus-1", _ => new NoopTransport());
await drv.InitializeAsync("{}", CancellationToken.None);
return drv;
}
/// <summary>The mapper reads an optional unitId into the definition.</summary>
[Fact]
public void FromTagConfig_reads_optional_unitId()
{
var json = """{"region":"HoldingRegisters","address":0,"dataType":"Int16","unitId":7}""";
ModbusTagDefinitionFactory.FromTagConfig(json, "cell/modbus/dev1/U7", out var def).ShouldBeTrue();
def.Name.ShouldBe("cell/modbus/dev1/U7");
def.UnitId.ShouldBe((byte)7);
}
/// <summary>An absent unitId leaves the definition on the driver-level default (null override).</summary>
[Fact]
public void FromTagConfig_absent_unitId_is_null()
{
var json = """{"region":"HoldingRegisters","address":0,"dataType":"Int16"}""";
ModbusTagDefinitionFactory.FromTagConfig(json, "cell/modbus/dev1/U0", out var def).ShouldBeTrue();
def.UnitId.ShouldBeNull();
}
/// <summary>An authored RawPath carrying a unitId keys its own per-unit host name.</summary>
[Fact]
public async Task RawPath_WithUnitId_ResolvesPerUnitHostName()
{
const string rawPath = "cell/modbus/dev1/U7";
var json = """{"region":"HoldingRegisters","address":0,"dataType":"Int16","unitId":7}""";
var drv = await NewDriverAsync(new RawTagEntry(rawPath, json, false));
drv.ResolveHost(rawPath).ShouldBe("10.0.0.1:502/unit7");
}
/// <summary>An authored RawPath without a unitId keys the driver-level unit host name.</summary>
[Fact]
public async Task RawPath_WithoutUnitId_KeysDriverDefaultUnit()
{
const string rawPath = "cell/modbus/dev1/U0";
var json = """{"region":"HoldingRegisters","address":0,"dataType":"Int16"}""";
var drv = await NewDriverAsync(new RawTagEntry(rawPath, json, false));
drv.ResolveHost(rawPath).ShouldBe("10.0.0.1:502/unit1");
}
}