A rewrite replaces a file only when the runtime's own reader opens it. C# drops ReadStoreHeader (a second, header-only verdict that let a flush replace files the reader refuses) and asks DeserializeSnapshot from open, flush and commit; the reader now requires exactly one array, so C# refuses bytes after the store on read as TS and Python do. A zero-byte file is an empty store for all three paths. CultMesh.WriteSingleFileDocument reads the file it replaces through the Mesh reader before writing. Rust reads and rewrites through one classification (trailing bytes refused on read; a non-store first slot must be a legacy envelope array the reader accepts). TypeScript checks a legacy array with pullAll's own decoder before replacing it. Python already conformed. Shared byte vectors and per-runtime verdicts live in tests/vectors/document-variants-c2a/readability; every runtime's test reads them. Holds gains second-dictionary-value and second-list-item cases. The persistence format contract now marks v3 by content and states the readability rule. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
115 lines
5.2 KiB
C#
115 lines
5.2 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using GameCult.Caching.MessagePack;
|
|
using NUnit.Framework;
|
|
using static GameCult.Caching.Tests.ElementIdTests;
|
|
|
|
namespace GameCult.Caching.Tests
|
|
{
|
|
// A file is replaced by a flush or a commit exactly when this runtime's own reader opens it: one verdict per file, asked by
|
|
// open, flush and commit alike. The bytes and the verdict of every runtime are shared: tests/vectors/document-variants-c2a/readability.
|
|
public class StoreReadabilityTests
|
|
{
|
|
private const int CSharp = 1;
|
|
private static readonly CultDocumentRegistry Registry = CultDocumentRegistry.ForTypes(new[] { typeof(IdDeck) });
|
|
private string _directory = "";
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_directory = Path.Combine(Path.GetTempPath(), "cultlib-readability-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(_directory);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
if (Directory.Exists(_directory))
|
|
Directory.Delete(_directory, true);
|
|
}
|
|
|
|
private static string VectorRoot()
|
|
{
|
|
for (var dir = new DirectoryInfo(TestContext.CurrentContext.TestDirectory); dir != null; dir = dir.Parent)
|
|
{
|
|
var candidate = Path.Combine(dir.FullName, "tests", "vectors", "document-variants-c2a", "readability");
|
|
if (Directory.Exists(candidate))
|
|
return candidate;
|
|
}
|
|
|
|
throw new DirectoryNotFoundException($"Shared readability vectors not found above {TestContext.CurrentContext.TestDirectory}.");
|
|
}
|
|
|
|
public static IEnumerable<TestCaseData> Vectors() =>
|
|
File.ReadAllLines(Path.Combine(VectorRoot(), "manifest.txt"))
|
|
.Where(line => line.Length > 0 && line[0] != '#')
|
|
.Select(line => line.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries))
|
|
.Select(cells => new TestCaseData(cells[0], cells[CSharp] == "reads").SetName($"{cells[0]} {cells[CSharp]}"));
|
|
|
|
private static string Header(string path) => CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path)).FormatVersion;
|
|
|
|
private string Seed(string name)
|
|
{
|
|
var path = Path.Combine(_directory, name);
|
|
using var cache = CultCacheMessagePack.Create(path, new CultCacheOpenOptions { Registry = Registry });
|
|
cache.Commit(batch => batch.Upsert(typeof(IdDeck), new IdDeck { Name = "d" }, new CultRecordKey("d")));
|
|
return path;
|
|
}
|
|
|
|
[TestCaseSource(nameof(Vectors))]
|
|
public void OpenReadsExactlyTheFilesTheVectorsSayItReads(string vector, bool reads)
|
|
{
|
|
var path = Path.Combine(_directory, "open.cc");
|
|
File.WriteAllBytes(path, File.ReadAllBytes(Path.Combine(VectorRoot(), vector)));
|
|
|
|
using var cache = new CultCache();
|
|
cache.AddBackingStore(new SingleFileMessagePackBackingStore(path));
|
|
if (reads)
|
|
Assert.DoesNotThrow(() => cache.PullAllBackingStoresAsync().GetAwaiter().GetResult());
|
|
else
|
|
Assert.That(() => cache.PullAllBackingStoresAsync().GetAwaiter().GetResult(), Throws.Exception);
|
|
}
|
|
|
|
[TestCaseSource(nameof(Vectors))]
|
|
public void AFlushReplacesAFileExactlyWhenItReads(string vector, bool reads)
|
|
{
|
|
var path = Seed("flush.cc");
|
|
using var cache = CultCacheMessagePack.Create(path, new CultCacheOpenOptions { Registry = Registry });
|
|
var bytes = File.ReadAllBytes(Path.Combine(VectorRoot(), vector));
|
|
File.WriteAllBytes(path, bytes);
|
|
var store = cache.BackingStores[0];
|
|
|
|
if (reads)
|
|
{
|
|
store.PushAll();
|
|
Assert.That(Header(path), Does.StartWith("cultcache.store.v"), "the file is a store this runtime wrote");
|
|
return;
|
|
}
|
|
|
|
Assert.That(() => store.PushAll(), Throws.Exception);
|
|
Assert.That(File.ReadAllBytes(path), Is.EqualTo(bytes), "a file this runtime cannot read was rewritten");
|
|
}
|
|
|
|
[TestCaseSource(nameof(Vectors))]
|
|
public void AnUnconditionalCommitReplacesAFileExactlyWhenItReads(string vector, bool reads)
|
|
{
|
|
var path = Seed("commit.cc");
|
|
using var cache = CultCacheMessagePack.Create(path, new CultCacheOpenOptions { Registry = Registry });
|
|
var bytes = File.ReadAllBytes(Path.Combine(VectorRoot(), vector));
|
|
File.WriteAllBytes(path, bytes);
|
|
|
|
if (reads)
|
|
{
|
|
cache.Commit(batch => batch.Upsert(typeof(IdDeck), new IdDeck { Name = "e" }, new CultRecordKey("e")));
|
|
Assert.That(Header(path), Does.StartWith("cultcache.store.v"), "the file is a store this runtime wrote");
|
|
return;
|
|
}
|
|
|
|
Assert.That(() => cache.Commit(batch => batch.Upsert(typeof(IdDeck), new IdDeck { Name = "e" }, new CultRecordKey("e"))), Throws.Exception);
|
|
Assert.That(File.ReadAllBytes(path), Is.EqualTo(bytes), "a file this runtime cannot read was rewritten");
|
|
}
|
|
}
|
|
}
|