A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Statecraft.Sim;
/// <summary>Issue #13: a headless run of the whole M3 slice (15 May 1948 to the end of the 1948-51 content: GoldenRun.Until1952,
/// the last turn ends 27 February 1952) through the command path the UI uses, checking invariants after every turn.
///
/// The UI's only door into the sim is SimBridge (unity/Assets/Scripts/UI/SimBridge.cs, a Unity assembly .NET cannot
/// reference): it queues Commands, refuses one it cannot afford in attention, and End Turn calls GameSession.Step with the
/// queue; autosave writes GameSession.Save.Write() and a restart loads it with GameSession.Load. This test drives that same
/// GameSession with the same Command objects the presenters queue (EventCardPresenter, BudgetPresenter, CabinetPresenter,
/// DefencePresenter, InfoPanelPresenter), and applies SimBridge.Queue's attention check before each order.
///
/// Each turn: (1) no order that is legal by the sim's own checks (pending event and option, Cabinet.Check, MapActions.Check,
/// a level in range) and affordable at this turn's prices is refused; (2) no content id and no ISO date shows in the text
/// the player sees after the turn (Why lines, refusals, the event cards, the top bar's sim-formatted values, the cabinet
/// screen's reasons, map-action reasons); (3) the save loads back to the same hash with no warning, and the game goes on
/// from the loaded session, ending where an uninterrupted run ends.</summary>
public class SliceSmokeTests
{
static readonly Content Real = ContentLoader.Load(RealContentTests.ContentRoot());
// Generous elapsed hang guard, not a performance budget: busy-machine timing belongs outside this smoke gate.
static readonly TimeSpan HangGuard = TimeSpan.FromMinutes(5);
/// <summary>docs/determinism.md: the WebGL smoke (?smoke=5) ends here; its policy is FirstOption.</summary>
static string WebSmoke5 => "SMOKE OK 5 turns 1948-10-15 " + GoldenManifest.Expected("smoke5");
static readonly Regex IsoDate = new Regex(@"\d{4}-\d{2}-\d{2}");
delegate List<Command> Policy(Content c, GameState s, int turn);
/// <summary>What GameRoot.Smoke does (GoldenRun.SmokeOrders, shared with Unity's golden check): first options only, while
/// the Attention pays and their operation units are in place at the turn's start.</summary>
static List<Command> FirstOption(Content c, GameState s, int turn) => GoldenRun.SmokeOrders(c, s);
/// <summary>The golden runs' player (GoldenRun.Orders): HistoricalAI's orders, then the reference player's GDD orders.</summary>
static List<Command> Historical(Content c, GameState s, int turn) => GoldenRun.Orders(c, s).ToList();
/// <summary>A player who uses every UI order: the event card's first options, then (in turn) an appointment, a
/// budget change and a map build, each only when the sim's own check says it is legal now. The call-up change and
/// the security-level change left with the retired controls (R-WAR-002, #750).</summary>
static List<Command> Player(Content c, GameState s, int turn)
{
var cmds = FirstOption(c, s, turn);
if (turn % 8 == 4)
{
var pick = Cabinet.Lineup(c, s).SelectMany(seat => Cabinet.Candidates(c, s, seat.Office.Id)
.Where(x => x.reason == null).Select(x => (office: seat.Office.Id, person: x.person.Id))).FirstOrDefault();
if (pick.office != null) cmds.Add(new Appoint(pick.office, pick.person));
}
if (turn % 6 == 1)
{
// BudgetPresenter: move one line by 5 points and rescale; here the change moves between two lines.
var keys = s.Budget.Keys.OrderBy(k => k, StringComparer.Ordinal).ToList();
var (from, to) = turn % 12 == 1 ? (keys[0], keys[keys.Count - 1]) : (keys[keys.Count - 1], keys[0]);
if (s.Budget[from] >= 0.05)
cmds.Add(new AllocateBudget(new Dictionary<string, double>(s.Budget) { [from] = s.Budget[from] - 0.05, [to] = s.Budget[to] + 0.05 }));
}
if (turn % 2 == 0)
foreach (var action in new[] { MapActions.TransitCamp, MapActions.Housing })
{
var hex = c.Hexes.FirstOrDefault(h => MapActions.Check(c, s, action, h.Q, h.R) == null);
if (hex != null) { cmds.Add(new MapAction(action, hex.Q, hex.R)); break; }
}
return cmds;
}
/// <summary>SimBridge.Queue's own refusal: an order that costs more attention than is left at this turn's prices.
/// The orders the bridge would accept are returned; the others are reported (the WebGL smoke fails on them).</summary>
static List<Command> Queue(Content c, GameState s, IEnumerable<Command> wanted, List<string> refusedByQueue)
{
var queued = new List<Command>();
double left = s.Attention;
foreach (var cmd in wanted)
{
var price = AttentionSystem.Cost(c, s, cmd.Kind);
if (price > left + 1e-9) { refusedByQueue.Add($"{cmd.Kind} needs {price:0.0} attention, {left:0.0} left"); continue; }
left -= price;
queued.Add(cmd);
}
return queued;
}
/// <summary>The text the screens show for the session's current state, as far as the sim produces it. Prose is the
/// event body text content writes in plain English, where a word such as "rationing" or "housing" is a word, not a
/// leaked id; every other line is sim wording or a content display string.</summary>
static IEnumerable<(string text, bool prose)> Shown(Content c, GameSession g)
{
var s = g.State;
if (g.Last != null)
{
foreach (var w in g.Last.Why) yield return (w.Message, false); // WhyPresenter
foreach (var r in g.Last.Rejected) yield return (r.Reason, false); // the toast
}
foreach (var e in s.PendingEvents.Select(id => c.Events.First(x => x.Id == id))) // EventCardPresenter
{
var date = EventSystem.FillDate(e, s.Date);
yield return (Cabinet.Fill(c, s, e.Title, date), false);
yield return (EventSystem.Render(c, s, e), true);
foreach (var o in e.Options) yield return (Cabinet.Fill(c, s, o.Text, date), false);
var holder = e.Office == "" ? null : Cabinet.Holder(c, s, e.Office, date);
if (holder != null) yield return (c.Offices.First(o => o.Id == e.Office).Title + ": " + holder.Name, false);
}
// HudModel's sim-formatted values (the strings.en.yaml wrappers around them are Unity-side).
yield return (PlayerText.Date(s.Date), false);
yield return (PlayerText.Money(c, "treasury", s.Treasury - s.Debt), false);
yield return (PlayerText.Money(c, "fx_reserves", s.FxReserves), false);
foreach (var level in new[] { s.Indicators["security"], s.Indicators["intl_standing"], EffectsSystem.Approval(c, s) })
yield return (PlayerText.Percent(level), false);
// CabinetPresenter: every seat, its holder and why each person cannot take it.
foreach (var seat in Cabinet.Lineup(c, s))
{
yield return (seat.Office.Title + ": " + string.Join(", ", seat.Holders.Select(p => p.Name)), false);
foreach (var reason in Cabinet.Candidates(c, s, seat.Office.Id).Select(x => x.reason?.Text).Where(x => x != null).Distinct())
yield return (reason!, false);
}
// InfoPanelPresenter: why a build is not possible on a hex (a sample of the map).
foreach (var h in c.Hexes.Where((_, i) => i % 25 == 0))
foreach (var action in MapActions.Actions)
{
var reason = MapActions.Check(c, s, action, h.Q, h.R);
if (reason != null) yield return (reason, false);
}
}
sealed class Outcome
{
public readonly List<string> Hashes = new List<string>(); // after each turn
public readonly List<string> Failures = new List<string>();
public readonly List<string> QueueRefusals = new List<string>();
public readonly HashSet<string> Carried = new HashSet<string>(); // kinds of orders the sim carried out
public int Orders, Turns;
public GameState Final = null!;
}
/// <summary>Plays from 15 May 1948 until the slice ends (GoldenRun.Until1952), the game ends or <paramref name="maxTurns"/>
/// pass. With <paramref name="reload"/>, every turn's save is loaded back and play continues from the loaded session.</summary>
static Outcome Play(Content c, Policy policy, bool reload, Func<IEnumerable<string>, List<string>>? guard, int maxTurns = 1000)
{
var o = new Outcome();
var g = GameSession.New(c, GoldenRun.Seed, Mode.Historical);
if (guard != null) Check(0, c, guard, Shown(c, g).ToList(), o);
for (int turn = 0; turn < maxTurns && g.State.Date < GoldenRun.Until1952 && g.State.GameOver == null; turn++)
{
var queueRefused = new List<string>();
var wanted = policy(c, g.State, turn);
// Bound policy work before Queue can discard duplicate/extra proposals for lack of attention.
// These three policies can answer each event/capture once, try at most an attack and pioneer work
// per unit, and add at most three global orders (appointment/budget/build or housing/train/call-up).
int proposalLimit = g.State.PendingEvents.Count + g.State.Settlements.Count + 2 * g.State.Units.Count + 3;
Assert.InRange(wanted.Count, 0, proposalLimit);
var answers = wanted.OfType<ChooseEventOption>().Select(x => x.EventId).ToList();
Assert.Equal(answers.Count, answers.Distinct(StringComparer.Ordinal).Count());
Assert.All(answers, id => Assert.Contains(id, g.State.PendingEvents));
var cmds = Queue(c, g.State, wanted, queueRefused);
o.QueueRefusals.AddRange(queueRefused.Select(r => $"turn {turn + 1} ({PlayerText.Date(g.State.Date)}): {r}"));
o.Orders += cmds.Count;
var beforeDate = g.State.Date;
int beforeTurn = g.State.Turn;
var r = g.Step(cmds);
// A smoke step must make actual forward progress, once: a stalled or duplicated Step fails here,
// rather than burning the 1000-turn safety cap or depending on the host's load.
Assert.True(g.State.Date > beforeDate, $"turn {turn + 1}: the simulation did not advance its date");
Assert.Equal(beforeTurn + 1, g.State.Turn);
// The next reload replays this history: exactly one batch per turn and exactly the queued commands.
// Accumulating old batches/commands again would multiply replay work even on an otherwise valid state.
var recorded = g.Save.Turns; // this property parses fresh command objects; read it only once
Assert.Equal(turn + 1, recorded.Count);
Assert.Equal(o.Orders, recorded.Sum(batch => batch.Count));
foreach (var cmd in cmds.Where(x => r.Rejected.All(j => j.Command != x))) o.Carried.Add(cmd.Kind);
foreach (var x in r.Rejected.Where(j => !WarEvents1948Tests.IsTheMalkiyaRefusal(j)))
o.Failures.Add($"turn {turn + 1}: refused {Describe(x.Command)}: {x.Reason}");
if (guard != null) Check(turn + 1, c, guard, Shown(c, g).ToList(), o);
if (reload)
{
var text = g.Save.Write();
var loaded = GameSession.Load(c, text, out var warnings);
if (warnings.Count > 0) o.Failures.Add($"turn {turn + 1}: load warned: {string.Join("; ", warnings)}");
if (loaded.State.Hash() != g.State.Hash()) o.Failures.Add($"turn {turn + 1}: the loaded game's hash differs");
if (loaded.Save.Write() != text) o.Failures.Add($"turn {turn + 1}: the loaded game writes a different save");
if (!loaded.Last!.Why.Select(w => w.Message).SequenceEqual(r.Why.Select(w => w.Message)))
o.Failures.Add($"turn {turn + 1}: the loaded game reports different causes for the last turn");
g = loaded; // play on from the loaded game
}
o.Hashes.Add(g.State.Hash());
o.Turns = turn + 1;
}
o.Final = g.State;
return o;
}
/// <summary>Sim wording gets PlayerTextTests' guard (any content id as a whole word outside a display string). Prose
/// gets the ids that cannot be English words (snake_case, "operation_yoav"). Every line: no ISO date, no unfilled
/// "{placeholder}".</summary>
static void Check(int turn, Content c, Func<IEnumerable<string>, List<string>> guard, List<(string text, bool prose)> shown, Outcome o)
{
o.Failures.AddRange(guard(shown.Where(x => !x.prose).Select(x => x.text)).Select(l => $"turn {turn}: content id {l}"));
foreach (var t in shown.Where(x => x.prose).Select(x => x.text).Distinct())
foreach (var id in SnakeIds(c).Where(id => Regex.IsMatch(t, $@"\b{Regex.Escape(id)}\b")))
o.Failures.Add($"turn {turn}: content id '{id}' in: {t}");
foreach (var t in shown.Select(x => x.text).Distinct())
{
if (IsoDate.IsMatch(t)) o.Failures.Add($"turn {turn}: ISO date in: {t}");
if (t.Contains("{") || t.Contains("}")) o.Failures.Add($"turn {turn}: unfilled placeholder in: {t}");
}
}
/// <summary>Every content id with an underscore: these never read as English.</summary>
static List<string> SnakeIds(Content c) => c.Indicators.Select(x => x.Id).Concat(c.Policies.Select(x => x.Id))
.Concat(c.Effects.Select(x => x.Id)).Concat(c.Situations.Select(x => x.Id)).Concat(c.Money.Select(x => x.Id))
.Concat(c.Regions.Select(x => x.Id)).Concat(c.Offices.Select(x => x.Id)).Concat(c.Events.Select(x => x.Id))
.Concat(c.Events.SelectMany(e => e.Options.Select(x => x.Id))).Concat(c.People.Select(x => x.Id))
.Concat(c.Parties.Select(x => x.Id)).Concat(c.Governments.Select(x => x.Id)).Concat(c.Rules.MapActions.Keys)
.Where(id => id.Contains("_")).Distinct().ToList();
static string Describe(Command cmd) => cmd switch
{
ChooseEventOption e => $"choose {e.EventId}/{e.OptionId}",
Appoint a => $"appoint {a.Person} as {a.Office}",
MapAction m => $"{m.Action} at ({m.Q}, {m.R})",
SetMobilization m => $"call-up {m.Level}",
SecurityOrder x => $"security {x.Delta:+0;-0}",
_ => cmd.Kind,
};
static void AssertClean(string name, Outcome run, Outcome straight)
{
Assert.True(run.Failures.Count == 0, $"{name}: {run.Failures.Count} invariant failures:\n" + string.Join("\n", run.Failures.Take(60)));
Assert.True(run.Final.Date >= GoldenRun.Until1952, $"{name}: the run stopped on {PlayerText.Date(run.Final.Date)} (game over: {run.Final.GameOver})");
Assert.True(run.Final.GameOver == null || run.Final.GameOver == GameOverCodes.MvpEnded, $"{name}: game over: {run.Final.GameOver}"); // only the game's own end (R-LOOP-004)
// Every real step advances at least a day; work through this fixed slice is calendar-bounded.
// Keep the per-step checks in Play too: reaching the end alone could hide a duplicated step.
Assert.InRange(run.Turns, 1, (GoldenRun.Until1952 - Real.Rules.Start).Days);
Assert.Equal(straight.Turns, run.Turns);
Assert.Equal(straight.Orders, run.Orders);
Assert.Equal(straight.Hashes, run.Hashes); // reloading every turn changes nothing
}
/// <summary>#2621 (R-ENEMY-007): the smoke plays what the WebGL ?smoke run's GameRoot.Smoke can press. The event card
/// enables an option from the turn-start state (EventCardPresenter: OperationStatus on bridge.State), and the smoke gives
/// no unit orders, so a first option whose units are not in place at the turn's start is not pressed: the smoke stops
/// there, as it does when the Attention runs out, and the rest of the turn's cards go to their ministers.</summary>
[Fact]
public void TheSmokeAnswersOnlyFirstOptionsWhoseUnitsAreInPlaceAtTheTurnStart()
{
var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
int unmet = 0;
for (int turn = 0; turn < 5; turn++)
{
var orders = GoldenRun.SmokeOrders(Real, s);
Assert.All(orders, o => Assert.IsType<ChooseEventOption>(o)); // the smoke skips every unit: no Move, no go-to
var shown = Simulation.Preview(Real, s, Array.Empty<Command>()).State; // what the card shows (SimBridge.State)
var expected = new List<Command>();
var left = s.Attention;
foreach (var e in s.PendingEvents.Select(id => Real.Events.First(x => x.Id == id)))
{
var cmd = new ChooseEventOption(e.Id, e.Options[0].Id);
var price = AttentionSystem.Cost(Real, s, cmd.Kind);
if (price > left + 1e-9) break;
if (!EventSystem.OperationStatus(Real, shown, e.Options[0]).Met) { unmet++; break; }
left -= price;
expected.Add(cmd);
}
Assert.Equal(expected.Cast<ChooseEventOption>().Select(x => (x.EventId, x.OptionId)),
orders.Cast<ChooseEventOption>().Select(x => (x.EventId, x.OptionId)));
s = Simulation.Step(Real, s, orders).State;
}
Assert.True(unmet > 0, "the five turns hold a first option whose units are not in place (ten_days' central_front)");
}
/// <summary>#2621 review: the card enables an option from what the screen shows (SimBridge.State), which with a unit
/// on a standing go-to is the no-orders preview (the routes have moved). So the smoke judges a first option's units on
/// that preview too, and the turn's resolution (EventSystem.Choose's re-check after the routes) takes the same answer.
/// Here ten_days' central_front is unmet at the turn start, and each missing unit gets a standing go-to into its
/// region that it reaches this turn.</summary>
[Fact]
public void TheSmokeJudgesAnOperationOnTheStateTheCardShows()
{
var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
var ten = Real.Events.First(e => e.Id == "ten_days");
var central = ten.Options[0];
Assert.Equal("central_front", central.Id);
for (int turn = 0; turn < 12 && !(s.PendingEvents.Contains(ten.Id) && !EventSystem.OperationStatus(Real, s, central).Met); turn++)
s = Simulation.Step(Real, s, GoldenRun.SmokeOrders(Real, s)).State;
var status = EventSystem.OperationStatus(Real, s, central);
Assert.True(s.PendingEvents.Contains(ten.Id) && !status.Met, "the smoke reaches a ten_days turn with central_front unmet");
var staged = s.Clone();
foreach (var need in status.Missing)
{
int id = Real.StartingForces.FindIndex(f => f.Row == need.Unit);
var u = staged.Units[id];
var to = Movement.Reachable(Real, staged, id).Keys.OrderBy(h => h.Q).ThenBy(h => h.R)
.Where(h => Real.PlayerRegions.AtHex(h.Q, h.R) is PlayerRegionGeography g && need.Regions.Contains(g.Id, StringComparer.Ordinal))
.Cast<(int Q, int R)?>().FirstOrDefault();
Assert.True(to != null, $"{need.Unit} can reach its region this turn");
staged.Units[id] = new Unit(u.Id, u.Type, u.Q, u.R, u.Service, u.Side, u.Strength, Movement.GoTo,
FormattableString.Invariant($"{to!.Value.Q},{to.Value.R}"), u.HomeCity, u.Full, u.CombatXp);
}
var shown = Simulation.Preview(Real, staged, Array.Empty<Command>()).State;
Assert.True(EventSystem.OperationStatus(Real, shown, central).Met, "the routes bring the units into place in the shown state");
var orders = GoldenRun.SmokeOrders(Real, staged);
Assert.Contains(orders, o => o is ChooseEventOption x && x.EventId == ten.Id && x.OptionId == central.Id);
var after = Simulation.Step(Real, staged, orders).State;
Assert.Equal(central.Id, after.ChosenOptions[ten.Id]); // the resolution's re-check after the routes takes it too
}
[Fact]
public void TheFirstOptionSmokeRunPlaysTheWholeSliceCleanly()
{
var clock = Stopwatch.StartNew();
var run = Play(Real, FirstOption, reload: true, PlayerTextTests.LeakGuard(Real));
var straight = Play(Real, FirstOption, reload: false, guard: null);
AssertClean("first option", run, straight);
Assert.True(run.QueueRefusals.Count == 0, "the UI would refuse an event answer (the WebGL smoke fails):\n" + string.Join("\n", run.QueueRefusals));
Assert.True(run.Orders > 0);
// The same policy over five turns ends where the WebGL player's ?smoke=5 run ends (docs/determinism.md).
var five = Play(Real, FirstOption, reload: false, guard: null, maxTurns: 5).Final;
var expected = WebSmoke5; // read before an update rewrites the manifest, so a moved value still fails this run
if (GoldenManifest.UpdateRequested) GoldenManifest.Update("smoke5", five.Hash());
Assert.Equal(expected, FormattableString.Invariant($"SMOKE OK 5 turns {five.Date:yyyy-MM-dd} {five.Hash()}"));
// The library's run (Unity's golden check, #394) ends on the same state as the session run above.
Assert.Equal(five.Hash(), GoldenRun.PlaySmoke(Real, 5).Hash());
Assert.True(clock.Elapsed < HangGuard, $"hang guard, not a performance budget: took {clock.Elapsed} (limit {HangGuard})");
}
/// <summary>The smoke-orders golden (GDD Phase 5 PR 15, #754): one turn whose only order is one Move, the one the WebGL
/// ?smoke-orders run gives through the unit card's buttons (GameRoot.SmokeOrders); events go to their ministers.</summary>
[Fact]
public void TheSmokeOrdersRunMovesOneUnitAndEndsOnTheGolden()
{
var start = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
var move = GoldenRun.SmokeMove(Real, start);
var reach = Movement.Reachable(Real, start, move.Unit).Keys;
var places = Movement.NoEntry(Real, start);
// At 5 km the first lit hex was Nazareth's, a place (#1304) the move skipped; at 4.4 km (#1183) it is (8, 12), no place.
Assert.DoesNotContain(reach.First(), places);
Assert.Equal(reach.First(h => !places.Contains(h)), (move.Q, move.R)); // the move takes the first lit hex that is no place
var end = GoldenRun.PlaySmokeOrders(Real);
var expected = GoldenManifest.Expected("smoke-orders"); // read before an update rewrites the manifest
if (GoldenManifest.UpdateRequested) GoldenManifest.Update("smoke-orders", end.Hash());
Assert.Equal((move.Q, move.R), (end.Units[move.Unit].Q, end.Units[move.Unit].R)); // the order took effect
Assert.Empty(end.Settlements); // a plain move: no golden captures a place (docs/determinism.md)
Assert.NotEqual(Simulation.Step(Real, start, new List<Command>()).State.Hash(), end.Hash()); // and shows in the hash
Assert.Equal(expected, end.Hash());
}
[Fact]
public void TheHistoricalRunPlaysTheWholeSliceCleanlyAndEndsOnTheGolden()
{
var clock = Stopwatch.StartNew();
var run = Play(Real, Historical, reload: true, PlayerTextTests.LeakGuard(Real));
AssertClean("historical", run, Play(Real, Historical, reload: false, guard: null));
Assert.True(run.QueueRefusals.Count == 0, "the reference player sent an order the UI would refuse:\n" + string.Join("\n", run.QueueRefusals));
Assert.Equal(GoldenRun.Hash1952, run.Final.Hash());
Assert.True(clock.Elapsed < HangGuard, $"hang guard, not a performance budget: took {clock.Elapsed} (limit {HangGuard})");
}
[Fact]
public void APlayerUsingEveryUiOrderPlaysTheWholeSliceCleanly()
{
var clock = Stopwatch.StartNew();
var run = Play(Real, Player, reload: true, PlayerTextTests.LeakGuard(Real));
AssertClean("player", run, Play(Real, Player, reload: false, guard: null));
// Attention runs out some turns: the UI refuses those extra orders at queue time, which is not a sim refusal. This
// checked "more orders than turns" until the call-up and security orders left the GDD loop (R-WAR-002, #750); it now
// asserts the queue refusals themselves.
Assert.True(run.QueueRefusals.Count > 0, $"{run.Orders} orders in {run.Turns} turns and none refused by the queue");
// No map_action: the GDD loop refuses it (R-PROJ-004) and the info panel offers no legacy Build button there.
// R-WAR-002 (#750): the GDD loop refuses set_mobilization and security_order and the Defence screen hides them there,
// so the reference player never queues them.
var uiKinds = new[] { "choose_event_option", "appoint", "allocate_budget" };
Assert.Empty(uiKinds.Except(run.Carried)); // every order a UI screen can queue was carried out
Assert.True(clock.Elapsed < HangGuard, $"hang guard, not a performance budget: took {clock.Elapsed} (limit {HangGuard})");
}
}