Catalog-member vectors: Rust-written members-[] store and the C# test (rs-member-rows-vectors)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 22:22:29 +01:00
commit 6089f060ee
2 changed files with 176 additions and 0 deletions

View file

@ -0,0 +1,176 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GameCult.Caching;
using GameCult.Caching.MessagePack;
using MessagePack;
using NUnit.Framework;
namespace GameCult.Caching.Tests
{
// tests/vectors/catalog-members (variants:ruling:rs-catalog-member-rows-every-member): a record holding a value in every
// member shape, in two stores. csharp-members-store.cc is written by this reference, under CULTCACHE_WRITE_VECTORS=1, and
// carries one catalog row per member; every other run compares its bytes. rust-members-empty-store.cc is written by
// cultcache-rs as it stood at e503edf2, whose catalog entry holds members [], and is never regenerated; it is only read.
public class CatalogMemberVectorTests
{
private const string Vector = "catalog-members";
[MessagePackObject]
public sealed class CatalogMemberMark
{
[Key(0)] public string Label = string.Empty;
[Key(1)] public int Weight;
}
[Union(0, typeof(CatalogMemberTextArm))]
[Union(1, typeof(CatalogMemberCountArm))]
public abstract class CatalogMemberArm { }
[MessagePackObject]
public sealed class CatalogMemberTextArm : CatalogMemberArm
{
[Key(0)] public string Text = string.Empty;
}
[MessagePackObject]
public sealed class CatalogMemberCountArm : CatalogMemberArm
{
[Key(0)] public int Count;
}
[CultDocument("tests.catalog_members", "tests.catalog_members.v1")]
[MessagePackObject]
public sealed class CatalogMembersDocument
{
[Key(0)] [CultName] public string Title = string.Empty;
[Key(1)] [CultIndex("sku")] public string Sku = string.Empty;
[Key(2)] public int Count;
[Key(3)] public ulong Total;
[Key(4)] public double Ratio;
[Key(5)] public float Scale;
[Key(6)] public bool Flag;
[Key(7)] public int? MaybeCount;
[Key(8)] public string? Note;
[Key(9)] public List<string> Tags = new();
[Key(10)] public string[] Labels = Array.Empty<string>();
[Key(11)] public byte[] Raw = Array.Empty<byte>();
[Key(12)] public List<byte[]> Chunks = new();
[Key(13)] public Dictionary<string, int> Weights = new();
[Key(14)] public CatalogMemberMark Mark = new();
[Key(15)] public CatalogMemberArm? Arm;
// Slot 16 is a gap.
[Key(17)] public long Late;
}
private static readonly CultDocumentRegistry Registry = CultDocumentRegistry.ForTypes(new[] { typeof(CatalogMembersDocument) });
private static CatalogMembersDocument Expected() => new()
{
Title = "Brass lantern",
Sku = "LAN-0042",
Count = -7,
Total = 10_000_000_000UL,
Ratio = 0.25,
Scale = 1.5f,
Flag = true,
MaybeCount = 3,
Note = "handle with care",
Tags = new List<string> { "fragile", "lit" },
Labels = new[] { "a", "b", "c" },
Raw = new byte[] { 0, 1, 2, 254, 255 },
Chunks = new List<byte[]> { new byte[] { 9, 8 }, new byte[] { 7 } },
Weights = new Dictionary<string, int> { ["brass"] = 12, ["glass"] = 3 },
Mark = new CatalogMemberMark { Label = "stamp", Weight = 5 },
Arm = new CatalogMemberCountArm { Count = 21 },
Late = -9_000_000_000L,
};
private static void AssertEqualToExpected(CatalogMembersDocument? actual)
{
var expected = Expected();
Assert.That(actual, Is.Not.Null);
Assert.That(actual!.Title, Is.EqualTo(expected.Title));
Assert.That(actual.Sku, Is.EqualTo(expected.Sku));
Assert.That(actual.Count, Is.EqualTo(expected.Count));
Assert.That(actual.Total, Is.EqualTo(expected.Total));
Assert.That(actual.Ratio, Is.EqualTo(expected.Ratio));
Assert.That(actual.Scale, Is.EqualTo(expected.Scale));
Assert.That(actual.Flag, Is.EqualTo(expected.Flag));
Assert.That(actual.MaybeCount, Is.EqualTo(expected.MaybeCount));
Assert.That(actual.Note, Is.EqualTo(expected.Note));
Assert.That(actual.Tags, Is.EqualTo(expected.Tags));
Assert.That(actual.Labels, Is.EqualTo(expected.Labels));
Assert.That(actual.Raw, Is.EqualTo(expected.Raw));
Assert.That(actual.Chunks, Is.EqualTo(expected.Chunks));
Assert.That(actual.Weights, Is.EquivalentTo(expected.Weights));
Assert.That(actual.Mark.Label, Is.EqualTo(expected.Mark.Label));
Assert.That(actual.Mark.Weight, Is.EqualTo(expected.Mark.Weight));
Assert.That(actual.Arm, Is.TypeOf<CatalogMemberCountArm>());
Assert.That(((CatalogMemberCountArm)actual.Arm!).Count, Is.EqualTo(21));
Assert.That(actual.Late, Is.EqualTo(expected.Late));
}
private static byte[] StoreBytes()
{
var directory = Path.Combine(Path.GetTempPath(), $"cultlib-cmv-{Guid.NewGuid():N}");
Directory.CreateDirectory(directory);
try
{
var path = Path.Combine(directory, "store.cc");
using (var cache = CultCacheMessagePack.Create(path, new CultCacheOpenOptions { Registry = Registry, StoreFlushOnDispose = true }))
{
cache.Clock = () => VectorFiles.StoredAt;
cache.UpsertAsync(typeof(CatalogMembersDocument), Expected(), new CultRecordKey("m:1")).GetAwaiter().GetResult();
cache.FlushAsync().GetAwaiter().GetResult();
}
return File.ReadAllBytes(path);
}
finally
{
Directory.Delete(directory, recursive: true);
}
}
private static CatalogMembersDocument? ReadM1(string name, out CultSchemaMigrationReport[] reports)
{
var directory = Path.Combine(Path.GetTempPath(), $"cultlib-cmr-{Guid.NewGuid():N}");
Directory.CreateDirectory(directory);
try
{
var path = Path.Combine(directory, "store.cc");
File.WriteAllBytes(path, VectorFiles.Committed(Vector, name));
var store = new SingleFileMessagePackBackingStore(path);
using var cache = new CultCache();
cache.AddBackingStore(store);
cache.PullAllBackingStoresAsync().GetAwaiter().GetResult();
reports = store.LastSchemaMigrationReports.ToArray();
return cache.Get<CatalogMembersDocument>(new CultRecordKey("m:1"));
}
finally
{
Directory.Delete(directory, recursive: true);
}
}
[Test]
public void TheCSharpStoreIsTheReferencesBytesAndReadsBack()
{
VectorFiles.Check(Vector, "csharp-members-store.cc", StoreBytes());
AssertEqualToExpected(ReadM1("csharp-members-store.cc", out var reports));
Assert.That(reports.SelectMany(report => report.Warnings), Is.Empty);
}
[Test]
public void MembersEmptyRustStoreStillLoads()
{
AssertEqualToExpected(ReadM1("rust-members-empty-store.cc", out var reports));
var warnings = reports.SelectMany(report => report.Warnings).Select(warning => warning.Code).Distinct().ToArray();
Assert.That(warnings, Is.Not.Empty);
Assert.That(warnings, Is.All.EqualTo("defaulted_missing_slot"));
}
}
}