TryAllReactionsTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Content.Shared.Chemistry.Reaction;
  2. using Content.Shared.Chemistry.Components;
  3. using Robust.Shared.GameObjects;
  4. using Robust.Shared.Map;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Utility;
  7. using System.Linq;
  8. using Content.Shared.Chemistry.EntitySystems;
  9. namespace Content.IntegrationTests.Tests.Chemistry
  10. {
  11. [TestFixture]
  12. [TestOf(typeof(ReactionPrototype))]
  13. public sealed class TryAllReactionsTest
  14. {
  15. [TestPrototypes]
  16. private const string Prototypes = @"
  17. - type: entity
  18. id: TestSolutionContainer
  19. components:
  20. - type: SolutionContainerManager
  21. solutions:
  22. beaker:
  23. maxVol: 50
  24. canMix: true";
  25. [Test]
  26. public async Task TryAllTest()
  27. {
  28. await using var pair = await PoolManager.GetServerClient();
  29. var server = pair.Server;
  30. var entityManager = server.ResolveDependency<IEntityManager>();
  31. var prototypeManager = server.ResolveDependency<IPrototypeManager>();
  32. var testMap = await pair.CreateTestMap();
  33. var coordinates = testMap.GridCoords;
  34. var solutionContainerSystem = entityManager.System<SharedSolutionContainerSystem>();
  35. foreach (var reactionPrototype in prototypeManager.EnumeratePrototypes<ReactionPrototype>())
  36. {
  37. //since i have no clue how to isolate each loop assert-wise im just gonna throw this one in for good measure
  38. Console.WriteLine($"Testing {reactionPrototype.ID}");
  39. EntityUid beaker = default;
  40. Entity<SolutionComponent>? solutionEnt = default!;
  41. Solution solution = null;
  42. await server.WaitAssertion(() =>
  43. {
  44. beaker = entityManager.SpawnEntity("TestSolutionContainer", coordinates);
  45. Assert.That(solutionContainerSystem
  46. .TryGetSolution(beaker, "beaker", out solutionEnt, out solution));
  47. foreach (var (id, reactant) in reactionPrototype.Reactants)
  48. {
  49. #pragma warning disable NUnit2045
  50. Assert.That(solutionContainerSystem
  51. .TryAddReagent(solutionEnt.Value, id, reactant.Amount, out var quantity));
  52. Assert.That(reactant.Amount, Is.EqualTo(quantity));
  53. #pragma warning restore NUnit2045
  54. }
  55. solutionContainerSystem.SetTemperature(solutionEnt.Value, reactionPrototype.MinimumTemperature);
  56. if (reactionPrototype.MixingCategories != null)
  57. {
  58. var dummyEntity = entityManager.SpawnEntity(null, MapCoordinates.Nullspace);
  59. var mixerComponent = entityManager.AddComponent<ReactionMixerComponent>(dummyEntity);
  60. mixerComponent.ReactionTypes = reactionPrototype.MixingCategories;
  61. solutionContainerSystem.UpdateChemicals(solutionEnt.Value, true, mixerComponent);
  62. }
  63. });
  64. await server.WaitIdleAsync();
  65. await server.WaitAssertion(() =>
  66. {
  67. //you just got linq'd fool
  68. //(i'm sorry)
  69. var foundProductsMap = reactionPrototype.Products
  70. .Concat(reactionPrototype.Reactants.Where(x => x.Value.Catalyst).ToDictionary(x => x.Key, x => x.Value.Amount))
  71. .ToDictionary(x => x, _ => false);
  72. foreach (var (reagent, quantity) in solution.Contents)
  73. {
  74. Assert.That(foundProductsMap.TryFirstOrNull(x => x.Key.Key == reagent.Prototype && x.Key.Value == quantity, out var foundProduct));
  75. foundProductsMap[foundProduct.Value.Key] = true;
  76. }
  77. Assert.That(foundProductsMap.All(x => x.Value));
  78. });
  79. }
  80. await pair.CleanReturnAsync();
  81. }
  82. }
  83. }