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.

92.2 KB
Raw Blame
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Statecraft.Game;
using Statecraft.Game.Boot;
using Statecraft.Game.Map;
using Statecraft.Game.Map.Units;
using Statecraft.Game.UI;
using Statecraft.Sim;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

/// <summary>The first-turn advisor (#42; Q-ONB-1, owner, 17 October 2026, #784): it shows by itself on the first new game only,
/// never on a Continue or a second game; it can be skipped and opened again from How to play; every step's target is on
/// screen at 1280x720 and 390x844 in English and Hebrew, with the panel clear of it; and it changes neither the sim nor the
/// save.</summary>
public class AdvisorOpeningParityTests
{
    // Approval is a test-only in-memory copy; no shipped review field is changed.
    static AdvisorCatalog Approved() => AdvisorCatalog.Parse(File.ReadAllText(AdvisorCatalog.Path).Replace("he_review: pending", "he_review: approved"));
    static string[] Steps => AdvisorPresenter.ChainIds(AdvisorCatalog.Load()).ToArray();
    /// <summary>The city step of AdvisorTour1688Fixture.WithACityStep.</summary>
    static int CityStep => Array.IndexOf(Steps, "opening.cities");
    static void Begin(GameRoot game, GameInputs inputs, int smoke, bool startScreen = false)
    {
        game.Begin(inputs, smoke, startScreen: startScreen);
    }
    static readonly Content C = ContentLoader.Load(Paths.Content);
    static UiText En => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
    static UiText He => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.StringsHebrew).text, "strings.he.yaml");
    /// <summary>The settlements' names for the info panel's title (InfoPanelLayoutTests' list).</summary>
    static Dictionary<string, string> Names() => SettlementPlacer.Parse(File.ReadAllText(Path.Combine(Paths.Content, "map", SettlementPlacer.DefaultFileName)))
        .ToDictionary(t => t.Id, t => t.Name);
    static List<UnitEntry> UnitRows => UnitData.Parse(File.ReadAllText(Path.Combine(Paths.Content, "map", UnitData.DefaultFileName)));

    /// <summary>The info panel and the advisor as GameRoot wires them, without the map: the advisor's city (CityHex) and its unit
    /// (UnitToShow, among the tokens a new game draws) shown as a click would show them.</summary>
    static (InfoPanelPresenter info, AdvisorPresenter advisor) PanelAndAdvisor(VisualElement root, SimBridge bridge, UiText text,
        SettingsStore? settings, EventCardPresenter card, Action closeScreens, AdvisorCatalog? catalog = null)
    {
        var tokens = UnitRoster.FromState(UnitRows, bridge.Content, bridge.State, _ => true, (row, a, q, r) => a);
        PlacedToken? shown = null;
        var info = new InfoPanelPresenter(root, bridge, text, Names(), selectedUnit: () => shown);
        var session = new AdvisorSession(catalog ?? Approved(), new AdvisorSettings(settings), autoShow: true);
        var advisor = new AdvisorPresenter(root, bridge, text, session, hooks: new AdvisorHooks
        {
            Card = card, CloseScreens = closeScreens,
            ShowCity = () => { if (AdvisorTargets.CityHex(bridge.Content, bridge.State) is (int q, int r)) info.Show(q, r); },
            ShowUnit = () =>
            {
                if (AdvisorTargets.UnitToShow(bridge, tokens) is not string id) return;
                shown = new PlacedToken(tokens.First(t => t.Unit.Id == id), new AnchorSpot(0, 0, 0, 0, false)); info.ShowUnit();
            },
        });
        return (info, advisor);
    }

    readonly List<string> temp = new List<string>();
    string TempDir()
    {
        var dir = Path.Combine(Path.GetTempPath(), "statecraft-advisor-" + Guid.NewGuid().ToString("N"));
        temp.Add(dir);
        return dir;
    }

    [TearDown]
    public void Clean()
    {
        EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);   // after the scene tests
        foreach (var d in temp) { try { if (Directory.Exists(d)) Directory.Delete(d, true); } catch (Exception) { } }
        temp.Clear();
    }

    // --- When it shows ----------------------------------------------------------------------------------------------

    static GameInputs? inputs;
    static GameInputs Inputs => inputs ??= GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);
    static VisualElement Ui => Object.FindFirstObjectByType<UIDocument>().rootVisualElement;

    static GameRoot OpenScene(SaveStore store, SettingsStore settings)
    {
        EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
        Object.FindFirstObjectByType<MapScene>().Build(Inputs.Map);
        var root = Object.FindFirstObjectByType<GameRoot>();
        root.Store = store; root.Settings = settings; root.Language = UiLanguage.English;
        root.AdvisorCatalogForTests = Approved();
        return root;
    }

    /// <summary>Map.unity as a player boots it: the advisor waits for the start screen, opens when New Game closes it (at its
    /// first step, the card hidden behind "Decision waiting") and notes in the settings file that it has shown; the next new
    /// game, from the start screen or the top bar, does not show it again.</summary>
    [Test]
    public void ShowsOnTheFirstNewGameOnly()
    {
        var settings = new SettingsStore(TempDir());
        var game = OpenScene(new SaveStore(TempDir()), settings);
        Begin(game, Inputs, 0, startScreen: true);                       // a fresh profile: no save
        Assert.IsFalse(game.Advisor!.InChain, "not under the start screen");
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        Assert.IsTrue(game.Advisor!.InChain, "New Game closes the screen and the advisor opens");
        Assert.AreEqual(0, game.Advisor.ChainStep);
        Assert.IsTrue(game.Card!.Shown != null && game.Card.Collapsed, "the opening card waits behind Decision waiting");
        Assert.AreEqual("seen", settings.Read(AdvisorSettings.OpeningKey));
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        Assert.IsFalse(game.Advisor.InChain);
        Assert.IsFalse(game.Card.Collapsed, "skipping puts the card back");

        Begin(game, Inputs, 0, startScreen: true);                       // a reload with no save: a new game again
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        Assert.IsFalse(game.Advisor!.InChain, "a second new game: not again");
        UiTestKit.Press(Ui.Q<Button>("new-game")); UiTestKit.Press(Ui.Q<Button>("new-game"));
        Assert.IsFalse(game.Advisor!.InChain, "nor from the top bar's New Game");
    }

    /// <summary>A Continue (a loaded save) never shows it, even before it was ever seen; a new game from the top bar then
    /// does. Automated runs never show it: no start screen (Begin(inputs, n)) or no settings file.</summary>
    [Test]
    public void NeverShowsOnAContinueOrAnAutomatedRun()
    {
        var settings = new SettingsStore(TempDir());
        var store = new SaveStore(TempDir());
        var played = new SimBridge(GameSession.New(C, 9, Mode.Historical), En);
        played.EndTurn(); played.EndTurn();
        store.WriteAutosave(played.Session.Save.Write());
        var game = OpenScene(store, settings);
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-continue"));
        Assert.IsFalse(game.Advisor!.InChain, "Continue");
        Assert.IsNull(settings.Read(AdvisorSettings.OpeningKey), "and still unseen");

        Begin(game, Inputs, 0);                                          // the tests' and tools' Begin: no start screen
        UiTestKit.Press(Ui.Q<Button>("new-game")); UiTestKit.Press(Ui.Q<Button>("new-game"));
        Assert.IsFalse(game.Advisor!.InChain, "an automated run");
        // #2693: the top bar's New Game writes the new game's autosave (it used to delete the old one), so the start screens
        // below have a save, and their New Game takes a confirming second press (StartScreenPresenter: a resumed screen goes
        // through NewGamePresenter.Press), as ANewGameFromASavedStartScreen... presses.
        Assert.IsNotNull(store.ReadAutosave(), "precondition: the top bar's New Game left an autosave");
        game.Settings = null;                                           // EditMode with no settings file
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game")); UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        Assert.IsFalse(game.Advisor!.InChain, "nowhere to remember it: never by itself");

        game.Settings = settings;
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game")); UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        Assert.IsTrue(game.Advisor!.InChain, "the first new game in this profile");
    }

    /// <summary>Actual Task7 callback path: a saved start screen closes from NewGamePresenter.Started, and both notify advisors.</summary>
    [Test]
    public void ANewGameFromASavedStartScreenKeepsItsOpeningChain()
    {
        var store = new SaveStore(TempDir()); var settings = new SettingsStore(TempDir());
        var played = new SimBridge(GameSession.New(C, 9, Mode.Historical), En); played.EndTurn(); played.EndTurn();
        store.WriteAutosave(played.Session.Save.Write());
        var game = OpenScene(store, settings); Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game")); UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        Assert.IsTrue(game.Advisor!.InChain); Assert.AreEqual(0, game.Advisor.ChainStep);
        Assert.AreEqual("seen", settings.Read(AdvisorSettings.OpeningKey));
    }

    /// <summary>Review focus3: actual Settings rebuilds preserve the step, hide pending Hebrew and resume English.</summary>
    [Test]
    public void ALanguageSwitchMidChainKeepsTheStep()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        game.AdvisorCatalogForTests = AdvisorCatalog.Parse(File.ReadAllText(AdvisorCatalog.Path).Replace("he_review: approved", "he_review: pending"));
        Begin(game, Inputs, 0, startScreen: true); UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        game.Advisor!.NextStep(); game.Advisor.NextStep(); var session = game.AdvisorSession;
        Assert.AreEqual(2, game.Advisor.ChainStep);
        UiTestKit.Press(Ui.Q<Button>("nav-settings")); UiTestKit.Press(Ui.Q<Button>("settings-language-he"));
        Assert.AreSame(session, game.AdvisorSession); Assert.IsFalse(game.Advisor!.IsOpen); Assert.AreEqual(2, session!.Chain.Step);
        if (!Ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass)) UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        UiTestKit.Press(Ui.Q<Button>("settings-language-en"));
        if (Ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass)) UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        Assert.AreSame(session, game.AdvisorSession); Assert.IsTrue(game.Advisor!.InChain); Assert.AreEqual(2, game.Advisor.ChainStep);
    }

    /// <summary>A language switch on a step that shows its city: the rebuilt chain shows the city again, and closing it puts
    /// back the hex the player had selected before the chain opened, not the advisor's city (review of the S2 head).</summary>
    [Test]
    public void ALanguageSwitchOnTheCityStepStillPutsBackTheSelectionItFound()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        game.AdvisorCatalogForTests = AdvisorTour1688Fixture.WithACityStep();
        Begin(game, Inputs, 0, startScreen: true); UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        game.Advisor!.CloseChain();
        var map = Object.FindFirstObjectByType<MapScene>(); var bridge = game.Bridge!;
        var city = AdvisorTargets.CityHex(bridge.Content, bridge.State)!.Value;
        var hex = bridge.Content.Hexes.Where(h => h.Land).Select(h => (h.Q, h.R)).First(h => h != city && game.Units!.UnitToSelectAt(h.Item1, h.Item2) == null);
        GameRoot.OnHexClicked(map, game.Units!, game.Info!, hex.Item1, hex.Item2);
        Assert.AreEqual(hex, game.Info!.Selected, "precondition: the player's hex");
        game.Advisor.OpenChain();
        for (int i = 0; i < CityStep; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        Assert.AreEqual(city, game.Info!.Selected, "precondition: the city step shows its city");

        UiTestKit.Press(Ui.Q<Button>("nav-settings")); UiTestKit.Press(Ui.Q<Button>("settings-language-he"));
        if (Ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass)) UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        Assert.IsTrue(game.Advisor!.InChain, "the rebuilt chain"); Assert.AreEqual("opening.cities", Steps[game.Advisor.ChainStep]);
        Assert.AreEqual(city, game.Info!.Selected, "shows its city again");
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        Assert.AreEqual(hex, game.Info!.Selected, "closing puts back the player's hex");
    }

    /// <summary>A pioneer group's card is a selection of its own (#1549, review of #1788): closing the advisor's city step puts
    /// back the pioneer card it found, not an empty panel; a pioneer the player opens meanwhile stays.</summary>
    [Test]
    public void ClosingPutsBackAnOpenPioneerCard()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        game.AdvisorCatalogForTests = AdvisorTour1688Fixture.WithACityStep();
        Begin(game, Inputs, 0, startScreen: true); UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        game.Advisor!.CloseChain();
        var bridge = game.Bridge!; var info = game.Info!; var units = game.Units!;
        var city = AdvisorTargets.CityHex(bridge.Content, bridge.State)!.Value;
        var hex = bridge.Content.Hexes.First(h => h.Land && (h.Q, h.R) != city && units.UnitToSelectAt(h.Q, h.R) == null
            && !bridge.Content.SettlementHexes.Any(x => x.Q == h.Q && x.R == h.R) && !bridge.Content.Sites.Any(x => x.Q == h.Q && x.R == h.R));
        var id = PioneerCardTests.Add(bridge.State, hex.Q, hex.R);
        var other = PioneerCardTests.Add(bridge.State, hex.Q, hex.R);
        void OpenCityStep()
        {
            game.Advisor!.OpenChain();
            for (int i = 0; i < CityStep; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
            Assert.AreEqual(city, info.Selected, "precondition: the city step shows its city");
            Assert.IsNull(info.Pioneer, "precondition: the advisor's city, not the pioneer card");
        }

        if (game.Card!.Shown != null && !game.Card.Collapsed) GameSceneTests.HideTheOpeningCard(Ui);   // the card over the HUD
        info.Show(hex.Q, hex.R);
        var link = PioneerCardTests.Link(Ui, id);
        Assert.IsTrue(link != null && link.enabledInHierarchy, "precondition: the hex lists the pioneer group, pressable");
        UiTestKit.Press(link!);
        Assert.AreEqual(id, info.Pioneer, "precondition: the pioneer's card");
        Assert.IsTrue(info.ShowsUnit);
        OpenCityStep();
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        Assert.AreEqual(id, info.Pioneer, "closing puts back the pioneer card the advisor found");
        Assert.IsTrue(info.ShowsUnit);
        Assert.AreEqual(GddHexPanel.Group(En, bridge.State, id), Ui.Q<Label>("info-title").text);
        Assert.IsNull(units.Selected, "no military token selected with it");

        OpenCityStep();
        info.Show(hex.Q, hex.R); UiTestKit.Press(PioneerCardTests.Link(Ui, other)!);   // the player opens another group meanwhile
        Assert.AreEqual(other, info.Pioneer, "precondition: the player's newer pioneer card");
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        Assert.AreEqual(other, info.Pioneer, "a pioneer the player opened meanwhile stays");
    }

    // --- Skip, Escape and How to play -------------------------------------------------------------------------------

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

        public Rig(UiText text, SettingsStore? settings = null, AdvisorCatalog? catalog = null)
        {
            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));
            var (info, advisor) = PanelAndAdvisor(Ui.Root, Bridge, text, settings, Card, () => Nav.Close(), catalog);
            Info = Own(info);
            Advisor = Own(advisor);
            Own(new EncyclopediaPresenter(Ui.Root, text, AdvisorCatalog.Load(), Nav, Advisor));
        }

        T Own<T>(T d) where T : IDisposable { owned.Add(d); return d; }

        public void Dispose()
        {
            for (int i = owned.Count - 1; i >= 0; i--) owned[i].Dispose();
            Ui.Dispose();
        }
    }

    /// <summary>The ten steps, in the order a player meets them (#2933): the war and its armies, then the moves of the first
    /// turn on a real unit, the card and End turn. Objectives and the city's two lines left for the later turns.</summary>
    [Test]
    public void HasTenStepsInOrder()
    {
        CollectionAssert.AreEqual(new[] { "opening.greeting", "opening.invasion", "opening.units", "opening.cities", "opening.attention", "opening.move_how", "opening.move", "opening.orders", "opening.event", "opening.end_turn" }, Steps);
        Assert.AreEqual(8, AdvisorPresenter.EventStep);
        var targets = Steps.Select(id => AdvisorCatalog.Load().Messages.Single(m => m.Id == id).Actions.FirstOrDefault(a => a.ShowMe != "")?.ShowMe ?? "").ToArray();
        CollectionAssert.AreEqual(new[] { "", "", "", "", "attention", "unit_move", "unit_move", "unit_orders", "decision_waiting", "end_turn" }, targets,
            "each step's target, in order");
        foreach (var text in new[] { En, He }) foreach (var key in Steps)
            Assert.IsFalse(string.IsNullOrWhiteSpace(text["advisor." + key + ".text"]), key);
    }

    /// <summary>The unit the move step shows: the first drawn unit (units.yaml order) that the sim lets move now (a hex in
    /// Movement.Reachable), so its card has the Move button; none in the legacy loop, which has no starting forces.</summary>
    [Test]
    public void TheMoveStepsUnitCanMove()
    {
        var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), En);
        var s = bridge.State;
        var rows = UnitRoster.FromState(UnitRows, C, s, _ => true, (row, a, q, r) => a).Select(t => t.Unit).ToList();
        var id = AdvisorTargets.UnitToShow(bridge, rows);
        Assert.IsNotNull(id, "a new game has a unit that can move");
        var row = rows.Single(u => u.Id == id);
        Assert.AreEqual(UnitData.Israel, row.Side);
        Assert.IsNotEmpty(Movement.Reachable(C, s, UnitCard.SimUnit(C, s, row)!.Value), "the sim has a hex for it");
        var first = rows.First(u => UnitCard.SimUnit(C, s, u) is int i && Movement.Reachable(C, s, i).Count > 0);
        Assert.AreEqual(first.Id, id, "the first such row");
        var legacy = new SimBridge(GameSession.New(SimBridgeGddTests.Legacy, GoldenRun.Seed, Mode.Historical), En);
        Assert.IsNull(AdvisorTargets.UnitToShow(legacy, rows), "the legacy loop: no unit to show");
    }

    /// <summary>Since moves are instant (#1527), a unit can spend its movement, or have Send home queued, before How to play
    /// reopens the tour: the move step then shows the next unit whose card still has Move (InfoPanelPresenter.MoveReach, the
    /// card's rule), and none once no unit can move, as in the legacy loop (the step's text without a target).</summary>
    [Test]
    public void TheMoveStepSkipsAUnitWhoseCardHasNoMove()
    {
        var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), En);
        var rows = UnitRoster.FromState(UnitRows, C, bridge.State, _ => true, (row, a, q, r) => a).Select(t => t.Unit).ToList();
        int Sim(string row) => UnitCard.SimUnit(C, bridge.State, rows.Single(u => u.Id == row))!.Value;
        void SpendAll(string row)
        {
            var id = Sim(row);
            for (int i = 0; i < 20; i++)
            {
                var reach = Movement.Reachable(C, bridge.State, id, bridge.Spent);
                if (reach.Count == 0) return;
                var far = reach.OrderByDescending(kv => kv.Value).First().Key;
                Assert.IsNull(bridge.Queue(new Move(id, far.Q, far.R)), $"{row}: the sim takes a move it offered");
            }
            Assert.Fail($"{row}: still has movement after 20 moves");
        }
        var first = AdvisorTargets.UnitToShow(bridge, rows);
        Assert.IsNotNull(first, "precondition: a new game has a unit that can move");
        SpendAll(first!);
        Assert.IsNotEmpty(Movement.Reachable(C, bridge.State, Sim(first!)), "precondition: with nothing spent the sim would still offer it hexes");
        var second = AdvisorTargets.UnitToShow(bridge, rows);
        Assert.IsNotNull(second, "another unit can still move");
        Assert.AreNotEqual(first, second, "the unit that spent its movement is not shown");
        Assert.IsNotNull(InfoPanelPresenter.MoveReach(bridge, Sim(second!)), "the shown unit's card has Move");
        Assert.IsNull(bridge.Queue(new UnitOrder(Sim(second!), Demobilization.SendHome)));
        Assert.IsNotEmpty(Movement.Reachable(C, bridge.State, Sim(second!), bridge.Spent), "precondition: the sim alone would still move it");
        var third = AdvisorTargets.UnitToShow(bridge, rows);
        Assert.IsTrue(third != first && third != second, $"a unit going home is not shown either ({third})");
        foreach (var row in rows.Where(u => UnitCard.SimUnit(C, bridge.State, u) is int id && InfoPanelPresenter.MoveReach(bridge, id) != null).Select(u => u.Id).ToList())
            SpendAll(row);
        Assert.IsNull(AdvisorTargets.UnitToShow(bridge, rows), "no unit can move: none to show");
    }

    /// <summary>Skip at any step closes it; Next walks the ten steps and Done closes it; Escape skips; How to play's button
    /// opens it again at its first step, closing that screen; each step shows its own strings.</summary>
    [UnityTest] public IEnumerator CanBeSkippedInEnglish() { yield return CanBeSkippedAtAnyStepAndReopened(false); }
    [UnityTest] public IEnumerator CanBeSkippedInHebrew() { yield return CanBeSkippedAtAnyStepAndReopened(true); }
    IEnumerator CanBeSkippedAtAnyStepAndReopened(bool hebrew)
    {
        var text = hebrew ? He : En;
        using var rig = new Rig(text, new SettingsStore(TempDir()));
        using var objectives = new ObjectivesPresenter(rig.Ui.Root, rig.Bridge, text);
        rig.Advisor.NewGame();
        Assert.IsTrue(rig.Advisor.InChain);
        for (int step = 0; step < Steps.Length; step++)
        {
            var key = Steps[step];
            Assert.AreEqual(step, rig.Advisor.ChainStep);
            Assert.AreEqual(text.Format("advisor.step", step + 1, Steps.Length), rig.Ui.Q<Label>("advisor-count").text);
            var speaker = AdvisorCatalog.Load().Messages.Single(m => m.Id == key).Advisor;
            Assert.AreEqual(text["advisor." + speaker + (key == "opening.cities" ? ".name_first" : ".name")], rig.Ui.Q<Label>("advisor-name").text);
            Assert.AreEqual(text["advisor." + key + ".text"], rig.Ui.Q<Label>("advisor-text").text);
            Assert.AreEqual(text[step == Steps.Length - 1 ? "advisor.done" : "advisor.next"], rig.Ui.Q<Button>("advisor-got-it").text);
            Assert.IsTrue(rig.Card.Collapsed, "the chain speaks before the card opens");
            if (key == "opening.end_turn")
            {
                // #791: from the advisor's last step the real Objectives button opens the new parts and the standalone goal.
                CollectionAssert.AreEquivalent(new[] { "railway", "burma_road", "yishuvim", "absorb_immigrants" },
                    C.Objectives.Select(o => o.Id).ToArray(), "the current objectives, not the retired standalone first yishuv");
                rig.Ui.Press("nav-objectives");
                Assert.AreEqual("objectives", rig.Nav.Current, "the advisor's Objectives destination opens");
                var rows = rig.Ui.Q<ScrollView>("objectives-rows").Query(className: "objectives-row").ToList();
                CollectionAssert.AreEqual(C.Objectives.Select(o => "objectives-row-" + o.Id).ToArray(),
                    rows.Select(row => row.name).ToArray(), "every current objective in the helper's order");
                for (int i = 0; i < C.Objectives.Count; i++)
                {
                    var expectedName = hebrew ? C.Objectives[i].NameHe : C.Objectives[i].Name;
                    Assert.IsFalse(string.IsNullOrWhiteSpace(expectedName), "the objective has a name in this language");
                    Assert.AreEqual(expectedName, rows[i].Q<Label>(className: "objectives-name").text,
                        C.Objectives[i].Id + ": the displayed name matches content in this language");
                }
                rig.Nav.Close();
            }
            rig.Ui.Press("advisor-got-it");
        }
        Assert.IsFalse(rig.Advisor.InChain, "Done closes it");
        Assert.IsFalse(rig.Card.Collapsed, "and the card is as it was");

        for (int at = 0; at < Steps.Length; at++)
        {
            rig.Ui.Press("nav-encyclopedia");
            Assert.AreEqual("encyclopedia", rig.Nav.Current);
            Assert.AreEqual(text["encyclopedia.opening"], rig.Ui.Q<Button>("encyclopedia-opening").text);
            rig.Ui.Press("encyclopedia-opening");
            Assert.IsTrue(rig.Advisor.InChain, "the encyclopedia opens it again");
            Assert.IsNull(rig.Nav.Current, "and closes itself");
            Assert.AreEqual(0, rig.Advisor.ChainStep);
            for (int i = 0; i < at; i++) rig.Ui.Press("advisor-got-it");
            rig.Ui.Press("advisor-close");
            Assert.IsFalse(rig.Advisor.InChain, $"Skip at step {at + 1}");
            Assert.IsFalse(rig.Card.Collapsed, $"the card is back after a Skip at step {at + 1}");
        }

        rig.Advisor.OpenChain();
        yield return null; rig.Ui.Q<Button>("advisor-got-it").Focus();
        Assert.IsTrue(rig.Advisor.Escape(), "Escape skips it");
        Assert.IsFalse(rig.Advisor.InChain);
        // The guide's "Escape does nothing when closed" cannot be asked again here: Time.frameCount does not move in an
        // edit-mode test, so Escape keeps answering "used this frame" (AdvisorPopupTests.AnInjectedScreenNavHookLeavesTheScreenUnderThePopupOpen).
        Assert.IsFalse(rig.Ui.Root.Q("root").ClassListContains(AdvisorPresenter.OpenClass), "and it stays closed");

        rig.Card.Collapse();                                           // the player hid the card before opening the advisor
        rig.Advisor.OpenChain();
        for (int i = 0; i < AdvisorPresenter.EventStep; i++) rig.Ui.Press("advisor-got-it");
        rig.Ui.Press("advisor-show-me");
        Assert.IsFalse(rig.Card.Collapsed, "Show me opens the card");
        rig.Card.Collapse();
        rig.Ui.Press("advisor-close");
        Assert.IsTrue(rig.Card.Collapsed, "and Skip hides it again, as the player left it");
    }

    /// <summary>End turn pressed with the advisor open (it takes no pointer outside its panel, so the HUD stays usable): the
    /// advisor closes as the turn advances, at any step, and the new turn's card is left to its own rule (a new event opens),
    /// not hidden or shown by what the advisor found on the old card (review of #1209, follow-up 1).</summary>
    [Test]
    public void EndTurnClosesTheChain([Values(0, 1, 2, AdvisorPresenter.EventStep)] int step, [Values(false, true)] bool cardHidden)
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        Assert.IsTrue(game.Advisor!.InChain, "precondition: the first new game opens the advisor");
        game.Advisor.CloseChain();
        var card = game.Card!;
        Assert.IsNotNull(card.Shown, "precondition: a new game opens on a card");
        var first = card.Shown!.Id;
        if (cardHidden) card.Collapse();
        game.Advisor.OpenChain();
        for (int i = 0; i < step; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        var turn = game.Bridge!.State.Turn;
        foreach (var id in game.Bridge.NeedsOrders.ToList()) game.Bridge.Skip(id);
        UiTestKit.Press(Ui.Q<Button>("end-turn"));
        Assert.AreEqual(turn + 1, game.Bridge.State.Turn, "precondition: End turn ran the turn");
        Assert.IsFalse(game.Advisor.InChain, "the advisor closes when the turn advances");
        Assert.AreEqual(-1, game.Advisor.ChainStep);
        Assert.IsFalse(Ui.Q("root").ClassListContains(AdvisorPresenter.OpenClass));
        if (card.Shown != null && card.Shown.Id != first)
            Assert.IsFalse(card.Collapsed, $"the new turn's card ({card.Shown.Id}) opens by its own rule");
        else if (card.Shown != null)
            Assert.AreEqual(cardHidden, card.Collapsed, "the same card is put back as the advisor found it");
        Assert.AreEqual(card.Shown != null && !card.Collapsed, Ui.Q("root").ClassListContains(EventCardPresenter.OpenClass),
            "the backdrop shows with an open card only");
    }

    /// <summary>The advisor shows its city on steps 2 and 3; Done, Skip or Escape puts back the selection it found (none, a
    /// hex or a unit) with the info panel and the map's outline; a selection the player made meanwhile is kept (review of
    /// #1209, follow-up 3).</summary>
    [UnityTest]
    public IEnumerator ClosingPutsBackTheSelectionItFound()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        game.AdvisorCatalogForTests = AdvisorTour1688Fixture.WithACityStep();
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        var advisor = game.Advisor!; var info = game.Info!; var units = game.Units!; var bridge = game.Bridge!;
        var map = Object.FindFirstObjectByType<MapScene>();
        var city = AdvisorTargets.CityHex(bridge.Content, bridge.State)!.Value;
        bool Outlined() { var t = map.Terrain.transform.Find(MapScene.SelectionName); return t != null && t.GetComponent<MeshRenderer>().enabled; }
        void ToCity()
        {
            for (int i = 0; i < CityStep; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
            Assert.AreEqual(city, info.Selected, "the city step shows the advisor's city");
        }
        void Click(int q, int r) => GameRoot.OnHexClicked(map, units, info, q, r);

        Assert.IsTrue(advisor.InChain && info.Selected == null && !info.ShowsUnit, "precondition: a new game, nothing selected");
        ToCity();
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        Assert.IsNull(info.Selected, "Skip: nothing selected again");
        Assert.IsFalse(info.ShowsUnit);
        Assert.IsNull(units.Selected);
        Assert.IsFalse(Outlined(), "and no outline on the map");

        var hex = bridge.Content.Hexes.Where(h => h.Land).Select(h => (h.Q, h.R)).First(h => h != city && units.UnitToSelectAt(h.Item1, h.Item2) == null);
        Click(hex.Item1, hex.Item2);
        Assert.AreEqual(hex, info.Selected, "precondition: a hex selected");
        advisor.OpenChain(); ToCity();
        for (int i = CityStep; i < Steps.Length; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        Assert.IsFalse(advisor.InChain, "Done");
        Assert.AreEqual(hex, info.Selected, "Done: the hex the advisor found");
        Assert.IsTrue(Outlined(), "outlined again");

        Assert.IsNotEmpty(units.Tokens, "precondition: units in play");
        var token = units.Tokens[0];
        Click(token.Q, token.R);
        var unit = units.Selected;
        Assert.IsTrue(unit != null && info.ShowsUnit, "precondition: a unit selected");
        advisor.OpenChain(); ToCity();
        yield return null; Ui.Q<Button>("advisor-got-it").Focus(); Assert.IsTrue(advisor.Escape());
        Assert.AreEqual(unit, units.Selected, "Escape: the unit the advisor found");
        Assert.IsTrue(info.ShowsUnit, "its card in the info panel");
        Assert.IsFalse(Outlined(), "with its ring, not a hex outline");

        Click(hex.Item1, hex.Item2);
        advisor.OpenChain(); ToCity();
        var other = bridge.Content.Hexes.Where(h => h.Land).Select(h => (h.Q, h.R)).First(h => h != city && h != hex && units.UnitToSelectAt(h.Item1, h.Item2) == null);
        Click(other.Item1, other.Item2);                                // the player picks a hex under the open advisor
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        Assert.AreEqual(other, info.Selected, "a selection the player made meanwhile stays");
    }

    /// <summary>In the game as a player has it, the move step shows the advisor's unit (UnitToShow) as a click on it would: its
    /// ring and its card with the Move button. Skip puts back the selection the advisor found (none), as on the city's steps.</summary>
    [Test]
    public void TheMoveStepShowsAUnitsCardWithMove()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        var advisor = game.Advisor!; var info = game.Info!; var units = game.Units!; var bridge = game.Bridge!;
        Assert.IsTrue(advisor.InChain && info.Selected == null && !info.ShowsUnit, "precondition: a new game, nothing selected");
        var move = Array.IndexOf(Steps, "opening.move");
        for (int i = 0; i < move; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        Assert.AreEqual(move, advisor.ChainStep);
        var id = AdvisorTargets.UnitToShow(bridge, units.Tokens.Select(t => t.Token));
        Assert.IsNotNull(id, "precondition: a drawn unit can move");
        Assert.AreEqual(id, units.Selected, "the advisor's unit, with its ring");
        Assert.IsTrue(info.ShowsUnit, "its card in the info panel");
        Assert.AreEqual(DisplayStyle.Flex, Ui.Q<Button>("info-move").style.display.value, "with the Move button");
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        Assert.IsNull(units.Selected, "Skip: no unit selected again");
        Assert.IsFalse(info.ShowsUnit);
        Assert.IsNull(info.Selected, "and no hex");
    }

    /// <summary>#2933: the unit steps name one unit from step to step, the card keeping Move while the chain walks them.</summary>
    [Test]
    public void TheUnitStepsKeepOneUnitsCard()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        var advisor = game.Advisor!; var info = game.Info!; var units = game.Units!;
        for (int i = 0; i < Array.IndexOf(Steps, "opening.move_how"); i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        var id = AdvisorTargets.UnitToShow(game.Bridge!, units.Tokens.Select(t => t.Token));
        foreach (var step in new[] { "opening.move_how", "opening.move", "opening.orders" })
        {
            Assert.AreEqual(step, Steps[advisor.ChainStep]);
            Assert.AreEqual(id, units.Selected, step); Assert.IsTrue(info.ShowsUnit, step);
            UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        }
        Assert.IsNull(units.Selected, "the event step puts it away");
    }

    /// <summary>Issue #1392: the move step brings UnitToShow's hex into the map view, clear of the info panel and the advisor's
    /// card, with at least one of Movement.Reachable's lit destinations visible too. The real scene, camera projection and HUD
    /// layout are used at the advisor's supported desktop, itch embed and phone sizes, in both UI directions.</summary>
    [UnityTest]
    public IEnumerator TheMoveStepShowsTheUnitAndAReachableHexOnTheMap()
    {
        var problems = new List<string>();
        foreach (var hebrew in new[] { false, true })
            foreach (var (w, h) in new[] { (1920, 1080), (1280, 720), (390, 844) })
            {
                // The scene first: opening it in Single mode destroys textures made before it.
                var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
                var rt = new RenderTexture(w, h, 0);
                var camRt = new RenderTexture(w, h, 24);
                var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));
                ps.targetTexture = rt;
                var camera = Camera.main;
                try
                {
                    game.Language = hebrew ? UiLanguage.Hebrew : UiLanguage.English;
                    Object.FindFirstObjectByType<UIDocument>().panelSettings = ps;
                    camera.targetTexture = camRt;
                    Begin(game, Inputs, 0, startScreen: true);
                    UiTestKit.Press(Ui.Q<Button>("start-new-game"));
                    var advisor = game.Advisor!; var bridge = game.Bridge!; var units = game.Units!;
                    var map = Object.FindFirstObjectByType<MapScene>();
                    var move = Array.IndexOf(Steps, "opening.move");
                    for (int step = 0; step < move; step++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
                    Assert.AreEqual(move, advisor.ChainStep, "precondition: the advisor is on its move step");
                    for (int frame = 0; frame < 5; frame++) yield return null;

                    var id = AdvisorTargets.UnitToShow(bridge, units.Tokens.Select(t => t.Token));
                    Assert.IsNotNull(id, "precondition: a drawn unit can move");
                    var token = units.Tokens.Single(t => t.Token.Unit.Id == id);
                    var simUnit = UnitCard.SimUnit(bridge.Content, bridge.State, token.Token);
                    Assert.IsNotNull(simUnit, "precondition: the advisor's token has a sim unit");
                    var reachable = Movement.Reachable(bridge.Content, bridge.State, simUnit!.Value).Keys.ToList();
                    Assert.IsNotEmpty(reachable, "precondition: the advisor's unit has a destination");
                    UiTestKit.Press(Ui.Q<Button>("info-move"));
                    Assert.IsTrue(map.Terrain.transform.Find(MapScene.ReachableName)?.GetComponent<MeshRenderer>().enabled == true,
                        "precondition: Move lit the reachable hexes");

                    var root = Ui;
                    var info = root.Q("info-panel").worldBound;
                    var card = root.Q("advisor-popup").worldBound;   // the advisor's own card is opaque too
                    string at = $"{(hebrew ? "he" : "en")} {w}x{h}";
                    (bool visible, bool clear, Vector3 viewport, Vector2 panel) Project(int q, int r)
                    {
                        var hex = bridge.Content.HexAt(q, r)!;
                        var ground = map.Frame.ToWorld(hex.Lon, hex.Lat, 0);
                        ground.y = map.Terrain.SampleHeight(ground) + map.Terrain.transform.position.y;
                        var viewport = camera.WorldToViewportPoint(ground);
                        var panel = MapLabelsPresenter.ToPanel(viewport, camera.rect, root.panel.visualTree.layout.size, Vector2.zero);
                        return (viewport.z > 0f && viewport.x >= 0f && viewport.x <= 1f && viewport.y >= 0f && viewport.y <= 1f,
                            !info.Contains(panel) && !card.Contains(panel), viewport, panel);
                    }

                    var unit = Project(token.Q, token.R);
                    if (!unit.visible) problems.Add($"{at}: unit ({token.Q},{token.R}) is outside the viewport at {unit.viewport}");
                    else if (!unit.clear) problems.Add($"{at}: unit ({token.Q},{token.R}) at {unit.panel} is under the info panel {info} or the advisor {card}");
                    if (!reachable.Select(p => (at: p, projection: Project(p.Q, p.R))).Any(p => p.projection.visible && p.projection.clear))
                        problems.Add($"{at}: no reachable hex is visible and clear of the info panel and the advisor");
                }
                finally
                {
                    camera.targetTexture = null;
                    Object.FindFirstObjectByType<UIDocument>().panelSettings = AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel);
                    Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt); camRt.Release(); Object.DestroyImmediate(camRt);
                    EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
                }
            }
        Assert.IsEmpty(problems, string.Join("\n", problems));
    }

    /// <summary>Spec 7.3: End turn, while a unit needs orders, selects it and centres the camera on its hex.</summary>
    [UnityTest]
    public IEnumerator EndTurnFocusesTheNextUnitThatNeedsOrders()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        game.Begin(Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        yield return null;
        var bridge = game.Bridge!; var units = game.Units!;
        // At the advisor's move-step framing, the map bounds permit the selected hex to be centred exactly.
        Object.FindFirstObjectByType<CameraRig>().Distance = AdvisorTargets.MoveStepDistanceM;
        var next = bridge.NeedsOrders[0];
        UiTestKit.Press(Ui.Q<Button>("end-turn"));
        yield return null;
        var token = units.Tokens.Single(t => UnitCard.SimUnit(bridge.Content, bridge.State, t.Token.Unit) == next);
        Assert.AreEqual(token.Token.Unit.Id, units.Selected);
        var hex = bridge.Content.HexAt(token.Q, token.R)!;
        var map = Object.FindFirstObjectByType<MapScene>();
        var rig = Object.FindFirstObjectByType<CameraRig>();
        Assert.Less(Vector3.Distance(map.Frame.ToWorld(hex.Lon, hex.Lat, 0), rig.LookAt), 1f);
    }

    /// <summary>Spec 7.3: repeated blocked End turn presses move past the selected needing unit and wrap.</summary>
    [UnityTest]
    public IEnumerator BlockedEndTurnCyclesPastTheSelectionAndWraps()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        game.Begin(Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        yield return null;
        var bridge = game.Bridge!; var units = game.Units!;
        var needs = bridge.NeedsOrders.ToList();
        Assert.Greater(needs.Count, 1, "precondition: several units need orders");
        var turn = bridge.TurnStart.Turn;
        int? SelectedId() => units.Selected is string row
            ? units.Tokens.Where(t => t.Token.Unit.Id == row)
                .Select(t => UnitCard.SimUnit(bridge.Content, bridge.State, t.Token.Unit)).FirstOrDefault()
            : null;
        // Begin from the first needing unit, then prove advance and a complete wrap without issuing an order.
        UiTestKit.Press(Ui.Q<Button>("end-turn"));
        yield return null;
        Assert.AreEqual(needs[0], SelectedId());
        for (var step = 1; step <= needs.Count; step++)
        {
            UiTestKit.Press(Ui.Q<Button>("end-turn"));
            yield return null;
            Assert.AreEqual(needs[step % needs.Count], SelectedId(),
                "blocked End turn advances beyond the selected unit and wraps");
        }
        Assert.AreEqual(turn, bridge.TurnStart.Turn, "cycling never ends the turn");
    }

    // --- Each step with what it explains (owner's report on v0.1.4) ---------------------------------------------------

    /// <summary>The first card's first option, pressed as a click would.</summary>
    static void AnswerTheCard() => UiTestKit.Press(Ui.Q("event-options").Query<Button>().First());

    /// <summary>The opening turn's two cards answered, One Army then the defence of Degania (R-EVT-006; owner, 24 October
    /// 2026, #1568: "One Army first").</summary>
    static void AnswerTheOpeningCards() { AnswerTheCard(); AnswerTheCard(); }

    /// <summary>The owner's report on v0.1.4 ("they open for a split second after the user closes the screen they were
    /// supposed to explain"), path A: the player answers the card while the event step explains it. The card closes, so the
    /// advisor goes on to the next step at once, with Next focused, instead of explaining a card that is gone.</summary>
    [UnityTest]
    public IEnumerator AnsweringTheCardOnItsStepGoesOnToTheNext()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        Begin(game, Inputs, 0, startScreen: true); UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        var advisor = game.Advisor!; var card = game.Card!;
        for (int i = 0; i < AdvisorPresenter.EventStep; i++) advisor.NextStep();
        Assert.IsTrue(card.Shown != null && card.Collapsed);
        UiTestKit.Press(Ui.Q<Button>("advisor-show-me")); Assert.IsFalse(advisor.IsOpen);
        card.Collapse(); Assert.AreEqual(AdvisorPresenter.EventStep, advisor.ChainStep); Assert.IsTrue(advisor.IsOpen);
        UiTestKit.Press(Ui.Q<Button>("advisor-show-me")); AnswerTheCard();
        Assert.AreEqual("degania_defence", card.Shown?.Id, "One Army answered, the turn's second card opens (R-EVT-006)");
        Assert.IsFalse(advisor.IsOpen, "the advisor steps aside while the second card is open, its event step kept");
        AnswerTheCard();
        Assert.IsNull(card.Shown, "the opening turn's two cards answered"); Assert.IsTrue(advisor.InChain); Assert.AreEqual(AdvisorPresenter.EventStep + 1, advisor.ChainStep);
        Assert.AreEqual(En.Format("advisor.step", 10, 10), Ui.Q<Label>("advisor-count").text, "the count the player was shown stays");
        Assert.AreEqual(En["advisor.opening.end_turn.text"], Ui.Q<Label>("advisor-text").text, "on to End turn");
        for (int i = 0; i < 3; i++) yield return null;                   // the popup was hidden under the card: styles resolve
        Assert.AreSame(Ui.Q<Button>("advisor-got-it"), Ui.Q("root").focusController.focusedElement, "Next has the focus again (#1323)");
    }

    /// <summary>Path B: the card was answered before the advisor reaches its event step (a returning player opening it from How
    /// to play, or one who answered it under the advisor): there is no card to explain, so the step is left out and the
    /// advisor counts nine steps.</summary>
    [Test]
    public void WithNoCardTheEventStepIsLeftOut()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        var advisor = game.Advisor!; var card = game.Card!;
        advisor.CloseChain();
        AnswerTheOpeningCards();
        Assert.IsNull(card.Shown, "precondition: no card left this turn");
        UiTestKit.Press(Ui.Q<Button>("nav-encyclopedia")); UiTestKit.Press(Ui.Q<Button>("encyclopedia-opening"));
        Assert.IsTrue(advisor.InChain);
        var seen = new List<string>();
        for (int shown = 1; advisor.InChain; shown++)
        {
            seen.Add(Steps[advisor.ChainStep]);
            Assert.AreEqual(En.Format("advisor.step", shown, 9), Ui.Q<Label>("advisor-count").text, $"step {shown} of nine");
            Assert.AreEqual(En[shown == 9 ? "advisor.done" : "advisor.next"], Ui.Q<Button>("advisor-got-it").text);
            Assert.LessOrEqual(shown, 9, "nine steps, then Done");
            UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        }
        CollectionAssert.AreEqual(new[] { "opening.greeting", "opening.invasion", "opening.units", "opening.cities", "opening.attention", "opening.move_how", "opening.move", "opening.orders", "opening.end_turn" }, seen);
    }

    /// <summary>The unit the advisor shows for its unit steps is put away when the advisor moves on to a step that does not
    /// explain it (the event card, End turn), with or without a card, so no stale panel stays under the later steps.</summary>
    [Test]
    public void LeavingTheLastUnitStepPutsItsUnitCardAway([Values(false, true)] bool cardAnswered)
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        var advisor = game.Advisor!; var info = game.Info!; var units = game.Units!;
        if (cardAnswered) { advisor.CloseChain(); AnswerTheOpeningCards(); advisor.OpenChain(); }
        var orders = Array.IndexOf(Steps, "opening.orders");
        for (int i = 0; i < orders; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        Assert.IsTrue(info.ShowsUnit && units.Selected != null, "precondition: the orders step shows a unit's card");
        UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        Assert.AreEqual(cardAnswered ? "opening.end_turn" : "opening.event", Steps[advisor.ChainStep]);
        Assert.IsFalse(info.ShowsUnit, "the unit's card is put away");
        Assert.IsNull(units.Selected, "and its unit unselected, as the advisor found it");
        Assert.IsNull(info.Selected, "with no hex shown either");
    }

    /// <summary>A closed or disposed advisor takes no notice of the game changing (it listens to SimBridge.Changed only while it
    /// lives, and acts on it only while it is open).</summary>
    [Test]
    public void AClosedChainIgnoresTheGameChanging()
    {
        var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        var advisor = game.Advisor!;
        for (int i = 0; i < AdvisorPresenter.EventStep; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
        UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        AnswerTheOpeningCards();
        Assert.IsFalse(advisor.InChain, "closed, it stays closed");
        Assert.AreEqual(-1, advisor.ChainStep);
        Assert.IsFalse(Ui.Q("root").ClassListContains(AdvisorPresenter.OpenClass));

        using var rig = new Rig(En);
        var bridge = rig.Bridge;
        var lone = new AdvisorPresenter(rig.Ui.Root, bridge, En, new AdvisorSession(Approved(), new AdvisorSettings(null), false), hooks: new AdvisorHooks { Card = rig.Card });
        lone.Dispose();
        rig.Advisor.OpenChain();                                               // another advisor open on the same HUD (the root's class)
        for (int i = 0; i < AdvisorPresenter.EventStep; i++) rig.Advisor.NextStep();
        rig.Card.Reopen();                                                     // the chain's event step waits for the card
        UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());   // One Army
        UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());   // then Degania (R-EVT-006)
        Assert.IsNull(rig.Card.Shown, "precondition: the opening turn's two cards answered");
        Assert.AreEqual(-1, lone.ChainStep, "the disposed advisor was not called");
    }

    /// <summary>The game changing under an open step (a + or − on the city's workers raises SimBridge.Changed) draws the step
    /// again without showing its city again: a hex the player picked meanwhile stays selected (review of the S2 draft).</summary>
    [Test]
    public void TheGameChangingKeepsThePlayersSelectionOnAStep()
    {
        using var rig = new Rig(En, new SettingsStore(TempDir()), AdvisorTour1688Fixture.WithACityStep());
        rig.Advisor.OpenChain(); for (int i = 0; i < CityStep; i++) rig.Advisor.NextStep();
        Assert.AreEqual("opening.cities", Steps[rig.Advisor.ChainStep], "precondition: the city step");
        var city = AdvisorTargets.CityHex(rig.Bridge.Content, rig.Bridge.State)!.Value;
        Assert.AreEqual(city, rig.Info.Selected, "precondition: it shows its city");
        var other = rig.Bridge.Content.Hexes.Where(h => h.Land).Select(h => (h.Q, h.R)).First(h => h != city);
        rig.Info.Show(other.Item1, other.Item2);                        // the player picks another hex under the open advisor
        int changed = 0; rig.Bridge.Changed += () => changed++;
        Assert.IsNull(rig.Bridge.Queue(new AssignWorkers("tel_aviv", "job", 1)), "precondition: the order is queued");
        Assert.AreEqual(1, changed, "precondition: the game changed");
        Assert.AreEqual(other, rig.Info.Selected, "the player's hex stays");
        Assert.IsTrue(rig.Advisor.InChain); Assert.AreEqual("opening.cities", Steps[rig.Advisor.ChainStep], "on the same step");
        rig.Ui.Press("advisor-close");
        Assert.AreEqual(other, rig.Info.Selected, "and Skip keeps the player's choice");
    }

    // --- Presentation only ------------------------------------------------------------------------------------------

    /// <summary>The whole advisor, by itself, through How to play, through every step, Skip and Escape: the state hash, the
    /// session, the queued orders and the save file are what they were.</summary>
    [UnityTest]
    public IEnumerator ChangesNeitherTheSimNorTheSave()
    {
        var store = new SaveStore(TempDir());
        var settings = new SettingsStore(TempDir());
        var game = OpenScene(store, settings);
        Begin(game, Inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        Assert.IsTrue(game.Advisor!.InChain);
        var bridge = game.Bridge!;
        bridge.EndTurn();                                               // a turn played, so there is a save to compare
        if (Ui.Q("root").ClassListContains(TurnReportPresenter.OpenClass)) UiTestKit.Press(Ui.Q<Button>("turn-report-close"));
        var session = bridge.Session;
        var hash = bridge.State.Hash();
        var save = store.ReadAutosave();
        Assert.IsNotNull(save, "precondition: the turn was saved");
        var queued = bridge.Queued.Count;

        void Next(int times) { for (int i = 0; i < times; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it")); }
        void FromTheEncyclopedia() { UiTestKit.Press(Ui.Q<Button>("nav-encyclopedia")); UiTestKit.Press(Ui.Q<Button>("encyclopedia-opening")); }
        Next(Steps.Length);                     // the advisor that opened by itself, to Done
        Assert.IsFalse(game.Advisor.InChain);
        FromTheEncyclopedia(); Next(2); UiTestKit.Press(Ui.Q<Button>("advisor-close"));
        FromTheEncyclopedia(); Next(3); yield return null; Ui.Q<Button>("advisor-got-it").Focus(); Assert.IsTrue(game.Advisor.Escape());
        Assert.IsFalse(game.Advisor.InChain);
        Assert.AreSame(session, bridge.Session, "the same game");
        Assert.AreEqual(hash, bridge.State.Hash(), "the state hash");
        Assert.AreEqual(queued, bridge.Queued.Count, "no order queued");
        Assert.AreEqual(save, store.ReadAutosave(), "the save file");
    }

    // --- Where each step points --------------------------------------------------------------------------------------

    /// <summary>Every step's target is on screen, in the window, with the gold ring round it (inside the window) and the panel
    /// inside the window and clear of it, at 1920x1080 (the one-row bar), 1280x720 and 390x844 (the phone the three-row bar
    /// is tuned for), in English and Hebrew, on a new game's first turn (the opening card open). Attention's ring keeps clear of
    /// End turn's border, so the two read as two boxes (review of #1209, follow-up 2).</summary>
    [UnityTest]
    public IEnumerator EveryStepPointsAtAVisibleElement()
    {
        var problems = new List<string>();
        foreach (var hebrew in new[] { false, true })
            foreach (var (w, h) in new[] { (1920, 1080), (1280, 720), (390, 844) })
                yield return EachStep(w, h, hebrew, (root, advisor, step, eventCard) =>
                {
                    string at = $"{(hebrew ? "he" : "en")} {w}x{h} step {step + 1} ({Steps[step]})";
                    var window = root.worldBound;
                    var targets = Targets(root, advisor, eventCard, step);
                    if (Steps[step] == "opening.end_turn" &&
                        (targets.Count != 1 || targets[0].name != "end-turn"))
                        problems.Add($"{at}: the last step must point at End turn");
                    if (Steps[step] == "opening.orders" &&
                        (targets.Count == 0 || targets.Any(t => !new[] { "info-move", "info-fortify", "info-hold", "info-skip" }.Contains(t.name))))
                        problems.Add($"{at}: the orders step must point at the unit card's orders, not [{string.Join(", ", targets.Select(t => t.name))}]");
                    if (Steps[step] == "opening.move" &&
                        (targets.Count != 1 || targets[0].name != "info-move" || ((Button)targets[0]).text != (hebrew ? He : En)["unit.order.move"]))
                        problems.Add($"{at}: the move step must point at the unit card's Move button");
                    if (advisor.Chain[step].Actions.All(a => a.ShowMe == ""))
                    {
                        if (targets.Count != 0 || root.Q("advisor-ring").resolvedStyle.display == DisplayStyle.Flex) problems.Add($"{at}: a step with no target has a ring");
                        if (!Inside(root.Q("advisor-popup").worldBound, window)) problems.Add($"{at}: popup outside window");
                        return;
                    }
                    if (targets.Count == 0) { problems.Add($"{at}: no target shows"); return; }
                    var area = AdvisorTargets.Area(targets)!.Value;
                    if (!Inside(area, window)) problems.Add($"{at}: the target {area} leaves the window {window}");
                    var ring = root.Q("advisor-ring");
                    if (ring.resolvedStyle.display != DisplayStyle.Flex) problems.Add($"{at}: no ring");
                    else
                    {
                        if (!Inside(area, ring.worldBound)) problems.Add($"{at}: the ring {ring.worldBound} is not round the target {area}");
                        if (!Inside(ring.worldBound, window)) problems.Add($"{at}: the ring {ring.worldBound} leaves the window {window}");
                        // The ring round Attention must read as its own box, not one with End turn's gold border under it.
                        var endTurn = root.Q("end-turn").worldBound;
                        if (Steps[step] == "opening.attention" && Gap(ring.worldBound, endTurn) < RingClearPx)
                            problems.Add($"{at}: the ring {ring.worldBound} is {Gap(ring.worldBound, endTurn):0.#} px from End turn {endTurn} (under {RingClearPx})");
                    }
                    var panel = root.Q("advisor-popup").worldBound;
                    if (!(panel.width > 0) || !Inside(panel, window)) problems.Add($"{at}: the panel {panel} leaves the window {window}");
                    if (panel.Overlaps(area)) problems.Add($"{at}: the panel {panel} covers the target {area}");
                    // The ring too (its gap and border round the target), so the larger text (player feedback, 21 October
                    // 2026) never pushes the panel onto the gold box it points at.
                    if (ring.resolvedStyle.display == DisplayStyle.Flex && panel.Overlaps(ring.worldBound))
                        problems.Add($"{at}: the panel {panel} covers the ring {ring.worldBound}");
                    foreach (var name in new[] { "advisor-name", "advisor-text", "advisor-close", "advisor-got-it" })
                        if (!Inside(root.Q(name).worldBound, panel)) problems.Add($"{at}: {name} {root.Q(name).worldBound} leaves the panel {panel}");
                });
        Assert.IsEmpty(problems, string.Join("\n", problems));
    }

    /// <summary>Player feedback relayed by the owner (21 October 2026: the advisor's text was too small; "make it slightly
    /// bigger"): at 1280x720 (the itch embed) and 1920x1080, in English and Hebrew, the advisor's text is at least
    /// <see cref="GuideTextOnScreenPx"/> on screen and its smallest line, the step count, at least the owner's text floor
    /// (HudPresenter.TextFloorPx). Before the change they were 12.7 and 10.7 at 1280x720. On screen px are the resolved size
    /// times the panel's scale (EditMode draws at one pixel per CSS px). Every size and step is logged, the phone (390x844)
    /// too, which is held to the floor alone (about 0.49 scale; <see cref="TheChainKeepsTheTextFloorOnEveryWindow"/>).</summary>
    [UnityTest]
    public IEnumerator TheChainReadsAtTheItchEmbedSize()
    {
        var problems = new List<string>();
        foreach (var hebrew in new[] { false, true })
            foreach (var (w, h) in new[] { (1920, 1080), (1280, 720), (390, 844) })
                yield return EachStep(w, h, hebrew, (root, advisor, step, eventCard) =>
                {
                    string at = $"{(hebrew ? "he" : "en")} {w}x{h} step {step + 1}";
                    float scale = root.panel.scaledPixelsPerPoint;
                    float OnScreen(string name) => root.Q(name).resolvedStyle.fontSize * scale;
                    var panel = root.Q("advisor-popup").worldBound;
                    Debug.Log($"ADVISOR SIZE {at}: scale {scale:0.###}, text {OnScreen("advisor-text"):0.#} px, title {OnScreen("advisor-name"):0.#} px, " +
                              $"count {OnScreen("advisor-count"):0.#} px, buttons {OnScreen("advisor-got-it"):0.#} px on screen; panel {panel.width * scale:0}x{panel.height * scale:0} px");
                    if (w == 390) return;
                    if (OnScreen("advisor-text") < GuideTextOnScreenPx - 0.01f)
                        problems.Add($"{at}: the advisor's text is {OnScreen("advisor-text"):0.##} px on screen (under {GuideTextOnScreenPx})");
                    if (OnScreen("advisor-count") < HudPresenter.TextFloorPx - 0.01f)
                        problems.Add($"{at}: the step count is {OnScreen("advisor-count"):0.##} px on screen (under the {HudPresenter.TextFloorPx} px floor)");
                    if (OnScreen("advisor-name") <= OnScreen("advisor-text"))
                        problems.Add($"{at}: the title ({OnScreen("advisor-name"):0.#} px) is not larger than the text ({OnScreen("advisor-text"):0.#} px)");
                });
        Assert.IsEmpty(problems, string.Join("\n", problems));
    }

    /// <summary>The owner's text floor on the advisor too (21 October 2026, after #1283): at every window, the phone held upright
    /// (390x844, about 0.49 scale) included, in English and Hebrew, the step count, the title, the text and both buttons
    /// are at least HudPresenter.TextFloorPx on screen, the top bar's rule (HudPresenter.SmallTextSize); where the scale
    /// keeps them over it (1920x1080, 1280x720) they are the stylesheet's sizes from #1283, unchanged. Before the change
    /// the phone's text was about 10.8 px on screen.</summary>
    [UnityTest]
    public IEnumerator TheChainKeepsTheTextFloorOnEveryWindow()
    {
        var problems = new List<string>();
        // Statecraft.uss: .advisor-count, .advisor-name, .advisor-text, Button (Skip and Next).
        var stylesheet = new Dictionary<string, float> { ["advisor-count"] = 15f, ["advisor-name"] = 24f, ["advisor-text"] = 22f, ["advisor-close"] = 19f, ["advisor-got-it"] = 19f };
        CollectionAssert.AreEqual(new[] { 15f, 24f, 22f, 19f },
            new[] { AdvisorPresenter.CountPx, AdvisorPresenter.NamePx, AdvisorPresenter.TextPx, AdvisorPresenter.ButtonPx },
            "the presenter's sizes mirror the stylesheet's");
        foreach (var hebrew in new[] { false, true })
            foreach (var (w, h) in new[] { (1920, 1080), (1280, 720), (1024, 768), (390, 844) })
                yield return EachStep(w, h, hebrew, (root, advisor, step, eventCard) =>
                {
                    string at = $"{(hebrew ? "he" : "en")} {w}x{h} step {step + 1}";
                    float scale = root.panel.scaledPixelsPerPoint;
                    foreach (var (name, px) in stylesheet)
                    {
                        float size = root.Q(name).resolvedStyle.fontSize;
                        if (size * scale < HudPresenter.TextFloorPx - 0.01f)
                            problems.Add($"{at}: {name} is {size * scale:0.##} px on screen ({size} px at {scale:0.###}; under the {HudPresenter.TextFloorPx} px floor)");
                        if (Mathf.Abs(size - HudPresenter.FlooredTextSize(px, HudPresenter.ScreenPxPerPanelPx(root.panel))) > 0.01f)
                            problems.Add($"{at}: {name} is {size} px, not the stylesheet's {px} px (the desktop sizes stay)");
                    }
                    // Where the floor lifts both (the phone), the name (24 px) and the text (22 px) meet at the floor; it is
                    // never smaller. TheChainReadsAtTheItchEmbedSize holds it larger where neither is floored.
                    if (root.Q("advisor-name").resolvedStyle.fontSize < root.Q("advisor-text").resolvedStyle.fontSize)
                        problems.Add($"{at}: the title is smaller than the text");
                });
        Assert.IsEmpty(problems, string.Join("\n", problems));
    }

    /// <summary>The advisor's text on screen at 1280x720 and larger, in px (22 panel px at 0.67 scale is 14.7).</summary>
    const float GuideTextOnScreenPx = 14f;

    /// <summary>The least clear space between the ring and End turn for them to read as two boxes, in panel px.</summary>
    const float RingClearPx = 4f;

    /// <summary>The clear space between two rects (the larger of the horizontal and vertical gaps; negative when they overlap).</summary>
    static float Gap(Rect a, Rect b) =>
        Mathf.Max(Mathf.Max(a.xMin - b.xMax, b.xMin - a.xMax), Mathf.Max(a.yMin - b.yMax, b.yMin - a.yMax));

    static bool Inside(Rect inner, Rect outer) =>
        inner.xMin >= outer.xMin - 0.5f && inner.xMax <= outer.xMax + 0.5f && inner.yMin >= outer.yMin - 0.5f && inner.yMax <= outer.yMax + 0.5f;

    /// <summary>The real Game.uxml on the real PanelSettings at w x h with the HUD, the opening card and the info panel on a
    /// new game; the advisor opened, and <paramref name="check"/> run at each step once the layout has settled.</summary>
    static IReadOnlyList<VisualElement> Targets(VisualElement root, AdvisorPresenter advisor, EventCardPresenter card, int step)
    {
        var action = advisor.Chain[step].Actions.FirstOrDefault(a => a.ShowMe != "");
        return action == null ? Array.Empty<VisualElement>() : AdvisorTargets.Find(root.Q("root"), action.ShowMe, card);
    }

    static IEnumerator EachStep(int w, int h, bool hebrew, Action<VisualElement, AdvisorPresenter, int, EventCardPresenter> check)
    {
        var text = hebrew ? He : En;
        var rt = new RenderTexture(w, h, 0);
        var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));
        ps.targetTexture = rt;
        var go = new GameObject("AdvisorOpeningTest");
        go.SetActive(false);
        var doc = go.AddComponent<UIDocument>();
        doc.panelSettings = ps;
        doc.visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(UiPaths.GameUxml);
        go.SetActive(true);
        var root = doc.rootVisualElement;
        root.Q("root").RemoveFromClassList("loading");
        if (hebrew) root.Q("root").AddToClassList(UiLanguage.HebrewClass);
        var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), text);
        var hud = new HudPresenter(root, bridge, text);
        var nav = new ScreenNav(root);
        var card = new EventCardPresenter(root, bridge, text, null);
        var (info, advisor) = PanelAndAdvisor(root, bridge, text, null, card, () => nav.Close());
        try
        {
            Assert.IsNotNull(card.Shown, "precondition: a new game opens on a card");
            advisor.OpenChain();
            for (int step = 0; step < Steps.Length; step++)
            {
                if (step > 0) advisor.NextStep();
                for (int i = 0; i < 20 && step > 0 && Targets(root, advisor, card, step).Count == 0; i++) yield return null;
                for (int i = 0; i < 3; i++) yield return null;   // the layout settles
                advisor.Place();
                for (int i = 0; i < 2; i++) yield return null;   // the panel's dock follows the ring's side
                check(root, advisor, step, card);
            }
        }
        finally
        {
            advisor.Dispose(); info.Dispose(); card.Dispose(); nav.Dispose(); hud.Dispose();
            Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt);
        }
    }
}


public class AdvisorOpeningNoAttentionParityTests
{
    // R-GOV-002: both language rebuild directions preserve indices and visible positions.
    [TestCase(false)] [TestCase(true)]
    public void RebuildKeepsEveryVisibleStepAndItsPosition(bool startsHebrew)
    {
        using var rig = new AdvisorTour1688Fixture.Rig(startsHebrew); rig.Advisor.OpenChain();
        for (int n = 0; n < AdvisorTour1688Fixture.Visible.Length; n++)
        {
            var id = AdvisorTour1688Fixture.Visible[n]; Assert.AreEqual(id, rig.Advisor.Shown?.Id);
            var index = rig.Session.Chain.Step; rig.Rebuild(!startsHebrew);
            Assert.AreEqual(id, rig.Advisor.Shown?.Id); Assert.AreEqual(index, rig.Advisor.ChainStep);
            Assert.AreEqual(AdvisorTour1688Fixture.Text(!startsHebrew).Format("advisor.step", n + 1, AdvisorTour1688Fixture.Visible.Length), rig.Ui.Q<Label>("advisor-count").text);
            rig.Rebuild(startsHebrew); Assert.AreEqual(id, rig.Advisor.Shown?.Id); rig.Advisor.NextStep();
        }
        Assert.IsFalse(rig.Advisor.InChain); Assert.AreEqual("player", rig.Selection);
        Assert.IsFalse(rig.Session.Settings.Seen("opening.attention"));
    }

    // R-GOV-002: base has no Back control. Restore earlier retained steps, including
    // the original Attention index; it must route forward without being marked seen.
    [Test]
    public void BackwardRetainedStepRestorationNeverRendersAttention()
    {
        using var rig = new AdvisorTour1688Fixture.Rig(); rig.Advisor.OpenChain();
        for (int i = 0; i < AdvisorTour1688Fixture.PastCities; i++) rig.Advisor.NextStep();
        Assert.AreEqual("opening.move_how", rig.Advisor.Shown?.Id);
        var attention = AdvisorPresenter.ChainIds(rig.Session.Catalog).ToList().IndexOf("opening.attention");
        foreach (var index in new[] { 0, attention, 0, attention + 1 })
        {
            rig.Advisor.Dispose(); rig.Session.Chain.Step = index; rig.Rebuild(false);
            Assert.AreEqual(index < 1 ? "opening.greeting" : "opening.move_how", rig.Advisor.Shown?.Id);
            Assert.IsFalse(rig.Session.Settings.Seen("opening.attention"));
        }
    }

    // R-GOV-002: the event message opens the card, after its visible predecessors.
    [TestCase(false)] [TestCase(true)]
    public void EventMessageOpensTheCardAndResumesAfterBothSyntheticDecisions(bool he)
    {
        using var rig = new AdvisorTour1688Fixture.Rig(he); rig.Advisor.OpenChain();
        foreach (var id in AdvisorTour1688Fixture.Visible.Take(7))
        {
            Assert.AreEqual(id, rig.Advisor.Shown?.Id); Assert.IsTrue(rig.Card.Collapsed); rig.Advisor.NextStep();
        }
        Assert.AreEqual("opening.event", rig.Advisor.Shown?.Id); Assert.IsTrue(rig.Card.Collapsed);
        rig.Ui.Press("advisor-show-me"); Assert.IsFalse(rig.Card.Collapsed); Assert.IsFalse(rig.Advisor.IsOpen);
        rig.Card.Collapse(); Assert.AreEqual("opening.event", rig.Advisor.Shown?.Id); Assert.IsTrue(rig.Advisor.IsOpen);
        rig.Ui.Press("advisor-show-me"); UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());
        Assert.AreEqual("tour_test_b", rig.Card.Shown?.Id); Assert.IsFalse(rig.Advisor.IsOpen);
        UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());
        Assert.IsNull(rig.Card.Shown); Assert.AreEqual("opening.end_turn", rig.Advisor.Shown?.Id);
        Assert.AreEqual(AdvisorTour1688Fixture.Text(he).Format("advisor.step", 9, 9), rig.Ui.Q<Label>("advisor-count").text);
        Assert.IsTrue(rig.Advisor.IsOpen); Assert.AreEqual("player", rig.Selection);
    }

    // R-GOV-002: a missing decision is another skip; requires cannot block the step after it.
    [Test]
    public void NoDecisionSkipsEventWithoutBlockingObjectives()
    {
        using var rig = new AdvisorTour1688Fixture.Rig();
        UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());
        UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());
        rig.Advisor.OpenChain(); var ids = new List<string>();
        while (rig.Advisor.InChain) { ids.Add(rig.Advisor.Shown!.Id); Assert.LessOrEqual(ids.Count, 8); rig.Advisor.NextStep(); }
        CollectionAssert.AreEqual(AdvisorTour1688Fixture.Visible.Where(id => id != "opening.event"), ids);
    }
}


public class AdvisorOpeningEventIdentityTests
{
    // R-GOV-002: shift the catalogue event index with a synthetic, condition-skipped step.
    // A positional EventStep check would lose the event from the total after its card is answered.
    [Test]
    public void EventIdentitySurvivesAnAdditionalSkippedPredecessor()
    {
        var yaml = File.ReadAllText(AdvisorCatalog.Path).Replace("he_review: pending", "he_review: approved")
            .Replace("requires: [opening.move]", "requires: [opening.detour]")
            .Replace("\npages:\n", "\n  - { id: opening.detour, advisor: ben_gurion, signal: opening, level: new, he_review: approved, requires: [opening.move], when: { cards_waiting_at_least: 99 } }\n\npages:\n");
        using var rig = new AdvisorTour1688Fixture.Rig(catalog: AdvisorCatalog.Parse(yaml));
        rig.Advisor.OpenChain();
        for (int i = 0; i < 7; i++) rig.Advisor.NextStep();
        Assert.AreEqual("opening.event", rig.Advisor.Shown?.Id);
        Assert.AreNotEqual(AdvisorPresenter.EventStep, rig.Advisor.ChainStep, "shifted catalogue precondition");
        rig.Ui.Press("advisor-show-me");
        UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());
        UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());
        Assert.AreEqual("opening.end_turn", rig.Advisor.Shown?.Id);
        Assert.AreEqual(AdvisorTour1688Fixture.Text(false).Format("advisor.step", 9, 9), rig.Ui.Q<Label>("advisor-count").text);
        Assert.IsTrue(rig.Advisor.IsOpen);
    }

    // R-GOV-002: close restores the initially collapsed card too.
    [Test]
    public void ClosingEventRestoresTheOriginallyCollapsedCard()
    {
        using var rig = new AdvisorTour1688Fixture.Rig(); rig.Card.Collapse(); rig.Advisor.OpenChain();
        for (int i = 0; i < 7; i++) rig.Advisor.NextStep();
        Assert.AreEqual("opening.event", rig.Advisor.Shown?.Id);
        rig.Ui.Press("advisor-show-me"); Assert.IsFalse(rig.Card.Collapsed);
        rig.Advisor.CloseChain(); Assert.IsTrue(rig.Card.Collapsed);
        Assert.AreEqual("player", rig.Selection);
    }
}


public class AdvisorOpeningNoAttentionGameRootTests
{
    readonly List<string> dirs = new List<string>();
    static VisualElement Ui => Object.FindFirstObjectByType<UIDocument>().rootVisualElement;
    string TempDir()
    {
        var dir = Path.Combine(Path.GetTempPath(), "statecraft-tour1688-root-" + Guid.NewGuid().ToString("N"));
        dirs.Add(dir); return dir;
    }
    [TearDown]
    public void Clean()
    {
        EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
        foreach (var dir in dirs) if (Directory.Exists(dir)) Directory.Delete(dir, true);
        dirs.Clear();
    }

    // R-GOV-002 / #1688 S1: Settings invokes GameRoot.SwitchLanguage -> Begin(keep: Bridge),
    // rebuilding the HUD, localized bridge, card and advisor, not just the advisor presenter.
    // Red-first: base reaches opening.attention instead of the step after it.
    [Test]
    public void SettingsRebuildKeepsGdd03TourIdentityAndRestoresCardAndSelection()
    {
        var inputs = new GameInputs { Content = AdvisorTour1688Fixture.Content(),
            Map = GameInputs.MapFromDisk(Paths.Content, Paths.StreamingMap), Goldens = new GoldenData() };
        var settings = new SettingsStore(TempDir());
        var game = AdvisorGameTests.OpenScene(new SaveStore(TempDir()), settings);
        game.Language = UiLanguage.English;
        game.AdvisorCatalogForTests = AdvisorTour1688Fixture.WithACityStep();
        game.Begin(inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        game.Advisor!.CloseChain();
        Assert.AreEqual(SaveGame.Gdd03Ruleset, game.Bridge!.Content.Ruleset);
        Assert.AreEqual("tour_test_a", game.Card!.Shown?.Id);
        Assert.IsFalse(game.Card.Collapsed, "the card the tour must put back");
        Assert.IsFalse(Ui.Q("root").ClassListContains(UiLanguage.HebrewClass));

        var map = Object.FindFirstObjectByType<MapScene>();
        var city = AdvisorTargets.CityHex(game.Bridge.Content, game.Bridge.State)!.Value;
        var hex = game.Bridge.Content.Hexes.Where(h => h.Land).Select(h => (h.Q, h.R))
            .First(h => h != city && game.Units!.UnitToSelectAt(h.Item1, h.Item2) == null);
        GameRoot.OnHexClicked(map, game.Units!, game.Info!, hex.Item1, hex.Item2);
        Assert.AreEqual(hex, game.Info!.Selected, "the player's selection before the tour");
        game.Advisor.OpenChain(); for (int i = 0; i < 3; i++) game.Advisor.NextStep();
        Assert.AreEqual("opening.cities", game.Advisor.Shown?.Id);
        Assert.AreEqual(city, game.Info.Selected, "the tour temporarily selects its city");
        RoundTrip("opening.cities", 4);
        Assert.AreEqual(city, game.Info!.Selected, "the rebuilt city step selects its city again");
        game.Advisor!.CloseChain();
        Assert.AreEqual(hex, game.Info.Selected, "closing restores the player's selection in the rebuilt info panel");
        Assert.AreEqual("tour_test_a", game.Card!.Shown?.Id);
        Assert.IsFalse(game.Card.Collapsed, "closing restores the previously open card in the rebuilt UI");

        game.Advisor.OpenChain();
        foreach (var id in AdvisorTour1688Fixture.Visible)
        {
            Assert.AreEqual(id, game.Advisor!.Shown?.Id);
            Assert.AreNotEqual("opening.attention", game.Advisor.Shown?.Id);
            Assert.IsFalse(game.AdvisorSession!.Settings.Seen("opening.attention"));
            if (id == "opening.event")
            {
                RoundTrip(id, 8);
                UiTestKit.Press(Ui.Q<Button>("advisor-show-me"));
                Assert.AreEqual("tour_test_a", game.Card!.Shown?.Id);
                Assert.IsFalse(game.Card.Collapsed, "the rebuilt event action opens the same decision by identity");
                game.Card.Collapse();
                Assert.AreEqual(id, game.Advisor!.Shown?.Id);
            }
            game.Advisor.NextStep();
        }
        Assert.IsFalse(game.Advisor!.InChain);
        Assert.AreEqual("tour_test_a", game.Card!.Shown?.Id);
        Assert.IsFalse(game.Card.Collapsed, "finishing restores the initially open decision");
        AssertNoAttention();

        void AssertNoAttention()
        {
            Assert.IsFalse(game.AdvisorSession!.Settings.Seen("opening.attention"));
            Assert.IsFalse(new AdvisorSettings(settings).Seen("opening.attention"), "no shared preference was written");
            Assert.IsFalse(game.AdvisorSession.Queue.History.Any(e => e.Id == "opening.attention"));
        }
        void RoundTrip(string id, int position)
        {
            var session = game.AdvisorSession;
            var sim = game.Bridge!.Session;
            var index = game.Advisor!.ChainStep;
            foreach (var language in new[] { UiLanguage.Hebrew, UiLanguage.English })
            {
                var oldAdvisor = game.Advisor; var oldCard = game.Card; var oldBridge = game.Bridge;
                UiTestKit.Press(Ui.Q<Button>("nav-settings"));
                UiTestKit.Press(Ui.Q<Button>("settings-language-" + language));
                Assert.AreEqual(language == UiLanguage.Hebrew, Ui.Q("root").ClassListContains(UiLanguage.HebrewClass));
                Assert.AreNotSame(oldAdvisor, game.Advisor); Assert.AreNotSame(oldCard, game.Card);
                Assert.AreNotSame(oldBridge, game.Bridge, "Begin rebuilds the localized bridge over the retained game");
                Assert.AreSame(sim, game.Bridge!.Session); Assert.AreSame(session, game.AdvisorSession);
                if (Ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass)) UiTestKit.Press(Ui.Q<Button>("nav-settings"));
                Assert.IsTrue(game.Advisor!.InChain);
                Assert.AreEqual(id, game.Advisor.Shown?.Id); Assert.AreEqual(index, game.Advisor.ChainStep);
                Assert.AreNotEqual("opening.attention", game.Advisor.Shown?.Id);
                Assert.AreEqual(AdvisorTour1688Fixture.Text(language == UiLanguage.Hebrew).Format("advisor.step", position, AdvisorTour1688Fixture.Visible.Length),
                    Ui.Q<Label>("advisor-count").text);
                Assert.AreEqual("tour_test_a", game.Card!.Shown?.Id); Assert.IsTrue(game.Card.Collapsed);
                AssertNoAttention();
            }
        }
    }

    // #1915: a mid-tour language switch rebuilds the UI and resumes the tour; Next keeps the focus, so Enter goes on
    // and its gold focus fill shows. Red-first: the rebuilt presenter resumed the chain without giving Next the focus
    // back, and the button stayed dark for the rest of the tour.
    [UnityTest]
    public IEnumerator SettingsSwitchToHebrewKeepsTheTourNextFocused()
    {
        var inputs = new GameInputs { Content = AdvisorTour1688Fixture.Content(),
            Map = GameInputs.MapFromDisk(Paths.Content, Paths.StreamingMap), Goldens = new GoldenData() };
        var settings = new SettingsStore(TempDir());
        var game = AdvisorGameTests.OpenScene(new SaveStore(TempDir()), settings);
        game.Language = UiLanguage.English;
        game.AdvisorCatalogForTests = AdvisorCatalog.Parse(File.ReadAllText(AdvisorCatalog.Path)
            .Replace("he_review: pending", "he_review: approved"));
        game.Begin(inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        game.Advisor!.OpenChain();
        Assert.IsTrue(game.Advisor.InChain);

        UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        UiTestKit.Press(Ui.Q<Button>("settings-language-" + UiLanguage.Hebrew));
        Assert.IsTrue(Ui.Q("root").ClassListContains(UiLanguage.HebrewClass));
        Assert.IsTrue(game.Advisor!.InChain, "the rebuilt presenter resumes the tour");
        // A quiet EditMode panel runs its styles, layout and scheduler only on player-loop requests (SafeAreaTests), so each
        // wait asks for one; bare yields would leave every scheduled step unrun and both checks below vacuous.
        for (int i = 0; i < 5; i++) { EditorApplication.QueuePlayerLoopUpdate(); yield return null; }
        Assert.IsTrue(Ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass), "Settings reopens over the resumed tour (#1036)");
        Assert.AreSame(Ui.Q<Button>("settings-language-" + UiLanguage.Hebrew), Ui.Q("root").focusController.focusedElement,
            "Next does not steal Settings' focus while the screen is open (#1915)");

        UiTestKit.Press(Ui.Q<Button>("nav-settings"));               // Settings closes over the resumed tour
        var next = Ui.Q<Button>("advisor-got-it");
        for (int i = 0; i < 10 && Ui.Q("root").focusController.focusedElement != next; i++)
        { EditorApplication.QueuePlayerLoopUpdate(); yield return null; }   // the popup comes back and the hand-off runs
        for (int i = 0; i < 3; i++) { EditorApplication.QueuePlayerLoopUpdate(); yield return null; }   // and it stays there
        Assert.IsFalse(Ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass));
        Assert.AreSame(next, Ui.Q("root").focusController.focusedElement,
            "once no screen is open Next has the focus: Enter goes on, and its gold focus fill shows (#1915)");
    }

    // The rest of Agam's #1918 (Refs #1915) Next hand-off, which the test above does not reach: the pending hand-off is
    // dropped when a screen opens again before the popup is back (Hide), when the tour ends (CloseChain) and when a tour
    // opens (OpenChain), so it never moves the focus later or into another tour; and a rebuild with no screen open hands
    // Next the focus at once (TakeFocusFromTheCard).

    (GameRoot game, GameInputs inputs) StartTheTour()
    {
        var inputs = new GameInputs { Content = AdvisorTour1688Fixture.Content(),
            Map = GameInputs.MapFromDisk(Paths.Content, Paths.StreamingMap), Goldens = new GoldenData() };
        var game = AdvisorGameTests.OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));
        game.Language = UiLanguage.English;
        game.AdvisorCatalogForTests = AdvisorCatalog.Parse(File.ReadAllText(AdvisorCatalog.Path)
            .Replace("he_review: pending", "he_review: approved"));
        game.Begin(inputs, 0, startScreen: true);
        UiTestKit.Press(Ui.Q<Button>("start-new-game"));
        game.Advisor!.OpenChain();
        Assert.IsTrue(game.Advisor.InChain, "the tour is open");
        return (game, inputs);
    }

    static Button Next => Ui.Q<Button>("advisor-got-it");
    static Focusable? Focused => Ui.Q("root").focusController.focusedElement;
    static bool ScreenOpen => Ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass);

    /// <summary>A quiet EditMode panel runs its styles, layout and scheduler only on player-loop requests, as above.</summary>
    static IEnumerator Frames(int count)
    {
        for (int i = 0; i < count; i++) { EditorApplication.QueuePlayerLoopUpdate(); yield return null; }
    }

    /// <summary>Records whether Next takes the focus from now on, even for a moment.</summary>
    static Func<bool> WatchNext()
    {
        bool took = false;
        Next.RegisterCallback<FocusInEvent>(_ => took = true);
        return () => took;
    }

    /// <summary>The first half of SettingsSwitchToHebrewKeepsTheTourNextFocused: a mid-tour switch to Hebrew rebuilds the UI,
    /// Settings reopens over the resumed tour, and the rebuilt presenter arms the hand-off for when Settings closes.</summary>
    static IEnumerator ResumeTheTourUnderSettings(GameRoot game)
    {
        UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        UiTestKit.Press(Ui.Q<Button>("settings-language-" + UiLanguage.Hebrew));
        Assert.IsTrue(game.Advisor!.InChain, "the rebuilt presenter resumes the tour");
        yield return Frames(5);
        Assert.IsTrue(ScreenOpen, "Settings reopens over the resumed tour (#1036)");
        Assert.AreSame(Ui.Q<Button>("settings-language-" + UiLanguage.Hebrew), Focused, "Settings keeps its focus while it is open");
    }

    /// <summary>Opens a tour again with the focus moved off Next at once, as a player's Tab would, and starts watching Next.</summary>
    static Func<bool> ReopenTheTourAndTabAway(GameRoot game)
    {
        if (game.Card?.Shown != null && !game.Card.Collapsed) game.Card.Collapse(takeFocus: false);
        game.Advisor!.OpenChain();
        Assert.IsTrue(game.Advisor.InChain, "the tour is open again");
        var endTurn = Ui.Q<Button>("end-turn");
        endTurn.Focus();
        Assert.AreSame(endTurn, Focused, "the player moved the focus off Next");
        return WatchNext();
    }

    /// <summary>Settings opens over the tour and closes again: a cover change that a stale hand-off would act on.</summary>
    static IEnumerator OpenAndCloseSettings(GameRoot game)
    {
        UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        Assert.IsTrue(ScreenOpen);
        yield return Frames(5);
        UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        Assert.IsFalse(ScreenOpen);
        yield return Frames(15);
        Assert.IsTrue(game.Advisor!.InChain, "the tour is back once Settings closes");
    }

    // #1918: Hide drops a hand-off still waiting for Place. Red-first with that clear reverted: the hand-off armed by the
    // first close survives Settings opening again, and Place hands Next the focus when Settings closes the second time.
    [UnityTest]
    public IEnumerator SettingsOpenedAgainBeforeTheTourIsBackCancelsTheNextHandOff()
    {
        var (game, _) = StartTheTour();
        yield return ResumeTheTourUnderSettings(game);
        UiTestKit.Press(Ui.Q<Button>("nav-settings"));               // Settings closes over the resumed tour
        Assert.IsFalse(ScreenOpen);
        Assert.AreNotSame(Next, Focused, "the popup is not placed yet, so the hand-off waits for Place");
        var nextTookTheFocus = WatchNext();
        UiTestKit.Press(Ui.Q<Button>("nav-settings"));               // and opens again before the popup is placed
        Assert.IsTrue(ScreenOpen);
        yield return Frames(5);
        UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        Assert.IsFalse(ScreenOpen);
        yield return Frames(15);
        Assert.IsTrue(game.Advisor!.InChain, "the tour is back once Settings closes");
        Assert.IsFalse(nextTookTheFocus(), "Settings opening again cancelled the pending hand-off: Next does not take the focus later (#1915)");
    }

    // #1918: CloseChain and OpenChain both drop the pending hand-off. Red-first only with both clears reverted (OpenChain
    // drops whatever CloseChain leaves, and every later tour starts there): the hand-off armed for the closed tour moves
    // the focus to the next tour's Next at its first cover change, after the player has moved the focus away.
    [UnityTest]
    public IEnumerator ATourClosedUnderSettingsLeavesTheNextTourNoPendingHandOff()
    {
        var (game, _) = StartTheTour();
        yield return ResumeTheTourUnderSettings(game);
        game.Advisor!.CloseChain();                                   // the tour ends while Settings is still open over it
        Assert.AreEqual(-1, game.AdvisorSession!.Chain.Step, "the tour has ended");
        UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        Assert.IsFalse(ScreenOpen);
        yield return Frames(3);
        var nextTookTheFocus = ReopenTheTourAndTabAway(game);
        yield return OpenAndCloseSettings(game);
        Assert.IsFalse(nextTookTheFocus(), "the closed tour's pending hand-off does not move the focus in the next tour (#1915)");
    }

    // #1918: OpenChain drops a pending hand-off that no CloseChain did. A new game (the top bar's New game:
    // NewGamePresenter.Press replaces the session, then Started calls NewGame) ends the tour without CloseChain.
    // Red-first with OpenChain's clear reverted: the old game's hand-off moves the focus to the reopened tour's Next at
    // its first cover change, after the player has moved the focus away.
    [UnityTest]
    public IEnumerator ANewGameUnderSettingsLeavesTheReopenedTourNoPendingHandOff()
    {
        var (game, _) = StartTheTour();
        yield return ResumeTheTourUnderSettings(game);
        game.Bridge!.Replace(GameSession.New(game.Bridge.Content, GoldenRun.Seed, Mode.Historical));
        game.Advisor!.NewGame();
        Assert.AreEqual(-1, game.AdvisorSession!.Chain.Step, "the new game drops the tour without CloseChain");
        if (ScreenOpen) UiTestKit.Press(Ui.Q<Button>("nav-settings"));
        Assert.IsFalse(ScreenOpen);
        yield return Frames(3);
        var nextTookTheFocus = ReopenTheTourAndTabAway(game);
        yield return OpenAndCloseSettings(game);
        Assert.IsFalse(nextTookTheFocus(), "the old game's pending hand-off does not move the focus in the reopened tour (#1915)");
    }

    // #1918: a rebuild with no screen open (Begin over the same game, as SwitchLanguage runs it, with no screen reopened
    // after it) hands the resumed tour's Next the focus at once. Red-first with TakeFocusFromTheCard reverted from the
    // resume's check: the focus stays where it was before the rebuild.
    [UnityTest]
    public IEnumerator ARebuildWithNoScreenOpenHandsTheResumedTourNextTheFocus()
    {
        var (game, inputs) = StartTheTour();
        yield return Frames(3);
        var endTurn = Ui.Q<Button>("end-turn");
        endTurn.Focus();
        Assert.AreSame(endTurn, Focused, "the focus is off Next before the rebuild");
        game.Begin(inputs, 0, keep: game.Bridge);
        Assert.IsTrue(game.Advisor!.InChain, "the rebuilt presenter resumes the tour");
        Assert.IsFalse(ScreenOpen);
        for (int i = 0; i < 10 && Focused != Next; i++) { EditorApplication.QueuePlayerLoopUpdate(); yield return null; }
        yield return Frames(3);                                        // and it stays there
        Assert.IsFalse(ScreenOpen);
        Assert.AreSame(Next, Focused, "with no screen open the resumed tour's Next has the focus: Enter goes on (#1915)");
    }
}