Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Commons.Tests/ArchitecturalConstraintTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

217 lines
9.2 KiB
C#

using System.Reflection;
using System.Xml.Linq;
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests;
public class ArchitecturalConstraintTests
{
private static readonly Assembly CommonsAssembly = typeof(ZB.MOM.WW.ScadaBridge.Commons.Types.RetryPolicy).Assembly;
private static string GetCsprojPath()
{
// Walk up from test output to find the Commons csproj
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir != null)
{
var candidate = Path.Combine(dir.FullName, "src", "ZB.MOM.WW.ScadaBridge.Commons", "ZB.MOM.WW.ScadaBridge.Commons.csproj");
if (File.Exists(candidate))
return candidate;
dir = dir.Parent;
}
throw new InvalidOperationException("Could not find ZB.MOM.WW.ScadaBridge.Commons.csproj");
}
private static string GetCommonsSourceDirectory()
{
var csproj = GetCsprojPath();
return Path.GetDirectoryName(csproj)!;
}
/// <summary>Verifies that the Commons project file declares none of the forbidden package references.</summary>
[Fact]
public void Csproj_ShouldNotHaveForbiddenPackageReferences()
{
var csprojPath = GetCsprojPath();
var doc = XDocument.Load(csprojPath);
var ns = doc.Root!.GetDefaultNamespace();
var forbiddenPrefixes = new[] { "Akka.", "Microsoft.AspNetCore.", "Microsoft.EntityFrameworkCore." };
var packageRefs = doc.Descendants(ns + "PackageReference")
.Select(e => e.Attribute("Include")?.Value)
.Where(v => v != null)
.ToList();
foreach (var pkg in packageRefs)
{
foreach (var prefix in forbiddenPrefixes)
{
Assert.False(pkg!.StartsWith(prefix, StringComparison.OrdinalIgnoreCase),
$"Commons has forbidden package reference: {pkg}");
}
}
}
/// <summary>Verifies that the Commons assembly references none of the forbidden assemblies.</summary>
[Fact]
public void Assembly_ShouldNotReferenceForbiddenAssemblies()
{
var forbiddenPrefixes = new[] { "Akka.", "Microsoft.AspNetCore.", "Microsoft.EntityFrameworkCore." };
var referencedAssemblies = CommonsAssembly.GetReferencedAssemblies();
foreach (var asmName in referencedAssemblies)
{
foreach (var prefix in forbiddenPrefixes)
{
Assert.False(asmName.Name!.StartsWith(prefix, StringComparison.OrdinalIgnoreCase),
$"Commons references forbidden assembly: {asmName.Name}");
}
}
}
/// <summary>Verifies that Commons contains no service or actor implementations.</summary>
[Fact]
public void Commons_ShouldNotContainServiceOrActorImplementations()
{
// Heuristic: class has > 3 public methods that are neither constructors,
// property accessors, common Object overrides, nor interface-implementation
// methods (a dictionary wrapper exposing ContainsKey/TryGetValue/GetEnumerator
// via IReadOnlyDictionary isn't a service — that's just the interface).
var types = CommonsAssembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && !t.IsInterface);
foreach (var type in types)
{
var interfaceMethodNames = type.GetInterfaces()
.SelectMany(i => i.GetMethods())
.Select(m => m.Name)
.ToHashSet(StringComparer.Ordinal);
var publicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(m => !m.IsSpecialName) // excludes property getters/setters
.Where(m => !m.Name.StartsWith("<")) // excludes compiler-generated
.Where(m => m.Name != "ToString" && m.Name != "GetHashCode" &&
m.Name != "Equals" && m.Name != "Deconstruct" &&
m.Name != "PrintMembers" && m.Name != "GetType")
.Where(m => !interfaceMethodNames.Contains(m.Name))
.ToList();
Assert.True(publicMethods.Count <= 3,
$"Type {type.FullName} has {publicMethods.Count} public non-interface methods ({string.Join(", ", publicMethods.Select(m => m.Name))}), which suggests it may contain service/actor logic");
}
}
/// <summary>Verifies that no source file calls <c>ToLocalTime</c>, since all timestamps are UTC.</summary>
[Fact]
public void SourceFiles_ShouldNotContainToLocalTime()
{
var sourceDir = GetCommonsSourceDirectory();
var csFiles = Directory.GetFiles(sourceDir, "*.cs", SearchOption.AllDirectories);
foreach (var file in csFiles)
{
var content = File.ReadAllText(file);
Assert.DoesNotContain("ToLocalTime()", content);
}
}
/// <summary>Verifies that no entity class carries Entity Framework attributes.</summary>
[Fact]
public void AllEntities_ShouldHaveNoEfAttributes()
{
var efAttributeNames = new HashSet<string>
{
"KeyAttribute", "ForeignKeyAttribute", "TableAttribute",
"ColumnAttribute", "RequiredAttribute", "MaxLengthAttribute",
"StringLengthAttribute", "DatabaseGeneratedAttribute",
"NotMappedAttribute", "IndexAttribute", "InversePropertyAttribute"
};
var entityTypes = CommonsAssembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.Namespace != null
&& t.Namespace.Contains(".Entities."));
foreach (var type in entityTypes)
{
foreach (var attr in type.GetCustomAttributes(true))
{
Assert.DoesNotContain(attr.GetType().Name, efAttributeNames);
}
foreach (var prop in type.GetProperties())
{
foreach (var attr in prop.GetCustomAttributes(true))
{
Assert.False(efAttributeNames.Contains(attr.GetType().Name),
$"{type.Name}.{prop.Name} has EF attribute {attr.GetType().Name}");
}
}
}
}
/// <summary>Verifies that the retired <c>DebugStreamEvent</c> type does not exist.</summary>
[Fact]
public void DebugStreamEvent_ShouldNotExist()
{
// DebugStreamEvent was removed when debug streaming moved from ClusterClient to gRPC.
// Events now flow via SiteStreamManager → StreamRelayActor → gRPC channel.
var type = CommonsAssembly.GetTypes().FirstOrDefault(t => t.Name == "DebugStreamEvent");
Assert.Null(type);
}
/// <summary>Verifies that every enum has a singular name.</summary>
[Fact]
public void AllEnums_ShouldBeSingularNamed()
{
var enums = CommonsAssembly.GetTypes().Where(t => t.IsEnum);
foreach (var enumType in enums)
{
var name = enumType.Name;
// Singular names should not end with 's' (except words ending in 'ss' or 'us' which are singular)
Assert.False(name.EndsWith("s") && !name.EndsWith("ss") && !name.EndsWith("us"),
$"Enum {name} appears to be plural");
}
}
/// <summary>Verifies that every message type is a record.</summary>
[Fact]
public void AllMessageTypes_ShouldBeRecords()
{
var messageTypes = CommonsAssembly.GetTypes()
.Where(t => t.Namespace != null
&& t.Namespace.Contains(".Messages.")
&& !t.IsEnum && !t.IsInterface
&& !(t.IsAbstract && t.IsSealed) // exclude static classes (utilities)
&& !t.Name.StartsWith("<") // exclude compiler-generated types
&& (t.IsClass || (t.IsValueType && !t.IsPrimitive)));
foreach (var type in messageTypes)
{
var cloneMethod = type.GetMethod("<Clone>$", BindingFlags.Public | BindingFlags.Instance);
Assert.True(cloneMethod != null,
$"{type.FullName} in Messages namespace should be a record type");
}
}
/// <summary>Verifies that the key entities use <see cref="DateTimeOffset"/> for their timestamp properties.</summary>
[Fact]
public void KeyEntities_ShouldHaveDateTimeOffsetTimestamps()
{
// Spot-check key entity timestamp properties
var deploymentRecord = CommonsAssembly.GetType("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.DeploymentRecord");
Assert.NotNull(deploymentRecord);
Assert.Equal(typeof(DateTimeOffset), deploymentRecord!.GetProperty("DeployedAt")!.PropertyType);
Assert.Equal(typeof(DateTimeOffset?), deploymentRecord.GetProperty("CompletedAt")!.PropertyType);
var artifactRecord = CommonsAssembly.GetType("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.SystemArtifactDeploymentRecord");
Assert.NotNull(artifactRecord);
Assert.Equal(typeof(DateTimeOffset), artifactRecord!.GetProperty("DeployedAt")!.PropertyType);
var auditEntry = CommonsAssembly.GetType("ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit.AuditLogEntry");
Assert.NotNull(auditEntry);
Assert.Equal(typeof(DateTimeOffset), auditEntry!.GetProperty("Timestamp")!.PropertyType);
}
}