namespace Khaemhat.Core.Tests.Architecture;
///
/// ADR 0007 and ADR 0025: the Italian pack is reached only through the interfaces of Khaemhat.Fiscal.Abstractions.
/// Project references already keep it out of Application; these tests keep it out of the sources of
/// Web too, except for the composition root.
///
[TestClass]
public sealed class FiscalPackIsolationTests
{
private const string ItalianNamespace = "Khaemhat.Fiscal.Italy";
// The composition root registers the pack; nothing else names it.
private static readonly HashSet AllowedOutsideThePack = new(StringComparer.Ordinal)
{
"src/Khaemhat.Web/Hosting/KhaemhatServices.cs",
};
[TestMethod]
public void OnlyTheCompositionRoot_NamesTheItalianPack()
{
var violations = SourceFiles()
.Where(f => !f.Relative.StartsWith("src/Khaemhat.Fiscal.Italy/", StringComparison.Ordinal))
.Where(f => !AllowedOutsideThePack.Contains(f.Relative))
.Where(f => File.ReadAllText(f.Path).Contains(ItalianNamespace, StringComparison.Ordinal))
.Select(f => f.Relative)
.ToList();
Assert.IsEmpty(violations, "Use IFiscalPack and FiscalPackCatalog instead of the Italian pack in: " + string.Join(", ", violations));
}
[TestMethod]
public void TheAbstractions_SayNothingItalian()
{
string[] italianTerms = ["Italy", "Italian", "FatturaPa", "Iva"];
var violations = SourceFiles()
.Where(f => f.Relative.StartsWith("src/Khaemhat.Fiscal.Abstractions/", StringComparison.Ordinal))
.SelectMany(f => italianTerms
.Where(term => CodeLines(f.Path).Any(line => line.Contains(term, StringComparison.Ordinal)))
.Select(term => $"{f.Relative}: {term}"))
.ToList();
Assert.IsEmpty(violations, "Country-specific names belong in the country pack: " + string.Join("; ", violations));
}
[TestMethod]
public void TheCompositionRoot_RegistersThePackBehindTheCatalog()
{
var root = File.ReadAllText(Path.Combine(RepositoryLayout.Root, "src", "Khaemhat.Web", "Hosting", "KhaemhatServices.cs"));
StringAssert.Contains(root, "AddSingleton()");
StringAssert.Contains(root, "new FiscalPackCatalog(");
}
private static IEnumerable<(string Path, string Relative)> SourceFiles() =>
Directory.EnumerateFiles(Path.Combine(RepositoryLayout.Root, "src"), "*.*", SearchOption.AllDirectories)
.Where(f => f.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".razor", StringComparison.OrdinalIgnoreCase))
.Select(f => (Path: f, Relative: RepositoryLayout.Relative(f)))
.Where(f => !f.Relative.Contains("/obj/", StringComparison.Ordinal) && !f.Relative.Contains("/bin/", StringComparison.Ordinal));
// Comments may cite the country as an example (Italy: N1...N7); code may not.
private static IEnumerable CodeLines(string path) =>
File.ReadLines(path).Where(line => !line.TrimStart().StartsWith("//", StringComparison.Ordinal));
}