1
0

DisposalUnitTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #nullable enable annotations
  2. using System.Linq;
  3. using System.Numerics;
  4. using Content.Server.Disposal.Tube.Components;
  5. using Content.Server.Disposal.Unit.Components;
  6. using Content.Server.Disposal.Unit.EntitySystems;
  7. using Content.Server.Power.Components;
  8. using Content.Shared.Disposal;
  9. using Content.Shared.Disposal.Components;
  10. using NUnit.Framework;
  11. using Robust.Shared.GameObjects;
  12. using Robust.Shared.Reflection;
  13. namespace Content.IntegrationTests.Tests.Disposal
  14. {
  15. [TestFixture]
  16. [TestOf(typeof(DisposalHolderComponent))]
  17. [TestOf(typeof(DisposalEntryComponent))]
  18. [TestOf(typeof(DisposalUnitComponent))]
  19. public sealed class DisposalUnitTest
  20. {
  21. [Reflect(false)]
  22. private sealed class DisposalUnitTestSystem : EntitySystem
  23. {
  24. public override void Initialize()
  25. {
  26. base.Initialize();
  27. SubscribeLocalEvent<DoInsertDisposalUnitEvent>(ev =>
  28. {
  29. var (_, toInsert, unit) = ev;
  30. var insertTransform = EntityManager.GetComponent<TransformComponent>(toInsert);
  31. var unitTransform = EntityManager.GetComponent<TransformComponent>(unit);
  32. // Not in a tube yet
  33. Assert.That(insertTransform.ParentUid, Is.EqualTo(unit));
  34. }, after: new[] { typeof(SharedDisposalUnitSystem) });
  35. }
  36. }
  37. private static void UnitInsert(EntityUid uid, DisposalUnitComponent unit, bool result, DisposalUnitSystem disposalSystem, params EntityUid[] entities)
  38. {
  39. foreach (var entity in entities)
  40. {
  41. Assert.That(disposalSystem.CanInsert(uid, unit, entity), Is.EqualTo(result));
  42. disposalSystem.TryInsert(uid, entity, null);
  43. }
  44. }
  45. private static void UnitContains(DisposalUnitComponent unit, bool result, params EntityUid[] entities)
  46. {
  47. foreach (var entity in entities)
  48. {
  49. Assert.That(unit.Container.ContainedEntities.Contains(entity), Is.EqualTo(result));
  50. }
  51. }
  52. private static void UnitInsertContains(EntityUid uid, DisposalUnitComponent unit, bool result, DisposalUnitSystem disposalSystem, params EntityUid[] entities)
  53. {
  54. UnitInsert(uid, unit, result, disposalSystem, entities);
  55. UnitContains(unit, result, entities);
  56. }
  57. private static void Flush(EntityUid unitEntity, DisposalUnitComponent unit, bool result, DisposalUnitSystem disposalSystem, params EntityUid[] entities)
  58. {
  59. Assert.Multiple(() =>
  60. {
  61. Assert.That(unit.Container.ContainedEntities, Is.SupersetOf(entities));
  62. Assert.That(entities, Has.Length.EqualTo(unit.Container.ContainedEntities.Count));
  63. Assert.That(result, Is.EqualTo(disposalSystem.TryFlush(unitEntity, unit)));
  64. Assert.That(result || entities.Length == 0, Is.EqualTo(unit.Container.ContainedEntities.Count == 0));
  65. });
  66. }
  67. [TestPrototypes]
  68. private const string Prototypes = @"
  69. - type: entity
  70. name: HumanDisposalDummy
  71. id: HumanDisposalDummy
  72. components:
  73. - type: Body
  74. prototype: Human
  75. - type: MobState
  76. - type: MobThresholds
  77. thresholds:
  78. 0: Alive
  79. 200: Dead
  80. - type: Damageable
  81. damageContainer: Biological
  82. - type: Physics
  83. bodyType: KinematicController
  84. - type: Fixtures
  85. fixtures:
  86. fix1:
  87. shape:
  88. !type:PhysShapeCircle
  89. radius: 0.35
  90. - type: DoAfter
  91. - type: entity
  92. name: WrenchDummy
  93. id: WrenchDummy
  94. components:
  95. - type: Item
  96. - type: Tool
  97. qualities:
  98. - Anchoring
  99. - type: Physics
  100. bodyType: Dynamic
  101. - type: Fixtures
  102. fixtures:
  103. fix1:
  104. shape:
  105. !type:PhysShapeCircle
  106. radius: 0.35
  107. - type: DoAfter
  108. - type: entity
  109. name: DisposalUnitDummy
  110. id: DisposalUnitDummy
  111. components:
  112. - type: DisposalUnit
  113. entryDelay: 0
  114. draggedEntryDelay: 0
  115. flushTime: 0
  116. - type: Anchorable
  117. - type: ApcPowerReceiver
  118. - type: Physics
  119. bodyType: Static
  120. - type: Fixtures
  121. fixtures:
  122. fix1:
  123. shape:
  124. !type:PhysShapeCircle
  125. radius: 0.35
  126. - type: entity
  127. name: DisposalTrunkDummy
  128. id: DisposalTrunkDummy
  129. components:
  130. - type: DisposalEntry
  131. - type: DisposalTube
  132. - type: Transform
  133. anchored: true
  134. ";
  135. [Test]
  136. public async Task Test()
  137. {
  138. await using var pair = await PoolManager.GetServerClient();
  139. var server = pair.Server;
  140. var testMap = await pair.CreateTestMap();
  141. EntityUid human = default!;
  142. EntityUid wrench = default!;
  143. EntityUid disposalUnit = default!;
  144. EntityUid disposalTrunk = default!;
  145. EntityUid unitUid = default;
  146. DisposalUnitComponent unitComponent = default!;
  147. var entityManager = server.ResolveDependency<IEntityManager>();
  148. var xformSystem = entityManager.System<SharedTransformSystem>();
  149. var disposalSystem = entityManager.System<DisposalUnitSystem>();
  150. await server.WaitAssertion(() =>
  151. {
  152. // Spawn the entities
  153. var coordinates = testMap.GridCoords;
  154. human = entityManager.SpawnEntity("HumanDisposalDummy", coordinates);
  155. wrench = entityManager.SpawnEntity("WrenchDummy", coordinates);
  156. disposalUnit = entityManager.SpawnEntity("DisposalUnitDummy", coordinates);
  157. disposalTrunk = entityManager.SpawnEntity("DisposalTrunkDummy", coordinates);
  158. // Test for components existing
  159. unitUid = disposalUnit;
  160. Assert.Multiple(() =>
  161. {
  162. Assert.That(entityManager.TryGetComponent(disposalUnit, out unitComponent));
  163. Assert.That(entityManager.HasComponent<DisposalEntryComponent>(disposalTrunk));
  164. });
  165. // Can't insert, unanchored and unpowered
  166. xformSystem.Unanchor(unitUid, entityManager.GetComponent<TransformComponent>(unitUid));
  167. UnitInsertContains(disposalUnit, unitComponent, false, disposalSystem, human, wrench, disposalUnit, disposalTrunk);
  168. });
  169. await server.WaitAssertion(() =>
  170. {
  171. // Anchor the disposal unit
  172. xformSystem.AnchorEntity(unitUid, entityManager.GetComponent<TransformComponent>(unitUid));
  173. // No power
  174. Assert.That(unitComponent.Powered, Is.False);
  175. // Can't insert the trunk or the unit into itself
  176. UnitInsertContains(unitUid, unitComponent, false, disposalSystem, disposalUnit, disposalTrunk);
  177. // Can insert mobs and items
  178. UnitInsertContains(unitUid, unitComponent, true, disposalSystem, human, wrench);
  179. });
  180. await server.WaitAssertion(() =>
  181. {
  182. var worldPos = xformSystem.GetWorldPosition(disposalTrunk);
  183. // Move the disposal trunk away
  184. xformSystem.SetWorldPosition(disposalTrunk, worldPos + new Vector2(1, 0));
  185. // Fail to flush with a mob and an item
  186. Flush(disposalUnit, unitComponent, false, disposalSystem, human, wrench);
  187. });
  188. await server.WaitAssertion(() =>
  189. {
  190. var xform = entityManager.GetComponent<TransformComponent>(disposalTrunk);
  191. var worldPos = xformSystem.GetWorldPosition(disposalUnit);
  192. // Move the disposal trunk back
  193. xformSystem.SetWorldPosition(disposalTrunk, worldPos);
  194. xformSystem.AnchorEntity((disposalTrunk, xform));
  195. // Fail to flush with a mob and an item, no power
  196. Flush(disposalUnit, unitComponent, false, disposalSystem, human, wrench);
  197. });
  198. await server.WaitAssertion(() =>
  199. {
  200. // Remove power need
  201. Assert.That(entityManager.TryGetComponent(disposalUnit, out ApcPowerReceiverComponent power));
  202. power!.NeedsPower = false;
  203. unitComponent.Powered = true; //Power state changed event doesn't get fired smh
  204. // Flush with a mob and an item
  205. Flush(disposalUnit, unitComponent, true, disposalSystem, human, wrench);
  206. });
  207. await server.WaitAssertion(() =>
  208. {
  209. // Re-pressurizing
  210. Flush(disposalUnit, unitComponent, false, disposalSystem);
  211. });
  212. await pair.CleanReturnAsync();
  213. }
  214. }
  215. }