C2a: mark the header by content, refuse an element twice in a list, route CultMesh single-file through the id rule

A store is written v3 (v5 for a directory) only when a record in it holds a non-empty
element id. CultElementIds.Plan reports that as IdPlan.HoldsIds; the admission stamps it
on the stored record and every store reads it. The descriptor's CarriesElementIds is
deleted. A file already marked on disk stays marked. CultPersistedStoreSnapshot.FormatFor
is the one header decision.

Plan refuses one element object sitting twice in a list at write, naming the record and
list, instead of writing both ids and refusing the store at load.

CultMesh single-file write mints, refuses and marks as a cache write does; the read applies
the load-time plan, so a write and a load refuse the same things.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-29 19:40:36 +02:00
commit 490f7b811f
7 changed files with 326 additions and 41 deletions

View file

@ -647,6 +647,110 @@ namespace GameCult.Caching.Tests
Assert.That(cache.Get<IdDeck>(new CultRecordKey("plain")), Is.Null);
}
// ---- C2a fix batch 3: mark by content, one object twice ----
private static IdDeck EmptyDeck(string name) => new() { Name = name };
[Test]
public void ASingleFileStoreIsMarkedByTheIdsItHoldsNotByTheTypesItCouldHold()
{
var empty = PathOf("empty-deck.cc");
using (var cache = Open(empty))
cache.Commit(batch => batch.Upsert(typeof(IdDeck), EmptyDeck("e"), new CultRecordKey("e")));
Assert.That(HeaderOf(empty), Is.EqualTo("cultcache.store.v1"), "a deck with no elements holds no id");
var flushed = PathOf("flushed.cc");
using (var cache = Open(flushed))
{
cache.UpsertAsync(typeof(IdDeck), EmptyDeck("e"), new CultRecordKey("e")).GetAwaiter().GetResult();
cache.FlushAllBackingStores();
}
Assert.That(HeaderOf(flushed), Is.EqualTo("cultcache.store.v1"), "the whole-store flush decides by content too");
using (var cache = Open(flushed))
{
cache.UpsertAsync(typeof(IdDeck), Deck("d"), new CultRecordKey("d")).GetAwaiter().GetResult();
cache.FlushAllBackingStores();
}
Assert.That(HeaderOf(flushed), Is.EqualTo("cultcache.store.v3"), "one element with an id marks the flush");
var marked = PathOf("marked.cc");
using (var cache = Open(marked))
cache.Commit(batch => batch.Upsert(typeof(IdDeck), Deck("d"), new CultRecordKey("d")));
using (var cache = Open(marked))
{
Assert.That(cache.Commit(batch =>
{
batch.Expect(new CultRecordKey("e"), null);
batch.Upsert(typeof(IdDeck), EmptyDeck("e"), new CultRecordKey("e"));
}), Is.True);
}
Assert.That(HeaderOf(marked), Is.EqualTo("cultcache.store.v3"), "a file already marked on disk stays marked");
}
[Test]
public void ADirectoryStoreIsMarkedByTheIdsItWritesAndAnEmptyDeckDoesNotMarkIt()
{
var empty = PathOf("empty-dir.cc");
using (var cache = OpenWith(empty, true, typeof(IdDeck)))
cache.Commit(batch => batch.Upsert(typeof(IdDeck), EmptyDeck("e"), new CultRecordKey("e")));
Assert.That(HeaderOf(empty), Is.EqualTo("cultcache.store.v4.directory-content-addressed-pages"));
using (var cache = OpenWith(empty, true, typeof(IdDeck)))
cache.Commit(batch => batch.Upsert(typeof(IdDeck), Deck("d"), new CultRecordKey("d")));
Assert.That(HeaderOf(empty), Is.EqualTo("cultcache.store.v5.directory-content-addressed-pages"));
}
[Test]
public void AVariantIsMarkedByItsOverrideIdsAndOtherwiseIsV2()
{
var plain = PathOf("variant-plain.cc");
using (var cache = Open(plain))
{
cache.Commit(batch =>
{
batch.Upsert(typeof(IdDeck), EmptyDeck("base"), new CultRecordKey("base"));
batch.UpsertVariant(new CultRecordKey("v"), new CultRecordKey("base"), new[] { cache.Override<IdDeck>(nameof(IdDeck.Name), "v") });
});
}
Assert.That(HeaderOf(plain), Is.EqualTo("cultcache.store.v2"));
var withIds = PathOf("variant-ids.cc");
using (var cache = Open(withIds))
{
cache.Commit(batch =>
{
batch.Upsert(typeof(IdDeck), EmptyDeck("base"), new CultRecordKey("base"));
batch.UpsertVariant(new CultRecordKey("v"), new CultRecordKey("base"),
new[] { cache.Override<IdDeck>(nameof(IdDeck.Reels), new List<IdReel> { new() { Label = "r" } }) });
});
}
Assert.That(HeaderOf(withIds), Is.EqualTo("cultcache.store.v3"));
}
[Test]
public void AnElementObjectTwiceInOneListIsRefusedAtWriteAndNothingIsPersisted()
{
var path = PathOf("shared.cc");
using (var cache = Open(path))
{
var shared = new IdMark { Text = "s" };
var deck = new IdDeck { Name = "shared", Reels = { new IdReel { Label = "r", Marks = { shared, shared } } } };
var refusal = Assert.Throws<CultElementIdException>(() => cache.Commit(batch => batch.Upsert(typeof(IdDeck), deck, new CultRecordKey("shared"))))!;
Assert.That(refusal.Message, Does.Contain("shared").And.Contain("twice"));
Assert.That(refusal.RecordKey, Is.EqualTo("shared"));
Assert.That(shared.Id, Is.Empty, "the refusal minted nothing");
Assert.That(cache.Get<IdDeck>(new CultRecordKey("shared")), Is.Null);
}
Assert.That(File.Exists(path), Is.False, "nothing was persisted");
}
[Test]
public void ADerivedIdWhoseSourceIsEmptyIsRefusedAtWriteNamingTheSourceMember()
{

View file

@ -0,0 +1,123 @@
using System;
using System.IO;
using System.Linq;
using FluentAssertions;
using GameCult.Caching;
using GameCult.Caching.MessagePack;
using MessagePack;
using NUnit.Framework;
#nullable enable
namespace GameCult.Mesh.Tests;
// C2a: a single-file document written or read through CultMesh is judged as a cache judges it: ids minted, a list that
// cannot hold ids refused, the header decided by what the file holds.
public sealed class CultMeshSingleFileElementIdTests
{
private const string HexA = "aaaaaaaaaaaa";
private static readonly CultRecordKey Key = new("publication");
private string _root = null!;
[SetUp]
public void SetUp()
{
_root = Path.Combine(Path.GetTempPath(), "cultmesh-single-file-ids", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_root);
}
[TearDown]
public void TearDown()
{
if (Directory.Exists(_root)) Directory.Delete(_root, true);
}
private static CultMeshBodyPublicationDocument Publication(params CultMeshBodyDescriptor[] representations) => new()
{
BodyId = "aetheria:entities",
ProducerId = "aetheria",
SchemaId = "eve.entity_soa.v1",
Representations = representations
};
private static string HeaderOf(string path) =>
CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path)).FormatVersion;
private static CultMeshBodyDescriptor Descriptor(string id = "") => new() { Id = id, BodyId = "body" };
private string Path_(string name) => Path.Combine(_root, name);
[Test]
public void TypedWriteMintsTheElementIdsAndMarksTheFileThatHoldsThem()
{
var path = Path_("ids.cc");
var publication = Publication(Descriptor(), Descriptor());
CultMesh.WriteSingleFileDocument(path, Key, publication);
publication.Representations.Select(entry => entry.Id).Should().OnlyContain(id => id.Length == 12).And.OnlyHaveUniqueItems();
HeaderOf(path).Should().Be("cultcache.store.v3");
CultMesh.ReadSingleFileDocument<CultMeshBodyPublicationDocument>(path, Key).Representations.Select(entry => entry.Id)
.Should().Equal(publication.Representations.Select(entry => entry.Id));
}
[Test]
public void TypedWriteOfADocumentHoldingNoIdStaysV1AndAMarkedFileStaysMarked()
{
var path = Path_("none.cc");
CultMesh.WriteSingleFileDocument(path, Key, Publication());
HeaderOf(path).Should().Be("cultcache.store.v1");
CultMesh.WriteSingleFileDocument(path, Key, Publication(Descriptor()));
HeaderOf(path).Should().Be("cultcache.store.v3");
CultMesh.WriteSingleFileDocument(path, Key, Publication());
HeaderOf(path).Should().Be("cultcache.store.v3", "a file already marked on disk stays marked");
}
[Test]
public void TypedWriteRefusesADuplicateIdAndAnElementObjectTwiceAndLeavesTheFileAlone()
{
var path = Path_("refused.cc");
CultMesh.WriteSingleFileDocument(path, Key, Publication(Descriptor()));
var before = File.ReadAllBytes(path);
var duplicate = Publication(Descriptor(HexA), Descriptor(HexA));
var refusal = Assert.Throws<CultElementIdException>(() => CultMesh.WriteSingleFileDocument(path, Key, duplicate))!;
refusal.ElementId.Should().Be(HexA);
var once = Descriptor();
var twice = Publication(once, once);
Assert.Throws<CultElementIdException>(() => CultMesh.WriteSingleFileDocument(path, Key, twice))!.Message.Should().Contain("twice");
once.Id.Should().BeEmpty("a refusal mints nothing");
File.ReadAllBytes(path).Should().Equal(before);
}
[Test]
public void TypedReadRefusesWhatALoadRefusesAndMintsWhatALoadMints()
{
var path = Path_("disk.cc");
CultMesh.WriteSingleFileDocument(path, Key, Publication(Descriptor()));
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
var duplicate = Publication(Descriptor(HexA), Descriptor(HexA));
snapshot.Records[0].Payload = MessagePackSerializer.Serialize(duplicate);
File.WriteAllBytes(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
var refusal = Assert.Throws<CultElementIdException>(() => CultMesh.ReadSingleFileDocument<CultMeshBodyPublicationDocument>(path, Key))!;
refusal.ElementId.Should().Be(HexA);
// A payload written before ids: two reads, and a cache load, agree on the ids minted.
snapshot.Records[0].Payload = MessagePackSerializer.Serialize(Publication(Descriptor(), Descriptor()));
File.WriteAllBytes(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
var first = CultMesh.ReadSingleFileDocument<CultMeshBodyPublicationDocument>(path, Key).Representations.Select(entry => entry.Id).ToArray();
first.Should().OnlyContain(id => id.Length == 12);
CultMesh.ReadSingleFileDocument<CultMeshBodyPublicationDocument>(path, Key).Representations.Select(entry => entry.Id).Should().Equal(first);
using var cache = CultCacheMessagePack.Create(path, new CultCacheOpenOptions
{
Registry = CultDocumentRegistry.ForTypes(new[] { typeof(CultMeshBodyPublicationDocument) }),
ReadOnly = true
});
cache.Get<CultMeshBodyPublicationDocument>(Key)!.Representations.Select(entry => entry.Id).Should().Equal(first);
}
}