using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS;
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests;
///
/// CONV-4 — must resolve an authored raw tag (by its RawPath)
/// to its OWN device host so per-host breaker keys are correct for multi-device drivers. A reference
/// that resolves to no authored tag falls back to the first device rather than keying 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";
// The driver builds its RawPath → definition table (with each tag's resolved device host) at
// InitializeAsync, so the driver must be initialized before ResolveHost can resolve a tag — as it
// always is when ResolveHost runs at runtime. Probe off / no connect: init just parses devices + tags.
private static async Task NewDriverAsync(params FocasTagDefinition[] tags)
{
var drv = new FocasDriver(
new FocasDriverOptions
{
Devices = [new FocasDeviceOptions(DeviceA), new FocasDeviceOptions(DeviceB)],
RawTags = FocasRawTags.Entries(tags),
Probe = new FocasProbeOptions { Enabled = false },
},
"focas-1", new FakeFocasClientFactory());
await drv.InitializeAsync("{}", CancellationToken.None);
return drv;
}
/// An authored tag routed to device B resolves to device B's host, not the first device.
[Fact]
public async Task ResolveHost_AuthoredTag_ReturnsItsOwnDeviceHost()
{
var drv = await NewDriverAsync(new FocasTagDefinition("eq/onB", DeviceB, "R100", FocasDataType.Byte));
drv.ResolveHost("eq/onB").ShouldBe(DeviceB);
}
/// A reference that resolves to no authored tag falls back to the first device, not an empty host.
[Fact]
public async Task ResolveHost_UnresolvedRef_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);
}
}