Phase 0 WP-0.2–0.9: Implement Commons (types, entities, interfaces, messages, protocol, tests)
- WP-0.2: Namespace/folder skeleton (26 directories) - WP-0.3: Shared data types (6 enums, RetryPolicy, Result<T>) - WP-0.4: 24 domain entity POCOs across 10 domain areas - WP-0.5: 7 repository interfaces with full CRUD signatures - WP-0.6: IAuditService cross-cutting interface - WP-0.7: 26 message contract records across 8 concern areas - WP-0.8: IDataConnection protocol abstraction with batch ops - WP-0.9: 8 architectural constraint enforcement tests All 40 tests pass, zero warnings.
This commit is contained in:
187
tests/ScadaLink.Commons.Tests/ArchitecturalConstraintTests.cs
Normal file
187
tests/ScadaLink.Commons.Tests/ArchitecturalConstraintTests.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
using System.Reflection;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ScadaLink.Commons.Tests;
|
||||
|
||||
public class ArchitecturalConstraintTests
|
||||
{
|
||||
private static readonly Assembly CommonsAssembly = typeof(ScadaLink.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", "ScadaLink.Commons", "ScadaLink.Commons.csproj");
|
||||
if (File.Exists(candidate))
|
||||
return candidate;
|
||||
dir = dir.Parent;
|
||||
}
|
||||
throw new InvalidOperationException("Could not find ScadaLink.Commons.csproj");
|
||||
}
|
||||
|
||||
private static string GetCommonsSourceDirectory()
|
||||
{
|
||||
var csproj = GetCsprojPath();
|
||||
return Path.GetDirectoryName(csproj)!;
|
||||
}
|
||||
|
||||
[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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Commons_ShouldNotContainServiceOrActorImplementations()
|
||||
{
|
||||
// Heuristic: class has > 3 public non-property methods that are not constructors
|
||||
var types = CommonsAssembly.GetTypes()
|
||||
.Where(t => t.IsClass && !t.IsAbstract && !t.IsInterface);
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
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")
|
||||
.ToList();
|
||||
|
||||
Assert.True(publicMethods.Count <= 3,
|
||||
$"Type {type.FullName} has {publicMethods.Count} public methods ({string.Join(", ", publicMethods.Select(m => m.Name))}), which suggests it may contain service/actor logic");
|
||||
}
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
||||
[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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[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");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllMessageTypes_ShouldBeRecords()
|
||||
{
|
||||
var messageTypes = CommonsAssembly.GetTypes()
|
||||
.Where(t => t.Namespace != null
|
||||
&& t.Namespace.Contains(".Messages.")
|
||||
&& !t.IsEnum && !t.IsInterface
|
||||
&& (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");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KeyEntities_ShouldHaveDateTimeOffsetTimestamps()
|
||||
{
|
||||
// Spot-check key entity timestamp properties
|
||||
var deploymentRecord = CommonsAssembly.GetType("ScadaLink.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("ScadaLink.Commons.Entities.Deployment.SystemArtifactDeploymentRecord");
|
||||
Assert.NotNull(artifactRecord);
|
||||
Assert.Equal(typeof(DateTimeOffset), artifactRecord!.GetProperty("DeployedAt")!.PropertyType);
|
||||
|
||||
var auditEntry = CommonsAssembly.GetType("ScadaLink.Commons.Entities.Audit.AuditLogEntry");
|
||||
Assert.NotNull(auditEntry);
|
||||
Assert.Equal(typeof(DateTimeOffset), auditEntry!.GetProperty("Timestamp")!.PropertyType);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user