using System.Reflection;
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests;
///
/// Asserts that Core.Abstractions stays a true contract project — it must not depend on
/// any implementation type, any other OtOpcUa project, or anything beyond BCL + System types.
/// Per docs/v2/plan.md decision #59 (Core.Abstractions internal-only for now; design as
/// if public to minimize churn later).
///
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;
/// Verifies that the assembly has no references outside the BCL.
[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))}");
}
/// Verifies the assembly's driver and gRPC data contract namespaces.
[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)}");
}
/// Verifies that every capability interface is public.
/// The interface type to check.
[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.");
}
///
/// Gitea #655: HistoryRead is served server-side by IHistorianDataSource; no driver-side history
/// capability exists. Re-adding one without also adding a dispatch path would recreate dead surface.
///
[Fact]
public void NoDriverSideHistoryCapability_Exists()
{
Assembly.GetType("ZB.MOM.WW.OtOpcUa.Core.Abstractions.IHistoryProvider").ShouldBeNull();
Enum.GetNames().ShouldNotContain("HistoryRead");
}
}