using System.Text.RegularExpressions;
namespace TarninaHub.Tests.Architecture;
///
/// Guarantees that data contracts never expose object-valued dictionaries.
/// Such dictionaries break CBOR deserialization of SurrealDB rows and force
/// lossy conversions, so only string-valued maps are allowed.
///
public class NoObjectValuedDictionariesTests
{
private static readonly Regex Forbidden = new(@"Dictionary();
foreach (var directory in new[]
{
Path.Combine(root, "backend", "Application", "Dtos"),
Path.Combine(root, "backend", "Application", "Abstractions"),
Path.Combine(root, "backend", "Domain"),
})
{
files.AddRange(Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories));
}
files.AddRange(Directory.GetFiles(
Path.Combine(root, "backend", "Infrastructure", "Persistence"),
"*Row.cs",
SearchOption.AllDirectories));
var offenders = files
.Where(file => Forbidden.IsMatch(File.ReadAllText(file)))
.Select(file => Path.GetRelativePath(root, file))
.OrderBy(path => path, StringComparer.Ordinal)
.ToList();
Assert.True(
offenders.Count == 0,
"Object-valued dictionaries are forbidden in data contracts. " +
"Use string-valued maps instead:\n" + string.Join(Environment.NewLine, offenders));
}
private static string FindRepositoryRoot()
{
var directory = new DirectoryInfo(AppContext.BaseDirectory);
while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "TarninaHub.slnx")))
{
directory = directory.Parent;
}
Assert.NotNull(directory);
return directory!.FullName;
}
}