CatalogResaveTests: a fixture written under the old schema, one probe type with the appended member

Two same-named probe types in one assembly collide in every registry that scans it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-27 23:05:04 +01:00
commit 74469bdaa1
2 changed files with 22 additions and 44 deletions

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GameCult.Caching;
@ -8,91 +7,70 @@ using MessagePack;
using Xunit;
// AetherDb catalog resave (follow-up aetherdb-catalog-resave): a record stored under an older schema id is re-saved under the
// current one, a dry run writes nothing, and a second apply writes nothing. The two probe types share one schema name; the
// second appends a member, which is how a real schema change restamps a record.
// current one, a dry run writes nothing, and a second apply writes nothing. The fixture tests/fixtures/catalog-resave/probe-v1.cc
// holds two records of schema aetheria.test.resaveprobe as written by a type with one member; Probe appends a second, which is
// how a real schema change restamps a record.
public sealed class CatalogResaveTests : IDisposable
{
[MessagePackObject]
[CultDocument("aetheria.test.resaveprobe", "1")]
public sealed class ProbeV1
{
[Key(0)] public string Name = "";
}
[MessagePackObject]
[CultDocument("aetheria.test.resaveprobe", "1")]
public sealed class ProbeV2
public sealed class Probe
{
[Key(0)] public string Name = "";
[Key(1)] public int Appended = 7;
}
private readonly TempDirectory _directory = new TempDirectory();
private string Path_ => System.IO.Path.Combine(_directory.Path, "probe.cc");
private string Stored => System.IO.Path.Combine(_directory.Path, "probe.cc");
public CatalogResaveTests() =>
File.Copy(System.IO.Path.Combine(RunStartTests.FindRepoRoot(), "tests", "fixtures", "catalog-resave", "probe-v1.cc"), Stored);
public void Dispose() => _directory.Dispose();
private static CultCache Open(string path, Type type)
private CultCache Open()
{
var cache = new CultCache(CultDocumentRegistry.ForTypes(new[] { type }));
cache.AddBackingStore(new SingleFileMessagePackBackingStore(path), new[] { type });
var cache = new CultCache(CultDocumentRegistry.ForTypes(new[] { typeof(Probe) }));
cache.AddBackingStore(new SingleFileMessagePackBackingStore(Stored), new[] { typeof(Probe) });
return cache;
}
private void WriteOld()
{
using var cache = Open(Path_, typeof(ProbeV1));
cache.Commit(batch =>
{
batch.Upsert(typeof(ProbeV1), new ProbeV1 { Name = "a" }, new CultRecordKey("a"));
batch.Upsert(typeof(ProbeV1), new ProbeV1 { Name = "b" }, new CultRecordKey("b"));
});
cache.FlushAsync().Wait();
}
private string[] StoredIds() =>
CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(Path_)).Records.Select(r => r.SchemaId).ToArray();
CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(Stored)).Records.Select(r => r.SchemaId).ToArray();
[Fact]
public void ADryRunListsAndWritesNothing()
{
WriteOld();
var before = File.ReadAllBytes(Path_);
var oldIds = StoredIds();
using (var cache = Open(Path_, typeof(ProbeV2)))
var before = File.ReadAllBytes(Stored);
using (var cache = Open())
{
var output = new StringWriter();
Assert.Equal(0, CatalogResaveCommand.Run(cache, apply: false, output));
Assert.Contains("2 record(s) would be re-saved", output.ToString());
}
Assert.Equal(before, File.ReadAllBytes(Path_));
Assert.Equal(oldIds, StoredIds());
Assert.Equal(before, File.ReadAllBytes(Stored));
}
[Fact]
public void ApplyRestampsEveryRecordAndASecondApplyWritesNothing()
{
WriteOld();
var oldIds = StoredIds();
string[] newIds;
using (var cache = Open(Path_, typeof(ProbeV2)))
{
using (var cache = Open())
Assert.Equal(0, CatalogResaveCommand.Run(cache, apply: true, new StringWriter()));
}
newIds = StoredIds();
var newIds = StoredIds();
Assert.Equal(2, newIds.Length);
Assert.All(newIds, id => Assert.DoesNotContain(id, oldIds));
var written = File.ReadAllBytes(Path_);
var written = File.ReadAllBytes(Stored);
using (var cache = Open(Path_, typeof(ProbeV2)))
using (var cache = Open())
{
Assert.Equal(new[] { "a", "b" }, cache.GetAll<ProbeV2>().Select(p => p.Name).OrderBy(n => n));
Assert.All(cache.GetAll<ProbeV2>(), p => Assert.Equal(7, p.Appended));
Assert.Equal(new[] { "a", "b" }, cache.GetAll<Probe>().Select(p => p.Name).OrderBy(n => n));
Assert.All(cache.GetAll<Probe>(), p => Assert.Equal(7, p.Appended));
var output = new StringWriter();
Assert.Equal(0, CatalogResaveCommand.Run(cache, apply: true, output));
Assert.Contains("0 record(s) would be re-saved", output.ToString());
}
Assert.Equal(written, File.ReadAllBytes(Path_));
Assert.Equal(written, File.ReadAllBytes(Stored));
}
// The catalog the game ships is current: resaving it changes nothing.

Binary file not shown.