From 178897ce7a879c6c397e8ef46983aaadf47c4114 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 30 Sep 2026 14:59:50 +0200 Subject: [PATCH] Scenarios Cut 1: a failed staging reports what it admitted, not a crash "Zone unchanged on failure" was defended only by xunit formatting an entity sequence until the test host overflowed its stack. SafeAssert.Unchanged compares the zone's entities and agents by identity and order and names what changed by hull name; every Assert.Contains over zone.Entities in RunStartTests goes through SafeAssert.In for the same reason. AMissingDesignFailsNamedAndChangesNothing gains the partial-admission case: a player and a piloted entity that build cleanly, then a later entity whose preset is missing. Admitting the built player before the all-or-nothing return now fails by name ("the zone's entities changed, 5 -> 6: added [Djinni]"). Co-Authored-By: Claude Opus 5.5 --- tests/Aetheria.Shared.Tests/RunStartTests.cs | 33 +++++++++++++------- tests/Aetheria.Shared.Tests/SafeAssert.cs | 17 ++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/RunStartTests.cs b/tests/Aetheria.Shared.Tests/RunStartTests.cs index 31030a7e8..3ae6ead30 100644 --- a/tests/Aetheria.Shared.Tests/RunStartTests.cs +++ b/tests/Aetheria.Shared.Tests/RunStartTests.cs @@ -147,8 +147,8 @@ public sealed class RunStartTests : IDisposable var player = staged.Player; Assert.Equal(hullName, player.HullData.Name); Assert.True(player.IsPlayerShip); - Assert.Contains(player, zone.Entities); - Assert.Same(zone, player.Zone); + SafeAssert.In(zone, player, "the player is admitted"); + Assert.True(zone == player.Zone, "the player's zone is the arena"); Assert.Equal(float3.zero, player.Position); Assert.Same(_protagonist, player.Faction); Assert.Equal(0, AgentsOf(zone, player)); @@ -178,7 +178,7 @@ public sealed class RunStartTests : IDisposable Assert.Equal("Djinni", player.HullData.Name); Assert.True(player.IsPlayerShip); - Assert.Contains(player, zone.Entities); + SafeAssert.In(zone, player, "the player is admitted"); Assert.Equal(float3(120, 0, -40), player.Position); Assert.True(length(player.Direction - float2(.6f, .8f)) < 1e-5f, $"heading {player.Direction}"); Assert.Equal( @@ -206,8 +206,8 @@ public sealed class RunStartTests : IDisposable Assert.Equal(1, AgentsOf(zone, piloted)); Assert.Equal(0, AgentsOf(zone, unpiloted)); Assert.Equal(0, AgentsOf(zone, staged.Player)); - Assert.Contains(piloted, zone.Entities); - Assert.Contains(unpiloted, zone.Entities); + SafeAssert.In(zone, piloted, "the piloted entity is admitted"); + SafeAssert.In(zone, unpiloted, "the unpiloted entity is admitted"); var look = unpiloted.LookDirection; for (var tick = 0; tick < 50; tick++) @@ -276,7 +276,7 @@ public sealed class RunStartTests : IDisposable Placed(Bare(TurretHull()), 200, 50, ScenarioStance.Hostile, piloted: false))); var turret = Assert.IsType(staged.Entities[0]); Assert.False(turret.OrbitData.IsSet()); - Assert.Contains(turret, zone.Entities); + SafeAssert.In(zone, turret, "the turret is admitted"); for (var tick = 0; tick < 20; tick++) zone.Update(.1f); Assert.Equal(float2(200, 50), turret.Position.xz); } @@ -297,17 +297,19 @@ public sealed class RunStartTests : IDisposable }) { var zone = Arena(null); - var before = zone.Entities.ToList(); + var entities = zone.Entities.ToList(); + var agents = zone.Agents.ToList(); var failures = new List(); Assert.Null(RunStart.Stage(_items, zone, scenario, _startingHull, _protagonist, failures)); Assert.Contains(failures, failure => failure.Contains(expected)); - Assert.Equal(before, zone.Entities); + SafeAssert.Unchanged(zone, entities, agents); Assert.Contains(RunStart.Check(_items, scenario), failure => failure.Contains(expected)); } } // A scenario naming a missing design or preset fails with each named; the check entry finds the same failures with - // no zone and writes no document, and staging admits nothing, not even the parts that built. + // no zone and writes no document, and staging admits nothing, not even the parts that built: a player and a piloted + // entity that build cleanly stay out when a later entity fails. [Fact] public void AMissingDesignFailsNamedAndChangesNothing() { @@ -330,8 +332,15 @@ public sealed class RunStartTests : IDisposable var failures = new List(); Assert.Null(RunStart.Stage(_items, zone, scenario, _startingHull, _protagonist, failures)); Assert.Equal(checkFailures, failures); - Assert.Equal(entities, zone.Entities); - Assert.Equal(agents, zone.Agents); + SafeAssert.Unchanged(zone, entities, agents); + + var lateFailure = Quiet(At(djinni, 0, 0), + Placed(Bare(Hull("LonginusX")), 300, 0, ScenarioStance.Hostile, piloted: true), + Placed(new CultRecordRef(new CultRecordKey("absent-preset")), -300, 0, ScenarioStance.Neutral, false)); + failures.Clear(); + Assert.Null(RunStart.Stage(_items, zone, lateFailure, _startingHull, _protagonist, failures)); + Assert.Contains("absent-preset", Assert.Single(failures)); + SafeAssert.Unchanged(zone, entities, agents); Assert.Empty(RunStart.Check(_items, null)); } @@ -366,7 +375,7 @@ public sealed class RunStartTests : IDisposable var arriving = (Ship) Loadouts.Materialize(_items, zone, _cache.Get(Bare(Hull("Djinni"))), _ => true, new List()); var agents = zone.Agents.Count; zone.Admit(arriving, piloted: false); - Assert.Contains(arriving, zone.Entities); + SafeAssert.In(zone, arriving, "the arrival is admitted"); Assert.Equal(agents, zone.Agents.Count); } diff --git a/tests/Aetheria.Shared.Tests/SafeAssert.cs b/tests/Aetheria.Shared.Tests/SafeAssert.cs index 2ab52d6bd..ca29f071a 100644 --- a/tests/Aetheria.Shared.Tests/SafeAssert.cs +++ b/tests/Aetheria.Shared.Tests/SafeAssert.cs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System.Collections.Generic; +using System.Linq; using Xunit; // Assertions over collections of entities and shots. xunit formats a collection's contents into its failure @@ -34,4 +35,20 @@ internal static class SafeAssert public static void NotIn(Zone zone, Entity entity, string why = "entity should have left the zone") => Assert.False(zone.Entities.Contains(entity), why); + + // The zone holds exactly the entities and agents it held before, in the same order: nothing admitted, nothing + // lost. A failure names what changed by hull name. + public static void Unchanged(Zone zone, List entities, List agents) + { + Same(entities, zone.Entities.ToList(), entity => entity.Name, "entities"); + Same(agents, zone.Agents, agent => agent.Ship.Name, "agents"); + } + + private static void Same(List before, List now, System.Func name, string what) where T : class + { + var added = now.Where(x => !before.Contains(x)).Select(name).ToList(); + var lost = before.Where(x => !now.Contains(x)).Select(name).ToList(); + var inOrder = now.Count == before.Count && now.Zip(before, ReferenceEquals).All(same => same); + Assert.True(inOrder, $"the zone's {what} changed, {before.Count} -> {now.Count}: added [{string.Join(", ", added)}], lost [{string.Join(", ", lost)}]"); + } }