Cut 8b. tests/Aetheria.Shared.Tests (xunit, net10.0) builds a temp-directory catalog through AetheriaStores.Open(catalogWritable: true) per test: - OpenThenFlushLeavesEveryFileByteIdentical: the catalog, plus run and player stores seeded with their globals, a SavedZone and an InputLayout. - WeaponWrittenThroughGearHandleReloadsAsWeapon - CatalogRefusesWrites: bytes unchanged too. - SavedZoneNeverLandsInCatalog: no home without a run store; the snapshot schemas show where it landed. - FactionIsASingletonInstance: through a ref, a ref-keyed map key, and the name index. - MissingCatalogGlobalIsLoud: uses a test-only [CultGlobal] routed to the catalog. The game has no catalog globals yet. The first test found a real defect. PlayerInputSettings.InputActionMap was keyed by (string, int), and CultLib's untrusted-data MessagePack security has no hash-resistant comparer for a tuple, so a player store could never be read back. It is now action name to binding index to path; its two Unity readers change in the Unity commit. Each test was checked to fail with the rule it pins removed; see the report. Builds: Aetheria.Shared, AetherDb, Aetheria.Shared.Tests (6/6 pass). Unity does not build yet. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
133 lines
5.3 KiB
C#
133 lines
5.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using CultMath;
|
|
using GameCult.Caching;
|
|
using GameCult.Caching.MessagePack;
|
|
using MessagePack;
|
|
using Xunit;
|
|
|
|
// A [CultGlobal] routed to the catalog (PersonalityAttribute is a catalog home). The game has none yet, so this is
|
|
// the only way to prove a read-only catalog refuses to open without one.
|
|
[CultDocument("aetheria.tests.catalogglobal", "1"), CultGlobal, MessagePackObject]
|
|
public class TestCatalogGlobal : PersonalityAttribute { }
|
|
|
|
// Each test gets its own temp directory holding a catalog built through Open(catalogWritable: true).
|
|
public sealed class AetheriaStoresTests : IDisposable
|
|
{
|
|
private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-stores-" + Guid.NewGuid().ToString("N"));
|
|
private string Catalog => Path.Combine(_root, "Aetheria.cc");
|
|
private string Run => Path.Combine(_root, "run.cc");
|
|
private string Player => Path.Combine(_root, "player.cc");
|
|
|
|
public AetheriaStoresTests()
|
|
{
|
|
Directory.CreateDirectory(_root);
|
|
using var cache = AetheriaStores.Open(Catalog, catalogWritable: true);
|
|
cache.Upsert(new TestCatalogGlobal { Name = "Temperament" });
|
|
var faction = new Faction { Name = "Adrasteia", ShortName = "ADR", PrimaryColor = new float3(1, .5f, .25f) };
|
|
faction.Allegiance[cache.Upsert(faction)] = 1;
|
|
cache.Upsert(faction);
|
|
cache.Upsert(new WeaponItemData { Name = "Lance", Manufacturer = cache.RefOf(faction) });
|
|
cache.FlushAsync().Wait();
|
|
}
|
|
|
|
public void Dispose() => Directory.Delete(_root, true);
|
|
|
|
[Fact]
|
|
public void OpenThenFlushLeavesEveryFileByteIdentical()
|
|
{
|
|
using (var cache = AetheriaStores.Open(Catalog, Run, Player))
|
|
{
|
|
var zone = cache.Upsert(new SavedZone { Name = "Entrance", Position = new float2(1, 2), Contents = new ZonePack() });
|
|
cache.Upsert(new SavedGame { Zones = new[] { zone }, Factions = new[] { cache.RefOf(cache.GetAll<Faction>().Single()) } });
|
|
cache.Upsert(new PlayerSettings());
|
|
cache.Upsert(new InputLayout { Rows = new InputLayoutRow[] { new InputLayoutRowSpacer { Height = 1 } } });
|
|
cache.FlushAsync().Wait();
|
|
}
|
|
|
|
var before = new[] { Catalog, Run, Player }.Select(Hash).ToArray();
|
|
using (var cache = AetheriaStores.Open(Catalog, Run, Player))
|
|
{
|
|
Assert.NotNull(cache.GetGlobal<SavedGame>());
|
|
Assert.NotNull(cache.GetGlobal<PlayerSettings>());
|
|
cache.FlushAsync().Wait();
|
|
}
|
|
|
|
Assert.Equal(before, new[] { Catalog, Run, Player }.Select(Hash).ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void WeaponWrittenThroughGearHandleReloadsAsWeapon()
|
|
{
|
|
CultRecordHandle<GearData> handle;
|
|
using (var cache = AetheriaStores.Open(Catalog, catalogWritable: true))
|
|
{
|
|
handle = cache.UpsertAsync<GearData>(new WeaponItemData { Name = "Pike" }).Result;
|
|
cache.FlushAsync().Wait();
|
|
}
|
|
|
|
using (var cache = AetheriaStores.Open(Catalog))
|
|
Assert.Equal("Pike", Assert.IsType<WeaponItemData>(cache.Get(handle.Key)).Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void CatalogRefusesWrites()
|
|
{
|
|
var before = Hash(Catalog);
|
|
using (var cache = AetheriaStores.Open(Catalog))
|
|
{
|
|
Assert.Throws<InvalidOperationException>(() => cache.Upsert(new Faction { Name = "Usurper" }));
|
|
cache.FlushAsync().Wait();
|
|
}
|
|
|
|
Assert.Equal(before, Hash(Catalog));
|
|
}
|
|
|
|
[Fact]
|
|
public void SavedZoneNeverLandsInCatalog()
|
|
{
|
|
using (var cache = AetheriaStores.Open(Catalog, catalogWritable: true))
|
|
Assert.Throws<InvalidOperationException>(() => cache.Upsert(new SavedZone { Name = "Stray" }));
|
|
|
|
using (var cache = AetheriaStores.Open(Catalog, Run, catalogWritable: true))
|
|
{
|
|
cache.Upsert(new SavedZone { Name = "Home" });
|
|
cache.FlushAsync().Wait();
|
|
}
|
|
|
|
Assert.DoesNotContain(SchemaNames(Catalog), name => name == "aetheria.savedzone");
|
|
Assert.Contains("aetheria.savedzone", SchemaNames(Run));
|
|
}
|
|
|
|
[Fact]
|
|
public void FactionIsASingletonInstance()
|
|
{
|
|
using var cache = AetheriaStores.Open(Catalog);
|
|
var faction = cache.GetAll<Faction>().Single();
|
|
Assert.Same(faction, cache.Get(cache.GetAll<WeaponItemData>().Single().Manufacturer));
|
|
Assert.Same(faction, cache.Get(faction.Allegiance.Keys.Single()));
|
|
Assert.Same(faction, cache.GetByName<Faction>("Adrasteia"));
|
|
}
|
|
|
|
[Fact]
|
|
public void MissingCatalogGlobalIsLoud()
|
|
{
|
|
var empty = Path.Combine(_root, "empty.cc");
|
|
using (AetheriaStores.Open(empty, catalogWritable: true)) { }
|
|
var error = Assert.Throws<InvalidOperationException>(() => AetheriaStores.Open(empty));
|
|
Assert.Contains("aetheria.tests.catalogglobal", error.Message);
|
|
using (AetheriaStores.Open(Catalog)) { }
|
|
}
|
|
|
|
private static string Hash(string path) => Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path)));
|
|
|
|
private static string[] SchemaNames(string path)
|
|
{
|
|
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
|
|
return snapshot.Records
|
|
.Select(record => snapshot.SchemaCatalog.Single(entry => entry.SchemaId == record.SchemaId).SchemaName)
|
|
.ToArray();
|
|
}
|
|
}
|