using System.Text.Json;
using System.Text.RegularExpressions;
using OpenNest.Data;
namespace OpenNest.Server.Tests;
///
/// The container smoke compares a migrated legacy database with the manifest that
/// scripts/Server.Tests/legacy_fixture.py expects, field for field. It needs Docker, so this
/// catches a new record field the fixture does not list before the workflow does.
///
public sealed class LegacyFixtureTests
{
[Fact]
public void ExpectedLegacyMetadata_ListsEveryRecordField()
{
var script = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "legacy_fixture.py"));
// The legacy columns, then the values later migrations and history add.
var legacy = Regex.Match(script, @"dict\(zip\(\((?[^)]*)\)").Groups["names"].Value;
var added = Regex.Match(script, @"metadata\.update\((?[^)]*)\)").Groups["values"].Value;
var expected = Regex.Matches(legacy, @"'(\w+)'")
.Concat(Regex.Matches(added, @"(\w+)="))
.Select(match => match.Groups[1].Value)
.Distinct()
.Order(StringComparer.Ordinal);
var actual = JsonSerializer.SerializeToElement(new NestRecord(), new JsonSerializerOptions(JsonSerializerDefaults.Web))
.EnumerateObject()
.Select(property => property.Name)
.Order(StringComparer.Ordinal);
Assert.Equal(actual, expected);
}
}