A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
test(ui): cover scroll and session replacement behavior (#1997)
Eyal Golancfe85a565efe
diff --git a/changelog.d/release-ui-lifecycle-coverage.md b/changelog.d/release-ui-lifecycle-coverage.mdnew file mode 100644index 000000000..96a71045f--- /dev/null+++ b/changelog.d/release-ui-lifecycle-coverage.md@@ -0,0 +1,2 @@+### Tests+- Add regression coverage for event scroll state, purchases after session replacement, and Hebrew unit-number provenance and fallback.diff --git a/unity/Assets/Tests/EditMode/EventScrollLifecycleReleaseTests.cs b/unity/Assets/Tests/EditMode/EventScrollLifecycleReleaseTests.csnew file mode 100644index 000000000..2ba466aeb--- /dev/null+++ b/unity/Assets/Tests/EditMode/EventScrollLifecycleReleaseTests.cs@@ -0,0 +1,109 @@+#nullable enable+using System.Linq;+using NUnit.Framework;+using Statecraft.Game;+using Statecraft.Game.UI;+using Statecraft.Sim;+using UnityEditor;+using UnityEngine;+using UnityEngine.UIElements;++/// <summary>#1853, the data side of the body's scroll reset: the card puts its body back to the top only for a new event+/// or a new session, never on a re-render or Hide/"Decision waiting" of the same event. The scroller's range is forced so+/// these tests can set an offset without a layout pass. That makes them lifecycle tests, not geometry or overflow proofs.+/// Each test first asserts that the offset is nonzero, so a reset that has already happened cannot pass by accident.</summary>+public class EventScrollLifecycleReleaseTests+{+ static readonly Content C = ContentLoader.Load(Paths.Content);+ static UiText Text(bool hebrew) => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(hebrew ? UiPaths.StringsHebrew : UiPaths.Strings).text, hebrew ? "strings.he.yaml" : "strings.en.yaml");++ const string First = "armistice_egypt", Second = "armistice_lebanon";+ const float Offset = 250f;++ /// <summary>A historical session with exactly this event pending, on its own first day (EventCardTests.Showing's setup).</summary>+ static GameSession Pending(string eventId)+ {+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);+ g.State.Date = C.Events.Single(e => e.Id == eventId).From;+ g.State.PendingEvents.Clear(); g.State.PendingEvents.Add(eventId);+ return g;+ }++ /// <summary>Forces a vertical range on the body's scroller, scrolls part way down, and checks that the offset took.</summary>+ static ScrollView ScrollDown(UiTestKit ui)+ {+ var scroll = ui.Q<ScrollView>("event-scroll");+ scroll.verticalScroller.lowValue = 0f;+ scroll.verticalScroller.highValue = 1000f;+ scroll.scrollOffset = new Vector2(0f, Offset);+ Assert.AreEqual(Offset, scroll.scrollOffset.y, 0.01f, "precondition: the forced range holds a nonzero offset");+ return scroll;+ }++ [TestCase(false)]+ [TestCase(true)]+ public void RenderingTheSameEventAgainKeepsTheScroll(bool hebrew)+ {+ var text = Text(hebrew);+ using var ui = new UiTestKit();+ using var card = new EventCardPresenter(ui.Root, new SimBridge(Pending(First), text), text, null);+ Assert.AreEqual(First, card.Shown!.Id);+ var scroll = ScrollDown(ui);+ card.Render();+ Assert.AreEqual(First, card.Shown!.Id);+ Assert.AreSame(scroll, ui.Q<ScrollView>("event-scroll"), "the same scroller, not a rebuilt one");+ Assert.AreEqual(Offset, scroll.scrollOffset.y, 0.01f, "the same event keeps its place");+ }++ [TestCase(false)]+ [TestCase(true)]+ public void HideAndDecisionWaitingKeepTheScroll(bool hebrew)+ {+ var text = Text(hebrew);+ using var ui = new UiTestKit();+ using var card = new EventCardPresenter(ui.Root, new SimBridge(Pending(First), text), text, null);+ var scroll = ScrollDown(ui);+ ui.Press("event-hide"); // the card's Hide button+ Assert.IsTrue(card.Collapsed, "precondition: collapsed");+ ui.Press("event-reopen"); // "Decision waiting"+ Assert.IsFalse(card.Collapsed);+ Assert.AreSame(scroll, ui.Q<ScrollView>("event-scroll"), "the same scroller, not a rebuilt one");+ Assert.AreEqual(First, card.Shown!.Id);+ Assert.AreEqual(Offset, scroll.scrollOffset.y, 0.01f, "the reopened card is where the player left it");+ }++ [TestCase(false)]+ [TestCase(true)]+ public void ADifferentEventOpensAtTheTop(bool hebrew)+ {+ var text = Text(hebrew);+ using var ui = new UiTestKit();+ var bridge = new SimBridge(Pending(First), text);+ using var card = new EventCardPresenter(ui.Root, bridge, text, null);+ var scroll = ScrollDown(ui);+ var s = bridge.Session.State;+ s.Date = C.Events.Single(e => e.Id == Second).From;+ s.PendingEvents.Clear(); s.PendingEvents.Add(Second);+ card.Render();+ Assert.AreEqual(Second, card.Shown!.Id, "precondition: the other event shows");+ Assert.AreEqual(1000f, scroll.verticalScroller.highValue, "the range still holds: the presenter reset the offset, no clamp did");+ Assert.AreEqual(0f, scroll.scrollOffset.y, 0.01f, "a new event opens at its text");+ }++ [TestCase(false)]+ [TestCase(true)]+ public void ANewSessionWithTheSameEventOpensAtTheTop(bool hebrew)+ {+ var text = Text(hebrew);+ using var ui = new UiTestKit();+ var bridge = new SimBridge(Pending(First), text);+ using var card = new EventCardPresenter(ui.Root, bridge, text, null);+ var scroll = ScrollDown(ui);+ var old = bridge.Session;+ bridge.Replace(Pending(First)); // raises Changed: the card renders+ Assert.AreNotSame(old, bridge.Session, "precondition: a new session");+ Assert.AreEqual(First, card.Shown!.Id, "precondition: the same event id");+ Assert.AreEqual(1000f, scroll.verticalScroller.highValue, "the range still holds: the presenter reset the offset, no clamp did");+ Assert.AreEqual(0f, scroll.scrollOffset.y, 0.01f, "a new session (New Game, a load) forgets the old place");+ }+}\ No newline at end of filediff --git a/unity/Assets/Tests/EditMode/EventScrollLifecycleReleaseTests.cs.meta b/unity/Assets/Tests/EditMode/EventScrollLifecycleReleaseTests.cs.metanew file mode 100644index 000000000..5dcf607d2--- /dev/null+++ b/unity/Assets/Tests/EditMode/EventScrollLifecycleReleaseTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: e01ad72bca52452d812bde9c3e8e0fd2diff --git a/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs b/unity/Assets/Tests/EditMode/FoodArmsHudTests.csindex 6086c6dba..b0b33b9e7 100644--- a/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs+++ b/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs@@ -313,4 +313,48 @@ public class FoodArmsHudTests}}}++ /// <summary>Replacing the session (a load) drops the queued purchase, and the live Arms panel follows the new session:+ /// its stock, its Attention and a fresh purchase delivered from its own base.</summary>+ [TestCase(false, false)] [TestCase(false, true)]+ [TestCase(true, false)] [TestCase(true, true)]+ public void ReplacingTheSessionDropsTheQueuedPurchaseAndTheLivePanelFollows(bool modern, bool he)+ {+ var b = Bridge(modern, he); var t = Text(he);+ using var ui = new UiTestKit();+ using var arms = new ArmsPurchasePresenter(ui.Root, b, t);+ var count = ui.Q<IntegerField>("arms-count");+ count.value = 4; ui.Press("buy-arms");+ Assert.AreEqual(4, ((BuyArms)b.Queued.Single()).Count);+ Assert.AreEqual(t.Format("arms.queued", 4), ui.Q<Label>("arms-queued").text);++ var next = GameSession.New(b.Content, GoldenRun.Seed, Mode.Historical);+ next.State.Arms = b.State.Arms + 7; // distinguishes the new session's stock+ if (modern) next.State.Attention = double.NaN; // as the fixture does for gdd-0.3+ else next.State.Attention = b.State.Attention - 1.5; // and its Attention+ b.Replace(next);++ Assert.IsEmpty(b.Queued, "Replace drops queued orders");+ Assert.IsEmpty(ui.Q<Label>("arms-queued").text, "the live panel re-rendered through Changed");+ Assert.IsFalse(ui.Q<Button>("withdraw-arms").enabledSelf);+ Assert.AreEqual(t.Format("arms.stock", next.State.Arms), ui.Q<Label>("arms-stock").text);+ if (!modern)+ {+ Assert.AreEqual(next.State.Attention, b.AttentionLeft!.Value, 1e-9, "the new session's Attention, with no charge left over");+ Assert.AreEqual(t.Format("arms.attention", b.Price(new BuyArms(count.value))!.Value, b.AttentionLeft!.Value),+ ui.Q<Label>("arms-attention").text);+ }+ else+ {+ Assert.AreEqual(DisplayStyle.None, ui.Q<Label>("arms-attention").style.display.value);+ Assert.IsEmpty(ui.Q<Label>("arms-attention").text);+ }++ count.value = 2; ui.Press("buy-arms");+ Assert.AreEqual(2, ((BuyArms)b.Queued.Single()).Count);+ var start = b.State.Arms;+ b.EndTurn();+ Assert.AreEqual(start + 2, b.State.Arms, "delivered in the new session, from its base");+ Assert.AreEqual(t.Format("arms.stock", start + 2), ui.Q<Label>("arms-stock").text);+ }}diff --git a/unity/Assets/Tests/EditMode/UnitsInPlayTests.cs b/unity/Assets/Tests/EditMode/UnitsInPlayTests.csindex ab4b16e7f..21d9e3d3b 100644--- a/unity/Assets/Tests/EditMode/UnitsInPlayTests.cs+++ b/unity/Assets/Tests/EditMode/UnitsInPlayTests.cs@@ -1318,4 +1318,32 @@ public class UnitsInPlayTests}finally { foreach (var m in new[] { plain, again, prop }) UnityEngine.Object.DestroyImmediate(m); }}++ const string NumberHe = "number_he: מספר בדיקה, number_he_sources: " + HeSource + ", number_he_review: agent-checked, ";++ /// <summary>#1219 item 2, on a fixture row: number_he shows only in the Hebrew subtitle and falls back to the+ /// English number when left out; the English card is unchanged; its source joins both footers without clearing+ /// the draft note (#902), as the other Hebrew card texts' do (#558).</summary>+ [Test]+ public void TheHebrewNumberShowsOnlyInTheHebrewSubtitleAndFallsBackToTheEnglish()+ {+ var u = ProseUnit(unitHe: NumberHe);+ Assert.AreEqual(("מספר בדיקה", "agent-checked"), (u.NumberHe, u.NumberHeReview));+ var he = ProseCard(u, He);+ StringAssert.Contains("מספר בדיקה", he.Subtitle, "the Hebrew card's subtitle carries number_he");+ var english = ProseCard(u, Text);+ var plain = ProseCard(ProseUnit(), Text);+ Assert.AreEqual((plain.Title, plain.Subtitle, plain.Body, plain.Note), (english.Title, english.Subtitle, english.Body, english.Note),+ "the English card is the same as for the row without number_he");+ StringAssert.DoesNotContain("מספר בדיקה", english.Subtitle);+ StringAssert.Contains("he.example.org", he.Footer, "number_he's sources are listed, as the other Hebrew texts' are");+ StringAssert.Contains("he.example.org", english.Footer, "in both languages");+ Assert.AreEqual(He["unit.draft_wikipedia"], he.Note, "a Hebrew text's source does not check a fact (#902)");+ Assert.IsTrue(UnitData.IsWikipediaOnly(u));++ var fallback = ProseCard(ProseUnit(), He).Subtitle;+ Assert.AreEqual(he.Subtitle.Replace("מספר בדיקה", "1"), fallback, "no number_he: the Hebrew subtitle has the English number in its place");+ StringAssert.DoesNotContain("מספר בדיקה", fallback);+ }+}