1
0

DeviceLinkingTest.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Content.Server.DeviceLinking.Systems;
  4. using Content.Shared.DeviceLinking;
  5. using Content.Shared.Prototypes;
  6. using Robust.Shared.GameObjects;
  7. using Robust.Shared.Map;
  8. using Robust.Shared.Maths;
  9. using Robust.Shared.Prototypes;
  10. namespace Content.IntegrationTests.Tests.DeviceLinking;
  11. public sealed class DeviceLinkingTest
  12. {
  13. private const string PortTesterProtoId = "DeviceLinkingSinkPortTester";
  14. [TestPrototypes]
  15. private const string Prototypes = $@"
  16. - type: entity
  17. id: {PortTesterProtoId}
  18. components:
  19. - type: DeviceLinkSource
  20. ports:
  21. - Output
  22. ";
  23. /// <summary>
  24. /// Spawns every entity that has a <see cref="DeviceLinkSinkComponent"/>
  25. /// and sends a signal to every port to make sure nothing causes an error.
  26. /// </summary>
  27. [Test]
  28. public async Task AllDeviceLinkSinksWorkTest()
  29. {
  30. await using var pair = await PoolManager.GetServerClient();
  31. var server = pair.Server;
  32. var compFact = server.ResolveDependency<IComponentFactory>();
  33. var mapMan = server.ResolveDependency<IMapManager>();
  34. var mapSys = server.System<SharedMapSystem>();
  35. var deviceLinkSys = server.System<DeviceLinkSystem>();
  36. var prototypes = server.ProtoMan.EnumeratePrototypes<EntityPrototype>();
  37. await server.WaitAssertion(() =>
  38. {
  39. Assert.Multiple(() =>
  40. {
  41. foreach (var proto in prototypes)
  42. {
  43. if (proto.Abstract || pair.IsTestPrototype(proto))
  44. continue;
  45. if (!proto.TryGetComponent<DeviceLinkSinkComponent>(out var protoSinkComp, compFact))
  46. continue;
  47. foreach (var port in protoSinkComp.Ports)
  48. {
  49. // Create a map for each entity/port combo so they can't interfere
  50. mapSys.CreateMap(out var mapId);
  51. var grid = mapMan.CreateGridEntity(mapId);
  52. mapSys.SetTile(grid.Owner, grid.Comp, Vector2i.Zero, new Tile(1));
  53. var coord = new EntityCoordinates(grid.Owner, 0, 0);
  54. // Spawn the sink entity
  55. var sinkEnt = server.EntMan.SpawnEntity(proto.ID, coord);
  56. // Get the actual sink component, since the one we got from the prototype doesn't have its owner set up
  57. Assert.That(server.EntMan.TryGetComponent<DeviceLinkSinkComponent>(sinkEnt, out var sinkComp),
  58. $"Tester prototype does not have a DeviceLinkSourceComponent!");
  59. // Spawn the tester
  60. var sourceEnt = server.EntMan.SpawnEntity(PortTesterProtoId, coord);
  61. Assert.That(server.EntMan.TryGetComponent<DeviceLinkSourceComponent>(sourceEnt, out var sourceComp),
  62. $"Tester prototype does not have a DeviceLinkSourceComponent!");
  63. // Create a link from the tester's output to the target port on the sink
  64. deviceLinkSys.SaveLinks(null,
  65. sourceEnt,
  66. sinkEnt,
  67. [("Output", port.Id)],
  68. sourceComp,
  69. sinkComp);
  70. // Send a signal to the port
  71. Assert.DoesNotThrow(() => { deviceLinkSys.InvokePort(sourceEnt, "Output", null, sourceComp); },
  72. $"Exception thrown while triggering port {port.Id} of sink device {proto.ID}");
  73. mapSys.DeleteMap(mapId);
  74. }
  75. }
  76. });
  77. });
  78. await pair.CleanReturnAsync();
  79. }
  80. }