Files
Khaemhat/tests/Khaemhat.Core.Tests/Architecture/DependencyRulesTests.cs
T
Alby96andClaude Opus 5.5 12277733f5
CI / Build and test (SQLite) (pull_request) Successful in 43s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 15s
CI / Docker image (pull_request) Successful in 59s
feat: bootstrap solution, Docker image and Gitea pipelines (M0)
Start development with the skeleton every later milestone builds on, so
that code, image and release path exist and are verified from day one.

- Khaemhat.sln with the ten projects of SPEC section 3 and six MSTest
  projects; shared build rules with warnings as errors, analyzers and
  Central Package Management restricted to the allowlist.
- Architecture tests and scripts/check-packages.sh fail on any project
  reference or package that SPEC does not allow.
- Khaemhat.Web serves an Italian "under construction" page with the
  official icons and version, plus /health/live, /health/ready and
  /health (ADR 0012); honours KHAEMHAT_URLS and KHAEMHAT_LOG_LEVEL.
- docker/Dockerfile (non-root app user aligned to PUID/PGID by the
  entrypoint, pg_dump 17, healthcheck, OCI and Unraid labels) and
  compose files for PostgreSQL and a standalone SQLite demo.
- Gitea Actions: ci.yml (build, tests, image smoke test, PostgreSQL
  16/17 on tags, YAML check), release.yml (image and release from a
  vX.Y.Z tag, skipping versions already published) and wiki.yml.
- Development scripts, VS Code launch/tasks for debugging, and ADR
  0012-0017 deciding the M0 open points of docs/STATUS.md.

Closes #1, #2, #3, #4, #5, #114

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:53:39 +01:00

118 lines
4.9 KiB
C#

using System.Xml.Linq;
namespace Khaemhat.Core.Tests.Architecture;
/// <summary>
/// Project dependency rules of SPEC §3 and the package allowlist of SPEC §1.2 (ADR 0002). They read the project
/// files, so a forbidden reference fails here before anyone builds on it.
/// </summary>
[TestClass]
public sealed class DependencyRulesTests
{
// What each production project may reference: exactly SPEC §3.
private static readonly Dictionary<string, string[]> AllowedReferences = new(StringComparer.Ordinal)
{
["Khaemhat.Core"] = [],
["Khaemhat.Fiscal.Abstractions"] = ["Khaemhat.Core"],
["Khaemhat.Fiscal.Italy"] = ["Khaemhat.Core", "Khaemhat.Fiscal.Abstractions", "Khaemhat.Infrastructure"],
["Khaemhat.Data"] = ["Khaemhat.Core"],
["Khaemhat.Data.Sqlite"] = ["Khaemhat.Data"],
["Khaemhat.Data.PostgreSql"] = ["Khaemhat.Data"],
["Khaemhat.Infrastructure"] = ["Khaemhat.Core", "Khaemhat.Data"],
["Khaemhat.Application"] = ["Khaemhat.Core", "Khaemhat.Data", "Khaemhat.Infrastructure", "Khaemhat.Fiscal.Abstractions"],
["Khaemhat.Web"] = ["Khaemhat.Application", "Khaemhat.Data.Sqlite", "Khaemhat.Data.PostgreSql", "Khaemhat.Fiscal.Italy"],
["Khaemhat.LicenseTool"] = ["Khaemhat.Core", "Khaemhat.Infrastructure"],
};
// The only packages, and where each may be referenced.
private static readonly Dictionary<string, Func<string, bool>> AllowedPackages = new(StringComparer.OrdinalIgnoreCase)
{
["Npgsql"] = file => Name(file) == "Khaemhat.Data.PostgreSql",
["Microsoft.Data.Sqlite"] = file => Name(file) == "Khaemhat.Data.Sqlite",
["Microsoft.NET.Test.Sdk"] = IsTestFile,
["MSTest"] = IsTestFile,
};
[TestMethod]
public void EveryProductionProject_HasDependencyRules()
{
var unknown = RepositoryLayout.Projects().Keys
.Where(name => !name.EndsWith(".Tests", StringComparison.Ordinal) && !AllowedReferences.ContainsKey(name))
.ToList();
Assert.IsEmpty(unknown, "Projects without dependency rules (add them to SPEC §3 and this test): " + string.Join(", ", unknown));
}
[TestMethod]
public void ProductionProjects_ReferenceOnlyWhatSpecSection3Allows()
{
var violations = new List<string>();
foreach (var (name, file) in RepositoryLayout.Projects())
{
if (!AllowedReferences.TryGetValue(name, out var allowed))
{
continue;
}
foreach (var reference in RepositoryLayout.ProjectReferences(file).Except(allowed, StringComparer.Ordinal))
{
violations.Add($"{name} -> {reference}");
}
}
Assert.IsEmpty(violations, "Forbidden project references: " + string.Join("; ", violations));
}
[TestMethod]
public void TestProjects_ReferenceOnlyKhaemhatProjects()
{
var violations = RepositoryLayout.Projects()
.Where(p => p.Key.EndsWith(".Tests", StringComparison.Ordinal))
.SelectMany(p => RepositoryLayout.ProjectReferences(p.Value)
.Where(r => !r.StartsWith("Khaemhat.", StringComparison.Ordinal))
.Select(r => $"{p.Key} -> {r}"))
.ToList();
Assert.IsEmpty(violations, string.Join("; ", violations));
}
[TestMethod]
public void Packages_AreOnTheAllowlistAndInTheirProjects()
{
var violations = new List<string>();
foreach (var file in RepositoryLayout.MsBuildFiles())
{
foreach (var package in RepositoryLayout.PackageReferences(file))
{
if (!AllowedPackages.TryGetValue(package, out var allowedIn))
{
violations.Add($"{RepositoryLayout.Relative(file)}: {package} is not on the allowlist (SPEC §1.2)");
}
else if (!allowedIn(file))
{
violations.Add($"{RepositoryLayout.Relative(file)}: {package} is not allowed in this project");
}
}
}
Assert.IsEmpty(violations, string.Join(Environment.NewLine, violations));
}
[TestMethod]
public void CentralPackageVersions_ListOnlyAllowedPackages()
{
var file = Path.Combine(RepositoryLayout.Root, "Directory.Packages.props");
var extra = XDocument.Load(file).Descendants()
.Where(e => e.Name.LocalName == "PackageVersion")
.Select(e => (string?)e.Attribute("Include") ?? string.Empty)
.Where(p => !AllowedPackages.ContainsKey(p))
.ToList();
Assert.IsEmpty(extra, "Directory.Packages.props declares packages outside the allowlist: " + string.Join(", ", extra));
}
private static string Name(string file) => Path.GetFileNameWithoutExtension(file);
private static bool IsTestFile(string file) => RepositoryLayout.Relative(file).StartsWith("tests/", StringComparison.Ordinal);
}