Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/InterfaceIndependenceTests.cs
T
2026-11-23 09:55:04 -05:00

90 lines
3.8 KiB
C#

using System.Reflection;
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests;
/// <summary>
/// Asserts that <c>Core.Abstractions</c> stays a true contract project — it must not depend on
/// any implementation type, any other OtOpcUa project, or anything beyond BCL + System types.
/// Per <c>docs/v2/plan.md</c> decision #59 (Core.Abstractions internal-only for now; design as
/// if public to minimize churn later).
/// </summary>
public sealed class InterfaceIndependenceTests
{
private const string DriverContractNamespace = "ZB.MOM.WW.OtOpcUa.Core.Abstractions";
private const string DataConnectionContractNamespace = DriverContractNamespace + ".DataConnections";
private static readonly Assembly Assembly = typeof(IDriver).Assembly;
/// <summary>Verifies that the assembly has no references outside the BCL.</summary>
[Fact]
public void Assembly_HasNoReferencesOutsideBcl()
{
// Allowed reference assembly name prefixes — BCL + the assembly itself.
var allowed = new[]
{
"System",
"Microsoft.Win32",
"netstandard",
"mscorlib",
"ZB.MOM.WW.OtOpcUa.Core.Abstractions",
};
var referenced = Assembly.GetReferencedAssemblies();
var disallowed = referenced
.Where(r => !allowed.Any(a => r.Name!.StartsWith(a, StringComparison.Ordinal)))
.ToList();
disallowed.ShouldBeEmpty(
$"Core.Abstractions must reference only BCL/System assemblies. " +
$"Found disallowed references: {string.Join(", ", disallowed.Select(a => a.Name))}");
}
/// <summary>Verifies the assembly's driver and gRPC data contract namespaces.</summary>
[Fact]
public void AllPublicTypes_LiveInTheApprovedContractNamespaces()
{
var unexpectedTypes = Assembly.GetExportedTypes()
.Where(type => !string.Equals(type.Namespace, DriverContractNamespace, StringComparison.Ordinal) &&
!string.Equals(type.Namespace, DataConnectionContractNamespace, StringComparison.Ordinal))
.Select(type => type.FullName)
.ToArray();
unexpectedTypes.ShouldBeEmpty(
$"Core.Abstractions exports must use a documented contract namespace. Unexpected types: {string.Join(", ", unexpectedTypes)}");
}
/// <summary>Verifies that every capability interface is public.</summary>
/// <param name="type">The interface type to check.</param>
[Theory]
[InlineData(typeof(IDriver))]
[InlineData(typeof(ITagDiscovery))]
[InlineData(typeof(IReadable))]
[InlineData(typeof(IWritable))]
[InlineData(typeof(ISubscribable))]
[InlineData(typeof(IAlarmSource))]
[InlineData(typeof(IRediscoverable))]
[InlineData(typeof(IHostConnectivityProbe))]
[InlineData(typeof(ISkippedTagReporter))]
[InlineData(typeof(IDriverConfigEditor))]
[InlineData(typeof(IAddressSpaceBuilder))]
public void EveryCapabilityInterface_IsPublic(Type type)
{
type.Namespace.ShouldBe(DriverContractNamespace);
type.IsPublic.ShouldBeTrue($"{type.Name} must be public — drivers in separate assemblies implement it.");
type.IsInterface.ShouldBeTrue($"{type.Name} must be an interface, not a class.");
}
/// <summary>
/// Gitea #655: HistoryRead is served server-side by <c>IHistorianDataSource</c>; no driver-side history
/// capability exists. Re-adding one without also adding a dispatch path would recreate dead surface.
/// </summary>
[Fact]
public void NoDriverSideHistoryCapability_Exists()
{
Assembly.GetType("ZB.MOM.WW.OtOpcUa.Core.Abstractions.IHistoryProvider").ShouldBeNull();
Enum.GetNames<DriverCapability>().ShouldNotContain("HistoryRead");
}
}