using Opc.Ua;
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Tests;
///
/// Regression coverage for driver-specs.md §8 namespace handling — the
/// TargetNamespaceKind startup enforcement and the server-stable NodeId encoding
/// that produces (findings Driver.OpcUaClient-004).
///
[Trait("Category", "Unit")]
public sealed class OpcUaClientNamespaceTests
{
// ---- Driver.OpcUaClient-004: TargetNamespaceKind startup enforcement ----
[Fact]
public void ValidateNamespaceKind_Equipment_without_mapping_table_is_rejected()
{
// §8: an Equipment-kind instance gateways raw equipment data and MUST carry a
// config-driven UNS mapping table — remote nodes don't conform to UNS by default.
var opts = new OpcUaClientDriverOptions { TargetNamespaceKind = OpcUaTargetNamespaceKind.Equipment };
Should.Throw(() => OpcUaClientDriver.ValidateNamespaceKind(opts))
.Message.ShouldContain("UnsMappingTable");
}
[Fact]
public void ValidateNamespaceKind_Equipment_with_mapping_table_passes()
{
var opts = new OpcUaClientDriverOptions
{
TargetNamespaceKind = OpcUaTargetNamespaceKind.Equipment,
UnsMappingTable = new Dictionary { ["Line1"] = "Site/AreaA/Line1" },
};
Should.NotThrow(() => OpcUaClientDriver.ValidateNamespaceKind(opts));
}
[Fact]
public void ValidateNamespaceKind_SystemPlatform_with_mapping_table_is_rejected()
{
// §8: processed data preserves its own hierarchy — a UNS mapping table is ambiguous.
var opts = new OpcUaClientDriverOptions
{
TargetNamespaceKind = OpcUaTargetNamespaceKind.SystemPlatform,
UnsMappingTable = new Dictionary { ["x"] = "y" },
};
Should.Throw(() => OpcUaClientDriver.ValidateNamespaceKind(opts))
.Message.ShouldContain("SystemPlatform");
}
[Fact]
public void ValidateNamespaceKind_SystemPlatform_without_mapping_table_passes()
{
var opts = new OpcUaClientDriverOptions { TargetNamespaceKind = OpcUaTargetNamespaceKind.SystemPlatform };
Should.NotThrow(() => OpcUaClientDriver.ValidateNamespaceKind(opts));
}
[Fact]
public void Default_TargetNamespaceKind_is_Equipment()
{
// §8 comparison table: OPC UA Client default lane is Equipment.
new OpcUaClientDriverOptions().TargetNamespaceKind.ShouldBe(OpcUaTargetNamespaceKind.Equipment);
}
// ---- Driver.OpcUaClient-004: server-stable NodeId encoding ----
[Fact]
public void NamespaceMap_FromSession_rejects_null_session() =>
Should.Throw(() => NamespaceMap.FromSession(null!));
[Fact]
public void NamespaceMap_namespace0_NodeId_keeps_compact_form()
{
// Namespace 0 is fixed by the OPC UA spec — it never moves, so the compact i=… form
// is already server-stable and must not be rewritten to nsu=…
var map = TestMap("http://opcfoundation.org/UA/", "urn:remote:server");
var coreNode = new NodeId(2253u); // i=2253, Server object, namespace 0
map.ToStableReference(coreNode).ShouldBe(coreNode.ToString());
map.ToStableReference(coreNode).ShouldNotContain("nsu=");
}
[Fact]
public void NamespaceMap_nonzero_namespace_NodeId_is_encoded_with_uri_not_index()
{
// A ns=1 reference is session-relative; if the remote reorders its table the index
// points at the wrong namespace. ToStableReference must embed the URI instead.
var map = TestMap("http://opcfoundation.org/UA/", "urn:remote:server");
var node = new NodeId("Temperature", 1);
var stable = map.ToStableReference(node);
stable.ShouldContain("nsu=urn:remote:server");
stable.ShouldContain("s=Temperature");
stable.ShouldNotStartWith("ns=1");
}
[Fact]
public void NamespaceMap_unknown_namespace_index_falls_back_to_raw_form()
{
// An index past the captured table can't be URI-encoded; fall back rather than throw —
// the read/write path surfaces BadNodeIdInvalid if it truly can't resolve.
var map = TestMap("http://opcfoundation.org/UA/");
var node = new NodeId("Orphan", 5);
Should.NotThrow(() => map.ToStableReference(node));
}
[Fact]
public void NamespaceMap_index_and_uri_lookups_are_bidirectional()
{
var map = TestMap("http://opcfoundation.org/UA/", "urn:remote:server", "urn:remote:vendor");
map.Count.ShouldBe(3);
map.UriForIndex(1).ShouldBe("urn:remote:server");
map.IndexForUri("urn:remote:vendor").ShouldBe((ushort)2);
map.IndexForUri("urn:not:present").ShouldBeNull();
map.UriForIndex(99).ShouldBeNull();
}
[Fact]
public void NamespaceMap_TryResolve_rejects_empty_and_null_input()
{
NamespaceMap.TryResolve(null!, "ns=1;s=X", out _).ShouldBeFalse();
}
///
/// Build a directly from a URI table, mirroring what
/// snapshots — lets the encoding be tested
/// without standing up a live ISession.
///
private static NamespaceMap TestMap(params string[] uris)
{
var table = new NamespaceTable();
// Index 0 (the OPC UA core namespace) is seeded by NamespaceTable's ctor; append the
// rest. Skip uris[0] when it is the core namespace to avoid a duplicate.
for (var i = 0; i < uris.Length; i++)
{
if (i == 0 && uris[i] == "http://opcfoundation.org/UA/") continue;
table.Append(uris[i]);
}
return NamespaceMap.FromTable(table);
}
}