Files
Rail_Enterprise/tests/ViaFerrea.Tests/Persistence/PersistenceTests.cs
T
Claude 7cb98fa206 F1: economia headless, dati validati, generatore di mappa e salvataggi
- Base deterministica: Money, id tipizzati, DMath, XxHash64, calendario
- 12 file dati con schemi, validatore di schema interno, validazione semantica
  e hash dei dati; viaferrea-tools validate-data
- Generatore di mappa (rilievo, mare, priority-flood, fiumi, laghi, copertura,
  giacimenti, città, hub, industrie) e viaferrea-tools genmap
- Economia: città, industrie, hub, mercato con componente mondiale, prezzi
  locali, tariffe, lavoro in sede e a piedi, trasporto locale, registro a
  partita doppia, prestiti, rating, difficoltà
- Salvataggi Brotli con rimappatura per id e hash di stato giornaliero
- viaferrea-tools simulate con KPI CSV; 45 test verdi; ADR 0002-0006

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
2026-10-01 22:38:17 +00:00

105 lines
3.8 KiB
C#

using ViaFerrea.Core.Commands;
using ViaFerrea.Core.Common;
using ViaFerrea.Core.Persistence;
using ViaFerrea.Core.Simulation;
using ViaFerrea.Tests.Harness;
namespace ViaFerrea.Tests.Persistence;
public static class PersistenceTests
{
[Test(Description = "Determinismo: stesso seme e stessi comandi agli stessi tick → stesso hash")]
public static void SameSeedAndCommandsGiveSameHash()
{
ulong Run()
{
var w = TestWorlds.Small(200);
w.RunDays(45);
w.Enqueue(new TakeLoanCommand(Money.FromLire(50_000)) { Issuer = w.Player.Id, Seq = 1 });
w.RunDays(400);
return w.LastStateHash;
}
ulong a = Run();
ulong b = Run();
Assert.Equal(a, b);
var other = TestWorlds.Small(201);
other.RunDays(445);
Assert.NotEqual(a, other.LastStateHash);
}
[Test(Description = "Salvataggio e caricamento: stesso hash subito e dopo altri 200 giorni")]
public static void SaveLoadRoundTripPreservesFuture()
{
var a = TestWorlds.Small(202);
a.Enqueue(new TakeLoanCommand(Money.FromLire(80_000)) { Issuer = a.Player.Id });
a.RunDays(100);
byte[] file = SaveFile.Save(a, "test");
var b = SaveFile.Load(TestData.Data, file);
Assert.Equal(StateHasher.Compute(a), StateHasher.Compute(b));
Assert.Equal(a.LastStateHash, b.LastStateHash);
Assert.Equal(a.Clock.Tick, b.Clock.Tick);
Assert.Equal(a.Events.Total, b.Events.Total);
a.RunDays(200);
b.RunDays(200);
Assert.Equal(a.LastStateHash, b.LastStateHash);
Assert.Equal(0L, b.MoneyBalanceSum());
// un secondo giro di salvataggio produce un file identico
Assert.SequenceEqual(SaveFile.Save(a, "test"), SaveFile.Save(b, "test"));
}
[Test]
public static void SaveHeaderIsReadableWithoutDecompressing()
{
var w = TestWorlds.Small(203);
w.RunDays(10);
var info = SaveFile.ReadInfo(SaveFile.Save(w, "0.1.0"));
Assert.Equal("0.1.0", info.GameVersion);
Assert.Equal(w.Clock.Tick, info.Tick);
Assert.Equal(w.Settings.Seed, info.Seed);
Assert.Equal(w.Player.Name, info.CompanyName);
Assert.Equal(w.Data.Hash, info.DataHash);
}
[Test]
public static void CorruptSavesAreRejected()
{
var w = TestWorlds.Small(204);
byte[] file = SaveFile.Save(w, "test");
Assert.Throws<InvalidDataException>(() => SaveFile.Load(TestData.Data, "XXXX"u8.ToArray()));
var truncated = file.AsSpan(0, file.Length / 2).ToArray();
Assert.Throws<Exception>(() => SaveFile.Load(TestData.Data, truncated));
}
[Test]
public static void SaveWriterReaderRoundTripPrimitives()
{
var wtr = new SaveWriter(4);
wtr.Write((byte)7);
wtr.Write(true);
wtr.Write(-12345);
wtr.Write(long.MinValue);
wtr.Write(ulong.MaxValue);
wtr.Write(Math.PI);
wtr.Write("Città d'acqua €");
wtr.Write((string?)null);
wtr.Write(new[] { 1, 2, 3 }.AsSpan());
wtr.Write(new ushort[] { 0, 65535 }.AsSpan());
var r = new SaveReader(wtr.ToArray());
Assert.Equal((byte)7, r.ReadByte());
Assert.True(r.ReadBool());
Assert.Equal(-12345, r.ReadInt32());
Assert.Equal(long.MinValue, r.ReadInt64());
Assert.Equal(ulong.MaxValue, r.ReadUInt64());
Assert.Equal(Math.PI, r.ReadDouble());
Assert.Equal("Città d'acqua €", r.ReadString());
Assert.Equal(null, r.ReadString());
Assert.SequenceEqual(new[] { 1, 2, 3 }, r.ReadInt32Array());
Assert.SequenceEqual(new ushort[] { 0, 65535 }, r.ReadUInt16Array());
Assert.True(r.AtEnd);
Assert.Throws<InvalidDataException>(() => r.ReadInt32());
}
}