Files
Rail_Enterprise/tests/RailEnterprise.Tests/Persistence/PersistenceTests.cs
T
Alby96andClaude Opus 5.5 c1a0049bcf
CI / Build e test (pull_request) Successful in 1m20s
CI / Immagine Docker (pull_request) Successful in 37s
Infrastruttura: nome «Rail Enterprise», CI e rilasci su Gitea, documentazione
Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*,
variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per
allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos:
CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in
server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly.
Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e
aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:14:54 +01:00

105 lines
3.8 KiB
C#

using RailEnterprise.Core.Commands;
using RailEnterprise.Core.Common;
using RailEnterprise.Core.Persistence;
using RailEnterprise.Core.Simulation;
using RailEnterprise.Tests.Harness;
namespace RailEnterprise.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());
}
}