Skip to content

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): budget allocation must cover exactly the budgeted offices

eyalgolan8a5adcafdf0b
diff --git a/sim/Statecraft.Sim.Tests/EconomyTests.cs b/sim/Statecraft.Sim.Tests/EconomyTests.cs
index d49909559..997fdc03f 100644
--- a/sim/Statecraft.Sim.Tests/EconomyTests.cs
+++ b/sim/Statecraft.Sim.Tests/EconomyTests.cs
@@ -78,7 +78,7 @@ public class EconomyNegativeShareTests
[Fact]
public void NegativeShareRejectedEvenIfSumIsOne()
{
- var c = ContentLoader.Load(Fixture.Write());
+ var c = EconomyCoverageTests.TwoBudgets();
var s = new GameState();
EconomySystem.Init(c, s);
var err = EconomySystem.Allocate(c, s, new System.Collections.Generic.Dictionary<string, double> { ["finance_minister"] = 1.5, ["defense_minister"] = -0.5 });
@@ -119,3 +119,38 @@ public class EconomyInvalidShareTests
Assert.Equal(before, s.Budget);
}
}
+
+public class EconomyCoverageTests
+{
+ internal static Content TwoBudgets() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string>
+ {
+ ["rules.yaml"] = Fixture.Files["rules.yaml"].Replace("budget_start: { finance_minister: 1.0 }", "budget_start: { finance_minister: 0.5, defense_minister: 0.5 }"),
+ }));
+
+ [Fact]
+ public void PartialMapIsRejectedAndBudgetUnchanged()
+ {
+ var c = TwoBudgets(); var s = new GameState(); EconomySystem.Init(c, s);
+ var before = new Dictionary<string, double>(s.Budget);
+ Assert.Equal("Budget shares must cover exactly: defense_minister, finance_minister",
+ EconomySystem.Allocate(c, s, new Dictionary<string, double> { ["finance_minister"] = 1.0 }));
+ Assert.Equal(before, s.Budget);
+ }
+
+ [Fact]
+ public void MapWithAnUnbudgetedOfficeIsRejected()
+ {
+ var c = TwoBudgets(); var s = new GameState(); EconomySystem.Init(c, s);
+ Assert.Equal("Budget shares must cover exactly: defense_minister, finance_minister",
+ EconomySystem.Allocate(c, s, new Dictionary<string, double> { ["finance_minister"] = 0.4, ["defense_minister"] = 0.4, ["prime_minister"] = 0.2 }));
+ }
+
+ [Fact]
+ public void FullMapIsAccepted()
+ {
+ var c = TwoBudgets(); var s = new GameState(); EconomySystem.Init(c, s);
+ var shares = new Dictionary<string, double> { ["finance_minister"] = 0.3, ["defense_minister"] = 0.7 };
+ Assert.Null(EconomySystem.Allocate(c, s, shares));
+ Assert.Equal(shares, s.Budget);
+ }
+}
diff --git a/sim/Statecraft.Sim/EconomySystem.cs b/sim/Statecraft.Sim/EconomySystem.cs
index 857aae12a..b5c4bd82b 100644
--- a/sim/Statecraft.Sim/EconomySystem.cs
+++ b/sim/Statecraft.Sim/EconomySystem.cs
@@ -48,6 +48,8 @@ namespace Statecraft.Sim
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 (!new HashSet<string>(shares.Keys).SetEquals(c.Rules.BudgetStart.Keys))
+ return "Budget shares must cover exactly: " + string.Join(", ", c.Rules.BudgetStart.Keys.OrderBy(k => k, StringComparer.Ordinal));
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);