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>
205 lines
11 KiB
C#
205 lines
11 KiB
C#
using RailEnterprise.Core.Commands;
|
|
using RailEnterprise.Core.Common;
|
|
using RailEnterprise.Core.Data;
|
|
using RailEnterprise.Core.Economy;
|
|
using RailEnterprise.Core.Rail;
|
|
using RailEnterprise.Core.Simulation;
|
|
using RailEnterprise.Core.Simulation.Systems;
|
|
using RailEnterprise.Tests.Harness;
|
|
using static RailEnterprise.Tests.Rail.RailFixture;
|
|
|
|
namespace RailEnterprise.Tests.Rail;
|
|
|
|
public static class CommutingTests
|
|
{
|
|
/// <summary>Linea operai Borgo Ceresio ↔ acciaieria con un deposito sulla diramazione.</summary>
|
|
private static (RailFixture F, Industry Mill, int Train, int Line) WorkersLine(int coaches, int staffCoaches = 0)
|
|
{
|
|
var f = Valdoria();
|
|
int mill = f.Ok(new BuildStationCommand(K(10.6, 5.4), 2, "Acciaieria"));
|
|
int borgo = f.Ok(new BuildStationCommand(K(12.4, 8.0), 2, "Borgo Ceresio"));
|
|
int depot = f.Ok(new BuildDepotCommand(K(11.6, 7.4), "small", "Deposito di Borgo"));
|
|
f.Ok(new BuildTrackCommand(f.Scenario!.Route("borgo", f.Me).Polyline));
|
|
f.Ok(new BuildTrackCommand(new[] { K(11.6, 7.4), K(11.7, 6.85) }));
|
|
Assert.True(f.RunUntil(() => f.W.Network.Edges.All(e => e.State == EdgeState.Active), 400), "cantieri completati");
|
|
int train = staffCoaches > 0
|
|
? f.Train(depot, "Operai 1", ("loco_steam_1850", 1), ("coach_worker", coaches), ("coach_staff", staffCoaches))
|
|
: f.Train(depot, "Operai 1", ("loco_steam_1850", 1), ("coach_worker", coaches));
|
|
int line = f.Ok(new CreateLineCommand("Operai di Borgo", "#4f7cac",
|
|
new[] { new LineStop(new StationId(borgo)), new LineStop(new StationId(mill)) }));
|
|
f.Ok(new AssignTrainCommand(new TrainId(train), new LineId(line)));
|
|
return (f, f.W.Industries.First(i => i.Def.Id == "steel_mill"), train, line);
|
|
}
|
|
|
|
/// <summary>Avanza fino a dopo il prossimo abbinamento mensile.</summary>
|
|
private static void RunToNextMonth(RailFixture f)
|
|
{
|
|
long month = f.W.Clock.Day / f.W.Calendar.DaysPerMonth;
|
|
while (f.W.Clock.Day / f.W.Calendar.DaysPerMonth == month) f.W.RunDays(1);
|
|
f.W.RunDays(1);
|
|
}
|
|
|
|
[Test(Description = "Pendolarismo (14.5): con città raggiungibile e capacità sufficiente la copertura supera 0.9 in 60 giorni")]
|
|
public static void ReachableTownWithCapacityCoversJobsWithinSixtyDays()
|
|
{
|
|
var (f, mill, _, _) = WorkersLine(coaches: 2);
|
|
RunToNextMonth(f);
|
|
var contracts = f.W.Commutes.Where(c => !c.Walking && c.Workplace.Index == mill.Id.Value && c.Category == PaxClass.Worker).ToList();
|
|
Assert.True(contracts.Count > 0, "contratti su rotaia creati all'abbinamento mensile");
|
|
Assert.Equal(mill.JobsWorkers - (int)Math.Round(mill.JobsWorkers * f.W.Data.Balance.Industry.OnSiteWorkerShare), contracts.Sum(c => c.Headcount));
|
|
foreach (var c in contracts)
|
|
{
|
|
Assert.Equal("Borgo Ceresio", f.W.Towns[c.Town.Value].Name);
|
|
Assert.True(c.ExpectedMinutes <= f.W.Data.Balance.Employment.WorkerMaxMinutes);
|
|
}
|
|
f.W.RunDays(60);
|
|
foreach (var c in contracts) Assert.True(c.Coverage > 0.9, $"copertura del contratto {c.Coverage:F2}");
|
|
Assert.True(mill.CovWorkers > 0.9, $"copertura dei lavoratori dell'acciaieria {mill.CovWorkers:F2}");
|
|
Assert.True(f.W.Player.YearEntries[(int)LedgerEntry.PassengerRevenue] > 0, "tariffe incassate");
|
|
Assert.Equal(0L, f.W.MoneyBalanceSum());
|
|
}
|
|
|
|
[Test(Description = "L'abbinamento non supera la capacità residua stimata della linea")]
|
|
public static void MatchingRespectsLineCapacity()
|
|
{
|
|
var (f, mill, _, line) = WorkersLine(coaches: 1);
|
|
// l'acciaieria al livello 3 offre più posti di quanti una carrozza ne porti
|
|
mill.Level = 3;
|
|
RunToNextMonth(f);
|
|
var graph = f.W.LineGraphs.For(f.W, f.Me);
|
|
double seats = graph.DailyCapacity[line].Workers * f.W.Data.Balance.Employment.LineCapacityUtilization;
|
|
int contracted = f.W.Commutes.Where(c => !c.Walking).Sum(c => c.Headcount);
|
|
Assert.True(contracted > 0 && contracted <= seats, $"{contracted} pendolari per {seats:F0} posti al giorno");
|
|
Assert.True(contracted < EmploymentOpen(f, mill), "restano posti scoperti per mancanza di carrozze");
|
|
}
|
|
|
|
private static int EmploymentOpen(RailFixture f, Industry mill) =>
|
|
mill.JobsWorkers - (int)Math.Round(mill.JobsWorkers * f.W.Data.Balance.Industry.OnSiteWorkerShare);
|
|
|
|
[Test(Description = "Senza più treni sulla linea i contratti si sciolgono e la città se ne lamenta")]
|
|
public static void ContractsDissolveWhenTheRouteDisappears()
|
|
{
|
|
var (f, mill, train, _) = WorkersLine(coaches: 2);
|
|
RunToNextMonth(f);
|
|
Assert.True(f.W.Commutes.Any(c => !c.Walking));
|
|
var borgo = f.W.Towns.First(t => t.Name == "Borgo Ceresio");
|
|
int employedBefore = borgo.EmployedWorkers;
|
|
f.Ok(new AssignTrainCommand(new TrainId(train), LineId.None));
|
|
RunToNextMonth(f);
|
|
Assert.False(f.W.Commutes.Any(c => !c.Walking), "contratti sciolti");
|
|
Assert.True(borgo.WorkDissatisfaction > 0, "insoddisfazione «lavoro»");
|
|
Assert.True(borgo.EmployedWorkers < employedBefore, "i lavoratori tornano liberi");
|
|
Assert.True(f.W.Events.Recent.Any(e => e.Text.Contains("perdono il posto", StringComparison.Ordinal)));
|
|
Assert.Equal(0, mill.ContractedWorkers);
|
|
}
|
|
|
|
[Test(Description = "Il personale qualificato viene solo da città con servizi: Borgo Ceresio non ne fornisce")]
|
|
public static void StaffComesOnlyFromTownsWithServices()
|
|
{
|
|
var (f, mill, _, _) = WorkersLine(coaches: 2, staffCoaches: 1);
|
|
RunToNextMonth(f);
|
|
Assert.True(f.W.Commutes.Any(c => !c.Walking && c.Category == PaxClass.Worker));
|
|
Assert.False(f.W.Commutes.Any(c => !c.Walking && c.Category == PaxClass.Staff));
|
|
Assert.True(mill.StaffPresent < mill.JobsStaff, "il personale resta scoperto");
|
|
}
|
|
|
|
[Test(Description = "I depositi assumono pendolari: senza servizi in città manca il personale e l'officina rallenta")]
|
|
public static void DepotsHireCommutersAndExplainMissingStaff()
|
|
{
|
|
// il deposito di Borgo Ceresio: la città è troppo piccola per avere personale qualificato
|
|
var (f, _, _, _) = WorkersLine(coaches: 2);
|
|
var officina = f.W.Network.Depots[0];
|
|
Assert.True(officina.JobsWorkers > 0 && officina.JobsStaff == officina.Stalls * f.W.Data.Maintenance.Depots.StaffPerStall);
|
|
RunToNextMonth(f);
|
|
var borgo = f.W.Towns.First(t => t.Name == "Borgo Ceresio");
|
|
Assert.Equal(0, borgo.StaffPool(f.W.Data.Balance.Town));
|
|
Assert.Equal(officina.JobsWorkers, officina.ContractedWorkers);
|
|
Assert.True(f.W.Commutes.Any(c => c.Walking && c.Workplace == officina.Ref && c.Category == PaxClass.Worker), "lavoratori a piedi");
|
|
Assert.Approx(officina.JobsWorkers, officina.WorkersPresent, 1e-9);
|
|
Assert.Equal(UncoveredReason.NoStaffServices, officina.StaffReason);
|
|
Assert.True(FleetMaintenanceSystem.WorkRate(f.W, officina) < 0.5, "senza personale l'officina lavora a un terzo");
|
|
|
|
// quando la città ha i servizi, il personale arriva a piedi e l'officina lavora a pieno ritmo
|
|
var g = Valdoria();
|
|
var net = g.BuildMaintenanceNetwork(partsLine: false, trainsPerLine: 0);
|
|
var full = g.W.Network.Depots[net.Officina];
|
|
Assert.True(g.W.Towns.First(t => t.Name == "Valdoria").StaffPool(g.W.Data.Balance.Town) > 0);
|
|
RunToNextMonth(g);
|
|
Assert.Equal(full.JobsStaff, full.ContractedStaff);
|
|
Assert.Approx(full.JobsStaff, full.StaffPresent, 1e-9);
|
|
Assert.Equal(UncoveredReason.None, full.StaffReason);
|
|
Assert.Approx(1.0, FleetMaintenanceSystem.WorkRate(g.W, full), 1e-12);
|
|
}
|
|
|
|
[Test(Description = "Registro degli impieghi: posti scoperti per mancanza di stazioni o di capacità delle carrozze")]
|
|
public static void RegisterExplainsUncoveredJobs()
|
|
{
|
|
var bare = Valdoria();
|
|
RunToNextMonth(bare);
|
|
var lonelyMill = bare.W.Industries.First(i => i.Def.Id == "steel_mill");
|
|
Assert.Equal(UncoveredReason.NoStation, lonelyMill.WorkersReason);
|
|
|
|
var (f, mill, _, line) = WorkersLine(coaches: 1);
|
|
mill.Level = 3;
|
|
RunToNextMonth(f);
|
|
Assert.Equal(UncoveredReason.LineCapacity, mill.WorkersReason);
|
|
Assert.Equal(line, mill.WorkersReasonLine.Value);
|
|
// la linea non ha carrozze per tecnici e nessuna città è a distanza pedonale
|
|
Assert.Equal(UncoveredReason.NoTownInReach, mill.StaffReason);
|
|
}
|
|
|
|
[Test(Description = "Convenzioni: l'industria propone, il vettore accetta e incassa il canone; senza capacità salta con penale")]
|
|
public static void AgreementIsProposedPaidAndBrokenWithPenalty()
|
|
{
|
|
var (f, mill, train, _) = WorkersLine(coaches: 2);
|
|
CommuteAgreement? proposal = null;
|
|
for (int m = 0; m < 12 && proposal is null; m++)
|
|
{
|
|
RunToNextMonth(f);
|
|
proposal = f.W.Agreements.FirstOrDefault(a => a.State == AgreementState.Proposed);
|
|
}
|
|
Assert.True(proposal is not null, "proposta entro un anno");
|
|
Assert.Equal(mill.Id, proposal!.Industry);
|
|
Assert.True(proposal.SeatsPerDay >= f.W.Data.Balance.Employment.Agreements.MinSeats);
|
|
Assert.True(f.W.Events.Recent.Any(e => e.Severity == EventSeverity.Opportunity && e.Text.Contains("convenzione", StringComparison.Ordinal)));
|
|
Assert.False(f.Do(new AcceptAgreementCommand(new AgreementId(999))).Ok);
|
|
f.Ok(new AcceptAgreementCommand(proposal.Id));
|
|
Assert.Equal(AgreementState.Active, proposal.State);
|
|
Assert.False(f.Do(new RejectAgreementCommand(proposal.Id)).Ok, "non si rifiuta una convenzione attiva");
|
|
|
|
long fees = f.W.Player.YearEntries[(int)LedgerEntry.Contracts] + f.W.Player.LastYearEntries[(int)LedgerEntry.Contracts];
|
|
RunToNextMonth(f);
|
|
long feesAfter = f.W.Player.YearEntries[(int)LedgerEntry.Contracts] + f.W.Player.LastYearEntries[(int)LedgerEntry.Contracts];
|
|
Assert.True(feesAfter - fees >= proposal.MonthlyFee.Cents, "canone mensile incassato");
|
|
Assert.Equal(AgreementState.Active, proposal.State);
|
|
|
|
// senza treni la capacità garantita manca: dopo 30 giorni la convenzione salta con penale
|
|
f.Ok(new AssignTrainCommand(new TrainId(train), LineId.None));
|
|
Assert.True(f.RunUntil(() => proposal.State == AgreementState.Broken, 90), $"convenzione ancora {proposal.State}");
|
|
Assert.True(f.W.Player.YearEntries[(int)LedgerEntry.Penalties] < 0, "penale pagata");
|
|
Assert.True(f.W.Events.Recent.Any(e => e.Text.Contains("saltata", StringComparison.Ordinal)));
|
|
Assert.Equal(0L, f.W.MoneyBalanceSum());
|
|
}
|
|
|
|
[Test(Description = "Una convenzione rifiutata non viene riproposta prima del periodo di attesa")]
|
|
public static void RejectedAgreementWaitsBeforeANewProposal()
|
|
{
|
|
var (f, _, _, _) = WorkersLine(coaches: 2);
|
|
CommuteAgreement? proposal = null;
|
|
for (int m = 0; m < 12 && proposal is null; m++)
|
|
{
|
|
RunToNextMonth(f);
|
|
proposal = f.W.Agreements.FirstOrDefault(a => a.State == AgreementState.Proposed);
|
|
}
|
|
Assert.True(proposal is not null);
|
|
f.Ok(new RejectAgreementCommand(proposal!.Id));
|
|
Assert.Equal(AgreementState.Rejected, proposal.State);
|
|
int cooldown = f.W.Data.Balance.Employment.Agreements.CooldownMonths;
|
|
for (int m = 0; m < cooldown - 1; m++)
|
|
{
|
|
RunToNextMonth(f);
|
|
Assert.False(f.W.Agreements.Any(a => a.State == AgreementState.Proposed), "nessuna nuova proposta durante l'attesa");
|
|
}
|
|
}
|
|
}
|