From 4c7354be79140ff5f6b659a89afaa29e90ced045 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 00:49:30 +0100 Subject: [PATCH] UniqueIndexTests: pin the delete's Forgot and the foreign record's version in the moved-store gate Co-Authored-By: Claude Sonnet 5.5 --- .../UniqueIndexTests.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/GameCult.Caching.Tests/UniqueIndexTests.cs b/tests/GameCult.Caching.Tests/UniqueIndexTests.cs index 55258d48..c43671bb 100644 --- a/tests/GameCult.Caching.Tests/UniqueIndexTests.cs +++ b/tests/GameCult.Caching.Tests/UniqueIndexTests.cs @@ -335,6 +335,49 @@ namespace GameCult.Caching.Tests Assert.That(second.Commit(batch => batch.Upsert(typeof(UqItem), Item("x"), Key("c"))), Is.True, "only this cache wrote since it last read"); } + // A record this cache removed is gone from what it last read: its next indexed write is judged against the store after the + // removal, not refused for a record it deleted itself. + [Test] + public void ADeleteIsRecordedSoTheNextIndexedWriteIsJudgedAgainstTheStoreAfterIt([Values] StoreKind kind) + { + var path = PathOf("items.cc"); + using var seed = Open(path, kind); + seed.Commit(batch => + { + batch.Upsert(typeof(UqItem), Item("one"), Key("a")); + batch.Upsert(typeof(UqItem), Item("two"), Key("b")); + }); + using var cache = Open(path, kind); + Assert.That(cache.Commit(batch => batch.Remove(Key("b"))), Is.True); + + Assert.That(cache.Commit(batch => batch.Upsert(typeof(UqItem), Item("three"), Key("c"))), Is.True, + "nothing but this cache's own delete moved the store"); + using var reopened = Open(path, kind); + Assert.That(reopened.Get(Key("b")), Is.Null); + Assert.That(reopened.Get(Key("c")), Is.Not.Null); + } + + // A record of a type this cache does not define is part of the store it last read: unchanged, it never refuses an indexed + // write; changed by another writer, it does. + [Test] + public void AForeignTypedRecordIsReadWithTheStoreAndRefusesAnIndexedWriteOnlyWhenItMoves([Values] StoreKind kind) + { + var path = PathOf("items.cc"); + using var seed = Open(path, kind); + seed.Commit(batch => + { + batch.Upsert(typeof(UqItem), Item("one"), Key("a")); + batch.Upsert(typeof(UqTagged), new UqTagged { Kind = "x" }, Key("tag")); + }); + using var narrow = Open(path, kind, CultDocumentRegistry.ForTypes(new[] { typeof(UqItem) })); + Assert.That(narrow.Commit(batch => batch.Upsert(typeof(UqItem), Item("two"), Key("b"))), Is.True, + "the foreign record has not moved since this cache read the store"); + + seed.Commit(batch => batch.Upsert(typeof(UqTagged), new UqTagged { Kind = "y" }, Key("tag"))); + Assert.Throws( + () => narrow.Commit(batch => batch.Upsert(typeof(UqItem), Item("three"), Key("c")))); + } + // A conflict costs Update an attempt, not an error: it applies again to what the store holds and lands. [Test] public void AnUpdateOnAMovedDirectoryStoreRetriesAndLands()