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>
95 lines
5.0 KiB
C#
95 lines
5.0 KiB
C#
using RailEnterprise.Core.Commands;
|
|
using RailEnterprise.Core.Common;
|
|
using RailEnterprise.Core.Economy;
|
|
using RailEnterprise.Core.Persistence;
|
|
using RailEnterprise.Core.Rail;
|
|
using RailEnterprise.Tests.Harness;
|
|
using static RailEnterprise.Tests.Rail.RailFixture;
|
|
|
|
namespace RailEnterprise.Tests.Rail;
|
|
|
|
public static class TrainTests
|
|
{
|
|
private static (RailFixture F, Train Train, int Line) FreightLine(int wagons = 5)
|
|
{
|
|
var f = Valdoria();
|
|
var (mine, mill, depot) = f.BuildFreightNetwork();
|
|
int tr = f.Train(depot, "loco_steam_1850", 1, "wagon_hopper", wagons, "Treno 12");
|
|
int line = f.Ok(new CreateLineCommand("Linea 3, carbone", "#e0a526",
|
|
new[] { new LineStop(new StationId(mine), WaitFull: true, MaxWaitDays: 2), new LineStop(new StationId(mill)) }));
|
|
f.Ok(new AssignTrainCommand(new TrainId(tr), new LineId(line)));
|
|
return (f, f.W.Trains[tr], line);
|
|
}
|
|
|
|
[Test(Description = "La linea merci porta minerale e carbone all'acciaieria e il vettore incassa le tariffe")]
|
|
public static void FreightLineDeliversAndEarns()
|
|
{
|
|
var (f, t, _) = FreightLine();
|
|
var steel = f.W.Industries.First(i => i.Def.Id == "steel_mill");
|
|
long revenueBefore = f.W.Player.YearEntries[(int)LedgerEntry.FreightRevenue];
|
|
f.W.RunDays(60);
|
|
Assert.True(t.Odometer > 50_000, $"il treno ha viaggiato ({t.Odometer / 1000:F1} km)");
|
|
Assert.True(steel.InputStock.All(s => s > 0), "l'acciaieria riceve minerale e carbone");
|
|
Assert.True(f.W.Player.YearEntries[(int)LedgerEntry.FreightRevenue] > revenueBefore, "tariffe incassate");
|
|
Assert.True(steel.ServedWithin(f.W.Clock.Day, 10), "industria servita dal vettore");
|
|
Assert.Equal(0L, f.W.MoneyBalanceSum());
|
|
for (int g = 0; g < f.W.Data.GoodCount; g++)
|
|
Assert.Equal(f.W.Goods.ExpectedStock(g), f.W.TotalStock(g), $"merce {f.W.Data.Goods[g].Id}");
|
|
}
|
|
|
|
[Test(Description = "Due treni in versi opposti sullo stesso binario semplice si incrociano in stazione senza stallo")]
|
|
public static void OpposingTrainsOnSingleTrackDoNotDeadlock()
|
|
{
|
|
var (f, t1, line) = FreightLine();
|
|
int depot = f.W.Network.Depots[0].Id.Value;
|
|
int tr2 = f.Train(depot, "loco_steam_1850", 1, "wagon_hopper", 5, "Treno 13");
|
|
var t2 = f.W.Trains[tr2];
|
|
// il secondo parte più tardi: i due si troveranno in versi opposti
|
|
f.W.RunDays(3);
|
|
f.Ok(new AssignTrainCommand(new TrainId(tr2), new LineId(line)));
|
|
f.W.RunDays(120);
|
|
Assert.True(t1.Odometer > 150_000 && t2.Odometer > 150_000, $"entrambi viaggiano: {t1.Odometer / 1000:F0} e {t2.Odometer / 1000:F0} km");
|
|
foreach (var e in f.W.Network.Edges) Assert.True(e.LockCount <= 2);
|
|
Assert.False(f.W.Events.Recent.Any(e => e.Text.Contains("possibile stallo", StringComparison.Ordinal)));
|
|
}
|
|
|
|
[Test(Description = "Un treno troppo pesante per la salita resta fermo (Stalled) e genera un avviso")]
|
|
public static void OverloadedTrainStallsOnTheGrade()
|
|
{
|
|
var f = Valdoria();
|
|
var (mine, mill, depot) = f.BuildFreightNetwork();
|
|
int tr = f.Train(depot, "loco_steam_1850", 1, "wagon_hopper", 20, "Treno pesante");
|
|
// carica forzata: 20 tramogge piene di minerale (contabilizzate come prodotte)
|
|
var t = f.W.Trains[tr];
|
|
int ore = f.W.Data.GoodIndex("iron_ore");
|
|
var st = f.W.Network.Stations[mine];
|
|
st.Waiting.Add(new Packet { Id = f.W.NextIds.Packet++, Kind = PacketKind.Freight, Good = ore, Units = 400, Origin = st.Id, Destination = new StationId(mill), HopLine = LineId.None, Contract = -1, ExpiresTick = long.MaxValue });
|
|
f.W.Goods.Produced[ore] += 400;
|
|
int line = f.Ok(new CreateLineCommand("Pesante", "#b3573e", new[] { new LineStop(new StationId(mine)), new LineStop(new StationId(mill)) }));
|
|
for (int i = 0; i < st.Waiting.Count; i++) { var p = st.Waiting[i]; p.HopLine = new LineId(line); p.HopEnd = new StationId(mill); st.Waiting[i] = p; }
|
|
f.Ok(new AssignTrainCommand(new TrainId(tr), new LineId(line)));
|
|
Assert.True(f.RunUntil(() => t.State == TrainState.Stalled, 60), $"stato {t.State}");
|
|
Assert.True(f.W.Events.Recent.Any(e => e.Text.Contains("non supera la salita", StringComparison.Ordinal)));
|
|
}
|
|
|
|
[Test(Description = "Determinismo e salvataggio con treni in movimento")]
|
|
public static void TrainsAreDeterministicAndSurviveSaveLoad()
|
|
{
|
|
var (a, ta, _) = FreightLine();
|
|
a.W.RunDays(17);
|
|
a.W.RunTicks(77); // a metà strada, fuori dal confine del giorno
|
|
var b = SaveFile.Load(TestData.Data, SaveFile.Save(a.W, "test"));
|
|
Assert.Equal(StateHasher.Compute(a.W), StateHasher.Compute(b));
|
|
a.W.RunDays(40);
|
|
b.RunDays(40);
|
|
Assert.Equal(a.W.LastStateHash, b.LastStateHash);
|
|
Assert.Approx(ta.Odometer, b.Trains[ta.Id.Value].Odometer, 1e-9);
|
|
|
|
var (c, _, _) = FreightLine();
|
|
c.W.RunDays(17);
|
|
c.W.RunTicks(77);
|
|
c.W.RunDays(40);
|
|
Assert.Equal(a.W.LastStateHash, c.W.LastStateHash, "due esecuzioni identiche");
|
|
}
|
|
}
|