e42786df97
AEG-X-003: Architecture Tests (COMPLETED) ✅ Added 6th rule: No duplicate aggregate IDs across modules ✅ All 6 architecture tests PASS: 1. No prohibited source patterns (IGenericRepository, DateTime.Now, etc.) 2. Domain isolation from infrastructure (no Dapper, Npgsql, FastEndpoints) 3. SQL validation (no SELECT *, schema-qualified tables) 4. Endpoint authorization (Roles or Policies required) 5. No placeholder files (testfile, *.tmp) 6. No duplicate aggregate IDs (new) Acceptance_Evidence: Domain 기술의존 0, 모듈 직접 DB 접근 0, ID 중복 0 ✅ AEG-X-004: DbUp Recovery Rehearsal (Ready for DB Testing) - Tests located: tests/KArtSell.Integration.Tests/DbUpMigrationTests.cs (570L) - Covers 4 scenarios: Fresh install, Upgrade, Re-run, Failure recovery - Infrastructure: Requires PostgreSQL + SSH tunnel for execution - Evidence collection: Requires active DB connection (pending) Phase 1 Progress: - AEG-X-001: ✅ COMPLETED (VERSION_COVERAGE_MATRIX.md) - AEG-X-002: ✅ COMPLETED (CI.yml formalized) - AEG-X-003: ✅ COMPLETED (6 architecture tests PASS) - AEG-X-004: 📋 READY FOR DB TESTING (test structure exists) - AEG-X-005: 📋 PLANNED (next in sequence) Cumulative Status: 3/5 = 60% Phase 1 complete (3h/15h estimated) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
180 lines
7.5 KiB
C#
180 lines
7.5 KiB
C#
using Xunit;
|
|
|
|
namespace KArtSell.ArchitectureTests;
|
|
|
|
public sealed class RepositoryRulesTests
|
|
{
|
|
[Fact]
|
|
public void Prohibited_source_patterns_are_not_introduced()
|
|
{
|
|
var repositoryRoot = FindRepositoryRoot();
|
|
var sourceFiles = Directory.EnumerateFiles(repositoryRoot, "*.cs", SearchOption.AllDirectories)
|
|
.Where(x => !IsGeneratedOrTestOutput(x))
|
|
.ToArray();
|
|
|
|
AssertNoPattern(sourceFiles, "IGenericRepository", "Generic repository is prohibited.");
|
|
AssertNoPattern(sourceFiles, "DateTime.Now", "Use IClock and MarketCalendar.");
|
|
AssertNoPattern(sourceFiles, "DateTime.UtcNow", "Use IClock and MarketCalendar.");
|
|
AssertNoPattern(sourceFiles, "IServiceProvider.GetService", "Service locator is prohibited.");
|
|
AssertNoPattern(sourceFiles, "AllowAnonymous()", "Module endpoints cannot be anonymous.");
|
|
}
|
|
|
|
[Fact]
|
|
public void Domain_files_do_not_reference_infrastructure_frameworks()
|
|
{
|
|
var root = FindRepositoryRoot();
|
|
var domainFiles = Directory.EnumerateFiles(
|
|
Path.Combine(root, "src"),
|
|
"*.cs",
|
|
SearchOption.AllDirectories)
|
|
.Where(path => path.Contains(
|
|
$"{Path.DirectorySeparatorChar}Domain{Path.DirectorySeparatorChar}",
|
|
StringComparison.Ordinal))
|
|
.ToArray();
|
|
|
|
foreach (var prohibited in new[]
|
|
{
|
|
"using Dapper", "using Npgsql", "using FastEndpoints", "using Hangfire",
|
|
"HttpContext", "DbConnection", "IServiceCollection"
|
|
})
|
|
{
|
|
AssertNoPattern(domainFiles, prohibited,
|
|
$"Domain cannot depend on infrastructure framework: {prohibited}.");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Sql_does_not_use_select_star_or_unqualified_signal_tables()
|
|
{
|
|
var repositoryRoot = FindRepositoryRoot();
|
|
var files = Directory.EnumerateFiles(repositoryRoot, "*.*", SearchOption.AllDirectories)
|
|
.Where(x => x.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)
|
|
|| x.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
|
|
.Where(x => !IsGeneratedOrTestOutput(x))
|
|
.ToArray();
|
|
|
|
var selectStar = files.Where(x =>
|
|
File.ReadAllText(x).Contains("select *", StringComparison.OrdinalIgnoreCase)).ToArray();
|
|
Assert.True(selectStar.Length == 0,
|
|
"SELECT * is prohibited: " + string.Join(", ", selectStar));
|
|
|
|
var unqualified = files.Where(x =>
|
|
{
|
|
var text = File.ReadAllText(x);
|
|
return text.Contains(" from signal_decision", StringComparison.OrdinalIgnoreCase)
|
|
|| text.Contains(" into signal_decision", StringComparison.OrdinalIgnoreCase)
|
|
|| text.Contains(" from evidence_snapshot", StringComparison.OrdinalIgnoreCase);
|
|
}).ToArray();
|
|
Assert.True(unqualified.Length == 0,
|
|
"SignalEngine SQL must be schema-qualified: " + string.Join(", ", unqualified));
|
|
}
|
|
|
|
[Fact]
|
|
public void Every_module_endpoint_declares_roles_or_policies()
|
|
{
|
|
var root = FindRepositoryRoot();
|
|
var endpoints = Directory.EnumerateFiles(
|
|
Path.Combine(root, "src"),
|
|
"Endpoint.cs",
|
|
SearchOption.AllDirectories)
|
|
.Where(path => path.Contains(
|
|
$"{Path.DirectorySeparatorChar}Modules{Path.DirectorySeparatorChar}",
|
|
StringComparison.Ordinal))
|
|
.ToArray();
|
|
|
|
var violations = endpoints.Where(path =>
|
|
{
|
|
var text = File.ReadAllText(path);
|
|
return !text.Contains("Roles(", StringComparison.Ordinal)
|
|
&& !text.Contains("Policies(", StringComparison.Ordinal);
|
|
}).ToArray();
|
|
|
|
Assert.True(violations.Length == 0,
|
|
"Every module endpoint must declare Roles or Policies: " + string.Join(", ", violations));
|
|
}
|
|
|
|
[Fact]
|
|
public void Accidental_placeholder_files_are_not_committed()
|
|
{
|
|
var root = FindRepositoryRoot();
|
|
var names = Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories)
|
|
.Where(path => !IsGeneratedOrTestOutput(path))
|
|
.Where(path => Path.GetFileName(path).Equals("testfile", StringComparison.OrdinalIgnoreCase)
|
|
|| Path.GetFileName(path).EndsWith(".tmp", StringComparison.OrdinalIgnoreCase))
|
|
.ToArray();
|
|
|
|
Assert.True(names.Length == 0,
|
|
"Placeholder files are prohibited: " + string.Join(", ", names));
|
|
}
|
|
|
|
[Fact]
|
|
public void Aggregate_ids_are_unique_across_modules()
|
|
{
|
|
var root = FindRepositoryRoot();
|
|
var aggregateIdFiles = Directory.EnumerateFiles(
|
|
Path.Combine(root, "src"),
|
|
"*.cs",
|
|
SearchOption.AllDirectories)
|
|
.Where(path => !IsGeneratedOrTestOutput(path))
|
|
.ToArray();
|
|
|
|
var aggregateIds = new Dictionary<string, List<string>>();
|
|
|
|
foreach (var file in aggregateIdFiles)
|
|
{
|
|
var text = File.ReadAllText(file);
|
|
|
|
// Match aggregate ID definitions: Guid("00000000-0000-0000-0000-...")
|
|
var pattern = @"Guid\(\""[a-f0-9\-]{36}\""";
|
|
var matches = System.Text.RegularExpressions.Regex.Matches(text, pattern);
|
|
|
|
foreach (System.Text.RegularExpressions.Match match in matches)
|
|
{
|
|
var id = match.Value;
|
|
if (!aggregateIds.TryGetValue(id, out var list))
|
|
{
|
|
list = new List<string>();
|
|
aggregateIds[id] = list;
|
|
}
|
|
list.Add(file);
|
|
}
|
|
}
|
|
|
|
var duplicates = aggregateIds
|
|
.Where(kvp => kvp.Value.Count > 1)
|
|
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
|
|
|
|
Assert.True(duplicates.Count == 0,
|
|
duplicates.Count > 0
|
|
? $"Duplicate aggregate IDs detected: {string.Join("; ", duplicates.Select(d => $"{d.Key} in {string.Join(", ", d.Value)}"))}"
|
|
: "No duplicate aggregate IDs found.");
|
|
}
|
|
|
|
private static void AssertNoPattern(IEnumerable<string> files, string pattern, string message)
|
|
{
|
|
var violations = files
|
|
.Where(path => File.ReadAllText(path).Contains(pattern, StringComparison.Ordinal))
|
|
.ToArray();
|
|
Assert.True(violations.Length == 0, message + " " + string.Join(", ", violations));
|
|
}
|
|
|
|
private static bool IsGeneratedOrTestOutput(string path)
|
|
=> path.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
|
|
|| path.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
|
|
|| path.Contains($"{Path.DirectorySeparatorChar}tests{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
|
|
|| path.Contains($"{Path.DirectorySeparatorChar}attachments{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
|
|
|| path.Contains($"{Path.DirectorySeparatorChar}research{Path.DirectorySeparatorChar}original{Path.DirectorySeparatorChar}", StringComparison.Ordinal);
|
|
|
|
private static string FindRepositoryRoot()
|
|
{
|
|
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
|
while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "Directory.Build.props")))
|
|
{
|
|
directory = directory.Parent;
|
|
}
|
|
|
|
return directory?.FullName
|
|
?? throw new InvalidOperationException("Repository root not found.");
|
|
}
|
|
}
|