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.

14.9 KB
Raw Blame
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using Statecraft.Sim;

/// <summary>B1: the economy, population, effect and security lines carry a key and their values (CauseKeys), so the UI can
/// word them in the player's language. The key is result data only: Message, Delta and the state are what they were, and
/// the values are the same whatever the machine's culture.</summary>
public class CauseKeysTests
{
    static readonly Content Real = ContentLoader.Load(RealContentTests.ContentRoot());
    static readonly Content Fix = ContentLoader.Load(Fixture.Write());
    static readonly string[] Keyed = { "population", "effects", "situations", "security" };

    [Fact]
    public void EveryKeyIsListedOnceAndNoneIsAUnitKey()
    {
        Assert.Equal(CauseKeys.All.Distinct().Count(), CauseKeys.All.Count);
        Assert.Empty(CauseKeys.All.Intersect(WhyKeys.All));
        Assert.Equal(CauseKeys.Purpose.All.Distinct().Count(), CauseKeys.Purpose.All.Count);
    }

    /// <summary>The reference run to the end: every line of the population, effects, situations and security systems has a
    /// known key.</summary>
    [Fact]
    public void TheReferenceRunKeysEveryLineOfTheSlice()
    {
        var lines = Run(Real, 1000);
        Assert.NotEmpty(lines);
        foreach (var w in lines.Where(w => Keyed.Contains(w.System)))
            Assert.True(CauseKeys.All.Contains(w.Key), $"[{w.System}] {w.Message}: no key");
        // Each key the run reaches, at least once (the rest are covered below on the fixture).
        foreach (var key in new[] { CauseKeys.MoneyFlows, CauseKeys.FxFlows, CauseKeys.Borrowed, CauseKeys.FxCrisisBegan, CauseKeys.Arrived,
                     CauseKeys.EffectChange, CauseKeys.SituationBegan, CauseKeys.RungChanged, CauseKeys.CallUpRaised, CauseKeys.CallUpLowered })
            Assert.Contains(lines, w => w.Key == key);
    }

    /// <summary>Other files also write "economy" lines, so EconomySystem's own are checked at the producer: its turn step and
    /// the civilian upkeep, on every state of the reference run, as they are, with the Treasury short, and with the currency
    /// crisis starting and easing. Every line they write has a key.</summary>
    [Fact]
    public void EveryLineEconomySystemWritesHasAKey()
    {
        var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
        var lines = new List<Cause>();
        for (int t = 0; t < 1000 && s.GameOver == null; t++)
        {
            foreach (var change in new Action<GameState>[] { _ => { }, x => x.Treasury = -50, x => x.FxReserves = -1e6,
                         x => { x.ActiveSituations.Add(EconomySystem.FxCrisis); x.FxReserves = 1e6; } })
            {
                var x = s.Clone(); change(x);
                EconomySystem.Apply(Real, x, 0.25, lines);
                EconomySystem.PayCivilianUpkeep(Real, x, Real.RulesAt(x.Date).Mvp!, 0.25, lines);
            }
            s = Simulation.Step(Real, s, GoldenRun.Orders(Real, s)).State;
        }
        foreach (var w in lines) Assert.True(CauseKeys.All.Contains(w.Key), $"[{w.System}] {w.Message}: no key");
        foreach (var key in new[] { CauseKeys.MoneyFlows, CauseKeys.FxFlows, CauseKeys.Borrowed, CauseKeys.FxCrisisBegan, CauseKeys.FxCrisisEased })
            Assert.Contains(lines, w => w.Key == key);
        Assert.All(lines.Where(w => w.Key == CauseKeys.Borrowed), w => Assert.Contains(w.Args[0], CauseKeys.Purpose.All));
    }

    /// <summary>The values parse back to exactly the numbers the English line printed, as invariant text.</summary>
    [Fact]
    public void TheValuesAreTheNumbersTheLineNames()
    {
        var keyed = Run(Real, 1000).Where(w => w.Key != "").ToList();
        Assert.NotEmpty(keyed);
        foreach (var w in keyed)
            switch (w.Key)
            {
                case CauseKeys.MoneyFlows:
                    Assert.Equal($"{PlayerText.Change(Real, "treasury", w.Delta)} (tax {Money(w, 0)}, spending {Money(w, 1)}, reserve call-up {Money(w, 2)})", w.Message);
                    break;
                case CauseKeys.Arrived:
                    Assert.Equal(FormattableString.Invariant($"{w.Delta:N0} immigrants arrived ({PlayerText.Region(Real, w.Target)})"), w.Message);
                    break;
                case CauseKeys.EffectChange:
                    var names = w.Args.Select(id => Real.Effects.Single(e => e.Id == id).Name).ToList();
                    Assert.Equal(PlayerText.Change(Real, w.Target, w.Delta) + (names.Count > 0 ? $" ({string.Join(", ", names)})" : ""), w.Message);
                    break;
                case CauseKeys.RungChanged:
                    Assert.Equal($"Security level {w.Args[0]} to {w.Args[1]}", w.Message);
                    break;
                case CauseKeys.CallUpLowered:
                    Assert.Equal($"Reserves sent home: call-up lowered to {PlayerText.Percent(Number(w, 0))}, the minimum for security level {w.Args[1]}", w.Message);
                    break;
            }
    }

    /// <summary>Keys and values are the same under a comma-decimal culture: the UI reads them as invariant text.</summary>
    [Fact]
    public void TheValuesDoNotDependOnTheCulture()
    {
        string Lines() => string.Join("\n", Run(Real, 30).Select(w => w.Key + "|" + string.Join(";", w.Args)));
        var invariant = Lines();
        var culture = Thread.CurrentThread.CurrentCulture;
        try
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
            Assert.Equal(invariant, Lines());
        }
        finally { Thread.CurrentThread.CurrentCulture = culture; }
    }

    [Fact]
    public void TheFixtureLinesTheReferenceRunDoesNotReachAreKeyed()
    {
        var s = new GameState { Rung = 1 }; var why = new List<Cause>();
        Assert.Null(SecuritySystem.SetMobilization(Fix, s, 0.9, why));
        Assert.Equal(CauseKeys.CallUpSet, Assert.Single(why).Key);

        s = new GameState(); why = new List<Cause>();
        SecuritySystem.Init(Fix, s, why);
        for (int i = 0; i < 4; i++) SecuritySystem.Apply(Fix, s, TurnLength.Week, why);
        Assert.Equal(CauseKeys.Ceasefire, Assert.Single(why, w => w.Target == "ceasefire").Key);

        // Situation ended: the fixture's hunger turns off above 0.45.
        s = new GameState { Date = new DateTime(1948, 6, 1) };
        EffectsSystem.Init(Fix, s);
        s.ActiveSituations.Add("hunger"); s.Indicators["food_supply"] = 0.5;
        why = new List<Cause>();
        EffectsSystem.UpdateSituations(Fix, s, why);
        var ended = Assert.Single(why);
        Assert.Equal((CauseKeys.SituationEnded, "hunger"), (ended.Key, ended.Target));
        Assert.Equal(new[] { "food_supply", "0.5" }, ended.Args);
    }

    [Fact]
    public void TheCurrencyCrisisLinesAreKeyed()
    {
        var c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = Fixture.Files["rules.yaml"] + "fx_crisis_food_per_year: -0.4\n" }));
        var s = new GameState { Date = new DateTime(1948, 6, 1) };
        EffectsSystem.Init(c, s); EconomySystem.Init(c, s);
        s.Population["veterans"] = 1000;
        s.FxReserves = -20;
        var why = new List<Cause>();
        EconomySystem.Apply(c, s, 0.25, why);
        Assert.Equal(CauseKeys.FxCrisisBegan, Assert.Single(why, w => w.Target == EconomySystem.FxCrisis).Key);
        Assert.Equal(CauseKeys.FxCrisisFood, Assert.Single(why, w => w.Target == "food_supply").Key);

        s.FxReserves = 1000; why.Clear();
        EconomySystem.Apply(c, s, 0.25, why);
        Assert.Equal(CauseKeys.FxCrisisEased, Assert.Single(why, w => w.Target == EconomySystem.FxCrisis).Key);
    }

    [Fact]
    public void CivilianUpkeepAndItsBorrowingAreKeyed()
    {
        var s = new GameState { Treasury = 0, Debt = 0 }; var why = new List<Cause>();
        EconomySystem.PayNamedOrBorrow(Fix, s, 3, "for civilian upkeep", why, CauseKeys.Purpose.CivilianUpkeep);   // as PayCivilianUpkeep
        var line = Assert.Single(why);
        Assert.Equal((CauseKeys.Borrowed, "Borrowed IL\u00A03.0m for civilian upkeep"), (line.Key, line.Message));
        Assert.Equal(new[] { CauseKeys.Purpose.CivilianUpkeep }, line.Args);

        // #2199: PayOrBorrow names the whole cost as a Money line first, with the same purpose, then the borrowing.
        why.Clear();
        EconomySystem.PayOrBorrow(Fix, s, 3, "to build a housing project", why, CauseKeys.Purpose.Project, "housing");
        Assert.Equal(2, why.Count);
        Assert.Equal((CauseKeys.Paid, "treasury", -3.0), (why[0].Key, why[0].Target, why[0].Delta));
        Assert.Equal(new[] { CauseKeys.Purpose.Project, "housing" }, why[0].Args);
        Assert.Equal(CauseKeys.Borrowed, why[1].Key);
        Assert.Equal(new[] { CauseKeys.Purpose.Project, "housing" }, why[1].Args);
    }

    [Fact]
    public void ObjectiveCompletionInTheEventsGroupHasAKeyAndHistoricalNames()
    {
        var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
        var objective = Real.Objectives.First(o => !s.Objectives.ContainsKey(o.Id));
        var why = new List<Cause>();
        Assert.True(Objectives.Record(Real, s, Real.RulesAt(s.Date).Mvp!, objective.Id, s.Date, why));
        var row = Assert.Single(why);
        Assert.Equal(("events", objective.Id, CauseKeys.ObjectiveCompleted), (row.System, row.Target, row.Key));
        Assert.Contains(row.Key, CauseKeys.All);
        Assert.Equal(new[] { objective.Name, objective.NameHe }, row.Args);
        Assert.Equal(string.Format(CultureInfo.InvariantCulture, "{0}: completed, {1} points", objective.Name, (int)row.Delta), row.Message);
        Assert.False(Objectives.Record(Real, s, Real.RulesAt(s.Date).Mvp!, objective.Id, s.Date, why));
        Assert.Single(why);   // metadata must not cause the score/completion to be recorded twice
    }

    [Theory]
    [InlineData("egypt", "egypt")]
    [InlineData("jordan", "iraq")]
    public void BothPrisonerExchangeProducerLinesHaveKeys(string front, string side)
    {
        var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
        s.PrisonersHeld[side] = 2; s.SoldiersHeld[side] = 3;
        var why = new List<Cause>();
        Prisoners.Return(Real, s, front, why);
        Assert.Equal(2, why.Count);
        var returned = Assert.Single(why, w => w.Key == CauseKeys.PrisonersReturned);
        var home = Assert.Single(why, w => w.Key == CauseKeys.SoldiersReturned);
        int people = Real.RulesAt(s.Date).Mvp!.PeoplePerUnit;
        Assert.Equal(new[] { (2 * people).ToString(CultureInfo.InvariantCulture), front }, returned.Args);
        Assert.Equal(new[] { (3 * people).ToString(CultureInfo.InvariantCulture), front }, home.Args);
        Assert.Equal(FormattableString.Invariant($"Prisoners exchanged: {2 * people:N0} prisoners returned to {PlayerText.Capitalised(front)}"), returned.Message);
        Assert.Equal(FormattableString.Invariant($"Prisoners exchanged: {3 * people:N0} of our soldiers came home from {PlayerText.Capitalised(front)}"), home.Message);
        Assert.All(why, w => { Assert.Equal("events", w.System); Assert.Contains(w.Key, CauseKeys.All); });
    }

    /// <summary>#1680: check the producers on every state of a full reference game, not only a renderer's filtered set.</summary>
    [Fact]
    public void EveryEventAndMilitaryUpkeepProducerLineHasAKey()
    {
        var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
        int eventRows = 0, upkeepRows = 0;
        for (int t = 0; t < 1000 && s.GameOver == null; t++)
        {
            var eventLines = new List<Cause>();
            var triggered = s.Clone();
            EventSystem.Trigger(Real, triggered, eventLines, s.Date.AddMonths(-1));
            EventSystem.Delegate(Real, triggered, eventLines);
            foreach (var id in s.PendingEvents)
                foreach (var option in Real.Events.Single(e => e.Id == id).Options)
                {
                    var chosen = s.Clone();
                    if (option.RequiresUnitRegions.Count != 0)
                    {
                        var at = Real.PlayerRegions.Regions[option.RequiresUnitRegions[0]].MemberHexes[0];
                        foreach (var unitOrder in option.UnitOrders)
                        {
                            int unitId = Real.StartingForces.FindIndex(f => f.Row == unitOrder.Unit);
                            var force = Real.StartingForces[unitId];
                            var unit = chosen.Units.TryGetValue(unitId, out var present) ? present
                                : new Unit(unitId, force.Type, force.Q, force.R, side: force.Side, strength: force.Strength);
                            chosen.Units[unitId] = new Unit(unit.Id, unit.Type, at.Q, at.R, unit.Service, unit.Side, unit.Strength,
                                unit.Order, unit.OrderTarget, unit.HomeCity, unit.Full, unit.CombatXp);
                        }
                    }
                    Assert.Null(EventSystem.Choose(Real, chosen, id, option.Id, eventLines));
                    foreach (var row in EventSystem.OptionEffects(Real, s, option))
                    {
                        Assert.Contains(row.Key, CauseKeys.All);
                        eventRows++;
                    }
                }
            foreach (var row in eventLines.Where(w => w.System == "events"))
            {
                Assert.Contains(row.Key, CauseKeys.All);
                eventRows++;
            }
            var upkeep = new List<Cause>();
            Military.PayUpkeep(Real, s.Clone(), Real.RulesAt(s.Date).Mvp!, upkeep);
            foreach (var row in upkeep) Assert.Contains(row.Key, CauseKeys.All);
            upkeepRows += upkeep.Count(w => w.Key == CauseKeys.MilitaryUpkeep);
            s = Simulation.Step(Real, s, GoldenRun.Orders(Real, s)).State;
        }
        Assert.True(eventRows > 0, "precondition: actual event producer rows were checked");
        Assert.True(upkeepRows > 0, "precondition: upkeep was paid");
        foreach (var row in Run(Real, 1000).Where(w => w.System == "events"))
            Assert.Contains(row.Key, CauseKeys.All);
    }

    [Fact]
    public void EveryOptionPreviewHasAKeyIncludingEffectsNotReachedByTheReferencePlayer()
    {
        var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);
        var rows = Real.Events.SelectMany(e => e.Options).SelectMany(o => EventSystem.OptionEffects(Real, s, o)).ToList();
        Assert.NotEmpty(rows);
        foreach (var row in rows) Assert.Contains(row.Key, CauseKeys.All);
    }

    static string Money(Cause w, int i) => PlayerText.Money(Real, "treasury", Number(w, i));
    static double Number(Cause w, int i) => double.Parse(w.Args[i], NumberStyles.Float, CultureInfo.InvariantCulture);

    static List<Cause> Run(Content c, int turns)
    {
        var lines = new List<Cause>();
        var s = Simulation.NewGame(c, GoldenRun.Seed, Mode.Historical);
        for (int t = 0; t < turns && s.GameOver == null; t++)
        {
            var r = Simulation.Step(c, s, GoldenRun.Orders(c, s));
            lines.AddRange(r.Why);
            s = r.State;
        }
        return lines;
    }
}