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.

75.0 KB
Raw Blame
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Statecraft.Game.Map;
using Statecraft.Game.Map.Collection;
using Statecraft.Game.Map.Kits;
using Statecraft.Sim;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;

/// <summary>Issue #663, owner ruling of 17 October 2026: "The nine whole-town models ship (this reverses the 15 Oct 'layout
/// references only' ruling). They stream after the first frame; C89/C93 bounds their original-file residency by camera focus. The ma'abara and the
/// shikun appear only from their sourced 1950s dates." Tal Heuberger's nine whole-town models stand as overview
/// landmarks beside their towns (#507's mapping), streamed through CollectionStream, clear of every kit, and they fail open.</summary>
public class TownLandmarksTests
{
    Action<string, CollectionDetail, Action<GameObject?>> saved = null!;
    readonly Dictionary<string, Camera> geometryCameras = new Dictionary<string, Camera>(StringComparer.Ordinal);
    readonly List<(string id, CollectionDetail detail, Action<GameObject?> done)> asked = new List<(string, CollectionDetail, Action<GameObject?>)>();

    [SetUp]
    public void Seam()
    {
        saved = TownLandmarks.Request;
        asked.Clear(); geometryCameras.Clear();
        TownLandmarks.Request = (id, detail, done) => asked.Add((id, detail, done));
    }

    [TearDown]
    public void Reset()
    {
        TownLandmarks.Request = saved;
        EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
    }

    static readonly string[] Towns =
    {
        "tel-aviv-white-city-v1", "jerusalem-stone-city-v1-3denhance", "haifa-port-city-v1", "moshava-town-v1", "nahalal-moshav-v1",
        "kibbutz-compound-v2", "stone-village-v1", "maabara-camp-v1", "shikun-housing-v1",
        // Tal Heuberger's generated whole towns (#2085, #1288)
        "tiberias-town-v1", "safed-town-v1", "netanya-town-v1", "ramla-town-v1",
    };

    /// <summary>The four major cities (TownModels.IsMajorCity), written out as the tests' own expectation: no whole-town model
    /// (owner, 2 November 2026), their kits from 40 km out (owner, 6 November 2026, "Big blocks, every zoom", which
    /// superseded the 2 November 2026 ruling, "Yes, small buildings at every zoom", that drew their building models at every
    /// zoom) and their building models below the close switch (owner, 7 November 2026, "Tal's models close (Recommended)",
    /// which supersedes "Big blocks, every zoom" below 40 km). TownModelResidencyTests.TheMajorCitiesAreThePlacesOfSixtyThousandOrMoreIn1945 checks the
    /// predicate agrees.</summary>
    internal static readonly string[] BigCities = { "tel_aviv", "jerusalem", "haifa", "jaffa" };

    static MapScene OpenMap()
    {
        EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
        var map = Object.FindFirstObjectByType<MapScene>();
        map.Build();
        ResidencyCall(map.GetComponentInChildren<TownModelLayer>(true), typeof(TownModelLayer), "ConfigureResidency",
            1000f, 2000f, 1000f, 2, "test fixture only; not measured");
        return map;
    }

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

    // Geometry tests explicitly admit the complete fixture catalog; these cameras only check geometry and seats.
    // Production camera isolation/admission is checked independently by ResidencyFixture below.
    void AdmitGeometryTowns(MapScene map, TownLandmarks towns)
    {
        var layer = map.GetComponentInChildren<TownModelLayer>(true);
        var places = (IReadOnlyList<(string id, UnityEngine.Bounds bounds)>)typeof(TownModelLayer).GetField("places", ResidencyFlags)!.GetValue(layer)!;
        Assert.IsNotEmpty(places);
        // TEST ONLY: these geometry/seat checks intentionally cover every original town, not shipping admission.
        // Derive the allowance from this fixture's extent; focused admission is checked by ResidencyFixture below.
        float coverage = Mathf.Max(1f, places.Max(p => p.bounds.max.x) - places.Min(p => p.bounds.min.x)
            + places.Max(p => p.bounds.max.z) - places.Min(p => p.bounds.min.z));
        typeof(TownModelLayer).GetField("EdgeAllowance", ResidencyFlags)!.SetValue(layer,
            new Func<IReadOnlyList<string>, string, bool>((_, __) => true));
        ResidencyCall(layer, typeof(TownModelLayer), "ConfigureResidency", coverage, coverage, coverage, places.Count,
            "TEST ONLY all-town geometry coverage; NOT measured or accepted residency limits");
        foreach (var want in towns.Wanted)
        {
            if (!geometryCameras.TryGetValue(want.Key, out var camera))
                geometryCameras[want.Key] = camera = new GameObject("geometry owner " + want.Key).AddComponent<Camera>();
            var at = places.First(p => p.id == want.Key).bounds.center;
            at.y = 0; // Aim at the same ground plane used by focused admission, not the building bounds' height.
            camera.transform.position = at + new Vector3(0, 40000, -40000); camera.transform.LookAt(at);
            towns.Choose(default, camera);
            Assert.IsTrue((bool)ResidencyCall(layer, typeof(TownModelLayer), "WantsWholeTown", want.Key, camera)!, want.Key);
        }
    }
    void AnswerAll()
    {
        var map = Object.FindFirstObjectByType<MapScene>(); var towns = Object.FindFirstObjectByType<TownLandmarks>();
        AdmitGeometryTowns(map, towns);
        var pending = asked.ToList();
        asked.Clear();
        foreach (var (id, _, done) in pending) done(Far(id));
    }

    /// <summary>Tel Aviv's, Jerusalem's and Haifa's whole-town models stay in the repository and the streamed catalogue, unused
    /// on the map (owner, 2 November 2026, "Yes, small buildings at every zoom" (superseded): Tel Aviv, Jerusalem, Haifa and
    /// Jaffa have no whole-town model, and since 6 November 2026, "Big blocks, every zoom", draw their kits from 40 km out,
    /// their building models below the switch since 7 November 2026, "Tal's models close (Recommended)";
    /// Jaffa never had a whole town of its own, only the stone village). The other six are
    /// placed as #507 mapped them, with #2085's four generated towns.</summary>
    [Test]
    public void EveryTownModelIsUsedAndEachIsInTheStreamedTownsBundle()
    {
        var used = TownLandmarks.ById.Values.Concat(TownLandmarks.ByType.Values).Concat(TownLandmarks.ByAction.Values.Select(v => v.Model)).Distinct();
        var unused = new[] { TownLandmarks.TelAviv, TownLandmarks.Jerusalem3D, TownLandmarks.Haifa };
        CollectionAssert.AreEquivalent(Towns.Except(unused), used, "#507's mapping without Tel Aviv's, Jerusalem's and Haifa's whole towns (owner, 2 November 2026; Jaffa never had one), and #2085's generated towns");
        foreach (var id in Towns) Assert.AreEqual("collection-" + id, CollectionCatalog.BundleOf(id), id + " stays in the streamed catalogue");
        Assert.AreEqual(TownLandmarks.Kibbutz, TownLandmarks.ByType["kibbutz"]);
        Assert.AreEqual(TownLandmarks.StoneVillage, TownLandmarks.ByType["arab_town"], "the other Arab towns keep the stone village");
    }

    [Test]
    public void TheMaabaraAndTheShikunOnlyFromTheirDates()
    {
        Assert.AreEqual(new DateTime(1950, 5, 23), TownLandmarks.MaabaraFrom, "Davar of 23 May 1950 (content/events/maabarot_policy.yaml)");
        Assert.IsNull(TownLandmarks.ModelFor(MapActions.TransitCamp, new DateTime(1950, 5, 22)));
        Assert.AreEqual(TownLandmarks.Maabara, TownLandmarks.ModelFor(MapActions.TransitCamp, new DateTime(1950, 5, 23)));
        // Owner on #917 (17 October 2026): "Use 11 Dec 1949", single-source; the exact first project is a [GAP].
        Assert.AreEqual(new DateTime(1949, 12, 11), TownLandmarks.ShikunFrom);
        Assert.IsNull(TownLandmarks.ModelFor(MapActions.Housing, new DateTime(1949, 12, 10)));
        Assert.AreEqual(TownLandmarks.Shikun, TownLandmarks.ModelFor(MapActions.Housing, new DateTime(1949, 12, 11)));
        Assert.IsNull(TownLandmarks.ModelFor(MapActions.Housing, new DateTime(1949, 11, 15)), "not on the 15 November 1949 turn");
        Assert.AreEqual(TownLandmarks.Shikun, TownLandmarks.ModelFor(MapActions.Housing, new DateTime(1949, 12, 15)), "from the 15 December 1949 turn");
        Assert.IsNull(TownLandmarks.ModelFor("no_such_action", new DateTime(1951, 1, 1)));
    }

    /// <summary>Owner ruling, 2 November 2026: "we had tal's smaller buildings alreaedy for tel aviv, jerusalem and haifa and
    /// jaffa - instead of the huge buildings", confirmed as "Yes, small buildings at every zoom". This replaces the 18 October
    /// "Whole model far, buildings near" for these four, and the 21 October ruling on #708 that #336's second Jerusalem is
    /// Jerusalem's far-zoom model. On every date none of the four has a whole-town model, none is placed, and neither
    /// Jerusalem, nor Tel Aviv's or Haifa's whole town, is asked for. Jaffa's stone village still stands at the other Arab
    /// towns. The owner's ruling of 6 November 2026, "Big blocks, every zoom", superseded the 2 November one for what these
    /// four draw from 40 km out (their kits; below the close switch their building models since 7 November 2026, "Tal's
    /// models close (Recommended)") and keeps them without a whole-town model.</summary>
    [Test]
    public void TheFourCitiesHaveNoWholeTownModelOnAnyDate()
    {
        var content = ContentLoader.Load(Statecraft.Game.Paths.Content);
        var map = OpenMap();
        foreach (var id in BigCities)
            Assert.IsNull(TownLandmarks.ModelFor(map.Settlements.Single(s => s.Id == id)), id + ": no whole-town model");
        Assert.IsNotNull(TownLandmarks.ModelFor(map.Settlements.Single(s => s.Id == "petah_tikva")), "the other towns keep theirs");
        var models = new List<string>();
        foreach (var date in new[] { content.Rules.Start, new DateTime(1949, 4, 15), new DateTime(1951, 12, 15) })
        {
            var t = TownLandmarks.ShowAt(map, date, Array.Empty<Placement>(), content)!;
            models.AddRange(asked.Select(a => a.id));
            foreach (var unused in new[] { TownLandmarks.TelAviv, TownLandmarks.Jerusalem, TownLandmarks.Jerusalem3D, TownLandmarks.Haifa })
                CollectionAssert.DoesNotContain(models, unused, $"{date:d MMM yyyy}: {unused} is never asked for");
            AnswerAll();
            foreach (var id in BigCities)
            {
                Assert.IsFalse(t.Wanted.Any(w => w.Key == id), $"{date:d MMM yyyy}: {id} wants no landmark");
                Assert.IsFalse(t.Seats.ContainsKey(id), $"{date:d MMM yyyy}: {id} has no seat");
            }
        }
        CollectionAssert.Contains(models, TownLandmarks.StoneVillage, "the other Arab towns keep the stone village");
    }

    /// <summary>#1288: "An EditMode test that each of the three towns resolves to its model." Each of Tiberias, Safed and Netanya
    /// (type town, so no ByType row reaches it) resolves by id to Tal Heuberger's generated whole town (#2085), streamed in a
    /// bundle of its own, with the default yaw until a render tunes it; on the map its model stands on the
    /// town's own core.</summary>
    [TestCase("tiberias", CollectionCatalog.TiberiasTown)]
    [TestCase("safed", CollectionCatalog.SafedTown)]
    [TestCase("netanya", CollectionCatalog.NetanyaTown)]
    public void ATownOf1288ResolvesToItsGeneratedWholeTown(string town, string model)
    {
        Assert.AreEqual(model, TownLandmarks.ById[town]);
        Assert.IsTrue(CollectionCatalog.IsGeneratedTown(model), model);
        Assert.AreEqual("collection-" + model, CollectionCatalog.BundleOf(model));
        Assert.AreEqual(200f, TownLandmarks.YawOf(model), "no yaw row yet: the default");
        Assert.IsNotNull(Far(model), CollectionCatalog.PrefabPath(model, CollectionDetail.Far));
        var map = OpenMap();
        var s = map.Settlements.Single(x => x.Id == town);
        Assert.AreEqual("town", s.Type);
        Assert.AreEqual(model, TownLandmarks.ModelFor(s));
        var t = ShowAll(map);
        Assert.AreEqual(model, t.Placed.Single(p => p.Key == town).Model);
        Assert.IsTrue(t.Seats.ContainsKey(town), "on its core");
        CollectionAssert.DoesNotContain(t.Missing, town);
    }

    /// <summary>Owner, 2 November 2026: "Yes, Tal's Ramla (Recommended)" ("Ramla draws Tal's town instead of the stone village"): Ramla (an
    /// arab_town, which ByType would give the stone village) resolves by id to Tal Heuberger's own whole town (#2085), streamed in
    /// a bundle of its own, and it stands on Ramla's core. The other Arab towns keep the stone village.</summary>
    [Test]
    public void RamlaStandsAsTalsOwnTown()
    {
        string model = CollectionCatalog.RamlaTown;
        Assert.AreEqual(model, TownLandmarks.ById["ramla"]);
        Assert.IsTrue(CollectionCatalog.IsGeneratedTown(model), model);
        Assert.AreEqual("collection-" + model, CollectionCatalog.BundleOf(model));
        Assert.IsNotNull(Far(model), CollectionCatalog.PrefabPath(model, CollectionDetail.Far));
        var map = OpenMap();
        var s = map.Settlements.Single(x => x.Id == "ramla");
        Assert.AreEqual("arab_town", s.Type);
        Assert.AreEqual(model, TownLandmarks.ModelFor(s));
        Assert.AreEqual(TownLandmarks.StoneVillage, TownLandmarks.ModelFor(map.Settlements.Single(x => x.Id == "lod")), "only Ramla");
        var t = ShowAll(map);
        Assert.AreEqual(model, t.Placed.Single(p => p.Key == "ramla").Model);
        Assert.IsTrue(t.Seats.ContainsKey("ramla"), "on its core");
        CollectionAssert.DoesNotContain(t.Missing, "ramla");
    }

    /// <summary>The towns with a whole-town model, in the file's order.</summary>
    static List<Settlement> Mapped(MapScene map) => map.Settlements.Where(s => TownLandmarks.ModelFor(s) != null).ToList();

    TownLandmarks ShowAll(MapScene map)
    {
        var c = ContentLoader.Load(Statecraft.Game.Paths.Content);
        var t = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), c)!;
        AnswerAll();
        return t;
    }

    /// <summary>Renders the map's own camera at <paramref name="distanceM"/> over <paramref name="at"/> (the hooks run as
    /// in the player: RenderPipelineManager.beginCameraRendering).</summary>
    static void RenderAt(MapScene map, Vector2 at, float distanceM)
    {
        var cam = Camera.main; var rig = cam.GetComponent<CameraRig>();
        rig.LookAt = new Vector3(at.x, 0f, at.y); rig.Distance = distanceM; rig.Yaw = 0f; rig.Apply();
        RenderSample.Render(cam, 320, 180);
        // Inspect this camera's flags before EndCamera restores them, rather than asserting a stale global flag.
        var towns = map.GetComponentInChildren<TownLandmarks>(true);
        towns.Choose(default, cam);
    }

    /// <summary>Owner ruling, 18 October 2026 (on #917, #663, #659 and #700): the towns are Tal Heuberger's models, "Whole model far,
    /// buildings near". Far off, each mapped town's whole-town model stands ON its town, inside its kit footprint
    /// (MapScene.KitFootprints), as wide as its core (TownLandmarks.CoreOf: where most of its building models stand), not
    /// beside the town at a fixed 6 km.</summary>
    [Test]
    public void EveryTownLandmarkStandsOnItsTownSizedToItsCore()
    {
        var map = OpenMap();
        var t = ShowAll(map);
        CollectionAssert.IsEmpty(t.Missing);
        var footprint = new Dictionary<string, (Vector2 centre, float radius)>();
        foreach (var (centre, radius, id) in map.KitFootprints) if (!footprint.ContainsKey(id)) footprint[id] = (centre, radius);
        var mapped = Mapped(map);
        Assert.AreEqual(21, mapped.Count, "all 25 since #2085 and Ramla, less the four cities (owner, 2 November 2026)");
        foreach (var s in mapped)
        {
            var child = t.transform.Find(s.Id);
            Assert.IsNotNull(child, s.Id + " has its landmark");
            var (centre, radius) = footprint[s.Id];
            var b = WorldBounds(child!.gameObject);
            Assert.Less(new Vector2(b.center.x - centre.x, b.center.z - centre.y).magnitude, radius, $"{s.Id} stands on its town");
            Assert.LessOrEqual(Mathf.Max(b.size.x, b.size.z), 1.12f * 2f * radius, $"{s.Id} is no wider than its footprint ({radius:0} m radius)");
            Assert.IsTrue(t.Seats.TryGetValue(s.Id, out var seat), s.Id);
            Assert.AreEqual(seat.Edge, Mathf.Max(b.size.x, b.size.z), 0.12f * seat.Edge, $"{s.Id} is as wide as its core ({0.5f * seat.Edge:0} m radius)");
        }
    }

    /// <summary>The coordinator for the owner, 18 October 2026: the whole-town model is sized to its town's core, "so Tel Aviv
    /// doesn't cover Jaffa and Haifa doesn't reach into the bay". No town's whole-town model covers another game settlement:
    /// no other settlement's kit footprint centre lies inside its ground rectangle (its world bounds on x/z).</summary>
    [Test]
    public void NoTownLandmarkCoversAnotherSettlement()
    {
        var map = OpenMap();
        var t = ShowAll(map);
        var ids = map.Settlements.Select(s => s.Id).ToHashSet();
        var centres = new Dictionary<string, Vector2>();
        foreach (var (c, _, id) in map.KitFootprints) if (ids.Contains(id) && !centres.ContainsKey(id)) centres[id] = c;
        var bad = new List<string>();
        foreach (var s in Mapped(map))
        {
            var b = WorldBounds(t.transform.Find(s.Id)!.gameObject);
            foreach (var (id, c) in centres)
                if (id != s.Id && c.x > b.min.x && c.x < b.max.x && c.y > b.min.z && c.y < b.max.z) bad.Add($"{s.Id}'s model covers {id}");
        }
        CollectionAssert.IsEmpty(bad);
    }

    /// <summary>No whole-town model reaches far into the sea or a lake: at most TownLandmarks.MaxWaterShare
    /// (a fifth; a coastal town keeps its shore) of the ground under its disc (its largest edge across) is water.</summary>
    [Test]
    public void NoTownLandmarkReachesIntoTheSea()
    {
        var map = OpenMap();
        var t = ShowAll(map);
        var ground = PlacementLayer.CampGround(map);
        var bad = new List<string>(); var lines = new List<string>();
        foreach (var s in Mapped(map))
        {
            var b = WorldBounds(t.transform.Find(s.Id)!.gameObject);
            var c = new Vector2(b.center.x, b.center.z);
            float r = 0.5f * Mathf.Max(b.size.x, b.size.z);
            int n = 0, wet = 0;
            for (int i = 0; i <= 10; i++)
                for (int k = 0; k <= 10; k++)
                {
                    var q = c + new Vector2(-r + 2f * r * i / 10f, -r + 2f * r * k / 10f);
                    if ((q - c).sqrMagnitude > r * r) continue;
                    n++; if (ground.Water(q.x, q.y)) wet++;
                }
            float share = n == 0 ? 0f : (float)wet / n;
            lines.Add($"{s.Id} {share:0.00}");
            if (share > 0.2f + 1e-3f) bad.Add($"{s.Id}: {share:P0} of its model's ground is water");
        }
        TestContext.WriteLine(string.Join(", ", lines));
        CollectionAssert.IsEmpty(bad);
    }

    /// <summary>The swap with #700's building models: the landmarks draw where TownModelLayer draws the kits' far view and
    /// hide where it draws the building models (TownModelLayer.CloseAt: in below CloseEnterM, out above CloseExitM), with
    /// the same hysteresis, at the start view (CameraRig.StartDistance, 31 km), 90 km, the gameplay view (60 km), both sides
    /// of the band and the closest zoom. Over Petah Tikva (where this looked over Tel Aviv before the owner's ruling of
    /// 6 November 2026, "Big blocks, every zoom"; Tel Aviv draws its building models below the switch again since 7 November
    /// 2026, "Tal's models close (Recommended)").</summary>
    [Test]
    public void TheLandmarksSwapWithTheBuildingModelsAtTheThreshold()
    {
        var map = OpenMap();
        var t = ShowAll(map);
        var layer = map.transform.Find(KitLayer.RootName).GetComponentInChildren<TownModelLayer>();
        Assert.IsTrue(layer.HasModels);
        var renderers = t.GetComponentsInChildren<Renderer>(true);
        Assert.Greater(renderers.Length, 0);
        var petah = map.KitFootprints.First(f => f.id == "petah_tikva").centre;
        foreach (var (d, close, why) in new[]
        {
            (CameraRig.StartDistance, true, "the start view (owner, 3 November 2026: open at ~30 km)"),
            (90000f, false, "90 km"), (60000f, false, "the gameplay view"),
            (TownModelLayer.CloseEnterM + 1000f, false, "inside the band, coming in"), (TownModelLayer.CloseEnterM - 1000f, true, "past the threshold"),
            (TownModelLayer.CloseExitM - 2000f, true, "inside the band, going out"), (TownModelLayer.CloseExitM + 1000f, false, "out of the band"),
            (CameraController.MinDistance, true, "the closest zoom"),
        })
        {
            RenderAt(map, petah, d);
            Assert.AreEqual(close, layer.LastClose, $"{d / 1000f} km, {why}: the building models");
            foreach (var renderer in renderers)
            {
                var town = renderer.transform;
                while (town.parent != t.transform) town = town.parent;
                bool admitted = (bool)ResidencyCall(layer, typeof(TownModelLayer), "WantsWholeTown", town.name, Camera.main)!;
                // #2378 (Q15 option a): within the far band a town whose far-off copies are ready draws them instead.
                bool farCopies = !close && (bool)ResidencyCall(layer, typeof(TownModelLayer), "ReadyForTown", town.name, Camera.main)!;
                Assert.AreEqual(!admitted || farCopies, renderer.forceRenderingOff,
                    $"{why}: only this camera's admitted whole town draws, unless its far-off copies stand in");
            }
        }
    }

    /// <summary>C89/C93 scopes the older whole-far/buildings-near ruling by this camera's admitted towns. An original
    /// ready for another camera does not hide this camera's procedural fallback.</summary>
    [Test]
    public void OnlyTheCurrentCamerasReadyTownsSuppressProceduralHouses()
    {
        // Since #2085 every game settlement but the four major cities (no whole-town model since the owner's ruling of 2 November
        // 2026; their kits from 40 km out since 6 November 2026) has a whole-town model, so the test unmaps one town (Netanya)
        // for its run: it stands in for an
        // unrelated plain town, and its kit must keep the fallback.
        var byId = (Dictionary<string, string>)TownLandmarks.ById;
        string unmapped = "netanya", row = byId[unmapped];
        byId.Remove(unmapped);
        try
        {
            var map = OpenMap();
            var t = ShowAll(map);
            var root = map.transform.Find(KitLayer.RootName);
            var mapped = Mapped(map);
            var plain = map.Settlements.Single(s => s.Id == unmapped);
            Assert.IsNull(TownLandmarks.ModelFor(plain), unmapped + " is unmapped for the run");
            var plainKit = root.Find(plain.Id).GetComponent<MeshRenderer>();
            foreach (var (d, close) in new[] { (120000f, false), (60000f, false), (TownModelLayer.CloseEnterM - 1000f, true), (CameraController.MinDistance, true) })
            {
                RenderAt(map, map.KitFootprints.First(f => f.id == "tel_aviv").centre, d);
                foreach (var s in mapped)
                {
                    var layer = root.GetComponentInChildren<TownModelLayer>();
                    bool near = (bool)ResidencyCall(layer, typeof(TownModelLayer), "ReadyForTown", s.Id, Camera.main)!;
                    bool whole = (bool)ResidencyCall(layer, typeof(TownModelLayer), "WantsWholeTown", s.Id, Camera.main)! && t.Seats.ContainsKey(s.Id);
                    bool ready = near || whole;
                    Assert.AreEqual(ready, root.Find(s.Id).GetComponent<MeshRenderer>().forceRenderingOff, s.Id);
                    var closeR = root.Find(s.Id + KitLayer.CloseSuffix)?.GetComponent<MeshRenderer>();
                    Assert.IsNotNull(closeR, s.Id + " has a close mesh to swap");
                    Assert.AreEqual(!ready, closeR!.forceRenderingOff, s.Id);
                }
                var buildingLayer = root.GetComponentInChildren<TownModelLayer>();
                bool plainReady = (bool)ResidencyCall(buildingLayer, typeof(TownModelLayer), "ReadyForTown", plain.Id, Camera.main)!;
                Assert.AreEqual(plainReady, plainKit.forceRenderingOff, "an unrelated plain town keeps fallback");
                // Above the close switch a plain town (no whole-town model, not one of the four cities) takes no far-off
                // copies (#2378, Q15 option a: the cities and the towns with a whole-town model only), so its kit must draw.
                if (!close) Assert.IsFalse(plainReady, $"{d:F0} m: the plain town's building models are not admitted");
            }
        }
        finally { byId[unmapped] = row; }
    }

    /// <summary>The fail-open check: without #700's building models (no model list) nothing would draw a mapped town up
    /// close, so its whole-town model stays at every zoom, and its kit houses still never draw.</summary>
    [UnityTest]
    public IEnumerator WithoutTheBuildingModelsTheWholeTownModelStaysUpClose()
    {
        EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
        var map = Object.FindFirstObjectByType<MapScene>();
        map.ConfigureTownModels(null!);
        LogAssert.Expect(LogType.Error, new Regex("town models: the map scene has no model list"));
        map.Build();
        ResidencyCall(map.GetComponentInChildren<TownModelLayer>(true), typeof(TownModelLayer), "ConfigureResidency",
            1000f, 2000f, 1000f, 2, "test fixture only; not measured");
        yield return null;
        var t = ShowAll(map);
        var root = map.transform.Find(KitLayer.RootName);
        Assert.IsFalse(root.GetComponentInChildren<TownModelLayer>().HasModels);
        // Petah Tikva: Tel Aviv has no whole-town model since the owner's ruling of 2 November 2026.
        RenderAt(map, map.KitFootprints.First(f => f.id == "petah_tikva").centre, CameraController.MinDistance);
        Assert.IsFalse(t.transform.Find("petah_tikva").GetComponentsInChildren<Renderer>(true).All(r => r.forceRenderingOff),
            "the admitted whole town remains visible when individual buildings are unavailable");
        Assert.IsTrue(root.Find("petah_tikva").GetComponent<MeshRenderer>().forceRenderingOff, "Petah Tikva's kit houses still never draw");
    }

    /// <summary>Owner, 18 October 2026: the models "seem to be on their side on the map". Every Collection Far prefab's root
    /// carries the FBX axis conversion (-90 degrees about X; the .fbx.meta files have bakeAxisConversion 0), so a landmark
    /// must keep the prefab's own rotation under its yaw: its rotation is the yaw times the prefab's, and its height on the
    /// map is the model's own height (at the landmark's scale), not its length.</summary>
    [Test]
    public void EveryLandmarkStandsUprightAsItsModelDoes()
    {
        var map = OpenMap();
        var c = ContentLoader.Load(Statecraft.Game.Paths.Content);
        var t = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), c)!;
        AnswerAll();
        Assert.Greater(t.Placed.Count, 0);
        var heights = new Dictionary<string, float>();
        foreach (var (key, model, _) in t.Placed)
        {
            var prefab = Far(model);
            var child = t.transform.Find(key);
            Assert.IsNotNull(child, key);
            var tilt = t.Seats.TryGetValue(key, out var seat) ? Quaternion.FromToRotation(Vector3.up, seat.Normal) : Quaternion.identity;
            var want = t.transform.rotation * tilt * Quaternion.Euler(0f, TownLandmarks.YawOf(model), 0f) * prefab.transform.localRotation;
            Assert.Less(Quaternion.Angle(want, child!.rotation), 0.01f, $"{key} ({model}) keeps its prefab's rotation under its yaw and its seat's tilt");
            if (!heights.TryGetValue(model, out var h))
            {
                // The model as its prefab stands (unparented, its own rotation): a yaw about the vertical keeps this height.
                var probe = Object.Instantiate(prefab);
                try { heights[model] = h = WorldBounds(probe).size.y / probe.transform.localScale.y; }
                finally { Object.DestroyImmediate(probe); }
            }
            // Untilted (the seat's tilt taken off about the base), the landmark is as tall as its model at its scale.
            var keep = child.rotation; child.rotation = Quaternion.Inverse(tilt) * child.rotation;
            float scale = child.localScale.y;
            try { Assert.AreEqual(h * scale, WorldBounds(child.gameObject).size.y, 0.01f * h * scale, $"{key} ({model}) is as tall as its model"); }
            finally { child.rotation = keep; }
        }
    }

    static Bounds WorldBounds(GameObject go)
    {
        var rs = go.GetComponentsInChildren<Renderer>(true);
        var b = rs[0].bounds;
        foreach (var r in rs) b.Encapsulate(r.bounds);
        return b;
    }

    /// <summary>The whole-town model on a slope (the kibbutz model cut into its hill at the closest zoom, on #917): each one
    /// is tilted to the least-squares plane of the ground under its footprint (at most TownLandmarks.MaxTiltDeg) and seated at
    /// its lowest point, so it never floats: no land under its footprint lies below its base plane. Degania Alef stands on a
    /// slope, so its model leans with the ground.</summary>
    [Test]
    public void AWholeTownModelFollowsItsGroundAndNeverFloats()
    {
        var map = OpenMap();
        var t = ShowAll(map);
        var ground = PlacementLayer.CampGround(map);
        var lines = new List<string>();
        foreach (var s in Mapped(map))
        {
            Assert.IsTrue(t.Seats.TryGetValue(s.Id, out var seat), s.Id);
            float tilt = Vector3.Angle(Vector3.up, seat.Normal);
            Assert.LessOrEqual(tilt, TownLandmarks.MaxTiltDeg + 0.01f, s.Id);
            float r = 0.5f * seat.Edge, deepest = 0f;
            for (int i = 0; i <= 10; i++)
                for (int k = 0; k <= 10; k++)
                {
                    var q = seat.Centre + new Vector2(-r + 2f * r * i / 10f, -r + 2f * r * k / 10f);
                    if ((q - seat.Centre).sqrMagnitude > 0.999f * r * r || !ground.Inside(q.x, q.y) || ground.Water(q.x, q.y)) continue;
                    var d = q - seat.Centre;
                    float plane = seat.BaseY - (seat.Normal.x * d.x + seat.Normal.z * d.y) / seat.Normal.y;
                    float above = ground.Y(q.x, q.y) - plane;
                    Assert.GreaterOrEqual(above, -0.5f, $"{s.Id} floats {-above:0.0} m above its ground at {q}");
                    deepest = Mathf.Max(deepest, above);
                }
            lines.Add($"{s.Id} tilt {tilt:0.0} deg, ground up to {deepest:0} m above its base");
        }
        TestContext.WriteLine(string.Join("; ", lines));
        Assert.Greater(Vector3.Angle(Vector3.up, t.Seats["degania_alef"].Normal), 0.5f, "Degania Alef leans with its slope");
    }

    /// <summary>#588 (owner, 18 October 2026, option (a), "Clearing doesn't move the core"): a unit clearing's ground in a
    /// town counts as if it still held plots, so the token's clearing does not pull the whole-town model's core away from
    /// it (Carmeli's in Haifa pushed Haifa's model into the bay). A town's plots on a 100 m grid with a hole cut where a
    /// clearing stands keep the core of the whole grid; without the clearing the core moves off it.</summary>
    [Test]
    public void AUnitClearingDoesNotMoveATownsCore()
    {
        var centre = new Vector2(10000f, 10000f);
        var clearing = (centre: centre + new Vector2(250f, 0f), radius: 200f);
        var all = new List<Vector2>();
        for (int i = -6; i <= 6; i++) for (int k = -6; k <= 6; k++) all.Add(centre + new Vector2(100f * i, 100f * k));
        var holed = all.Where(p => (p - clearing.centre).magnitude >= clearing.radius).ToList();
        Assert.Less(holed.Count, all.Count, "the clearing takes plots");
        var none = new (Vector2, float)[0];
        bool Dry(float x, float z) => false;
        var whole = TownLandmarks.CoreOf(centre, 1500f, all, none, new Vector2[0], Dry);
        var kept = TownLandmarks.CoreOf(centre, 1500f, holed, new[] { clearing }, new Vector2[0], Dry);
        var moved = TownLandmarks.CoreOf(centre, 1500f, holed, none, new Vector2[0], Dry);
        Assert.Greater((moved.centre - whole.centre).magnitude, 10f, "without the clearing's ground the core moves away from it");
        Assert.Less((kept.centre - whole.centre).magnitude, 5f, "with it the core stays where the full town's is");
        // The grid ties at the core's edge, so which edge plots make the share shifts its radius by a few metres.
        Assert.AreEqual(whole.radius, kept.radius, 0.03f * whole.radius, "and keeps its size");
    }

    [Test]
    public void ASpotIsTheFirstFreeOneAndNoneWhenAllAreTaken()
    {
        var town = new Vector2(10000f, 10000f);
        var discs = new List<(Vector2 centre, float radius)> { (town, 1500f), (town + new Vector2(4000f, 4000f), 2500f) };
        var spot = TownLandmarks.Spot(town, 1500f, 2000f, (p, r) => TownLandmarks.Clear(p, r, discs));
        Assert.IsTrue(spot.HasValue);
        Assert.IsTrue(TownLandmarks.Clear(spot!.Value, 2000f, discs), "clear of every disc");
        Assert.Less((spot.Value - town).magnitude, 1500f + 2000f * (TownLandmarks.Rings + 1), "beside the town");
        Assert.IsNull(TownLandmarks.Spot(town, 1500f, 2000f, (p, r) => false));
    }

    /// <summary>Nothing is drawn until the stream answers (it never serves a request in the frame that made it), and only
    /// the strategic zoom's mesh is asked for, once per model.</summary>
    [Test]
    public void TheLandmarksStreamInOncePerModel()
    {
        var map = OpenMap();
        var c = ContentLoader.Load(Statecraft.Game.Paths.Content);
        var t = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), c)!;
        Assert.IsNotNull(t);
        CollectionAssert.IsEmpty(t.Placed, "nothing before the stream answers");
        Assert.AreEqual(0, t.transform.childCount);
        AdmitGeometryTowns(map, t);
        Assert.Greater(asked.Count, 0);
        Assert.IsTrue(asked.All(a => a.detail == CollectionDetail.Far), "the far mesh only");
        Assert.AreEqual(asked.Count, asked.Select(a => a.id).Distinct().Count(), "each model once");
        CollectionAssert.DoesNotContain(asked.Select(a => a.id), TownLandmarks.Maabara, "no ma'abara in 1949");
        CollectionAssert.DoesNotContain(asked.Select(a => a.id), TownLandmarks.Shikun);
        AnswerAll();

        CollectionAssert.IsEmpty(t.Missing, "every wanted landmark found a spot");
        Assert.AreEqual(t.Wanted.Count, t.Placed.Count);
        CollectionAssert.IsEmpty(t.Placed.Where(p => BigCities.Contains(p.Key)), "the four cities draw their kits instead");
        Assert.AreEqual(1, t.Placed.Count(p => p.Key == "petah_tikva" && p.Model == TownLandmarks.Moshava));
        Assert.AreEqual(map.Settlements.Count(s => s.Type == "kibbutz"), t.Placed.Count(p => p.Model == TownLandmarks.Kibbutz));
        // Owner on #917 (17 October 2026): "Keep at all seven" arab_town rows; on 2 November 2026 Ramla took Tal's own town
        // ("Yes, Tal's Ramla (Recommended)") and Jaffa lost its stone village ("Yes, small buildings at every zoom", superseded
        // on 6 November 2026 by "Big blocks, every zoom": Jaffa draws its kit from 40 km out, its building models below the
        // switch since 7 November 2026, "Tal's models close (Recommended)"), so the stone village stands at the other five.
        Assert.AreEqual(7, map.Settlements.Count(s => s.Type == "arab_town"));
        Assert.AreEqual(5, t.Placed.Count(p => p.Model == TownLandmarks.StoneVillage), "the stone village at the five Arab towns but Ramla and Jaffa");
        Assert.AreEqual(1, t.Placed.Count(p => p.Key == "ramla" && p.Model == TownLandmarks.Ramla), "Ramla stands as its own town");
    }

    [Test]
    public void TheMaabaraLandmarkComesWithATransitCampFromItsDate()
    {
        var map = OpenMap();
        var c = ContentLoader.Load(Statecraft.Game.Paths.Content);
        var hex = c.SettlementHexes.Single(s => s.Id == "revivim");
        var camp = new[] { new Placement(MapActions.TransitCamp, hex.Q, hex.R, new DateTime(1950, 2, 1)) };
        var t = TownLandmarks.ShowAt(map, new DateTime(1950, 5, 22), camp, c)!;
        AnswerAll();
        CollectionAssert.DoesNotContain(t.Placed.Select(p => p.Model), TownLandmarks.Maabara, "not before 23 May 1950");
        TownLandmarks.ShowAt(map, new DateTime(1950, 5, 23), camp, c);
        AdmitGeometryTowns(map, t);
        Assert.AreEqual(1, asked.Count(a => a.id == TownLandmarks.Maabara), "asked once it is wanted");
        AnswerAll();
        Assert.AreEqual(1, t.Placed.Count(p => p.Model == TownLandmarks.Maabara && p.Key == $"{MapActions.TransitCamp}_{hex.Q}_{hex.R}"));
    }

    /// <summary>Owner on #917 (17 October 2026): the shikun "Use 11 Dec 1949". A housing project draws its shikun landmark
    /// from the 15 December 1949 turn, the first on or after that date, and not on the 15 November 1949 turn.</summary>
    [Test]
    public void TheShikunLandmarkComesWithAHousingProjectFromTheDecember1949Turn()
    {
        var map = OpenMap();
        var c = ContentLoader.Load(Statecraft.Game.Paths.Content);
        var hex = c.SettlementHexes.Single(s => s.Id == "revivim");
        var housing = new[] { new Placement(MapActions.Housing, hex.Q, hex.R, new DateTime(1949, 11, 15)) };
        var t = TownLandmarks.ShowAt(map, new DateTime(1949, 11, 15), housing, c)!;
        AnswerAll();
        CollectionAssert.DoesNotContain(t.Placed.Select(p => p.Model), TownLandmarks.Shikun, "not on the 15 November 1949 turn");
        CollectionAssert.DoesNotContain(asked.Select(a => a.id), TownLandmarks.Shikun);
        TownLandmarks.ShowAt(map, new DateTime(1949, 12, 15), housing, c);
        AdmitGeometryTowns(map, t);
        Assert.AreEqual(1, asked.Count(a => a.id == TownLandmarks.Shikun), "asked once it is wanted");
        AnswerAll();
        Assert.AreEqual(1, t.Placed.Count(p => p.Model == TownLandmarks.Shikun && p.Key == $"{MapActions.Housing}_{hex.Q}_{hex.R}"));
    }

    [Test]
    public void AModelThatDoesNotStreamWarnsAndTheMapRunsOn()
    {
        var map = OpenMap();
        var c = ContentLoader.Load(Statecraft.Game.Paths.Content);
        var t = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), c)!;
        AdmitGeometryTowns(map, t);
        // The moshava town (Tel Aviv's White City until the owner's ruling of 2 November 2026).
        var moshava = asked.Single(a => a.id == TownLandmarks.Moshava);
        LogAssert.Expect(LogType.Warning, new Regex("^LANDMARKS: the town model moshava-town-v1 did not stream"));
        moshava.done(null);
        Assert.AreEqual(0, t.transform.childCount);
        foreach (var (id, _, done) in asked.Where(a => a.id != TownLandmarks.Moshava).ToList()) done(Far(id));
        Assert.Greater(t.Placed.Count, 0, "the others are drawn");
        CollectionAssert.DoesNotContain(t.Placed.Select(p => p.Key), "petah_tikva");
        CollectionAssert.Contains(t.Missing, "petah_tikva");
    }

    /// <summary>#2099, as #2046 for the camp tents: the edit-mode render callback (the beginCameraRendering handler itself)
    /// never acquires, instantiates or destroys the whole-town landmarks mid-render. A change (here the Towns.Enabled kill
    /// switch) waits for LateUpdate, as in play mode, and once applied the next render finds nothing pending (it asks for no
    /// further player-loop update); a direct Choose (no render) still applies a change at once.</summary>
    [Test]
    public void AnEditModeRenderCallbackDefersTheWholeTownsToLateUpdate()
    {
        Assert.IsFalse(Application.isPlaying, "the edit-mode render path");
        var map = OpenMap();
        // Made after OpenScene(Single), which destroys every object of the scene it replaces.
        var prefab = GameObject.CreatePrimitive(PrimitiveType.Cube); prefab.name = "fake original town";
        var camera = new GameObject("deferred whole-town camera").AddComponent<Camera>();
        try
        {
            TownLandmarks.Request = (id, detail, done) => { asked.Add((id, detail, done)); done(prefab); };
            var t = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), ContentLoader.Load(Statecraft.Game.Paths.Content))!;
            Assert.Greater(t.Placed.Count, 0, "precondition: the whole towns stand");
            var first = t.transform.GetChild(0).gameObject;
            camera.transform.SetPositionAndRotation(new Vector3(0f, 60000f, 0f), Quaternion.Euler(90f, 0f, 0f));
            Render(t, camera);
            Assert.IsFalse(t.RefreshPending, "precondition: settled, a render finds nothing pending");
            var enabled = typeof(ProductionResidency.TownPolicy).GetField("Enabled", ResidencyFlags)!;

            enabled.SetValue(map.Residency.Towns, false);
            Render(t, camera);
            Assert.IsTrue(first != null, "nothing is destroyed inside the render callback");
            Assert.Greater(t.Placed.Count, 0, "the whole towns still stand");
            Assert.IsTrue(t.RefreshPending, "the change waits for LateUpdate");
            ResidencyCall(t, typeof(TownLandmarks), "LateUpdate");
            Assert.IsTrue(first == null, "LateUpdate retires the whole towns");
            Assert.AreEqual(0, t.Placed.Count, "and none stands");
            Render(t, camera);
            Assert.IsFalse(t.RefreshPending, "once applied, the next render finds nothing pending");

            enabled.SetValue(map.Residency.Towns, true); int before = asked.Count;
            t.Choose(default, camera);
            Assert.Greater(asked.Count, before, "a direct call (no render) requests at once");
            Assert.Greater(t.Placed.Count, 0, "and places them at once");
            Render(t, camera);
            Assert.IsFalse(t.RefreshPending, "and leaves nothing pending");
        }
        finally { Object.DestroyImmediate(camera.gameObject); Object.DestroyImmediate(prefab); }
    }

    /// <summary>#2099: a model that does not stream (its request answers null) is still a settled state: render, LateUpdate and
    /// render again find nothing pending, so an edit-mode render never asks for a player-loop update on every frame.</summary>
    [Test]
    public void AMissingWholeTownLeavesNothingPendingAfterARender()
    {
        var map = OpenMap();
        var camera = new GameObject("missing whole-town camera").AddComponent<Camera>();
        try
        {
            TownLandmarks.Request = (id, detail, done) => { asked.Add((id, detail, done)); done(null); };
            var t = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), ContentLoader.Load(Statecraft.Game.Paths.Content))!;
            Assert.Greater(asked.Count, 0, "precondition: the models were asked for");
            Assert.AreEqual(0, t.Placed.Count, "precondition: none streamed");
            CollectionAssert.IsNotEmpty(t.Missing, "precondition: the towns are listed as not drawn");
            camera.transform.SetPositionAndRotation(new Vector3(0f, 60000f, 0f), Quaternion.Euler(90f, 0f, 0f));
            Render(t, camera);
            Assert.IsFalse(t.RefreshPending, "a render finds nothing pending");
            ResidencyCall(t, typeof(TownLandmarks), "LateUpdate");
            int count = asked.Count;
            Render(t, camera);
            Assert.IsFalse(t.RefreshPending, "nor after LateUpdate");
            Assert.AreEqual(count, asked.Count, "and nothing is asked for again");
        }
        finally { Object.DestroyImmediate(camera.gameObject); }
    }

    [Test]
    public void ASettledWholeTownTickAllocatesNothing()
    {
        var map = OpenMap();
        TownLandmarks.Request = (id, detail, done) => { asked.Add((id, detail, done)); done(null); };
        var t = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), ContentLoader.Load(Statecraft.Game.Paths.Content))!;
        Assert.IsFalse(t.RefreshPending, "precondition: the layer is settled");
        // LateUpdate is private; invoking it reflectively allocates the argument array/result that this assertion must exclude.
        // Tick is LateUpdate's allocation-free production seam, while the neighbouring residency tests cover their wiring.
        t.Tick();
        Assert.That(() => { for (int i = 0; i < 8; i++) t.Tick(); }, UnityEngine.TestTools.Constraints.ConstraintExtensions.AllocatingGCMemory(Is.Not));
        Assert.IsFalse(t.RefreshPending, "settled ticks leave no work pending");
    }

    static void Render(TownLandmarks t, Camera camera) => ResidencyCall(t, typeof(TownLandmarks), "OnBeginCameraRendering",
        default(UnityEngine.Rendering.ScriptableRenderContext), camera);

    const System.Reflection.BindingFlags ResidencyFlags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
    static object? ResidencyCall(object? owner, Type type, string method, params object?[] args)
    {
        var found = type.GetMethod(method, ResidencyFlags);
        Assert.IsNotNull(found, "focused residency must implement " + method);
        return found!.Invoke(owner, args);
    }
    sealed class ResidencySource : CollectionStream.ISource
    {
        public GameObject? Prefab;
        public int Opens, Unloads;
        public Action? BeforeUnload;
        public IEnumerator Open(string bundle, Action<string?> done) { Opens++; yield return null; done(null); }
        public IEnumerator Load(string bundle, string path, Action<GameObject?> done) { yield return null; done(Prefab); }
        public void Unload(string bundle) { BeforeUnload?.Invoke(); Unloads++; }
    }
    static void ResidencyPump(IEnumerator run, int frames = 20)
    {
        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();
            else if (top.Current is IEnumerator child) stack.Push(child);
            else seen++;
        }
    }
    sealed class ResidencyFixture : IDisposable
    {
        public readonly MapScene map;
        public readonly TownLandmarks towns;
        public readonly TownModelLayer buildings;
        public readonly Camera camera;
        public readonly TownLandmarks.Want focus;
        public readonly ResidencySource source;
        public readonly CollectionStream stream;
        public readonly List<string> acquired = new List<string>();
        public readonly List<Action<GameObject?>> callbacks = new List<Action<GameObject?>>();
        readonly List<GameObject> observedInstances = new List<GameObject>();
        readonly List<Renderer> observedRenderers = new List<Renderer>();
        readonly IEnumerator run;
        readonly System.Reflection.FieldInfo acquireField;
        readonly object? oldAcquire;
        readonly GameObject prefab;
        readonly Action<string, CollectionDetail, Action<GameObject?>> oldRequest;
        /// <param name="approved">The shipping town policy (ProductionResidency.Approved(): in the footprint only, view band
        /// 120/160 km) instead of the fixture's 1000/2000/1000/2 spatial values, and a focus admitted under it.</param>
        public ResidencyFixture(Action<string, CollectionDetail, Action<GameObject?>> defaultRequest, bool approved = false)
        {
            acquireField = typeof(TownLandmarks).GetField("Acquire", ResidencyFlags)!;
            Assert.IsNotNull(acquireField, "whole-town requests must use acquired ownership");
            oldAcquire = acquireField.GetValue(null); oldRequest = TownLandmarks.Request;
            map = OpenMap(); buildings = map.GetComponentInChildren<TownModelLayer>(true);
            var policy = ProductionResidency.Approved().Towns;
            if (approved) buildings.ConfigureProduction(policy);
            prefab = GameObject.CreatePrimitive(PrimitiveType.Cube); prefab.name = "fake original town";
            source = new ResidencySource { Prefab = prefab }; stream = new CollectionStream(source); run = stream.Run();
            TownLandmarks.Request = defaultRequest;
            acquireField.SetValue(null, new Func<string, CollectionDetail, Action<GameObject?>, IDisposable>((id, detail, answer) =>
            {
                acquired.Add(id); callbacks.Add(answer);
                var retain = typeof(CollectionStream).GetMethod("Retain", ResidencyFlags);
                Assert.IsNotNull(retain);
                return (IDisposable)retain!.Invoke(stream, new object[] { id, detail, answer })!;
            }));
            var content = ContentLoader.Load(Statecraft.Game.Paths.Content);
            towns = TownLandmarks.ShowAt(map, new DateTime(1949, 1, 1), Array.Empty<Placement>(), content)!;
            // Map.Build -> KitLayer.Build -> TownModelLayer.Init, including real explicit town/renderer identity wiring.
            var places = (IReadOnlyList<(string id, UnityEngine.Bounds bounds)>)typeof(TownModelLayer).GetField("places", ResidencyFlags)!.GetValue(buildings)!;
            var chosen = towns.Wanted.Where(w => w.OnTown && buildings.CountFor(w.Key) > 0).OrderBy(w => w.Key, StringComparer.Ordinal).First(w =>
            {
                var ids = approved
                    ? TownModelLayer.FocusedTowns(places, new Vector3(w.At.x, 0, w.At.y), null, null, policy.Enter, policy.Exit, policy.Edge, policy.MaxEdges)
                    : (string[])ResidencyCall(null, typeof(TownModelLayer), "FocusedTowns", places,
                        new Vector3(w.At.x, 0, w.At.y), null, null, 1000f, 2000f, 1000f, 2)!;
                return ids.Length > 0 && ids[0] == w.Key;
            });
            focus = chosen;
            var go = new GameObject("residency camera"); camera = go.AddComponent<Camera>();
        }
        public void Choose(bool close = false, bool outside = false)
        {
            var at = outside ? new Vector2(-10000000, -10000000) : focus.At;
            float height = close ? 10000 : 40000; // view-ray distance 14 km or 56 km, inside the original visibility gate
            camera.transform.position = new Vector3(at.x, height, at.y - height);
            camera.transform.LookAt(new Vector3(at.x, 0, at.y));
            towns.Choose(default, camera);
        }
        public void Arrive()
        {
            // The stream opens one bundle and loads one prefab a frame, one model after another: enough frames for every
            // whole town the map wants (eleven since #2085), with the old 20 as the floor.
            ResidencyPump(run, Math.Max(20, 2 + 2 * towns.Wanted.Select(w => w.Model).Distinct().Count()));
            var instance = towns.transform.Find(focus.Key);
            Assert.IsNotNull(instance, "capture a real instance before testing teardown");
            observedInstances.Add(instance.gameObject); observedRenderers.AddRange(instance.GetComponentsInChildren<Renderer>(true));
            Assert.IsNotEmpty(observedRenderers);
        }
        public void AssertConsumerCleared()
        {
            Assert.IsNotEmpty(observedInstances); Assert.IsNotEmpty(observedRenderers);
            foreach (var instance in observedInstances) Assert.IsTrue(instance == null, "detached instances must actually be destroyed");
            foreach (var renderer in observedRenderers) Assert.IsTrue(renderer == null, "a detached renderer is still a consumer");
            Assert.IsNull(towns.transform.Find(focus.Key), "instances retire before Unload(true)");
            Assert.IsFalse(towns.Seats.ContainsKey(focus.Key));
            var cached = (System.Collections.IDictionary)typeof(TownLandmarks).GetField("prefabs", ResidencyFlags)!.GetValue(towns)!;
            Assert.IsFalse(cached.Contains(focus.Model), "consumer prefab reference retires before source unload");
        }
        public void Dispose()
        {
            Object.DestroyImmediate(towns.gameObject); Object.DestroyImmediate(camera.gameObject);
            acquireField.SetValue(null, oldAcquire); TownLandmarks.Request = oldRequest;
            Object.DestroyImmediate(prefab);
        }
    }

    /// <summary>The still-applicable part of the owner's 18 October 2026 ruling ("no procedural houses in a mapped town at any
    /// zoom", the deleted AMappedTownDrawsNoProceduralHousesAtAnyZoom) under focused residency (#1273), across both
    /// hysteresis bands, inward and outward: the town view band (in at 120 km, out at 160 km) and the close band (buildings in
    /// below 40 km, out above 45 km). Once the town the camera is on has its original, its kit houses never draw; outside the
    /// view band (125 km on the way in, 165 km on the way out) the procedural fallback draws again. It runs under the shipping
    /// policy (ProductionResidency.Approved()), and each step also checks which original stands in: the building models
    /// below the close band, their far-off copies within the far band (#2378, owner, 3 November 2026, "Cities/towns only
    /// (Recommended)": in below 100 km, out above 112 km), the whole-town model beyond it.</summary>
    [Test]
    public void TheFocusedMappedTownDrawsNoProceduralHousesAtAnyZoom()
    {
        using (var f = new ResidencyFixture(saved, approved: true))
        {
            Assert.AreEqual(ProductionResidency.Approved().Towns.Evidence, f.buildings.SpatialPolicyEvidence, "the approved policy, not the fixture's values");
            var identity = (IDictionary<Renderer, string>)typeof(TownModelLayer).GetField("swapIds", ResidencyFlags)!.GetValue(f.buildings)!;
            var kit = identity.Where(p => p.Value == f.focus.Key).Select(p => p.Key).ToList();
            Assert.IsNotEmpty(kit, f.focus.Key + " has kit houses to hide");
            var root = f.map.transform.Find(KitLayer.RootName);
            var closeR = root.Find(f.focus.Key + KitLayer.CloseSuffix)?.GetComponent<MeshRenderer>();
            Assert.IsNotNull(closeR, f.focus.Key + " has a close mesh to swap");
            int arrived = 0;
            // original: an original stands in (kit houses hidden); buildings: it is the building models (else the whole town).
            var steps = new (float d, bool original, bool buildings, string why)[]
            {
                // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
                // The whole-town original therefore stands in beyond the 120/160 km view band too.
                (125000f, true, false, "in: outside the 120 km view band, map-wide whole town"), (119000f, true, false, "in: inside the view band"),
                (60000f, true, true, "in: the gameplay view, far-off copies"), (44000f, true, true, "in: above the 40 km close threshold, far-off copies"),
                (39000f, true, true, "in: below 40 km, building models"), (CameraController.MinDistance, true, true, "in: the closest zoom"),
                (44000f, true, true, "out: below the 45 km close exit, still buildings"), (46000f, true, true, "out: above 45 km, far-off copies"),
                (150000f, true, false, "out: inside the 160 km view exit"), (165000f, true, false, "out: beyond 160 km, map-wide whole town"),
            };
            foreach (var (d, original, buildings, why) in steps)
            {
                float h = d / Mathf.Sqrt(2f);   // the view ray at 45 degrees: distance d to the ground
                f.camera.transform.position = new Vector3(f.focus.At.x, h, f.focus.At.y - h);
                f.camera.transform.LookAt(new Vector3(f.focus.At.x, 0, f.focus.At.y));
                f.towns.Choose(default, f.camera);   // also prepares the building layer for this camera
                if (f.acquired.Count > arrived) { f.Arrive(); arrived = f.acquired.Count; f.towns.Choose(default, f.camera); }
                string at = FormattableString.Invariant($"{f.focus.Key} at {d / 1000f} km ({why})");
                Assert.AreEqual(buildings, f.buildings.ReadyForTown(f.focus.Key, f.camera), "building models: " + at);
                // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
                // The drawn whole-town instance (not the camera admission) is what stands in above the close band.
                var whole = f.towns.transform.Find(f.focus.Key);
                Assert.IsNotNull(whole, "whole-town instance: " + at);
                Assert.AreEqual(original && !buildings, !whole.GetComponentsInChildren<Renderer>(true).All(r => r.forceRenderingOff), "whole-town original: " + at);
                foreach (var r in kit) Assert.AreEqual(original, r.forceRenderingOff, "kit houses: " + at);
                Assert.AreEqual(!original, closeR!.forceRenderingOff, "close mesh: " + at);
            }
            Assert.Greater(arrived, 0, "the whole-town original was admitted and arrived");
        }
    }

    [Test]
    public void FocusedOverviewAcquiresOnlyItsWholeTownAndLoadingKeepsFallback()
    {
        using (var f = new ResidencyFixture(saved))
        {
            // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
            var mapWide = f.towns.Wanted.Select(w => w.Model).Distinct().ToList();
            CollectionAssert.AreEquivalent(mapWide, f.acquired, "every wanted whole-town model is acquired before any camera");
            f.Choose(); CollectionAssert.AreEquivalent(mapWide, f.acquired, "a camera adds no second whole-town request");
            Assert.IsFalse(f.buildings.Suppressed(f.focus.Key), "pending original cannot hide fallback");
            f.Arrive(); Assert.IsTrue(f.towns.Seats.ContainsKey(f.focus.Key));
            Assert.AreEqual(1, f.towns.Placed.Count(p => p.Key == f.focus.Key));
            Assert.AreEqual(mapWide.Count, f.source.Opens, "one bundle open per map-wide model, none twice");
            f.Choose();
            var identity = (IDictionary<Renderer, string>)typeof(TownModelLayer).GetField("swapIds", ResidencyFlags)!.GetValue(f.buildings)!;
            foreach (var pair in identity) if (pair.Value == f.focus.Key) Assert.IsTrue(pair.Key.forceRenderingOff);
        }
    }
    [Test]
    public void CloseBuildingsAndWholeTownNeverOwnDuplicateRepresentations()
    {
        using (var f = new ResidencyFixture(saved))
        {
            // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
            // The whole-town lease is held map-wide; up close only the individual Near buildings may draw.
            var mapWide = f.towns.Wanted.Select(w => w.Model).Distinct().ToList();
            f.Choose(close: true);
            CollectionAssert.AreEquivalent(mapWide, f.acquired, "a close camera adds no whole-town request");
            Assert.IsTrue((bool)ResidencyCall(f.buildings, typeof(TownModelLayer), "ReadyForTown", f.focus.Key, f.camera)!);
            f.Choose(); f.Arrive(); Assert.IsTrue(f.towns.Seats.ContainsKey(f.focus.Key));
            var whole = f.towns.transform.Find(f.focus.Key).GetComponentsInChildren<Renderer>(true);
            Assert.IsFalse(whole.All(r => r.forceRenderingOff), "the overview draws the whole town");
            f.Choose(close: true);
            Assert.IsTrue((bool)ResidencyCall(f.buildings, typeof(TownModelLayer), "ReadyForTown", f.focus.Key, f.camera)!);
            Assert.IsTrue(whole.All(r => r != null && r.forceRenderingOff), "individual Near buildings are the only drawn representation");
            Assert.AreEqual(0, f.source.Unloads, "the map-wide whole-town lease stays while its town shows Near buildings");
        }
    }
    [Test]
    public void LeavingFocusClearsInstancesAndConsumerReferencesBeforeLeaseUnload()
    {
        using (var f = new ResidencyFixture(saved))
        {
            f.Choose(); f.Arrive();
            // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
            // Leaving focus no longer unloads; layer teardown (LayerDisable...) keeps the clear-before-unload order.
            var instance = f.towns.transform.Find(f.focus.Key);
            f.source.BeforeUnload = f.AssertConsumerCleared;
            f.Choose(outside: true);
            Assert.AreEqual(0, f.source.Unloads, "leaving focus keeps the map-wide whole town");
            Assert.AreSame(instance, f.towns.transform.Find(f.focus.Key), "the drawn whole town is not rebuilt");
            Assert.IsTrue(f.towns.Seats.ContainsKey(f.focus.Key));
            var identity = (IDictionary<Renderer, string>)typeof(TownModelLayer).GetField("swapIds", ResidencyFlags)!.GetValue(f.buildings)!;
            foreach (var pair in identity) if (pair.Value == f.focus.Key)
                Assert.IsTrue(pair.Key.forceRenderingOff, "outside focus the whole-town original still replaces the kit houses");
        }
    }
    [Test]
    public void FailedOrLateWholeTownRepliesCannotCreateAHoleOrReviveAnEvictedTown()
    {
        using (var f = new ResidencyFixture(saved))
        {
            // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
            // A camera leaving no longer evicts a town; disabling the layer is the remaining eviction.
            var late = f.callbacks[f.acquired.IndexOf(f.focus.Model)];
            f.towns.enabled = false; f.towns.enabled = true;   // evict, then the re-enabled layer asks again
            Assert.Greater(f.acquired.LastIndexOf(f.focus.Model), f.acquired.IndexOf(f.focus.Model), "the re-enabled layer asks again");
            late(null); // stale reply must not create a warning, asset reference or instance
            Assert.IsFalse(f.towns.Seats.ContainsKey(f.focus.Key));
            Assert.IsFalse(f.buildings.Suppressed(f.focus.Key), "a stale reply cannot hide the fallback");
            LogAssert.Expect(LogType.Warning, new Regex("LANDMARKS: the town model .* did not stream"));
            f.callbacks[f.acquired.LastIndexOf(f.focus.Model)](null);
            Assert.IsFalse(f.buildings.Suppressed(f.focus.Key)); Assert.IsFalse(f.towns.Seats.ContainsKey(f.focus.Key));
        }
    }
    [Test]
    public void LayerDisableRetiresWholeTownOwnershipAfterInstancesAndReferences()
    {
        using (var f = new ResidencyFixture(saved))
        {
            f.Choose(); f.Arrive(); f.source.BeforeUnload = f.AssertConsumerCleared;
            f.towns.enabled = false;
            // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
            Assert.AreEqual(f.towns.Wanted.Select(w => w.Model).Distinct().Count(), f.source.Unloads,
                "disable retires every map-wide whole-town lease, each after its consumers"); f.AssertConsumerCleared();
        }
    }
    /// <summary>A merged locality tile is one renderer for many villages, never one named town. Owner ruling, 31 Oct 2026,
    /// "Load all in view (Recommended)": it swaps as one, once every village in it with building models has them ready,
    /// and keeps its fallback otherwise.</summary>
    [Test]
    public void ProductionKitWiringNeverTreatsAMixedRendererAsOneNamedTown()
    {
        using (var f = new ResidencyFixture(saved, approved: true))
        {
            var identity = (IDictionary<Renderer, string>)typeof(TownModelLayer).GetField("swapIds", ResidencyFlags)!.GetValue(f.buildings)!;
            var settlementIds = f.map.Settlements.Select(s => s.Id).ToHashSet(StringComparer.Ordinal);
            Assert.IsNotEmpty(identity);
            foreach (var pair in identity) Assert.IsTrue(settlementIds.Contains(pair.Value), "only built.Towns gets explicit identity");
            var swaps = (IEnumerable<(Renderer kit, Renderer? close)>)typeof(TownModelLayer).GetField("swaps", ResidencyFlags)!.GetValue(f.buildings)!;
            var mixed = swaps.Where(pair => !identity.ContainsKey(pair.kit)).ToList();
            Assert.IsNotEmpty(mixed, "Map.Build must include grouped localities/camps for this fixture");
            var villages = (IDictionary<Renderer, string[]>)typeof(TownModelLayer).GetField("tileIds", ResidencyFlags)!.GetValue(f.buildings)!;
            f.camera.nearClipPlane = 50f; f.camera.farClipPlane = 1_000_000f;   // CameraRig.Init's planes: the view reaches the ground
            f.Choose(close: true);
            int swapped = 0;
            foreach (var pair in mixed)
            {
                var ids = villages.TryGetValue(pair.kit, out var v) ? v : Array.Empty<string>();
                foreach (var id in ids) Assert.IsFalse(settlementIds.Contains(id), pair.kit.name + ": " + id + " is a locality, not a game town");
                bool ready = ids.Length > 0 && ids.All(id => f.buildings.ReadyForTown(id, f.camera));
                Assert.AreEqual(ready, pair.kit.forceRenderingOff, pair.kit.name + ": the tile swaps only as one, once every village in it is ready");
                if (ready) swapped++;
            }
            Assert.Greater(swapped, 0, "a mixed tile in view swaps once its villages are ready");
        }
    }

    /// <summary>Explicit spatial values (ConfigureResidency: tests and diagnostics) keep the camera-focus admission alone
    /// (TownModelLayer.viewAdmission is false): no merged locality tile swaps, even with villages in a view that reaches the
    /// ground. Re-pins what ProductionKitWiringNeverTreatsAMixedRendererAsOneNamedTown covered before it moved to the
    /// production policy. The control switches the same layer to the production policy at the same Imwas view, and Imwas's
    /// tile then swaps.</summary>
    [Test]
    public void ExplicitResidencyKeepsEveryMixedTileAsFallback()
    {
        using (var f = new ResidencyFixture(saved))
        {
            Assert.AreEqual("test fixture only; not measured", f.buildings.SpatialPolicyEvidence, "precondition: the explicit values");
            var identity = (IDictionary<Renderer, string>)typeof(TownModelLayer).GetField("swapIds", ResidencyFlags)!.GetValue(f.buildings)!;
            var swaps = (IEnumerable<(Renderer kit, Renderer? close)>)typeof(TownModelLayer).GetField("swaps", ResidencyFlags)!.GetValue(f.buildings)!;
            var villages = (IDictionary<Renderer, string[]>)typeof(TownModelLayer).GetField("tileIds", ResidencyFlags)!.GetValue(f.buildings)!;
            var mixed = swaps.Where(pair => !identity.ContainsKey(pair.kit)).ToList();
            Assert.IsNotEmpty(mixed, "Map.Build must include grouped localities/camps for this fixture");
            Assert.IsNotEmpty(villages, "Map.Build must include merged tiles with village building models");
            f.camera.nearClipPlane = 50f; f.camera.farClipPlane = 1_000_000f;   // CameraRig.Init's planes: the view reaches the ground
            f.camera.aspect = 16f / 9f;
            f.Choose(close: true);
            foreach (var pair in mixed) Assert.IsFalse(pair.kit.forceRenderingOff, pair.kit.name + ": an explicit-policy mixed tile keeps its fallback");

            var imwas = f.map.KitFootprints.Single(p => p.id == "imwas");
            string name = $"Localities {Mathf.FloorToInt(imwas.centre.x / SettlementKits.TileM)},{Mathf.FloorToInt(imwas.centre.y / SettlementKits.TileM)}";
            var found = f.map.transform.Find(KitLayer.RootName).Find(name)?.GetComponent<MeshRenderer>();
            Assert.IsNotNull(found, name + ": Imwas's merged locality tile");
            var tile = found!;
            Assert.IsTrue(villages.TryGetValue(tile, out var imwasTile) && imwasTile.Contains("imwas"), name + " carries Imwas");
            void LookAtImwas()
            {
                f.camera.transform.position = new Vector3(imwas.centre.x, 10000f, imwas.centre.y - 10000f);
                f.camera.transform.LookAt(new Vector3(imwas.centre.x, 0f, imwas.centre.y));
                f.towns.Choose(default, f.camera);
            }
            LookAtImwas();
            Assert.IsFalse(f.buildings.ReadyForTown("imwas", f.camera), "explicit policy: Imwas in view is not admitted");
            Assert.IsFalse(tile.forceRenderingOff, "explicit policy: Imwas's tile keeps its fallback");

            f.buildings.ConfigureProduction(ProductionResidency.Approved().Towns);
            LookAtImwas();
            Assert.IsTrue(f.buildings.ReadyForTown("imwas", f.camera), "control: under the production policy Imwas in view is admitted");
            Assert.IsTrue(tile.forceRenderingOff, "control: under the production policy the same view swaps Imwas's tile");
        }
    }

    /// <summary>Nazareth's whole-town model (stone-village-v1, the town's Far model) stands in for the town only above the
    /// close band. At 12 km with Nazareth in view but not under the screen centre (the minimap jump lands beside it), the
    /// town's building models are admitted and its whole-town model hides, as in v0.3.1.</summary>
    [Test]
    public void NazarethWholeTownModelHidesAtTwelveKmWhenInViewBesideTheFocus()
    {
        using (var f = new ResidencyFixture(saved, approved: true))
        {
            f.Choose(); f.Arrive();
            var nazareth = f.map.KitFootprints.Single(p => p.id == "nazareth");
            Assert.Greater(f.buildings.CountFor("nazareth"), 0, "Nazareth has building models");
            var whole = f.towns.transform.Find("nazareth");
            Assert.IsNotNull(whole, "Nazareth's whole-town model stands map-wide (ruling 29)");
            var lookAt = new Vector3(nazareth.centre.x, 0f, nazareth.centre.y - (nazareth.radius + 1000f));
            f.camera.transform.rotation = Quaternion.Euler(CameraRig.Pitch, 0f, 0f);
            f.camera.transform.position = lookAt - f.camera.transform.forward * 11_775f;
            f.camera.fieldOfView = CameraRig.Fov; f.camera.aspect = 16f / 9f;
            f.camera.nearClipPlane = 50f; f.camera.farClipPlane = 1_000_000f;   // CameraRig.Init's planes
            Assert.IsTrue(CollectionResidencyView.TryGroundFocus(f.camera, out var focus));
            Assert.Greater(Vector2.Distance(new Vector2(focus.x, focus.z), nazareth.centre), nazareth.radius,
                "precondition: the screen centre is outside Nazareth's footprint");
            f.towns.Choose(default, f.camera);
            Assert.IsTrue(f.buildings.ReadyForTown("nazareth", f.camera), "Nazareth in view at 12 km: building models");
            Assert.IsTrue(whole!.GetComponentsInChildren<Renderer>(true).All(r => r.forceRenderingOff),
                "Nazareth's whole-town model hides while its building models stand");
        }
    }


    [Test]
    public void AQuietCameraExpiresAndRetiresWholeTownOwnershipWithoutAnotherRender()
    {
        using (var f = new ResidencyFixture(saved))
        {
            f.Choose(); f.Arrive(); f.source.BeforeUnload = f.AssertConsumerCleared;
            ResidencyCall(f.buildings, typeof(TownModelLayer), "AdvanceCameraFrame", Time.frameCount + 3);
            ResidencyCall(f.towns, typeof(TownLandmarks), "RefreshWanted");
            // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
            Assert.AreEqual(0, f.source.Unloads, "a quiet camera expiring keeps the map-wide whole town");
            Assert.IsTrue(f.towns.Seats.ContainsKey(f.focus.Key));
            Assert.IsNotNull(f.towns.transform.Find(f.focus.Key));
        }
    }

    /// <summary>Was NoAdmissionLayerCannotFallBackToUnboundedWholeTownRequests: under ruling 29 no camera admission bounds a
    /// whole town any more; the only bound is the map's Towns.Enabled kill switch.</summary>
    [Test]
    public void WholeTownsAndPlacementsAreBoundOnlyByTheTownsKillSwitch()
    {
        var whole = new TownLandmarks.Want("town", TownLandmarks.TelAviv, Vector2.zero, 100, true);
        var placement = new TownLandmarks.Want("placement", TownLandmarks.Kibbutz, Vector2.zero, 100, false);
        var root = new GameObject("no town policy");
        try
        {
            var towns = root.AddComponent<TownLandmarks>();
            // Owner ruling 29 (30 Oct 2026): "Far models map-wide (Recommended)": whole towns stay map-wide; only near detail is camera-gated.
            // Without a map there is no Towns.Enabled kill switch to consult: whole towns are allowed as placements are.
            Assert.IsTrue((bool)ResidencyCall(towns, typeof(TownLandmarks), "Allowed", whole)!);
            Assert.IsTrue((bool)ResidencyCall(towns, typeof(TownLandmarks), "Allowed", placement)!);
        }
        finally { Object.DestroyImmediate(root); }
        using (var f = new ResidencyFixture(saved))
        {
            Assert.IsTrue((bool)ResidencyCall(f.towns, typeof(TownLandmarks), "Allowed", whole)!, "a map with towns enabled allows a whole town");
            Assert.IsTrue((bool)ResidencyCall(f.towns, typeof(TownLandmarks), "Allowed", placement)!);
            var enabled = typeof(ProductionResidency.TownPolicy).GetField("Enabled", ResidencyFlags)!;
            enabled.SetValue(f.map.Residency.Towns, false);
            try
            {
                Assert.IsFalse((bool)ResidencyCall(f.towns, typeof(TownLandmarks), "Allowed", whole)!, "the kill switch bounds whole towns");
                Assert.IsFalse((bool)ResidencyCall(f.towns, typeof(TownLandmarks), "Allowed", placement)!, "and placement landmarks");
            }
            finally { enabled.SetValue(f.map.Residency.Towns, true); }
        }
    }

    /// <summary>Frame cost finding of 7 November 2026 (the "none" switch set measured slower than "all" at Tel Aviv 39 km):
    /// with the "kits" switch off the building layer's Draw is false, so no town is ReadyForTown and every town swap in
    /// TownModelLayer.PrepareCameraCore asks TownLandmarks.WholeReady, which looked the building layer up with a
    /// GetComponentInChildren walk of the whole map. The layer is found once and kept: frames of the swap update walk the
    /// hierarchy no more, and a destroyed layer is looked up again.</summary>
    [Test]
    public void TheBuildingLayerIsLookedUpOnceNotOnEveryFrame()
    {
        var map = OpenMap(); var t = ShowAll(map);
        Assert.IsNotEmpty(t.Seats, "precondition: seated whole towns, so the swap update asks WholeReady");
        var layer = map.GetComponentInChildren<TownModelLayer>(true);
        layer.Draw = false;   // what the "kits" switch off does (KitLayer.ShowKits)
        var inner = layer.WholeTownReady!; int asks = 0;
        layer.WholeTownReady = (id, c) => { asks++; return inner(id, c); };
        var cam = new GameObject("frame camera").AddComponent<Camera>();
        var at = t.Seats.Keys.Select(k => t.Wanted.First(w => w.Key == k).At).First();
        cam.transform.position = new Vector3(at.x, 27000, at.y - 27000); cam.transform.LookAt(new Vector3(at.x, 0, at.y));
        t.Choose(default, cam); layer.PrepareCamera(cam);   // the first frame may resolve the layer
        int before = t.LayerLookups; asks = 0;
        const int frames = 30;
        for (int i = 0; i < frames; i++) { t.Choose(default, cam); layer.PrepareCamera(cam); }   // ChooseCore and Submit
        Assert.Greater(asks, 0, "the swap update asked WholeReady for the seated towns");
        Assert.AreEqual(0, t.LayerLookups - before,
            $"{frames} frames of the swap update ({asks} WholeReady asks) walked the map hierarchy for the building layer");
        Object.DestroyImmediate(layer.gameObject);
        t.Choose(default, cam);
        Assert.Greater(t.LayerLookups, before, "a destroyed building layer is looked up again");
        Object.DestroyImmediate(cam.gameObject);
    }
}