using Khaemhat.Data.Migrations;
namespace Khaemhat.Data.Tests;
/// docs/DATA-MODEL.md never drifts from the migrations (SPEC ยง18).
[TestClass]
public sealed class DataModelDocumentTests
{
[TestMethod]
public void GeneratedSection_IsUpToDate()
{
var path = Path.Combine(RepositoryRoot(), "docs", "DATA-MODEL.md");
if (!File.Exists(path))
{
Assert.Inconclusive("docs/ is not part of this checkout (the Docker build context excludes it).");
}
var current = File.ReadAllText(path).ReplaceLineEndings("\n");
var expected = DataModelDocument.Update(current, DataModelDocument.Render(MigrationCatalog.ForSystem(), MigrationCatalog.ForCompany()));
Assert.AreEqual(expected, current, "docs/DATA-MODEL.md is out of date: run scripts/gen-datamodel.sh");
}
[TestMethod]
public void Update_ReplacesOnlyTheGeneratedSection()
{
var document = $"intro\n{DataModelDocument.BeginMarker}\nold\n{DataModelDocument.EndMarker}\nfooter\n";
var updated = DataModelDocument.Update(document, "new\n");
Assert.AreEqual($"intro\n{DataModelDocument.BeginMarker}\n\nnew\n\n{DataModelDocument.EndMarker}\nfooter\n", updated);
Assert.ThrowsExactly(() => DataModelDocument.Update("no markers", "x"));
}
private static string RepositoryRoot()
{
for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent)
{
if (File.Exists(Path.Combine(dir.FullName, "Khaemhat.sln")))
{
return dir.FullName;
}
}
throw new InvalidOperationException("Khaemhat.sln not found above " + AppContext.BaseDirectory);
}
}