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.

18.1 KB
Raw Blame
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Statecraft.Game;
using Statecraft.Game.UI;
using Statecraft.Sim;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

/// <summary>Spec 4.2 and 7.1: one message at a time in the side popup, with the buttons of its kind; hidden at once under
/// every modal layer and back when it closes if its signal still holds; no focus taken; never by itself without a
/// settings file.</summary>
public class AdvisorPopupTests
{
    static readonly Content C = ContentLoader.Load(Paths.Content);
    internal static UiText En => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
    static readonly DateTime Day = new DateTime(1948, 5, 15);

    internal const string Fixture = "advisors:\n  - { id: ben_gurion, person: ben_gurion }\n  - { id: meyerson, person: golda_meir }\n  - { id: dayan, person: dayan }\n" +
        "messages:\n" +
        "  - { id: t.teach, advisor: meyerson, signal: new_game, level: new, actions: [{ open_screen: budget }] }\n" +
        "  - { id: t.warn, advisor: dayan, signal: turn_ended, level: warnings, actions: [{ open_screen: defence }] }\n" +
        "  - { id: t.crit, advisor: ben_gurion, signal: turn_ended, level: critical }\n";

    /// <summary>The real English strings plus the fixture's three texts.</summary>
    internal static UiText Text() => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text +
        "\nadvisor.t.teach.text: \"Teach.\"\nadvisor.t.warn.text: \"Warn.\"\nadvisor.t.crit.text: \"Critical.\"\n");

    internal sealed class Rig : IDisposable
    {
        public readonly UiTestKit Ui = new UiTestKit();
        public readonly SimBridge Bridge;
        public readonly ScreenNav Nav;
        public readonly EventCardPresenter Card;
        public readonly AdvisorSession Session;
        public readonly AdvisorPresenter Advisor;
        readonly List<IDisposable> owned = new List<IDisposable>();

        public Rig(bool autoShow = true, string catalog = Fixture, UiText? text = null, AdvisorHooks? hooks = null)
        {
            text ??= Text();
            Ui.Root.Q("root").RemoveFromClassList("loading");
            Bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), text);
            owned.Add(new HudPresenter(Ui.Root, Bridge, text));
            Nav = Own(new ScreenNav(Ui.Root));
            Card = Own(new EventCardPresenter(Ui.Root, Bridge, text, null));
            Card.Collapse(takeFocus: false);                            // a new game opens on a card: out of the way
            Session = new AdvisorSession(AdvisorCatalog.Parse(catalog), new AdvisorSettings(null), autoShow);
            hooks ??= new AdvisorHooks();
            hooks.Card = Card;
            Advisor = Own(new AdvisorPresenter(Ui.Root, Bridge, text, Session, hooks: hooks));
        }

        T Own<T>(T d) where T : IDisposable { owned.Add(d); return d; }
        public VisualElement HudRoot => Ui.Root.Q("root");
        /// <summary>End turn, then the new turn's card (if one opens) put behind "Decision waiting", as the opening one is.</summary>
        public void EndTurn()
        {
            Bridge.EndTurn();
            if (Card.Shown != null && !Card.Collapsed) Card.Collapse(takeFocus: false);
        }
        public void Dispose() { for (int i = owned.Count - 1; i >= 0; i--) owned[i].Dispose(); Ui.Dispose(); }
    }

    static bool Displayed(Button b) => b.style.display.value != DisplayStyle.None;

    /// <summary>Spec 7.1, plan Task 13: a teaching message with an encyclopedia action shows Tell me more, which opens that
    /// page through the OpenPage hook; a message without one, or with no hook, has no Tell me more.</summary>
    [Test]
    public void TellMeMoreOpensTheMessagesPage()
    {
        var opened = new List<string>();
        var catalog = Fixture.Replace("level: new, actions: [{ open_screen: budget }]", "level: new, actions: [{ open_screen: budget }, { encyclopedia: p }]") +
            "pages:\n  - { id: p, advisor: meyerson }\n";
        using (var rig = new Rig(catalog: catalog, hooks: new AdvisorHooks { OpenPage = opened.Add }))
        {
            rig.Advisor.NewGame();
            Assert.AreEqual("t.teach", rig.Advisor.Shown?.Id);
            Assert.IsTrue(Displayed(rig.Ui.Q<Button>("advisor-more")), "the page to open");
            rig.Ui.Press("advisor-more");
            CollectionAssert.AreEqual(new[] { "p" }, opened);
        }
        using (var rig = new Rig(catalog: catalog))
        {
            rig.Advisor.NewGame();
            Assert.IsFalse(Displayed(rig.Ui.Q<Button>("advisor-more")), "no hook, no Tell me more");
        }
        using (var plain = new Rig())
        {
            plain.Advisor.NewGame();
            Assert.IsFalse(Displayed(plain.Ui.Q<Button>("advisor-more")), "no encyclopedia action, no Tell me more");
        }
    }

    /// <summary>Spec 3.2: a real quote shows only from the verified list, styled as a quote with its source under it; the
    /// advisor's own line stays plain game dialogue.</summary>
    [Test]
    public void AQuoteShowsWithItsSourceAndOnlyThere()
    {
        using var rig = new Rig(catalog: Fixture.Replace("level: new, actions: [{ open_screen: budget }]", "level: new, quote: q1"));
        rig.Advisor.NewGame();
        Assert.AreEqual(En["advisor.quote.q1.text"], rig.Ui.Q<Label>("advisor-quote").text);
        Assert.AreEqual(En["advisor.quote.q1.source"], rig.Ui.Q<Label>("advisor-quote-source").text);
        Assert.AreEqual("Teach.", rig.Ui.Q<Label>("advisor-text").text, "the advisor's own line stays plain");
        rig.Ui.Press("advisor-got-it");
        rig.EndTurn();
        Assert.AreEqual(DisplayStyle.None, rig.Ui.Q<Label>("advisor-quote").style.display.value, "a message without a quote shows none");
    }
    [Test]
    public void AnUnverifiedQuoteNeverShowsEvenInAnUncheckedFixture()
    {
        using var rig = new Rig(catalog: Fixture.Replace("level: new, actions: [{ open_screen: budget }]", "level: new, quote: q9"));
        rig.Advisor.NewGame();
        Assert.AreEqual(DisplayStyle.None, rig.Ui.Q<Label>("advisor-quote").style.display.value);
        Assert.AreEqual("", rig.Ui.Q<Label>("advisor-quote-source").text);
    }

    [Test]
    public void ATeachingMessageShowsItsAdvisorTextAndButtons()
    {
        using var rig = new Rig();
        rig.Advisor.NewGame();
        Assert.IsTrue(rig.Advisor.IsOpen);
        Assert.AreEqual("t.teach", rig.Advisor.Shown!.Id);
        var text = Text();
        Assert.AreEqual(text["advisor.meyerson.name_first"], rig.Ui.Q<Label>("advisor-name").text, "her first appearance");
        Assert.AreEqual(text["advisor.meyerson.role"], rig.Ui.Q<Label>("advisor-role").text);
        Assert.AreEqual("Teach.", rig.Ui.Q<Label>("advisor-text").text);
        Assert.AreEqual(text["advisor.show_me"], rig.Ui.Q<Button>("advisor-show-me").text);
        Assert.IsTrue(Displayed(rig.Ui.Q<Button>("advisor-show-me")), "it has an action");
        Assert.IsFalse(Displayed(rig.Ui.Q<Button>("advisor-not-now")), "teaching: Tell me more, not Not now");
        Assert.IsFalse(Displayed(rig.Ui.Q<Button>("advisor-more")), "no encyclopedia action, no page to open");
        Assert.IsFalse(rig.Ui.Q<VisualElement>("advisor-popup").ClassListContains(AdvisorPresenter.CriticalClass));
    }

    [Test]
    public void AWarningHasNotNowAndACriticalOneHasNotAndShowsFirst()
    {
        using var rig = new Rig();
        rig.Advisor.NewGame();
        rig.Ui.Press("advisor-got-it");
        rig.EndTurn();
        Assert.AreEqual("t.crit", rig.Advisor.Shown!.Id, "critical first");
        Assert.IsTrue(rig.Ui.Q<VisualElement>("advisor-popup").ClassListContains(AdvisorPresenter.CriticalClass));
        Assert.IsFalse(Displayed(rig.Ui.Q<Button>("advisor-not-now")));
        rig.Ui.Press("advisor-close");                                  // the X is Got it
        Assert.AreEqual("t.warn", rig.Advisor.Shown!.Id);
        Assert.IsTrue(Displayed(rig.Ui.Q<Button>("advisor-not-now")));
        Assert.AreEqual(Text().Format("advisor.count", 2, 2), rig.Ui.Q<Label>("advisor-count").text);
        rig.Ui.Press("advisor-not-now");
        Assert.IsFalse(rig.Advisor.IsOpen);
        CollectionAssert.AreEqual(new[] { AdvisorQueue.Outcome.Shown, AdvisorQueue.Outcome.Shown, AdvisorQueue.Outcome.PutOff },
            rig.Session.Queue.History.Select(h => h.Outcome).ToArray());
    }

    /// <summary>Owner, 1 Nov 2026 (#2110): a warning on screen whose cause passes mid-turn stays until the player closes it;
    /// only waiting or put-off warnings are pruned.</summary>
    [Test]
    public void AShownWarningWhoseCausePassesStaysUntilClosed()
    {
        using var rig = new Rig(catalog: Fixture.Replace("signal: turn_ended, level: warnings", "signal: in_debt, level: warnings"));
        rig.Bridge.State.Debt = 1;
        rig.Advisor.Resume();                                           // Evaluate, as bridge.Changed runs it
        Assert.AreEqual("t.warn", rig.Advisor.Shown?.Id);
        rig.Bridge.State.Debt = 0;
        rig.Advisor.Resume();
        Assert.AreEqual("t.warn", rig.Advisor.Shown?.Id, "the debt is paid, but the player is reading it");
        Assert.IsTrue(rig.Advisor.IsOpen);
        rig.Ui.Press("advisor-got-it");
        Assert.IsNull(rig.Advisor.Shown);
        Assert.AreEqual(("t.warn", AdvisorQueue.Outcome.Shown), (rig.Session.Queue.History.Last().Id, rig.Session.Queue.History.Last().Outcome));
    }

    static readonly string[] Layers =
    {
        StartScreenPresenter.OpenClass, EventCardPresenter.OpenClass, CaptureDialogPresenter.OpenClass,
        TurnReportPresenter.OpenClass, OutcomePresenter.OpenClass, EndScreenPresenter.OpenClass, "two-row-screen",
    };

    /// <summary>Spec 4.2: each modal layer hides it at once, through ScreenNav.DisableCovered, which every one of them calls;
    /// when the layer closes it comes back, as its signal still holds this turn.</summary>
    [Test]
    public void HidesUnderEveryModalLayerAndComesBack([ValueSource(nameof(Layers))] string layer)
    {
        using var rig = new Rig();
        rig.Advisor.NewGame();
        Assert.IsTrue(rig.Advisor.IsOpen, "precondition");
        void Set(bool on)
        {
            if (layer == "two-row-screen")
            {
                rig.HudRoot.EnableInClassList(HudPresenter.TwoRowClass, on);
                rig.HudRoot.EnableInClassList(ScreenNav.ScreenOpenClass, on);
            }
            else rig.HudRoot.EnableInClassList(layer, on);
            ScreenNav.DisableCovered(rig.Ui.Root);
        }
        Set(true);
        Assert.IsFalse(rig.Advisor.IsOpen, layer + ": hidden");
        Assert.IsFalse(rig.HudRoot.ClassListContains(AdvisorPresenter.OpenClass));
        Set(false);
        Assert.IsTrue(rig.Advisor.IsOpen, layer + ": back, its signal holds this turn");
        Assert.AreEqual("t.teach", rig.Advisor.Shown!.Id);
    }

    /// <summary>Spec 4.2: a message whose signal no longer holds when the layer closes goes to the history unseen.</summary>
    [Test]
    public void AMessageWhoseSignalEndedUnderALayerGoesToTheHistory()
    {
        using var rig = new Rig(catalog: Fixture.Replace("level: warnings", "level: critical"));
        rig.Advisor.NewGame();
        rig.HudRoot.AddToClassList(TurnReportPresenter.OpenClass); ScreenNav.DisableCovered(rig.Ui.Root);
        rig.EndTurn();                                                   // new_game held on turn 1 only
        rig.HudRoot.RemoveFromClassList(TurnReportPresenter.OpenClass); ScreenNav.DisableCovered(rig.Ui.Root);
        Assert.AreEqual(AdvisorQueue.Outcome.Missed, rig.Session.Queue.History.First().Outcome);
        Assert.AreEqual("t.teach", rig.Session.Queue.History.First().Id);
        Assert.IsFalse(rig.Session.Settings.Seen("t.teach"));
        Assert.IsTrue(rig.Advisor.Shown!.Critical, "the turn's own (critical) message shows");
    }

    [Test]
    public void TakesNoFocusAndEscapeClosesItOnlyFromInside()
    {
        using var rig = new Rig();
        var endTurn = rig.Ui.Q<Button>("end-turn");
        endTurn.Focus();
        rig.Advisor.NewGame();
        Assert.AreSame(endTurn, rig.Ui.Root.focusController.focusedElement, "spec 16: not modal, steals no focus");
        Assert.IsFalse(rig.Advisor.Escape(), "Escape with the focus outside: not its key");
        Assert.IsTrue(rig.Advisor.IsOpen);
        rig.Ui.Q<Button>("advisor-got-it").Focus();
        Assert.IsTrue(rig.Advisor.Escape());
        Assert.IsFalse(rig.Advisor.IsOpen);
        Assert.IsTrue(rig.Session.Settings.Seen("t.teach"), "Escape is the X");
    }

    /// <summary>#3028: a queue row takes no focus, so the player reaches it only by Tab. When it closes with the focus inside,
    /// the focus goes to End turn, not to the control the player was on before (which only the opening tour gives back).</summary>
    [TestCase(false)] [TestCase(true)]
    public void AQueueRowReachedByTabGivesTheFocusToEndTurnWhenItCloses(bool hebrew)
    {
        using var rig = !hebrew ? new Rig() : new Rig(catalog: Fixture.Replace("level: new,", "level: new, he_review: approved,"),
            text: UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.StringsHebrew).text +
                "\nadvisor.t.teach.text: \"Teach.\"\nadvisor.t.warn.text: \"Warn.\"\nadvisor.t.crit.text: \"Critical.\"\n", "strings.he.yaml"));
        Focusable? Focused() => rig.Ui.Root.focusController.focusedElement;
        var cabinet = rig.Ui.Q<Button>("nav-cabinet");
        cabinet.Focus();
        rig.Advisor.NewGame();
        Assert.IsTrue(rig.Advisor.IsOpen, "precondition: the row shows");
        Assert.AreSame(cabinet, Focused(), "precondition: it took no focus");
        rig.Ui.Q<Button>("advisor-got-it").Focus();                    // as Tab would
        Assert.IsTrue(rig.Advisor.Escape());
        Assert.IsFalse(rig.Advisor.IsOpen);
        Assert.AreSame(rig.Ui.Q<Button>("end-turn"), Focused());
    }

    /// <summary>Spec 16: one Escape with the focus in the popup closes the popup only, as in the report panel (#1169).
    /// This unit fixture injects ScreenNav's TakesEscape ordering; GameRoot's boot hookup is covered by AdvisorGameTests.
    /// The Escape poll asks the popup before it
    /// closes the screen, also in the frame its Cancel already closed it. (Time.frameCount does not move in an edit-mode
    /// test, so after this the popup keeps saying "used this frame".)</summary>
    [Test]
    public void AnInjectedScreenNavHookLeavesTheScreenUnderThePopupOpen()
    {
        using var rig = new Rig();
        rig.Nav.TakesEscape = rig.Advisor.Escape;
        rig.Advisor.NewGame();
        rig.Nav.Open("budget");                                          // what Show me opens: the one-row HUD keeps it beside
        Assert.IsTrue(rig.Advisor.IsOpen, "precondition: a side screen does not cover it on the one-row HUD");
        var gotIt = rig.Ui.Q<Button>("advisor-got-it");
        gotIt.Focus();
        using (var esc = NavigationCancelEvent.GetPooled()) { esc.target = gotIt; gotIt.SendEvent(esc); }
        Assert.IsFalse(rig.Advisor.IsOpen, "Esc closes the popup");
        Assert.AreEqual("budget", rig.Nav.Current, "and only the popup");
        Assert.IsTrue(rig.Advisor.Escape(), "closed this frame: the poll's Escape is used, so it leaves the screen open");
        Assert.AreEqual("budget", rig.Nav.Current);
    }

    /// <summary>One Escape dismisses one message: when Got it brings the next message up at once (the focus still on Got
    /// it), the poll's Escape in the same frame does not dismiss that one too.</summary>
    [Test]
    public void OneEscapeDismissesOnlyOneMessage()
    {
        using var rig = new Rig();
        rig.Nav.TakesEscape = rig.Advisor.Escape;
        rig.Advisor.NewGame();
        rig.Ui.Press("advisor-got-it");
        rig.EndTurn();
        Assert.AreEqual("t.crit", rig.Advisor.Shown!.Id, "precondition: critical first, the warning behind it");
        var gotIt = rig.Ui.Q<Button>("advisor-got-it");
        gotIt.Focus();
        using (var esc = NavigationCancelEvent.GetPooled()) { esc.target = gotIt; gotIt.SendEvent(esc); }
        Assert.AreEqual("t.warn", rig.Advisor.Shown?.Id, "Cancel dismissed the critical one; the warning is up");
        Assert.IsTrue(rig.Advisor.Escape(), "the poll in the same frame: the key is used");
        Assert.IsTrue(rig.Advisor.IsOpen, "and the warning stays up");
        Assert.AreEqual("t.warn", rig.Advisor.Shown?.Id);
        Assert.IsFalse(rig.Session.Settings.Seen("t.warn"), "unread, so not seen");
    }

    /// <summary>OQ-4: in Hebrew a message shows only once its Hebrew is approved.</summary>
    [TestCase("pending", false)]
    [TestCase("approved", true)]
    public void InHebrewOnlyAnApprovedMessageShows(string review, bool shows)
    {
        var he = UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.StringsHebrew).text +
            "\nadvisor.t.teach.text: \"Teach.\"\nadvisor.t.warn.text: \"Warn.\"\nadvisor.t.crit.text: \"Critical.\"\n", "strings.he.yaml");
        using var rig = new Rig(catalog: Fixture.Replace("level: new,", $"level: new, he_review: {review},"), text: he);
        rig.Advisor.NewGame();
        Assert.AreEqual(shows, rig.Advisor.IsOpen, "he_review: " + review);
    }

    [Test]
    public void NeverShowsByItselfWithoutASettingsFile()
    {
        using var rig = new Rig(autoShow: false);
        rig.Advisor.NewGame();
        rig.EndTurn();
        Assert.IsFalse(rig.Advisor.IsOpen);
        Assert.IsEmpty(rig.Session.Queue.History);
    }

    [Test]
    public void NoPortraitHidesTheFrame()
    {
        using var rig = new Rig();                                        // no PortraitCatalog: none for anyone
        rig.Advisor.NewGame();
        Assert.IsTrue(rig.Ui.Q<VisualElement>("advisor-popup").ClassListContains(AdvisorPresenter.NoPortraitClass));
    }

    [Test]
    public void ChangesNeitherTheSimNorTheQueue()
    {
        using var rig = new Rig();
        var hash = rig.Bridge.State.Hash(); var queued = rig.Bridge.Queued.Count;
        rig.Advisor.NewGame(); rig.Ui.Press("advisor-show-me"); rig.Nav.Close(); rig.Ui.Press("advisor-got-it");
        Assert.AreEqual(hash, rig.Bridge.State.Hash());
        Assert.AreEqual(queued, rig.Bridge.Queued.Count, "it issues no command");
    }
}