VendingInteractionTest.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Linq;
  2. using Content.IntegrationTests.Tests.Interaction;
  3. using Content.Server.VendingMachines;
  4. using Content.Shared.Damage;
  5. using Content.Shared.Damage.Prototypes;
  6. using Content.Shared.FixedPoint;
  7. using Content.Shared.VendingMachines;
  8. namespace Content.IntegrationTests.Tests.Vending;
  9. public sealed class VendingInteractionTest : InteractionTest
  10. {
  11. private const string VendingMachineProtoId = "InteractionTestVendingMachine";
  12. private const string VendedItemProtoId = "InteractionTestItem";
  13. private const string RestockBoxProtoId = "InteractionTestRestockBox";
  14. private const string RestockBoxOtherProtoId = "InteractionTestRestockBoxOther";
  15. [TestPrototypes]
  16. private const string TestPrototypes = $@"
  17. - type: entity
  18. parent: BaseItem
  19. id: {VendedItemProtoId}
  20. name: {VendedItemProtoId}
  21. - type: vendingMachineInventory
  22. id: InteractionTestVendingInventory
  23. startingInventory:
  24. {VendedItemProtoId}: 5
  25. - type: vendingMachineInventory
  26. id: InteractionTestVendingInventoryOther
  27. startingInventory:
  28. {VendedItemProtoId}: 5
  29. - type: entity
  30. parent: BaseVendingMachineRestock
  31. id: {RestockBoxProtoId}
  32. components:
  33. - type: VendingMachineRestock
  34. canRestock:
  35. - InteractionTestVendingInventory
  36. - type: entity
  37. parent: BaseVendingMachineRestock
  38. id: {RestockBoxOtherProtoId}
  39. components:
  40. - type: VendingMachineRestock
  41. canRestock:
  42. - InteractionTestVendingInventoryOther
  43. - type: entity
  44. id: {VendingMachineProtoId}
  45. parent: VendingMachine
  46. components:
  47. - type: VendingMachine
  48. pack: InteractionTestVendingInventory
  49. ejectDelay: 0 # no delay to speed up tests
  50. - type: Sprite
  51. sprite: error.rsi
  52. ";
  53. [Test]
  54. public async Task InteractUITest()
  55. {
  56. await SpawnTarget(VendingMachineProtoId);
  57. // Should start with no BUI open
  58. Assert.That(IsUiOpen(VendingMachineUiKey.Key), Is.False, "BUI was open unexpectedly.");
  59. // Unpowered vending machine does not open BUI
  60. await Activate();
  61. Assert.That(IsUiOpen(VendingMachineUiKey.Key), Is.False, "BUI opened without power.");
  62. // Power the vending machine
  63. var apc = await SpawnEntity("APCBasic", SEntMan.GetCoordinates(TargetCoords));
  64. await RunTicks(1);
  65. // Interacting with powered vending machine opens BUI
  66. await Activate();
  67. Assert.That(IsUiOpen(VendingMachineUiKey.Key), "BUI failed to open.");
  68. // Interacting with it again closes the BUI
  69. await Activate();
  70. Assert.That(IsUiOpen(VendingMachineUiKey.Key), Is.False, "BUI failed to close on interaction.");
  71. // Reopen BUI for the next check
  72. await Activate();
  73. Assert.That(IsUiOpen(VendingMachineUiKey.Key), "BUI failed to reopen.");
  74. // Remove power
  75. await Delete(apc);
  76. await RunTicks(1);
  77. // The BUI should close when power is lost
  78. Assert.That(IsUiOpen(VendingMachineUiKey.Key), Is.False, "BUI failed to close on power loss.");
  79. }
  80. [Test]
  81. public async Task DispenseItemTest()
  82. {
  83. await SpawnTarget(VendingMachineProtoId);
  84. var vendorEnt = SEntMan.GetEntity(Target.Value);
  85. var vendingSystem = SEntMan.System<VendingMachineSystem>();
  86. var items = vendingSystem.GetAllInventory(vendorEnt);
  87. // Verify initial item count
  88. Assert.That(items, Is.Not.Empty, $"{VendingMachineProtoId} spawned with no items.");
  89. Assert.That(items.First().Amount, Is.EqualTo(5), $"{VendingMachineProtoId} spawned with unexpected item count.");
  90. // Power the vending machine
  91. await SpawnEntity("APCBasic", SEntMan.GetCoordinates(TargetCoords));
  92. await RunTicks(1);
  93. // Open the BUI
  94. await Activate();
  95. Assert.That(IsUiOpen(VendingMachineUiKey.Key), "BUI failed to open.");
  96. // Request an item be dispensed
  97. var ev = new VendingMachineEjectMessage(InventoryType.Regular, VendedItemProtoId);
  98. await SendBui(VendingMachineUiKey.Key, ev);
  99. // Make sure the stock decreased
  100. Assert.That(items.First().Amount, Is.EqualTo(4), "Stocked item count did not decrease.");
  101. // Make sure the dispensed item was spawned in to the world
  102. await AssertEntityLookup(
  103. ("APCBasic", 1),
  104. (VendedItemProtoId, 1)
  105. );
  106. }
  107. [Test]
  108. public async Task RestockTest()
  109. {
  110. var vendingSystem = SEntMan.System<VendingMachineSystem>();
  111. await SpawnTarget(VendingMachineProtoId);
  112. var vendorEnt = SEntMan.GetEntity(Target.Value);
  113. var items = vendingSystem.GetAllInventory(vendorEnt);
  114. Assert.That(items, Is.Not.Empty, $"{VendingMachineProtoId} spawned with no items.");
  115. Assert.That(items.First().Amount, Is.EqualTo(5), $"{VendingMachineProtoId} spawned with unexpected item count.");
  116. // Try to restock with the maintenance panel closed (nothing happens)
  117. await InteractUsing(RestockBoxProtoId);
  118. Assert.That(items.First().Amount, Is.EqualTo(5), "Restocked without opening maintenance panel.");
  119. // Open the maintenance panel
  120. await InteractUsing(Screw);
  121. // Try to restock using the wrong restock box (nothing happens)
  122. await InteractUsing(RestockBoxOtherProtoId);
  123. Assert.That(items.First().Amount, Is.EqualTo(5), "Restocked with wrong restock box.");
  124. // Restock the machine
  125. await InteractUsing(RestockBoxProtoId);
  126. Assert.That(items.First().Amount, Is.EqualTo(10), "Restocking resulted in unexpected item count.");
  127. }
  128. [Test]
  129. public async Task RepairTest()
  130. {
  131. await SpawnTarget(VendingMachineProtoId);
  132. // Power the vending machine
  133. await SpawnEntity("APCBasic", SEntMan.GetCoordinates(TargetCoords));
  134. await RunTicks(1);
  135. // Break it
  136. await BreakVendor();
  137. Assert.That(IsUiOpen(VendingMachineUiKey.Key), Is.False, "BUI did not close when vending machine broke.");
  138. // Make sure we can't open the BUI while it's broken
  139. await Activate();
  140. Assert.That(IsUiOpen(VendingMachineUiKey.Key), Is.False, "Opened BUI of broken vending machine.");
  141. // Repair the vending machine
  142. await InteractUsing(Weld);
  143. // Make sure the BUI can open now that the machine has been repaired
  144. await Activate();
  145. Assert.That(IsUiOpen(VendingMachineUiKey.Key), "Failed to open BUI after repair.");
  146. }
  147. private async Task BreakVendor()
  148. {
  149. var damageableSys = SEntMan.System<DamageableSystem>();
  150. Assert.That(TryComp<DamageableComponent>(out var damageableComp), $"{VendingMachineProtoId} does not have DamageableComponent.");
  151. Assert.That(damageableComp.Damage.GetTotal(), Is.EqualTo(FixedPoint2.Zero), $"{VendingMachineProtoId} started with unexpected damage.");
  152. // Damage the vending machine to the point that it breaks
  153. var damageType = ProtoMan.Index<DamageTypePrototype>("Blunt");
  154. var damage = new DamageSpecifier(damageType, FixedPoint2.New(100));
  155. await Server.WaitPost(() => damageableSys.TryChangeDamage(SEntMan.GetEntity(Target), damage, ignoreResistances: true));
  156. await RunTicks(5);
  157. Assert.That(damageableComp.Damage.GetTotal(), Is.GreaterThan(FixedPoint2.Zero), $"{VendingMachineProtoId} did not take damage.");
  158. }
  159. }