A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix(sim): reject non-finite and null budget shares
eyalgolan65348a553105
diff --git a/sim/Statecraft.Sim.Tests/EconomyTests.cs b/sim/Statecraft.Sim.Tests/EconomyTests.csindex 3000a94ae..d49909559 100644--- a/sim/Statecraft.Sim.Tests/EconomyTests.cs+++ b/sim/Statecraft.Sim.Tests/EconomyTests.cs@@ -85,3 +85,37 @@ public class EconomyNegativeShareTestsAssert.Equal("Budget shares cannot be negative", err);}}++public class EconomyInvalidShareTests+{+ static readonly Content C = ContentLoader.Load(Fixture.Write());++ static GameState Fresh() { var s = new GameState(); EconomySystem.Init(C, s); return s; }++ [Fact]+ public void NaNShareRejectedAndBudgetUnchanged()+ {+ var s = Fresh(); var before = new Dictionary<string, double>(s.Budget);+ var err = EconomySystem.Allocate(C, s, new Dictionary<string, double> { ["finance_minister"] = double.NaN });+ Assert.False(string.IsNullOrEmpty(err));+ Assert.Equal(before, s.Budget);+ }++ [Fact]+ public void InfinitePairRejectedAndBudgetUnchanged()+ {+ var s = Fresh(); var before = new Dictionary<string, double>(s.Budget);+ var err = EconomySystem.Allocate(C, s, new Dictionary<string, double> { ["finance_minister"] = double.PositiveInfinity, ["defense_minister"] = double.NegativeInfinity });+ Assert.False(string.IsNullOrEmpty(err));+ Assert.Equal(before, s.Budget);+ }++ [Fact]+ public void NullSharesRejectedAndBudgetUnchanged()+ {+ var s = Fresh(); var before = new Dictionary<string, double>(s.Budget);+ var err = EconomySystem.Allocate(C, s, null!);+ Assert.False(string.IsNullOrEmpty(err));+ Assert.Equal(before, s.Budget);+ }+}diff --git a/sim/Statecraft.Sim/EconomySystem.cs b/sim/Statecraft.Sim/EconomySystem.csindex 238375531..1c198425f 100644--- a/sim/Statecraft.Sim/EconomySystem.cs+++ b/sim/Statecraft.Sim/EconomySystem.cs@@ -44,8 +44,10 @@ namespace Statecraft.Sim/// <summary>Returns null on success, or the reason the allocation was rejected.</summary>public static string? Allocate(Content c, GameState s, Dictionary<string, double> shares){+ if (shares == null) return "Budget shares are required";var unknown = shares.Keys.FirstOrDefault(k => c.Offices.All(o => o.Id != k));if (unknown != null) return $"Unknown office '{unknown}'";+ if (shares.Values.Any(v => double.IsNaN(v) || double.IsInfinity(v))) return "Budget shares must be finite numbers";if (Math.Abs(shares.Values.Sum() - 1.0) > 1e-6) return "Budget shares must sum to 1";if (shares.Values.Any(v => v < 0)) return "Budget shares cannot be negative";s.Budget = new Dictionary<string, double>(shares);