diff --git a/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/README.md b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/README.md new file mode 100644 index 00000000..2ba005f2 --- /dev/null +++ b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/README.md @@ -0,0 +1,29 @@ +# Pre-Cut 2 store fixtures + +Bytes written by CultLib commit `0db1fe5` ("Apply Soul fixes to the store +composition docs and routing tests"), the last commit before Cut 2 changed the +MessagePack stores. They prove stores written before Cut 2 still open. + +Produced from a temporary git worktree at `0db1fe5`: a throwaway NUnit fixture +in that worktree's `tests/GameCult.Caching.Tests`, referencing its own Caching +projects and generator, upserted three records into a fresh `CultCache` and +called `FlushAllBackingStores()`. The worktree was removed afterwards. + +- `directory-v4/store.cc` plus `directory-v4/store.cc.records/*.msgpack`: + `new DirectoryMessagePackBackingStore(manifest)`, format + `cultcache.store.v4.directory-content-addressed-pages`. The empty + `.commit.lock` lease file was not copied. +- `single-file-v1/store.msgpack`: `new SingleFileMessagePackBackingStore(file)`, + format `cultcache.store.v1`. + +Both stores hold the same records. Document types are defined identically in +`../../PreCut2FixtureDocuments.cs`: + +| Key | Type (schema) | Values | +| --- | --- | --- | +| `item:anvil` | `PreCut2FixtureItem` (`fixtures.precut2.item.v1`) | `Name = "anvil"`, `Count = 3` | +| `item:bellows` | `PreCut2FixtureItem` (`fixtures.precut2.item.v1`) | `Name = "bellows"`, `Count = 42` | +| `note:origin` | `PreCut2FixtureNote` (`fixtures.precut2.note.v1`) | `Text = "written before cut 2"` | + +Do not regenerate these from a newer commit; their value is that they predate +Cut 2. Tests copy them to a temp directory before opening. diff --git a/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc new file mode 100644 index 00000000..8dda080c Binary files /dev/null and b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc differ diff --git a/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/26abbda63fe4c502d2ec3ec61481d08abb8d7c46b93d1641c5376058c7791ed6.msgpack b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/26abbda63fe4c502d2ec3ec61481d08abb8d7c46b93d1641c5376058c7791ed6.msgpack new file mode 100644 index 00000000..ec8ad267 --- /dev/null +++ b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/26abbda63fe4c502d2ec3ec61481d08abb8d7c46b93d1641c5376058c7791ed6.msgpack @@ -0,0 +1 @@ +”«note:originÙGsha256:670deacd06aedb90235e6bd873674c10467a05b4adcd62dbc019279efcbcbb11Ù!2026-09-13T15:14:36.4547792+00:00Ä‘´written before cut 2 \ No newline at end of file diff --git a/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/538952c2d5f600e672b4184d9fc00cf6d4c01a23593138a2eab02d710e79d6f8.msgpack b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/538952c2d5f600e672b4184d9fc00cf6d4c01a23593138a2eab02d710e79d6f8.msgpack new file mode 100644 index 00000000..d2e1758d --- /dev/null +++ b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/538952c2d5f600e672b4184d9fc00cf6d4c01a23593138a2eab02d710e79d6f8.msgpack @@ -0,0 +1 @@ +”ªitem:anvilÙGsha256:88d3fdf0a927acf3b163940d8f8c7fe62b3316542ce771a67ec8bc038f594788Ù!2026-09-13T15:14:36.4534706+00:00Ä’¥anvil \ No newline at end of file diff --git a/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/7724612992b5cda71fd6a663e30c5e115819fc978c9ea677ebb76d69505a26e1.msgpack b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/7724612992b5cda71fd6a663e30c5e115819fc978c9ea677ebb76d69505a26e1.msgpack new file mode 100644 index 00000000..9468d489 --- /dev/null +++ b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/directory-v4/store.cc.records/7724612992b5cda71fd6a663e30c5e115819fc978c9ea677ebb76d69505a26e1.msgpack @@ -0,0 +1,2 @@ +”¬item:bellowsÙGsha256:88d3fdf0a927acf3b163940d8f8c7fe62b3316542ce771a67ec8bc038f594788Ù!2026-09-13T15:14:36.4547616+00:00Ä +’§bellows* \ No newline at end of file diff --git a/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/single-file-v1/store.msgpack b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/single-file-v1/store.msgpack new file mode 100644 index 00000000..3e740551 Binary files /dev/null and b/tests/GameCult.Caching.Tests/Fixtures/pre-cut2/single-file-v1/store.msgpack differ diff --git a/tests/GameCult.Caching.Tests/GameCult.Caching.Tests.csproj b/tests/GameCult.Caching.Tests/GameCult.Caching.Tests.csproj index 6e8ec411..4b3d92d3 100644 --- a/tests/GameCult.Caching.Tests/GameCult.Caching.Tests.csproj +++ b/tests/GameCult.Caching.Tests/GameCult.Caching.Tests.csproj @@ -14,6 +14,10 @@ ReferenceOutputAssembly="false" /> + + + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/GameCult.Caching.Tests/PreCut2FixtureDocuments.cs b/tests/GameCult.Caching.Tests/PreCut2FixtureDocuments.cs new file mode 100644 index 00000000..4598b7a5 --- /dev/null +++ b/tests/GameCult.Caching.Tests/PreCut2FixtureDocuments.cs @@ -0,0 +1,23 @@ +using GameCult.Caching; +using MessagePack; + +namespace GameCult.Caching.Tests +{ + [CultDocument("fixtures.precut2.item", "fixtures.precut2.item.v1")] + internal sealed class PreCut2FixtureItem + { + [Key(0)] + [CultName] + public string Name = string.Empty; + + [Key(1)] + public int Count; + } + + [CultDocument("fixtures.precut2.note", "fixtures.precut2.note.v1")] + internal sealed class PreCut2FixtureNote + { + [Key(0)] + public string Text = string.Empty; + } +} diff --git a/tests/GameCult.Caching.Tests/PreCut2StoreFormatTests.cs b/tests/GameCult.Caching.Tests/PreCut2StoreFormatTests.cs new file mode 100644 index 00000000..3b442cf3 --- /dev/null +++ b/tests/GameCult.Caching.Tests/PreCut2StoreFormatTests.cs @@ -0,0 +1,120 @@ +#nullable enable +using System; +using System.IO; +using System.Threading.Tasks; +using GameCult.Caching; +using GameCult.Caching.MessagePack; +using NUnit.Framework; + +namespace GameCult.Caching.Tests +{ + public class PreCut2StoreFormatTests + { + private static string FixtureRoot => + Path.Combine(TestContext.CurrentContext.TestDirectory, "Fixtures", "pre-cut2"); + + private static string CopyFixture(string name) + { + var target = Path.Combine(Path.GetTempPath(), $"cultlib-precut2-{Guid.NewGuid():N}"); + var source = Path.Combine(FixtureRoot, name); + foreach (var file in Directory.GetFiles(source, "*", SearchOption.AllDirectories)) + { + var destination = Path.Combine(target, Path.GetRelativePath(source, file)); + Directory.CreateDirectory(Path.GetDirectoryName(destination)!); + File.Copy(file, destination); + } + return target; + } + + private static void AssertFixtureRecords(CultCache cache) + { + Assert.That(cache.Get(new CultRecordKey("item:anvil")), Is.Not.Null); + Assert.That(cache.Get(new CultRecordKey("item:anvil"))!.Name, Is.EqualTo("anvil")); + Assert.That(cache.Get(new CultRecordKey("item:anvil"))!.Count, Is.EqualTo(3)); + Assert.That(cache.Get(new CultRecordKey("item:bellows"))?.Name, Is.EqualTo("bellows")); + Assert.That(cache.Get(new CultRecordKey("item:bellows"))?.Count, Is.EqualTo(42)); + Assert.That(cache.Get(new CultRecordKey("note:origin"))?.Text, + Is.EqualTo("written before cut 2")); + Assert.That(cache.GetAll(), Has.Length.EqualTo(2)); + Assert.That(cache.GetAll(), Has.Length.EqualTo(1)); + } + + [Test] + public async Task DirectoryStoreOpensV4ManifestWrittenBeforeCut2() + { + var root = CopyFixture("directory-v4"); + try + { + var manifest = Path.Combine(root, "store.cc"); + Assert.That(CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(manifest)).FormatVersion, + Is.EqualTo("cultcache.store.v4.directory-content-addressed-pages")); + + using var cache = new CultCache(); + cache.AddBackingStore(new DirectoryMessagePackBackingStore(manifest)); + await cache.PullAllBackingStoresAsync(); + + AssertFixtureRecords(cache); + } + finally + { + if (Directory.Exists(root)) Directory.Delete(root, recursive: true); + } + } + + [Test] + public async Task SingleFileOpensV1SnapshotWrittenBeforeCut2() + { + var root = CopyFixture("single-file-v1"); + try + { + var file = Path.Combine(root, "store.msgpack"); + Assert.That(CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(file)).FormatVersion, + Is.EqualTo("cultcache.store.v1")); + + using var cache = new CultCache(); + cache.AddBackingStore(new SingleFileMessagePackBackingStore(file)); + await cache.PullAllBackingStoresAsync(); + + AssertFixtureRecords(cache); + } + finally + { + if (Directory.Exists(root)) Directory.Delete(root, recursive: true); + } + } + + // Format strings taken from DirectoryMessagePackBackingStore.cs at 0db1fe5. + [TestCase("cultcache.store.v1")] + [TestCase("cultcache.store.v1.directory")] + [TestCase("cultcache.store.v2.directory-indexed")] + [TestCase("cultcache.store.v3.directory-immutable-pages")] + public void DirectoryStoreRefusesNonV4Manifest(string formatVersion) + { + var root = Path.Combine(Path.GetTempPath(), $"cultlib-refuse-{Guid.NewGuid():N}"); + var manifest = Path.Combine(root, "store.cc"); + Directory.CreateDirectory(root); + try + { + var bytes = CultDocumentMessagePackSerialization.SerializeSnapshot(new CultPersistedStoreSnapshot + { + FormatVersion = formatVersion, + SchemaCatalog = Array.Empty(), + Records = Array.Empty() + }); + File.WriteAllBytes(manifest, bytes); + + using var cache = new CultCache(); + var store = new DirectoryMessagePackBackingStore(manifest); + cache.AddBackingStore(store); + + Assert.That(() => store.PullAll(), + Throws.TypeOf().With.Message.Contains(formatVersion)); + Assert.That(File.ReadAllBytes(manifest), Is.EqualTo(bytes)); + } + finally + { + if (Directory.Exists(root)) Directory.Delete(root, recursive: true); + } + } + } +}