Enhance entity deletion test to ignore timed despawn entities (#44077)

* Enhance entity deletion test with extra ticks

* Implement CleanupTransientEntities for test stability

Added CleanupTransientEntities method to handle deletion of transient side-effect entities during tests.

* Add using directive for Robust.Shared.Spawners

* corrections
This commit is contained in:
KloopRE 2026-05-26 20:34:19 +03:00 • committed by GitHub
parent ee26556753
commit 7d30f5ce1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,6 +13,7 @@ using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Spawners;
namespace Content.IntegrationTests.Tests
{
@ -290,6 +291,8 @@ namespace Content.IntegrationTests.Tests
// If the entity deleted itself, check that it didn't spawn other entities
if (!server.EntMan.EntityExists(uid))
{
await CleanupTransientEntities(pair, serverEntities);
Assert.That(Count(server.EntMan), Is.EqualTo(count), $"Server prototype {protoId} failed on deleting itself\n" +
BuildDiffString(serverEntities, Entities(server.EntMan), server.EntMan));
Assert.That(Count(client.EntMan), Is.EqualTo(clientCount), $"Client prototype {protoId} failed on deleting itself\n" +
@ -309,6 +312,7 @@ namespace Content.IntegrationTests.Tests
await server.WaitPost(() => server.EntMan.DeleteEntity(uid));
await pair.RunTicksSync(3);
await CleanupTransientEntities(pair, serverEntities);
// Check that the number of entities has gone back to the original value.
Assert.That(Count(server.EntMan), Is.EqualTo(count), $"Server prototype {protoId} failed on deletion: count didn't reset properly\n" +
@ -321,6 +325,33 @@ namespace Content.IntegrationTests.Tests
});
}
/// <summary>
/// Deletes any entities with <see cref="TimedDespawnComponent"/> that were not present in the baseline snapshot.
/// Some entities spawn transient side-effects on deletion (e.g. explosion visuals). These side-effect entities
/// use TimedDespawn and would persist across test iterations, corrupting baseline entity counts and causing
/// cascading assertion failures.
/// </summary>
private static async Task CleanupTransientEntities(Pair.TestPair pair, HashSet<EntityUid> baselineEntities)
{
var server = pair.Server;
await server.WaitPost(() =>
{
var toRemove = new List<EntityUid>();
var query = server.EntMan.AllEntityQueryEnumerator<TimedDespawnComponent>();
while (query.MoveNext(out var uid, out _))
{
if (!baselineEntities.Contains(uid))
toRemove.Add(uid);
}
foreach (var uid in toRemove)
{
server.EntMan.DeleteEntity(uid);
}
});
await pair.RunTicksSync(3);
}
private static string BuildDiffString(IEnumerable<EntityUid> oldEnts, IEnumerable<EntityUid> newEnts, IEntityManager entMan)
{
var sb = new StringBuilder();