Refactor Device Linking tests. (#43157)

* Add 416 more tests.

* fixes.
This commit is contained in:
Moony 2026-03-10 20:56:13 +01:00 committed by Coryler
parent bc5b6299a6
commit 43f8a9d395
1 changed files with 37 additions and 48 deletions

View File

@ -1,12 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using Content.IntegrationTests.Utility;
using Content.Server.DeviceLinking.Systems;
using Content.Shared.DeviceLinking;
using Content.Shared.Prototypes;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
namespace Content.IntegrationTests.Tests.DeviceLinking;
@ -24,69 +21,61 @@ public sealed class DeviceLinkingTest
- Output
";
/// <summary>
/// Spawns every entity that has a <see cref="DeviceLinkSinkComponent"/>
/// and sends a signal to every port to make sure nothing causes an error.
/// </summary>
private static string[] _entitiesWithDeviceLinkSink = GameDataScrounger.EntitiesWithComponent("DeviceLinkSink");
[Test]
public async Task AllDeviceLinkSinksWorkTest()
[TestOf(typeof(DeviceLinkSinkComponent))]
[TestCaseSource(nameof(_entitiesWithDeviceLinkSink))]
[Description("Ensures all devices that can sink signals will not cause exceptions when signaled.")]
public async Task DeviceLinkSinkAllPortsTest(string protoKey)
{
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
var protoMan = server.ProtoMan;
var compFact = server.ResolveDependency<IComponentFactory>();
var mapMan = server.ResolveDependency<IMapManager>();
var mapSys = server.System<SharedMapSystem>();
var deviceLinkSys = server.System<DeviceLinkSystem>();
var prototypes = server.ProtoMan.EnumeratePrototypes<EntityPrototype>();
await server.WaitAssertion(() =>
{
Assert.Multiple(() =>
using (Assert.EnterMultipleScope())
{
foreach (var proto in prototypes)
var proto = protoMan.Index(protoKey);
Assert.That(proto.TryGetComponent<DeviceLinkSinkComponent>(out var protoSinkComp, compFact));
foreach (var port in protoSinkComp!.Ports)
{
if (proto.Abstract || pair.IsTestPrototype(proto))
continue;
// Create a map for each entity/port combo so they can't interfere
mapSys.CreateMap(out var mapId);
var grid = mapMan.CreateGridEntity(mapId);
mapSys.SetTile(grid.Owner, grid.Comp, Vector2i.Zero, new Tile(1));
var coord = new EntityCoordinates(grid.Owner, 0, 0);
if (!proto.TryGetComponent<DeviceLinkSinkComponent>(out var protoSinkComp, compFact))
continue;
// Spawn the sink entity
var sinkEnt = server.EntMan.SpawnEntity(proto.ID, coord);
// Get the actual sink component, since the one we got from the prototype isn't initialized.
var sinkComp = server.EntMan.GetComponent<DeviceLinkSinkComponent>(sinkEnt);
foreach (var port in protoSinkComp.Ports)
{
// Create a map for each entity/port combo so they can't interfere
mapSys.CreateMap(out var mapId);
var grid = mapMan.CreateGridEntity(mapId);
mapSys.SetTile(grid.Owner, grid.Comp, Vector2i.Zero, new Tile(1));
var coord = new EntityCoordinates(grid.Owner, 0, 0);
// Spawn the tester
var sourceEnt = server.EntMan.SpawnEntity(PortTesterProtoId, coord);
var sourceComp = server.EntMan.GetComponent<DeviceLinkSourceComponent>(sourceEnt);
// Spawn the sink entity
var sinkEnt = server.EntMan.SpawnEntity(proto.ID, coord);
// Get the actual sink component, since the one we got from the prototype doesn't have its owner set up
Assert.That(server.EntMan.TryGetComponent<DeviceLinkSinkComponent>(sinkEnt, out var sinkComp),
$"{proto.ID} does not have a DeviceLinkSinkComponent!");
// Create a link from the tester's output to the target port on the sink
deviceLinkSys.SaveLinks(null,
sourceEnt,
sinkEnt,
[("Output", port.Id)],
sourceComp,
sinkComp);
// Spawn the tester
var sourceEnt = server.EntMan.SpawnEntity(PortTesterProtoId, coord);
Assert.That(server.EntMan.TryGetComponent<DeviceLinkSourceComponent>(sourceEnt, out var sourceComp),
$"Tester prototype does not have a DeviceLinkSourceComponent!");
// Send a signal to the port
Assert.DoesNotThrow(() => { deviceLinkSys.InvokePort(sourceEnt, "Output", null, sourceComp); },
$"Exception thrown while triggering port {port.Id} of the sink device.");
// Create a link from the tester's output to the target port on the sink
deviceLinkSys.SaveLinks(null,
sourceEnt,
sinkEnt,
[("Output", port.Id)],
sourceComp,
sinkComp);
// Send a signal to the port
Assert.DoesNotThrow(() => { deviceLinkSys.InvokePort(sourceEnt, "Output", null, sourceComp); },
$"Exception thrown while triggering port {port.Id} of sink device {proto.ID}");
mapSys.DeleteMap(mapId);
}
mapSys.DeleteMap(mapId);
}
});
}
});
await pair.CleanReturnAsync();