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.

57.4 KB
Raw Blame
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Statecraft.Game;
using Statecraft.Game.Map;
using Statecraft.Game.Map.Collection;
using Statecraft.Game.Map.Kits;
using Statecraft.Game.Map.Units;
using Statecraft.Sim;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools;

/// <summary>Issue #665: the 1948 Collection (Tal Heuberger, #507) on the army tokens, streamed after the first frame. Every one of
/// the original unit models retains its identity (owner, 17 October 2026, #663: every model is used): the Israeli figures,
/// the Arab Legion and C09 on the ALA, and the 65 mm gun and the jeep where a source puts them. New Syrian and
/// Iraqi figures join their existing formations, the Egyptian figure its force's one token and the ALA infantry the ALA token as its third figure. Until a model
/// arrives, and when its bundle is missing or damaged, an Israeli token keeps its code-built figures (fail open); an Arab
/// force's token is not drawn at all (#740: no IDF figures, no placeholder).</summary>
public class UnitFiguresTests
{
    /// <summary>The legacy loop, whose roster follows the rows' dated windows and the call-up: these tests sweep the war's
    /// tokens by date (the GDD loop draws the sim's units, UnitsFromStateTests).</summary>
    static readonly Content C = SimBridgeGddTests.Legacy;
    static List<UnitEntry> Units => UnitData.Parse(File.ReadAllText(Path.Combine(Paths.Content, "map", UnitData.DefaultFileName)));
    static readonly DateTime May20 = new DateTime(1948, 5, 20), July12 = new DateTime(1948, 7, 12), LastArmistice = new DateTime(1949, 7, 20);

    /// <summary>The models on the existing war roster: the unit models of #665, #336's 1948 soldier (#708; owner, 21 October
    /// 2026: "a third figure on some brigade unit tokens, next to C06 and C07"), drawn as his rigged copy (#2384), H01, the
    /// sandwich armoured car on Givati's token (#1536), and the Syrian and Iraqi figures. C09 retains its baseline ALA role;
    /// the new ALA candidate remains review-only.</summary>
    static readonly string[] WarModels =
    {
        "figure-c04-sabra-pioneer-v3", "figure-c05-pioneer-veteran-v2", "figure-c06-young-recruit-v2", "figure-c07-veteran-fighter-v2",
        "figure-c12f-palmach-scout-v2", "figure-c08-arab-legion-v2",
        "figure-syrian-infantry-1948-v1", "figure-c09-irregular-v2", "figure-iraqi-infantry-1948-v1", "figure-egyptian-infantry-1948-v1",
        "figure-ala-infantry-1948-v1", "figure-lebanese-regular-1948-v1",
        "figure-vehicle-napoleonchik-65mm-1948-v2", "figure-vehicle-willys-jeep-1948-v2",
        "figure-vehicle-sandwich-convoy-1948-v2",
        "figure-soldier-1948-v1-rigged",
    };

    [Test]
    public void SwapAndResidencyClearRefreshBoundsAtTheSameCameraPose()
    {
        var map = BuildMap();
        var prefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
        prefab.transform.position = Vector3.down * 1000000f;
        prefab.transform.localScale = new Vector3(50f, 1f, 1f);
        try
        {
            var layer = UnitLayer.Create(map, Units, C.Hexes, still: true);
            layer.Show(C, StateAt(May20, 0.8));
            var figure = Tokens(map).Find("kiryati").Find("Rifleman0");
            Assert.IsNull(figure.Find(UnitFigures.ModelName), "no original before the actual Swap");
            var owner = figure.GetComponent<UnitFigureResidency>();
            Assert.IsNotNull(owner, "actual per-consumer residency owns the model child");
            int index = layer.Tokens.ToList().FindIndex(p => p.Token.Unit.Id == "kiryati");
            Assert.GreaterOrEqual(index, 0);
            Assert.AreEqual(2, layer.KeepClear[index].Length);
            var camera = Camera.main;
            layer.FaceCamera(camera);
            var before = layer.KeepClear[index][1];
            UnitFigures.Swap(figure, UnitFigures.PioneerVeteran, prefab);
            var model = figure.Find(UnitFigures.ModelName).GetComponentInChildren<MeshRenderer>();
            Assert.IsNotNull(model, "actual Swap instantiates the replacement");
            var tip = model.bounds.center + Vector3.right * model.bounds.extents.x * 0.9f;
            Assert.IsFalse(before.Contains(tip), "replacement extends past the old keep-clear box");
            layer.FaceCamera(camera);
            Assert.IsTrue(layer.KeepClear[index][1].Contains(tip), "same-pose Swap refreshes bounds without a dead cached filter");
            owner.enabled = false;   // actual OnDisable -> Refresh(false) -> Clear
            Assert.IsNull(figure.Find(UnitFigures.ModelName), "actual residency Clear retires the model");
            layer.FaceCamera(camera);
            Assert.IsFalse(layer.KeepClear[index][1].Contains(tip), "same-pose retirement removes the old bounds");
        }
        finally { UnityEngine.Object.DestroyImmediate(map.gameObject); UnityEngine.Object.DestroyImmediate(prefab); }
    }

    [SetUp] public void Fresh() { UnitFigures.Source = null; UnitFigures.Forget(); }
    [TearDown] public void Restore() { UnitFigures.Source = null; UnitFigures.Forget(); }

    static GameState StateAt(DateTime date, double mobilization)
    {
        var s = Simulation.NewGame(C, GoldenRun.Seed, Mode.Historical).Clone();
        s.Date = date; s.Rung = 5; s.Mobilization = mobilization;
        return s;
    }

    static MapScene BuildMap()
    {
        EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
        var map = new GameObject("MapScene").AddComponent<MapScene>();
        map.Configure(Load("Terrain"), Load("SettlementMarker"), Load("HexSelection"), Load("Sea"), Load("MapFrame"));
        Statecraft.Game.Map.Kits.KitPopulation.Use(Statecraft.Game.Map.Kits.KitPopulation.Parse(
            File.ReadAllText(Path.Combine(Paths.Content, "map", Statecraft.Game.Map.Kits.KitPopulation.FileName))));
        map.Build();
        return map;
    }

    static Material Load(string name) => AssetDatabase.LoadAssetAtPath<Material>($"Assets/Materials/{name}.mat");

    static Transform Tokens(MapScene map) => map.Terrain.transform.Find(UnitLayer.RootName).Find("Tokens");

    /// <summary>Every figure and vehicle piece of the drawn tokens (code-built riflemen, an Arab force's figures, vehicles).</summary>
    static List<Transform> Pieces(MapScene map, params string[] prefixes) => Tokens(map).Cast<Transform>()
        .SelectMany(t => t.Cast<Transform>().Where(x => prefixes.Any(x.name.StartsWith))).ToList();

    static GameObject Prefab(string id) => AssetDatabase.LoadAssetAtPath<GameObject>(CollectionCatalog.PrefabPath(id, CollectionDetail.Far));
    static GameObject NearPrefab(string id) => AssetDatabase.LoadAssetAtPath<GameObject>(CollectionCatalog.PrefabPath(id, CollectionDetail.Near));

    /// <summary>The model ids a token draws: its figures', then its vehicles'.</summary>
    static IEnumerable<string> ModelsOf(UnitToken t) =>
        Enumerable.Range(0, t.Figures).Select(i => UnitFigures.FigureFor(t.Unit, i, i < t.Headgear.Count ? t.Headgear[i] : null)).Concat(t.Vehicles);

    /// <summary>The box of a piece's Collection model in the piece's own frame (real metres before FigureScale).</summary>
    static Bounds ModelBox(Transform piece)
    {
        var model = piece.Find(UnitFigures.ModelName);
        Bounds? box = null;
        void Add(Bounds b, Transform mesh)
        {
            var m = piece.worldToLocalMatrix * mesh.localToWorldMatrix;
            for (int c = 0; c < 8; c++)
            {
                var p = m.MultiplyPoint3x4(new Vector3((c & 1) == 0 ? b.min.x : b.max.x, (c & 2) == 0 ? b.min.y : b.max.y, (c & 4) == 0 ? b.min.z : b.max.z));
                if (box is Bounds bb) { bb.Encapsulate(p); box = bb; } else box = new Bounds(p, Vector3.zero);
            }
        }
        foreach (var f in model.GetComponentsInChildren<MeshFilter>(true))
            if (f.sharedMesh != null) Add(f.sharedMesh.bounds, f.transform);
        foreach (var skin in model.GetComponentsInChildren<SkinnedMeshRenderer>(true))
        {
            var posed = new Mesh();
            try
            {
                skin.BakeMesh(posed, true);
                foreach (var vertex in posed.vertices)
                {
                    var point = piece.InverseTransformPoint(skin.transform.TransformPoint(vertex));
                    if (box is Bounds have) { have.Encapsulate(point); box = have; } else box = new Bounds(point, Vector3.zero);
                }
            }
            finally { UnityEngine.Object.DestroyImmediate(posed); }
        }
        Assert.IsTrue(box.HasValue, "the original has static or posed skinned geometry");
        return box!.Value;
    }

    /// <summary>Steps a coroutine (nested enumerators included) by hand, for at most <paramref name="frames"/> frames.</summary>
    static void Step(IEnumerator run, int frames)
    {
        var stack = new Stack<IEnumerator>(); stack.Push(run); int seen = 0;
        while (stack.Count > 0 && seen < frames)
        {
            var top = stack.Peek();
            if (!top.MoveNext()) { stack.Pop(); continue; }
            if (top.Current is IEnumerator nested) stack.Push(nested); else seen++;
        }
    }

    /// <summary>The owner: "make sure we are using all of [his] models" (#663). Over every day of the war, with every model
    /// arrived and the full call-up, the tokens draw exactly their mapped models, each in the Collection's streamed bundles with its far
    /// prefab on main.</summary>
    [Test]
    public void EveryMappedWarModelStandsOnATokenSomeDayOfTheWar()
    {
        var units = Units;
        var used = new HashSet<string>();
        for (var d = C.Rules.Start; d < LastArmistice; d = d.AddDays(1))
            foreach (var t in UnitRoster.Build(units, C.Rules, d, 5, 0.8, id => true).Where(t => t.Unit.Service == UnitData.Army))
                foreach (var id in ModelsOf(t)) { Assert.IsNotNull(id, $"{d:yyyy-MM-dd} {t}: every figure has a model"); used.Add(id); }
        CollectionAssert.AreEquivalent(WarModels, used);
        foreach (var id in WarModels)
        {
            Assert.IsNotNull(CollectionCatalog.BundleOf(id), $"{id}: streamed");
            Assert.IsNotNull(Prefab(id), $"{id}: its far prefab is on main");
        }
    }

    /// <summary>The mapping table (docs/units.md, "The 1948 Collection on the tokens").</summary>
    [Test]
    public void TheTableGivesEachTokenItsFiguresAndVehicles()
    {
        var units = Units.ToDictionary(u => u.Id);
        string[] Trio(string id) => Enumerable.Range(0, 3).Select(i => UnitFigures.FigureFor(units[id], i, (units[id].Headgear ?? UnitData.DefaultHeadgear.ToList())[i])).ToArray();
        // By headgear, as #507 paired them: bush hat C04, steel helmet C06, stocking cap C07.
        CollectionAssert.AreEqual(new[] { UnitFigures.SabraPioneer, UnitFigures.YoungRecruit, UnitFigures.VeteranFighter },
            UnitData.Headgears.Select(h => UnitFigures.ByHeadgear[h]).ToArray());
        // The uniform default's third figure is the pioneer veteran, Harel's the Palmach scout; Yiftach's and Negev's the 1948
        // soldier, in the Palmach's stocking cap, where Negev's sourced third cap is that cap (so its token is C07, C06 and him).
        CollectionAssert.AreEqual(new[] { UnitFigures.SabraPioneer, UnitFigures.YoungRecruit, UnitFigures.PioneerVeteran }, Trio("givati"));
        CollectionAssert.AreEqual(new[] { UnitFigures.SabraPioneer, UnitFigures.YoungRecruit, UnitFigures.PalmachScout }, Trio("harel"));
        CollectionAssert.AreEqual(new[] { UnitFigures.SabraPioneer, UnitFigures.YoungRecruit, UnitFigures.Soldier1948 }, Trio("yiftach"));
        CollectionAssert.AreEqual(new[] { UnitFigures.VeteranFighter, UnitFigures.YoungRecruit, UnitFigures.Soldier1948 }, Trio("negev"));
        CollectionAssert.AreEqual(new[] { "harel", "negev", "yiftach" }, UnitFigures.ThirdFigure.Keys.OrderBy(k => k, StringComparer.Ordinal),
            "the three Palmach brigades (10th, 11th and 12th Brigade (Palmach), their rows' numbers)");
        foreach (var id in UnitFigures.ThirdFigure.Keys) StringAssert.EndsWith("(Palmach)", units[id].Number, id);
        // A third figure whose sourced cap is not the soldier's keeps its headgear's figure.
        var helmeted = new UnitEntry { Id = "negev", Side = UnitData.Israel, Headgear = new List<string> { UnitData.WoolCap, UnitData.SteelHelmet, UnitData.SteelHelmet } };
        Assert.AreEqual(UnitFigures.YoungRecruit, UnitFigures.FigureFor(helmeted, 2, UnitData.SteelHelmet), "a source's steel helmet is never swapped for his cap");
        CollectionAssert.AreEqual(Enumerable.Repeat(UnitFigures.ArabLegion, 3), Trio("arab_legion"));
        CollectionAssert.AreEqual(new[] { UnitFigures.Irregular, UnitFigures.Irregular, UnitFigures.AlaInfantry }, Trio("arab_liberation_army"),
            "two C09 and Tal Heuberger's ALA infantry third (owner, 27 October 2026: \"Third figure (Recommended)\")");
        CollectionAssert.AreEqual(Enumerable.Repeat(UnitFigures.LebaneseRegular, 3), Trio("lebanese_army"),
            "Tal Heuberger's Lebanese regular, three of it (owner, 27 October 2026: \"Map-only token (Recommended)\")");
        // The vehicles from their sourced days.
        CollectionAssert.IsEmpty(UnitFigures.VehiclesAt(units["harel"], new DateTime(1948, 5, 31)));
        CollectionAssert.AreEqual(new[] { UnitFigures.WillysJeep }, UnitFigures.VehiclesAt(units["harel"], new DateTime(1948, 6, 1)), "the Burma Road jeeps");
        CollectionAssert.AreEqual(new[] { UnitFigures.WillysJeep, UnitFigures.Napoleonchik }, UnitFigures.VehiclesAt(units["harel"], July12));
        CollectionAssert.AreEqual(new[] { UnitFigures.WillysJeep }, UnitFigures.VehiclesAt(units["negev"], new DateTime(1948, 6, 2)), "the Negev Beasts' jeeps");
        foreach (var id in new[] { "carmeli", "yiftach" })
        {
            CollectionAssert.IsEmpty(UnitFigures.VehiclesAt(units[id], new DateTime(1948, 7, 10)), id);
            CollectionAssert.AreEqual(new[] { UnitFigures.Napoleonchik }, UnitFigures.VehiclesAt(units[id], new DateTime(1948, 7, 11)), $"{id}: the 11 July distribution");
        }
        // Givati's armoured trucks: the sandwich model from the day of the GPO photograph (12 June 1948), not before; the gun joins on 11 July.
        CollectionAssert.IsEmpty(UnitFigures.VehiclesAt(units["givati"], new DateTime(1948, 6, 11)), "givati: no armoured truck before the photograph's day");
        CollectionAssert.AreEqual(new[] { UnitFigures.SandwichArmouredCar }, UnitFigures.VehiclesAt(units["givati"], new DateTime(1948, 6, 12)), "givati: the armoured unit of 12 June");
        CollectionAssert.AreEqual(new[] { UnitFigures.SandwichArmouredCar, UnitFigures.Napoleonchik }, UnitFigures.VehiclesAt(units["givati"], new DateTime(1948, 7, 11)), "givati: armoured trucks and the 11 July gun");
        CollectionAssert.IsEmpty(UnitFigures.VehiclesAt(units["golani"], July12), "no source puts either with Golani");
        // A token's vehicles change the roster on their day, so the tokens are rebuilt with them.
        var before = UnitRoster.Build(Units, C.Rules, new DateTime(1948, 5, 31), 5, 0.8);
        var after = UnitRoster.Build(Units, C.Rules, new DateTime(1948, 6, 1), 5, 0.8);
        Assert.IsFalse(before.SequenceEqual(after), "Harel's jeep arrives on 1 June 1948");
    }

    /// <summary>The order the stream opens bundles for <paramref name="requests"/> queued at once (CollectionStream.Next): a request
    /// whose bundle comes <see cref="CollectionCatalog.After"/> a group waits behind any queued request of that group.</summary>
    static List<string> ServedOrder(IEnumerable<string> requests)
    {
        bool InGroup(string id, string group) => CollectionCatalog.BundleOf(id) == group
            || CollectionCatalog.Groups.Any(g => g.Bundle == group && Array.IndexOf(g.Ids, id) >= 0);
        var queue = new Queue<string>(requests);
        var served = new List<string>();
        while (queue.Count > 0)
        {
            string next = null;
            for (int n = queue.Count; n > 0 && next == null; n--)
            {
                var id = queue.Dequeue();
                if (!CollectionCatalog.After.TryGetValue(CollectionCatalog.BundleOf(id)!, out var first) || !queue.Any(q => InGroup(q, first)))
                    next = id;
                else queue.Enqueue(id);
            }
            served.Add(next ?? queue.Dequeue());
        }
        return served;
    }

    /// <summary>Every figure on a token stands with its arms down in its far prefab, the one the tokens draw (C05 shipped in
    /// his T-pose: his static meshes are now posed at his walk's first frame, tools/slim_collection.py POSE_CLIP).</summary>
    [Test]
    public void EveryFigureOnATokenStandsWithItsArmsDown()
    {
        foreach (var id in WarModels.Where(i => !i.StartsWith("figure-vehicle-", StringComparison.Ordinal)))
        {
            var go = (GameObject)PrefabUtility.InstantiatePrefab(Prefab(id));
            try
            {
                // Far originals now keep their skin; use the placement's frame-zero operation, not the bind pose.
                var controllers = go.GetComponentsInChildren<Animator>().Select(x => x.runtimeAnimatorController).ToArray();
                if (CollectionCatalog.IsFullQualityGeneratedInfantry(id))
                {
                    foreach (var a in go.GetComponentsInChildren<Animator>()) { a.Rebind(); a.Update(0f); }
                }
                else
                {
                    OriginalModelPose.Sample(go);
                    Assert.IsTrue(go.GetComponentsInChildren<Animator>().All(x => !x.enabled), "far poses do not keep animators running");
                }
                CollectionAssert.AreEqual(controllers, go.GetComponentsInChildren<Animator>().Select(x => x.runtimeAnimatorController));
                var b = WorldBounds(go.transform); // the current rendered pose, not the source animation's culling envelope
                Debug.Log($"UNITS figures {id}: {b.size.x:0.000} x {b.size.z:0.000}, {b.size.y:0.000} tall");
                Assert.Less(Mathf.Max(b.size.x, b.size.z), 0.55f * b.size.y, $"{id}: arms down, not a T-pose (wider than half his height)");
            }
            finally { UnityEngine.Object.DestroyImmediate(go); }
        }
    }

    /// <summary>#2384: the 1948 soldier's rigged copy, the model Yiftach and Negev draw, shares C07's skeleton: the same 67
    /// bones, names and parents, each at C07's rest orientation in the prefab's frame (so C07's takes, bone-local rotations,
    /// play on it unchanged, and it stands and faces as C07 does), with its own idle, walk and fire takes on its controller's
    /// idle, walk and attack states, each as long as C07's. Tal's delivered soldier, unrigged, stays catalogued.</summary>
    [Test]
    public void TheRiggedSoldierSharesC07sSkeletonAndTakes()
    {
        Assert.AreEqual(CollectionCatalog.Soldier1948Rigged, UnitFigures.Soldier1948);
        CollectionAssert.Contains(CollectionCatalog.Animated.ToList(), UnitFigures.Soldier1948);
        CollectionAssert.Contains(CollectionCatalog.Groups.Single(g => g.Bundle == CollectionCatalog.People).Ids, "figure-soldier-1948-v1");
        CollectionAssert.Contains(CollectionCatalog.Groups.Single(g => g.Bundle == CollectionCatalog.People).Ids, UnitFigures.Soldier1948);
        Assert.IsFalse(CollectionCatalog.Animated.Contains("figure-soldier-1948-v1"), "the delivered soldier has no rig");
        var soldier = Prefab(UnitFigures.Soldier1948).GetComponentInChildren<SkinnedMeshRenderer>(true);
        var c07 = Prefab(UnitFigures.VeteranFighter).GetComponentInChildren<SkinnedMeshRenderer>(true);
        Assert.IsNotNull(soldier, "the rigged soldier is skinned");
        Assert.AreEqual(67, c07.bones.Length);
        Assert.AreEqual(67, soldier.bones.Length);
        CollectionAssert.AreEquivalent(c07.bones.Select(b => b.name).ToList(), soldier.bones.Select(b => b.name).ToList());
        foreach (var bone in soldier.bones)
        {
            var twin = c07.bones.Single(b => b.name == bone.name);
            Assert.AreEqual(twin.parent.name, bone.parent.name, bone.name + ": parent");
            Assert.Less(Quaternion.Angle(Rest(soldier, bone.name).rotation, Rest(c07, bone.name).rotation), 0.5f, bone.name + ": C07's rest orientation");
            // No scale along the chain: a weapon parented under the hand bone (UnitWeapons) takes the bone's scale, and an
            // export with its unit factor on the nodes instead of in the data put C07's socket a hundred times out.
            Assert.That(bone.lossyScale.x / soldier.transform.root.lossyScale.x, Is.EqualTo(1f).Within(1e-3f), bone.name + ": at scale 1 under the root, as C07's bones are");
            Assert.That(twin.lossyScale.x / c07.transform.root.lossyScale.x, Is.EqualTo(1f).Within(1e-3f), bone.name + ": C07");
        }
        Vector3 At(SkinnedMeshRenderer skin, string bone) => Rest(skin, bone).MultiplyPoint3x4(Vector3.zero);
        Assert.Greater(Vector3.Dot((At(soldier, "Left_Toes") - At(soldier, "Left_Foot")).normalized, (At(c07, "Left_Toes") - At(c07, "Left_Foot")).normalized), 0.95f, "faces as C07 does");
        Assert.Greater(Vector3.Dot((At(soldier, "Head") - At(soldier, "Hips")).normalized, (At(c07, "Head") - At(c07, "Hips")).normalized), 0.99f, "upright as C07 is");
        var controller = AssetDatabase.LoadAssetAtPath<UnityEditor.Animations.AnimatorController>($"{CollectionCatalog.Folder}/{UnitFigures.Soldier1948}/{UnitFigures.Soldier1948}-near.controller");
        Assert.IsNotNull(controller, "the rigged soldier's controller");
        Assert.AreSame(controller, Prefab(UnitFigures.Soldier1948).GetComponent<Animator>().runtimeAnimatorController);
        Dictionary<string, AnimationClip> States(string id) => AssetDatabase.LoadAssetAtPath<UnityEditor.Animations.AnimatorController>($"{CollectionCatalog.Folder}/{id}/{id}-near.controller")
            .layers[0].stateMachine.states.ToDictionary(s => s.state.name, s => (AnimationClip)s.state.motion);
        var states = States(UnitFigures.Soldier1948); var c07States = States(UnitFigures.VeteranFighter);
        CollectionAssert.AreEquivalent(new[] { UnitFigureMotion.IdleState, UnitFigureMotion.WalkState, UnitFigureMotion.AttackState }, states.Keys.ToList());
        foreach (var (state, take) in new[] { (UnitFigureMotion.IdleState, "idle"), (UnitFigureMotion.WalkState, "walk"), (UnitFigureMotion.AttackState, "fire") })
        {
            Assert.AreEqual("Armature|" + take, states[state].name, state);
            Assert.AreEqual(CollectionCatalog.SourceModelPath(UnitFigures.Soldier1948), AssetDatabase.GetAssetPath(states[state]), state + ": the take is the copy's own");
            Assert.AreEqual(c07States[state].length, states[state].length, 1e-3f, state + ": as long as C07's take");
        }
    }

    /// <summary>A bone's bind pose in the prefab's frame (the mesh node's transforms up to and including the root, times the
    /// inverse bind pose), so two models' rests compare as the token sees them.</summary>
    static Matrix4x4 Rest(SkinnedMeshRenderer skin, string bone)
    {
        int i = Array.FindIndex(skin.bones, b => b.name == bone);
        Assert.GreaterOrEqual(i, 0, bone);
        var root = Matrix4x4.identity;
        for (var at = skin.transform; at != null; at = at.parent) root = Matrix4x4.TRS(at.localPosition, at.localRotation, at.localScale) * root;
        return root * skin.sharedMesh.bindposes[i].inverse;
    }

    /// <summary>The tokens are built with the code-built figures and ask for their models; nothing changes in the frame of
    /// the request. When the models arrive every figure shows its model in its own frame, upright, as tall as the code-built
    /// figure, with the code-built renderer off and the figure where it stood; the vehicles arrive on their bases, side-on
    /// and lower than a figure. A roster rebuild dresses the new pieces at once, without asking again.</summary>
    [Test]
    public void TheFiguresAndVehiclesSwapInWhenTheModelsArrive()
    {
        var map = BuildMap();
        var asked = new List<(string id, CollectionDetail detail, Action<GameObject> done)>();
        UnitFigures.Source = (id, d, done) => asked.Add((id, d, done));
        try
        {
            var layer = UnitLayer.Create(map, Units, C.Hexes, still: true);
            layer.Show(C, StateAt(July12, 0.8));
            var figures = Pieces(map, "Rifleman");
            var vehicles = Pieces(map, "Vehicle");
            Assert.Greater(figures.Count, 10, "the riflemen of 12 July 1948 (not a vacuous pass)");
            CollectionAssert.AreEquivalent(new[] { "carmeli", "givati", "givati", "harel", "harel", "yiftach", "negev", "negev" }, vehicles.Select(v => v.parent.name),
                "the guns of the 11 July distribution, Givati's armoured trucks and the Harel and Negev jeeps");
            Assert.IsTrue(asked.All(a => a.detail == CollectionDetail.Far), "the strategic-zoom mesh");
            var activeModels = UnitRoster.Build(Units, C, StateAt(July12, 0.8), _ => true).SelectMany(ModelsOf).Distinct();
            CollectionAssert.AreEquivalent(activeModels, asked.Select(a => a.id), "each active roster model once; no inactive-force prefetch pin");
            Assert.IsTrue(figures.All(f => !UnitFigures.Dressed(f) && f.GetComponent<MeshRenderer>().enabled), "the code-built figures until the models arrive");
            Assert.IsTrue(vehicles.All(v => v.childCount == 0 && v.GetComponentsInChildren<Renderer>().Length == 0), "no vehicle is drawn before it arrives");
            var before = figures.ToDictionary(f => f, f => (f.position, f.rotation));
            // An Arab force's token waits for its side figure (the Syrian bridgehead, from 8 July 1948; #1270), so that model's
            // arrival changes the roster and rebuilds every piece: the side figures come last, after the in-place checks.
            var sideFigures = asked.Where(a => UnitFigures.BySide.Values.Contains(a.id)).ToList();
            foreach (var (id, _, done) in asked.Except(sideFigures).ToList()) done(Prefab(id));
            float man = UnitBadge.FigureHeightM;
            foreach (var f in figures)
            {
                Assert.IsTrue(UnitFigures.Dressed(f), $"{f.parent.name}/{f.name} shows its model");
                Assert.AreEqual(1, f.Cast<Transform>().Count(c => c.name == UnitFigures.ModelName), $"{f.name}: one model");
                Assert.AreEqual(before[f].position, f.position, $"{f.name}: the figure did not move");
                Assert.AreEqual(before[f].rotation, f.rotation, $"{f.name}: nor turned");
                var holder = f.Find(UnitFigures.ModelName);
                var prefab = Prefab(UnitFigures.FigureFor(Units.Single(u => u.Id == f.parent.name), int.Parse(f.name.Substring(8)),
                    layer.Tokens.Single(t => t.Token.Unit.Id == f.parent.name).Token.Headgear[int.Parse(f.name.Substring(8))]));
                Assert.AreEqual(prefab.transform.localRotation, holder.GetChild(0).localRotation, $"{f.name}: the prefab keeps its own rotation (the FBX axis turn)");
                var b = ModelBox(f); float want = f.GetComponent<MeshFilter>().sharedMesh.bounds.size.y;
                Assert.AreEqual(want, b.size.y, 0.02f * want, $"{f.name}: as tall as the code-built figure");
                Assert.Less(Mathf.Max(b.size.x, b.size.z), 0.55f * b.size.y, $"{f.name}: standing upright, arms down");
                Assert.AreEqual(0f, b.min.y, 0.01f, $"{f.name}: feet on the figure's origin");
            }
            foreach (var v in vehicles)
            {
                Assert.IsTrue(UnitFigures.Dressed(v), $"{v.parent.name}/{v.name} shows its vehicle");
                var b = ModelBox(v);
                Assert.AreEqual(UnitFigures.VehicleLengthShare * man, Mathf.Max(b.size.x, b.size.z), 0.01f, $"{v.parent.name}/{v.name}: sized against the figures");
                Assert.Less(b.size.y, 0.6f * man, $"{v.parent.name}/{v.name}: upright (lower than a man), not on its end");
                Assert.AreEqual(0f, b.min.y, 0.01f, $"{v.parent.name}/{v.name}: on its wheels");
                var plinth = v.parent.Find(UnitPlinth.PlinthName).localScale.x;
                var centre = v.parent.Find(UnitPlinth.PlinthName).position;
                Assert.Less(new Vector2(v.position.x - centre.x, v.position.z - centre.z).magnitude, 0.75f * plinth, $"{v.parent.name}/{v.name}: on its base");
            }
            Assert.IsTrue(map.Terrain.transform.Find(UnitLayer.RootName).TryGetComponent<UnitFigureDetail>(out _),
                "the close-zoom switch stands on the unit layer's root, and goes with it");
            foreach (var (id, _, done) in sideFigures) done(Prefab(id));
            // A roster change rebuilds the tokens: the new pieces are dressed at once, with no new request.
            int n = asked.Count;
            Assert.IsTrue(layer.Show(C, StateAt(July12, 0.3)), "the call-up changes the roster");
            Assert.Greater(Pieces(map, "Rifleman").Count, 5);
            Assert.IsTrue(Pieces(map, "Rifleman", "Vehicle").All(UnitFigures.Dressed), "the rebuilt pieces are dressed in the frame of the rebuild");
            Assert.AreEqual(n, asked.Count, "a model that has arrived is not asked for again");
        }
        finally { UnityEngine.Object.DestroyImmediate(map.gameObject); }
    }

    /// <summary>A figure standing on its own (a showcase figure, by the code-built rifleman), dressed with model
    /// <paramref name="id"/> once its far model is answered.</summary>
    static Transform DressedFigure(Transform root, string name, string id, Vector3 at, List<(string id, CollectionDetail detail, Action<GameObject> done)> asked)
    {
        var piece = UnitShowcase.Piece(root, name, UnitModels.Rifleman(UnitModels.Headgear.SlouchHat, UnitModels.RiflePose.Ready, false, name),
            Load("Terrain"), at, Quaternion.Euler(0f, 30f, 0f), UnitShowcase.FigureScale).transform;
        UnitFigures.Dress(piece, id);
        asked.Single(a => a.id == id && a.detail == CollectionDetail.Far).done(Prefab(id));
        Assert.IsTrue(UnitFigures.Dressed(piece), $"{name}: the far model is on");
        return piece;
    }

    static Renderer[] FarOf(Transform piece) => piece.Find(UnitFigures.ModelName).GetChild(0).GetComponentsInChildren<Renderer>(true);
    static Transform NearOf(Transform piece) => piece.Find(UnitFigures.ModelName).Find(UnitFigureDetail.NearName);

    /// <summary>Issue #665, close zoom (#663: "full detail and the idle clip only at close zoom"): a camera that comes in
    /// below TownModelLayer.CloseEnterM asks for the near model (and not before); once it arrives the figure shows it, skinned
    /// and playing its idle clip, as tall as the far model and on its feet, under the same holder (so the camera's yaw, #644,
    /// turns it too); the far mesh is off. The band's hysteresis holds it to CloseExitM; beyond, the far mesh is back and the
    /// clip stops. Coming close again asks for nothing new.</summary>
    [Test]
    public void AFigureShowsItsSkinnedIdleModelOnlyCloseUp()
    {
        var root = new GameObject("Units");
        var asked = new List<(string id, CollectionDetail detail, Action<GameObject> done)>();
        UnitFigures.Source = (id, detail, done) => asked.Add((id, detail, done));
        var camera = TopDownCamera(root.transform);
        try
        {
            var piece = DressedFigure(root.transform, "Rifleman0", UnitFigures.SabraPioneer, Vector3.zero, asked);
            Assert.IsTrue(root.TryGetComponent<UnitFigureDetail>(out var detail), "the switch stands on the figures' root");
            int Nears() => asked.Count(a => a.detail == CollectionDetail.Near);

            View(detail, camera, TownModelLayer.CloseEnterM + 1f);
            Assert.AreEqual(0, Nears(), "just beyond the switch the near model is not asked for");
            Assert.IsNull(NearOf(piece));
            Assert.IsTrue(FarOf(piece).All(r => !r.forceRenderingOff), "far off: the far mesh draws");

            View(detail, camera, TownModelLayer.CloseEnterM - 1f);
            Assert.AreEqual(1, Nears(), "close: the near model is asked for, once");
            Assert.IsTrue(FarOf(piece).All(r => !r.forceRenderingOff), "the far mesh draws while the near one streams");
            asked.Single(a => a.detail == CollectionDetail.Near).done(NearPrefab(UnitFigures.SabraPioneer));

            var holder = piece.Find(UnitFigures.ModelName);
            var near = NearOf(piece);
            Assert.IsNotNull(near, "the near model stands beside the far one");
            Assert.AreEqual(holder, near.parent, "under the same holder, so the figure's yaw turns it (#644)");
            Assert.AreEqual(NearPrefab(UnitFigures.SabraPioneer).transform.localRotation, near.localRotation, "the prefab keeps its own rotation (the FBX axis turn)");
            Assert.IsTrue(near.GetComponentsInChildren<SkinnedMeshRenderer>(true).Length > 0, "the near model is the skinned one");
            Assert.IsTrue(FarOf(piece).All(r => r.forceRenderingOff), "close: the far mesh is off");
            Assert.IsTrue(near.GetComponentsInChildren<Renderer>(true).All(r => !r.forceRenderingOff), "close: the near model draws");
            var animators = near.GetComponentsInChildren<Animator>(true);
            Assert.Greater(animators.Length, 0, "the near model has its animator");
            Assert.IsTrue(animators.All(a => a.enabled && a.runtimeAnimatorController != null), "close: the idle clip plays");
            // Sized by what Unity draws: the renderers' own world bounds (a skinned mesh's through its bones), not by BoxIn.
            var far = WorldBounds(holder.GetChild(0)); var box = WorldBounds(near);
            Assert.AreEqual(far.size.y, box.size.y, 0.05f * far.size.y, "as tall as the far model");
            Assert.AreEqual(far.min.y, box.min.y, 0.05f * far.size.y, "feet where the far model's are");
            Assert.AreEqual(0f, new Vector2(box.center.x - far.center.x, box.center.z - far.center.z).magnitude, 0.1f * far.size.y, "centred over them");

            View(detail, camera, TownModelLayer.CloseExitM - 1f);
            Assert.IsTrue(FarOf(piece).All(r => r.forceRenderingOff), "the band's hysteresis: still close inside CloseExitM");
            View(detail, camera, TownModelLayer.CloseExitM + 1f);
            Assert.IsTrue(FarOf(piece).All(r => !r.forceRenderingOff), "beyond the band: the far mesh again");
            Assert.IsTrue(near.GetComponentsInChildren<Renderer>(true).All(r => r.forceRenderingOff), "and the near model off");
            Assert.IsTrue(animators.All(a => !a.enabled), "the idle clip stops far off");

            View(detail, camera, TownModelLayer.CloseEnterM - 1f);
            Assert.AreEqual(1, Nears(), "close again: nothing asked for anew");
            Assert.AreEqual(1, piece.Find(UnitFigures.ModelName).Cast<Transform>().Count(c => c.name == UnitFigureDetail.NearName), "one near model");
            Assert.IsTrue(animators.All(a => a.enabled), "the idle clip plays again");
        }
        finally { UnityEngine.Object.DestroyImmediate(camera.gameObject); UnityEngine.Object.DestroyImmediate(root); }
    }

    /// <summary>Close up, a near model that never comes (null: a missing or damaged bundle) leaves the far mesh drawing (fail
    /// open); and C05, the pioneer veteran, has no idle clip (walk and run only), so his near model is never asked for and he
    /// keeps his still far model (owner, 18 October 2026, #665: "Keep its still model"), while the figures beside him swap.
    /// Close up that still model breathes in code (UnitFigureIdle, #2374), and stands still again far off.</summary>
    [Test]
    public void AMissingNearModelKeepsTheFarMeshAndC05KeepsHisStillModel()
    {
        var root = new GameObject("Units");
        var asked = new List<(string id, CollectionDetail detail, Action<GameObject> done)>();
        UnitFigures.Source = (id, detail, done) => asked.Add((id, detail, done));
        var camera = TopDownCamera(root.transform);
        try
        {
            var c04 = DressedFigure(root.transform, "C04", UnitFigures.SabraPioneer, Vector3.zero, asked);
            var c05 = DressedFigure(root.transform, "C05", UnitFigures.PioneerVeteran, Vector3.right, asked);
            var c06 = DressedFigure(root.transform, "C06", UnitFigures.YoungRecruit, Vector3.left, asked);
            Assert.IsNotNull(NearPrefab(UnitFigures.PioneerVeteran), "C05 has a near prefab, so leaving it out is a choice (not a vacuous pass)");
            View(root.GetComponent<UnitFigureDetail>(), camera, TownModelLayer.CloseEnterM - 1f);
            var nears = asked.Where(a => a.detail == CollectionDetail.Near).ToList();
            CollectionAssert.AreEquivalent(new[] { UnitFigures.SabraPioneer, UnitFigures.YoungRecruit }, nears.Select(a => a.id),
                "C05 has no idle clip: his near model is never asked for");
            nears.Single(a => a.id == UnitFigures.SabraPioneer).done(null);
            nears.Single(a => a.id == UnitFigures.YoungRecruit).done(NearPrefab(UnitFigures.YoungRecruit));
            Assert.IsNull(NearOf(c04), "C04's near model did not come");
            Assert.IsTrue(FarOf(c04).All(r => !r.forceRenderingOff), "so C04's far mesh draws (fail open)");
            Assert.IsNull(NearOf(c05), "C05 has no near model");
            Assert.IsTrue(FarOf(c05).All(r => !r.forceRenderingOff), "C05 keeps his still far model close up");
            Assert.IsNotNull(NearOf(c06), "C06 swaps beside him");
            Assert.IsTrue(FarOf(c06).All(r => r.forceRenderingOff));
            var idle = c05.Find(UnitFigures.ModelName).GetChild(0).GetComponent<UnitFigureIdle>();
            Assert.IsNotNull(idle, "C05's held pose has its code-driven idle (#2374), on the model the token draws");
            Assert.IsTrue(idle.enabled, "close up it breathes");
            Assert.IsTrue(c05.GetComponentsInChildren<Animator>(true).All(a => !a.enabled), "with his Animator still off");
            View(root.GetComponent<UnitFigureDetail>(), camera, TownModelLayer.CloseExitM + 1f);
            Assert.IsFalse(idle.enabled, "far off he stands still again");
            Assert.AreEqual(2, asked.Count(a => a.detail == CollectionDetail.Near), "and his near model was never asked for");
        }
        finally { UnityEngine.Object.DestroyImmediate(camera.gameObject); UnityEngine.Object.DestroyImmediate(root); }
    }

    static Bounds WorldBounds(Transform model)
    {
        Bounds? result = null;
        void Add(Vector3 point) { if (result is Bounds b) { b.Encapsulate(point); result = b; } else result = new Bounds(point, Vector3.zero); }
        foreach (var filter in model.GetComponentsInChildren<MeshFilter>(true))
        {
            var box = filter.sharedMesh.bounds;
            for (int i = 0; i < 8; i++) Add(filter.transform.TransformPoint(new Vector3((i & 1) == 0 ? box.min.x : box.max.x, (i & 2) == 0 ? box.min.y : box.max.y, (i & 4) == 0 ? box.min.z : box.max.z)));
        }
        foreach (var skin in model.GetComponentsInChildren<SkinnedMeshRenderer>(true))
        {
            var mesh = new Mesh();
            try { skin.BakeMesh(mesh, true); foreach (var point in mesh.vertices) Add(skin.transform.TransformPoint(point)); }
            finally { UnityEngine.Object.DestroyImmediate(mesh); }
        }
        Assert.IsTrue(result.HasValue, "the original has actual geometry");
        return result!.Value;
    }

    static Camera TopDownCamera(Transform parent)
    {
        var camera = new GameObject("Camera").AddComponent<Camera>();
        camera.transform.SetParent(parent, false);
        camera.transform.rotation = Quaternion.Euler(90f, 0f, 0f);   // looking straight down: the view distance is its height
        return camera;
    }

    static void View(UnitFigureDetail detail, Camera camera, float distanceM)
    {
        camera.transform.position = new Vector3(0f, distanceM, 0f);
        Assert.AreEqual(distanceM, TownModelLayer.ViewDistance(camera), 1f);
        detail.Choose(default, camera);
    }

    /// <summary>The Arab forces are drawn only once their own figure has arrived, three of it on
    /// a base under the neutral pennant, never an IDF figure, and on ground no Israeli force held on the window's first day
    /// (#226, ruling 9). A figure that fails (null: a missing or damaged bundle) draws its force with the plain built-in
    /// figure and warns once (owner, 31 October 2026: "Yes, fallback in v0.3.2").</summary>
    [Test]
    public void TheArabForcesAreDrawnOnlyWithTheirOwnFiguresOnceTheyArrive()
    {
        var map = BuildMap();
        var asked = new Dictionary<string, Action<GameObject>>();
        UnitFigures.Source = (id, d, done) => asked[id] = done;
        try
        {
            var units = Units;
            var layer = UnitLayer.Create(map, units, C.Hexes, still: true);
            layer.Show(C, StateAt(May20, 0.8));
            CollectionAssert.IsSubsetOf(new[] { UnitFigures.ArabLegion, UnitFigures.Irregular }, asked.Keys,
                "each force in this dated legacy roster is asked for");
            Assert.IsFalse(asked.ContainsKey(UnitFigures.SyrianInfantry), "no inactive global prefetch pin");
            Assert.IsFalse(asked.ContainsKey(UnitFigures.IraqiInfantry), "no inactive global prefetch pin");
            Assert.IsFalse(layer.Tokens.Any(t => t.Token.Unit.Side != UnitData.Israel), "no Arab force before its figure arrives");
            LogAssert.Expect(LogType.Warning, new Regex("^UNITS figures: ala's figure " + UnitFigures.Irregular + " did not load"));
            asked[UnitFigures.Irregular](null);   // the ALA's figure fails
            layer.Show(C, StateAt(May20, 0.8));
            CollectionAssert.AreEqual(new[] { "arab_liberation_army" }, layer.Tokens.Where(t => t.Token.Unit.Side != UnitData.Israel).Select(t => t.Token.Unit.Id),
                "a failed figure draws its force with the built-in fallback");
            foreach (var f in Tokens(map).Find("arab_liberation_army").Cast<Transform>().Where(c => c.name.StartsWith("Figure")))
                Assert.IsTrue(f.TryGetComponent<MeshRenderer>(out var mr) && mr.enabled && !UnitFigures.Dressed(f), $"ALA {f.name}: the built-in figure");
            asked[UnitFigures.ArabLegion](Prefab(UnitFigures.ArabLegion));
            var legion = layer.Tokens.Where(t => t.Token.Unit.Side == UnitFlags.Jordan).ToList();
            CollectionAssert.AreEqual(new[] { "arab_legion" }, legion.Select(t => t.Token.Unit.Id), "the Legion, drawn when its figure arrives");
            var go = Tokens(map).Find("arab_legion");
            Assert.AreEqual(0, go.Cast<Transform>().Count(c => c.name.StartsWith("Rifleman")), "no IDF figure");
            var figures = go.Cast<Transform>().Where(c => c.name.StartsWith("Figure")).ToList();
            Assert.AreEqual(3, figures.Count);
            foreach (var f in figures)
            {
                Assert.IsTrue(UnitFigures.Dressed(f), f.name);
                StringAssert.StartsWith(UnitFigures.ArabLegion, f.Find(UnitFigures.ModelName).GetChild(0).name, $"{f.name}: the Legion's own figure");
                var b = ModelBox(f);
                Assert.AreEqual(UnitBadge.FigureHeightM, b.size.y, 0.02f, $"{f.name}: a man's height");
                Assert.Less(Mathf.Max(b.size.x, b.size.z), 0.55f * b.size.y, $"{f.name}: upright");
            }
            var flag = go.Find(UnitPlinth.FlagName);
            Assert.IsNotNull(flag, "a marker on the pole");
            Assert.AreEqual(UnitFlags.MeshName(UnitFlags.Jordan), flag.GetComponent<MeshFilter>().sharedMesh.name, "Transjordan's sourced 1948 flag (#756), not the pennant");
            var cloth = flag.GetComponent<MeshRenderer>().sharedMaterials;
            var want = UnitFlags.Colours(UnitFlags.Jordan);
            Assert.AreEqual(1 + want.Count, cloth.Length, "the pole in the palette, then each cloth colour");
            for (int i = 0; i < want.Count; i++)   // a material's colour comes back through a float round trip
                Assert.Less(((Vector4)(cloth[i + 1].color - want[i])).magnitude, 1e-3f, $"cloth colour {i}");
            Assert.AreSame(Resources.Load<Material>(UnitMaterials.RingResource).shader, cloth[1].shader, "the cloth unlit");
            // On the force's own ground (#226, ruling 9): the spot's hex is not Israeli-held on the window's first day.
            var control = Control1948.Parse(File.ReadAllText(Path.Combine(Paths.Content, "map", Control1948.FileName)));
            foreach (var u in units.Where(u => u.Side != UnitData.Israel && UnitRoster.OnMap(u)))
                foreach (var a in u.Anchors)
                {
                    Assert.IsTrue(layer.Spots.TryGetValue(a, out var spot), $"{u.Id} at {a.Place}: placed");
                    var side = control.At(spot.Q, spot.R, UnitData.Day(a.From)).side;
                    TestContext.WriteLine($"{u.Id} at {a.Place}: hex ({spot.Q},{spot.R}) {side} on {a.From}");
                    Assert.AreNotEqual(Control1948.Side.Israel, side, $"{u.Id} at {a.Place}: not on ground Israel held");
                }
            LogAssert.NoUnexpectedReceived();
        }
        finally { UnityEngine.Object.DestroyImmediate(map.gameObject); }
    }

    [TestCase("syrian_army", UnitFigures.SyrianInfantry)]
    [TestCase("iraqi_expeditionary_force", UnitFigures.IraqiInfantry)]
    public void AFormationRequestsItsOwnModelOnlyWhileItIsPresentInTheSimState(string row, string model)
    {
        var map = BuildMap(); var asked = new Dictionary<string, Action<GameObject>>();
        UnitFigures.Source = (id, detail, done) => asked[id] = done;
        try
        {
            var content = ContentLoader.Load(Paths.Content); Assert.IsTrue(content.IsGdd);
            var force = content.StartingForces.Single(f => f.Row == row);
            int id = content.StartingForces.IndexOf(force);
            var state = Simulation.NewGame(content, GoldenRun.Seed, Mode.Historical);
            Assert.IsTrue(state.Units.TryGetValue(id, out var unit), "real GDD content supplies this existing formation");
            state.Units.Remove(id);
            var layer = UnitLayer.Create(map, Units, content.Hexes, still: true); layer.Show(content, state);
            Assert.IsFalse(asked.ContainsKey(model), "an absent unit has no residency demand");
            // Synthetic remove/restore of the real instance tests residency, not resolver arrival dates or saves.
            state.Units[id] = unit!;
            layer.Show(content, state);
            Assert.IsTrue(asked.ContainsKey(model));
            Assert.IsFalse(layer.Tokens.Any(t => t.Token.Unit.Id == row), "no placeholder before its own figure arrives");
            asked[model](Prefab(model));
            var token = layer.Tokens.Single(t => t.Token.Unit.Id == row);
            Assert.AreEqual(model, token.Token.ArtType);
            var figures = Tokens(map).Find(row).Cast<Transform>().Where(t => t.name.StartsWith("Figure")).ToList();
            Assert.AreEqual(3, figures.Count);
            foreach (var f in figures)
            {
                Assert.IsTrue(UnitFigures.Dressed(f));
                StringAssert.StartsWith(model, f.Find(UnitFigures.ModelName).GetChild(0).name);
                Assert.IsTrue(f.GetComponentInChildren<Animator>(true).enabled, "new generated rigs keep their existing animation");
            }
        }
        finally { UnityEngine.Object.DestroyImmediate(map.gameObject); }
    }

    /// <summary>#2056 (owner, 1 November 2026): tools/web_check.py fails a run without Israeli and enemy tokens on turns 1
    /// and 2. Under ?navlog the layer logs the tokens it draws, per side, whenever they change and on each new turn; the
    /// 31 October build (enemy tokens waiting for a figure never requested) logs enemy=0 until a figure arrives.</summary>
    [Test]
    public void UnderNavlogItLogsTheDrawnTokensOfEachSideWhenTheyChangeAndOnEachTurn()
    {
        var map = BuildMap(); var asked = new Dictionary<string, Action<GameObject>>();
        UnitFigures.Source = (id, detail, done) => asked[id] = done;
        var wasNav = Statecraft.Game.Boot.DiagLog.Nav;
        var logged = new List<string>();
        void Capture(string message, string trace, LogType type) { if (message.Contains(" units drawn ")) logged.Add(message); }
        Application.logMessageReceived += Capture;
        try
        {
            var content = ContentLoader.Load(Paths.Content); Assert.IsTrue(content.IsGdd);
            var state = Simulation.NewGame(content, GoldenRun.Seed, Mode.Historical);
            Statecraft.Game.Boot.DiagLog.Nav = true;
            var layer = UnitLayer.Create(map, Units, content.Hexes, still: true); layer.Show(content, state);
            string Expected(int turn)
            {
                int israel = layer.Tokens.Count(t => t.Token.Unit.Side == UnitData.Israel);
                var sides = layer.Tokens.Where(t => t.Token.Unit.Side != UnitData.Israel).GroupBy(t => t.Token.Unit.Side)
                    .OrderBy(g => g.Key, StringComparer.Ordinal).Select(g => $" {g.Key}={g.Count()}");
                return $"units drawn turn={turn} israel={israel} enemy={layer.Tokens.Count - israel}{string.Concat(sides)}";
            }
            // The first Show can be re-entered from inside its Rebuild (seen: two identical turn=0 lines 1 ms apart); the
            // latest line is what web_check reads.
            Assert.IsNotEmpty(logged);
            StringAssert.IsMatch(@"^NAV f=\d+ t=\d+ units drawn turn=0 israel=[1-9]\d* enemy=0$", logged[^1], "no figure has arrived");
            int first = logged.Count;
            layer.Show(content, state);
            Assert.AreEqual(first, logged.Count, "the same tokens on the same turn log nothing");
            var sideFigures = UnitData.Sides.Where(side => side != UnitData.Israel).Select(UnitFigures.SideFigure)
                .Where(id => id != null && asked.ContainsKey(id)).Distinct().ToList();
            Assert.IsNotEmpty(sideFigures, "the new game's enemy forces ask for their figures");
            foreach (var id in sideFigures) asked[id!](Prefab(id!));
            Assert.Greater(layer.Tokens.Count(t => t.Token.Unit.Side != UnitData.Israel), 0, "precondition: enemy tokens drawn");
            Assert.Greater(logged.Count, 1, "the arrival rebuilt the tokens and logged them");
            StringAssert.EndsWith(Expected(0), logged[^1]);
            var next = state.Clone(); next.Turn = 1;
            layer.Show(content, next);
            StringAssert.EndsWith(Expected(1), logged[^1], "a new turn logs the tokens again, changed or not");
            int count = logged.Count;
            Statecraft.Game.Boot.DiagLog.Nav = false;
            var later = next.Clone(); later.Turn = 2;
            layer.Show(content, later);
            Assert.AreEqual(count, logged.Count, "without ?navlog nothing is logged");
        }
        finally
        {
            Application.logMessageReceived -= Capture; Statecraft.Game.Boot.DiagLog.Nav = wasNav;
            UnityEngine.Object.DestroyImmediate(map.gameObject);
        }
    }

    /// <summary>A model that arrives after its token was rebuilt or destroyed is dropped quietly, and the showcase trio is
    /// dressed by headgear too (C04, C06, C07).</summary>
    [Test]
    public void ALateModelForADestroyedTokenIsDroppedAndTheShowcaseIsDressed()
    {
        var map = BuildMap();
        var asked = new List<(string id, Action<GameObject> done)>();
        UnitFigures.Source = (id, d, done) => asked.Add((id, done));
        try
        {
            var layer = UnitLayer.Create(map, Units, C.Hexes, still: true);
            layer.Show(C, StateAt(July12, 0.8));
            Assert.IsTrue(layer.Show(C, StateAt(July12, 0.3)), "the roster changes before any model arrives");
            Assert.AreEqual(asked.Select(a => a.id).Distinct().Count(), asked.Count, "each model is asked for once, rebuild or not");
            foreach (var (id, done) in asked.ToList()) done(Prefab(id));   // the first figures no longer exist
            LogAssert.NoUnexpectedReceived();
            Assert.IsTrue(Pieces(map, "Rifleman", "Vehicle").All(UnitFigures.Dressed), "the rebuilt pieces wear the models that arrived");
            var showcase = UnitShowcase.Spawn(map, still: true).transform;
            var trio = showcase.Cast<Transform>().Where(t => t.name.StartsWith("Rifleman")).ToList();
            Assert.AreEqual(3, trio.Count);
            Assert.IsTrue(trio.All(UnitFigures.Dressed), "the showcase trio wears the models that have arrived");
            CollectionAssert.AreEqual(new[] { UnitFigures.SabraPioneer, UnitFigures.YoungRecruit, UnitFigures.VeteranFighter },
                trio.Select(t => UnitFigures.ByHeadgear.Values.Single(id => t.Find(UnitFigures.ModelName).GetChild(0).name.StartsWith(id))));
        }
        finally { UnityEngine.Object.DestroyImmediate(map.gameObject); }
    }

    /// <summary>Fail open: with the people and vehicles bundles missing, or damaged, the real stream answers null with one
    /// warning per bundle; every Israeli figure keeps its code-built mesh, drawn, no vehicle is drawn, and each Arab force
    /// stands with the plain built-in figure, its side's failure logged once (owner, 31 October 2026: "Yes, fallback in v0.3.2").</summary>
    [Test]
    public void TheCodeBuiltFiguresStayWhenTheBundleIsMissingOrDamaged()
    {
        foreach (var damaged in new[] { false, true })
        {
            var folder = Path.Combine(Path.GetTempPath(), "statecraft-figures-" + Guid.NewGuid().ToString("N"));
            Directory.CreateDirectory(folder);
            var map = BuildMap();
            try
            {
                if (damaged)
                    foreach (var b in WarModels.Select(CollectionCatalog.BundleOf).Distinct())
                        File.WriteAllBytes(Path.Combine(folder, b), System.Text.Encoding.ASCII.GetBytes("not-a-bundle"));
                UnitFigures.Forget();
                var stream = new CollectionStream(new CollectionStream.BundleSource(folder));
                var requested = new List<string>();
                UnitFigures.Source = (id, d, done) => { requested.Add(id); stream.Enqueue(id, d, done); };
                var warnings = new List<string>();
                void Capture(string message, string _, LogType type) { if (type == LogType.Warning) warnings.Add(message); }
                Application.logMessageReceived += Capture;
                var layer = UnitLayer.Create(map, Units, C.Hexes, still: true);
                layer.Show(C, StateAt(May20, 0.8));
                layer.Show(C, StateAt(July12, 0.8));
                // A generated figure's bundle waits while a people's request is queued (#1763, CollectionCatalog.After), even when
                // its own request was queued first; otherwise the stream serves requests in order.
                Step(stream.Run(), 80);
                var figures = Pieces(map, "Rifleman");
                Assert.Greater(figures.Count, 10, "the riflemen of 12 July 1948 (not a vacuous pass)");
                foreach (var f in figures)
                {
                    Assert.IsFalse(UnitFigures.Dressed(f), $"{(damaged ? "damaged" : "missing")}: {f.name} keeps the code-built figure");
                    Assert.IsTrue(f.GetComponent<MeshRenderer>().enabled, $"{f.name}: drawn");
                }
                Assert.IsTrue(Pieces(map, "Vehicle").All(v => v.GetComponentsInChildren<Renderer>().Length == 0), "no vehicle is drawn");
                layer.Show(C, StateAt(May20, 0.8));
                Step(stream.Run(), 80);   // the forces of 20 May ask again; their failed bundles answer null at once
                // Every bundle served so far, the fallback tokens' third figures' included (the ALA infantry's bundle).
                var expectedBundles = ServedOrder(requested).Select(CollectionCatalog.BundleOf).Distinct().ToList();
                layer.Show(C, StateAt(May20, 0.8));
                Application.logMessageReceived -= Capture;
                var arab = layer.Tokens.Where(t => t.Token.Unit.Side != UnitData.Israel).ToList();
                Assert.IsNotEmpty(arab, "each Arab force of 20 May is drawn with the built-in fallback");
                foreach (var t in arab)
                {
                    var pieces = Tokens(map).Find(t.Token.Unit.Id).Cast<Transform>().Where(c => c.name.StartsWith("Figure")).ToList();
                    Assert.AreEqual(t.Token.Figures, pieces.Count, $"{t.Token.Unit.Id}: its figure count");
                    foreach (var f in pieces)
                        Assert.IsTrue(f.TryGetComponent<MeshRenderer>(out var mr) && mr.enabled && !UnitFigures.Dressed(f), $"{t.Token.Unit.Id} {f.name}: the built-in figure");
                }
                // The stream's warnings (one per bundle, in serving order) and the sides' (one per failed side) interleave;
                // LogAssert matches in order, so each is expected by one pattern and checked exactly here.
                foreach (var b in expectedBundles)
                    Assert.AreEqual(1, warnings.Count(w => Regex.IsMatch(w, "^Collection: bundle " + b + " did not load \\(" + (damaged ? "not an AssetBundle" : "no file"))), $"{b}: one stream warning");
                var sideWarnings = warnings.Where(w => w.StartsWith("UNITS figures: ", StringComparison.Ordinal)).ToList();
                var warnedSides = sideWarnings.Select(w => w.Substring("UNITS figures: ".Length).Split('\'')[0]).ToList();
                CollectionAssert.AllItemsAreUnique(warnedSides, "one warning per failed side");
                CollectionAssert.IsSubsetOf(arab.Select(t => t.Token.Unit.Side).Distinct(), warnedSides, "every drawn fallback side was logged");
                Assert.AreEqual(expectedBundles.Count + sideWarnings.Count, warnings.Count, "no other warning:\n" + string.Join("\n", warnings));
                foreach (var _ in warnings)
                    LogAssert.Expect(LogType.Warning, new Regex("^(Collection: bundle \\S+ did not load|UNITS figures: \\w+'s figure \\S+ did not load)"));
                LogAssert.NoUnexpectedReceived();
            }
            finally { UnityEngine.Object.DestroyImmediate(map.gameObject); Directory.Delete(folder, true); }
        }
    }
}