1
0

LatheTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Content.Shared.Lathe;
  4. using Content.Shared.Materials;
  5. using Content.Shared.Prototypes;
  6. using Content.Shared.Research.Prototypes;
  7. using Content.Shared.Whitelist;
  8. using Robust.Shared.GameObjects;
  9. using Robust.Shared.Prototypes;
  10. namespace Content.IntegrationTests.Tests.Lathe;
  11. [TestFixture]
  12. public sealed class LatheTest
  13. {
  14. [Test]
  15. public async Task TestLatheRecipeIngredientsFitLathe()
  16. {
  17. await using var pair = await PoolManager.GetServerClient();
  18. var server = pair.Server;
  19. var mapData = await pair.CreateTestMap();
  20. var entMan = server.EntMan;
  21. var protoMan = server.ProtoMan;
  22. var compFactory = server.ResolveDependency<IComponentFactory>();
  23. var materialStorageSystem = server.System<SharedMaterialStorageSystem>();
  24. var whitelistSystem = server.System<EntityWhitelistSystem>();
  25. var latheSystem = server.System<SharedLatheSystem>();
  26. await server.WaitAssertion(() =>
  27. {
  28. // Find all the lathes
  29. var latheProtos = protoMan.EnumeratePrototypes<EntityPrototype>()
  30. .Where(p => !p.Abstract)
  31. .Where(p => !pair.IsTestPrototype(p))
  32. .Where(p => p.HasComponent<LatheComponent>());
  33. // Find every EntityPrototype that can be inserted into a MaterialStorage
  34. var materialEntityProtos = protoMan.EnumeratePrototypes<EntityPrototype>()
  35. .Where(p => !p.Abstract)
  36. .Where(p => !pair.IsTestPrototype(p))
  37. .Where(p => p.HasComponent<PhysicalCompositionComponent>());
  38. // Spawn all of the above material EntityPrototypes - we need actual entities to do whitelist checks
  39. var materialEntities = new List<EntityUid>(materialEntityProtos.Count());
  40. foreach (var materialEntityProto in materialEntityProtos)
  41. {
  42. materialEntities.Add(entMan.SpawnEntity(materialEntityProto.ID, mapData.GridCoords));
  43. }
  44. Assert.Multiple(() =>
  45. {
  46. // Check each lathe individually
  47. foreach (var latheProto in latheProtos)
  48. {
  49. if (!latheProto.TryGetComponent<LatheComponent>(out var latheComp, compFactory))
  50. continue;
  51. if (!latheProto.TryGetComponent<MaterialStorageComponent>(out var storageComp, compFactory))
  52. continue;
  53. // Test which material-containing entities are accepted by this lathe
  54. var acceptedMaterials = new HashSet<ProtoId<MaterialPrototype>>();
  55. foreach (var materialEntity in materialEntities)
  56. {
  57. Assert.That(entMan.TryGetComponent<PhysicalCompositionComponent>(materialEntity, out var compositionComponent));
  58. if (whitelistSystem.IsWhitelistFail(storageComp.Whitelist, materialEntity))
  59. continue;
  60. // Mark the lathe as accepting each material in the entity
  61. foreach (var (material, _) in compositionComponent.MaterialComposition)
  62. {
  63. acceptedMaterials.Add(material);
  64. }
  65. }
  66. // Collect all possible recipes assigned to this lathe
  67. var recipes = new HashSet<ProtoId<LatheRecipePrototype>>();
  68. latheSystem.AddRecipesFromPacks(recipes, latheComp.StaticPacks);
  69. latheSystem.AddRecipesFromPacks(recipes, latheComp.DynamicPacks);
  70. if (latheProto.TryGetComponent<EmagLatheRecipesComponent>(out var emagRecipesComp, compFactory))
  71. {
  72. latheSystem.AddRecipesFromPacks(recipes, emagRecipesComp.EmagStaticPacks);
  73. latheSystem.AddRecipesFromPacks(recipes, emagRecipesComp.EmagDynamicPacks);
  74. }
  75. // Check each recipe assigned to this lathe
  76. foreach (var recipeId in recipes)
  77. {
  78. Assert.That(protoMan.TryIndex(recipeId, out var recipeProto));
  79. // Track the total material volume of the recipe
  80. var totalQuantity = 0;
  81. // Check each material called for by the recipe
  82. foreach (var (materialId, quantity) in recipeProto.Materials)
  83. {
  84. Assert.That(protoMan.TryIndex(materialId, out var materialProto));
  85. // Make sure the material is accepted by the lathe
  86. Assert.That(acceptedMaterials, Does.Contain(materialId), $"Lathe {latheProto.ID} has recipe {recipeId} but does not accept any materials containing {materialId}");
  87. totalQuantity += quantity;
  88. }
  89. // Make sure the recipe doesn't call for more material than the lathe can hold
  90. if (storageComp.StorageLimit != null)
  91. Assert.That(totalQuantity, Is.LessThanOrEqualTo(storageComp.StorageLimit), $"Lathe {latheProto.ID} has recipe {recipeId} which calls for {totalQuantity} units of materials but can only hold {storageComp.StorageLimit}");
  92. }
  93. }
  94. });
  95. });
  96. await pair.CleanReturnAsync();
  97. }
  98. [Test]
  99. public async Task AllLatheRecipesValidTest()
  100. {
  101. await using var pair = await PoolManager.GetServerClient();
  102. var server = pair.Server;
  103. var proto = server.ProtoMan;
  104. Assert.Multiple(() =>
  105. {
  106. foreach (var recipe in proto.EnumeratePrototypes<LatheRecipePrototype>())
  107. {
  108. if (recipe.Result == null)
  109. Assert.That(recipe.ResultReagents, Is.Not.Null, $"Recipe '{recipe.ID}' has no result or result reagents.");
  110. }
  111. });
  112. await pair.CleanReturnAsync();
  113. }
  114. }