From 9b6012fdfa26b336d922a201894758bfd1366277 Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Sat, 14 Dec 2024 20:50:55 -0500 Subject: [PATCH 1/6] Add a test to check that all lathes accept the materials for all their recipesy --- .../Tests/Lathe/LatheTest.cs | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Content.IntegrationTests/Tests/Lathe/LatheTest.cs diff --git a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs new file mode 100644 index 000000000000..8856be525834 --- /dev/null +++ b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs @@ -0,0 +1,95 @@ +using System.Collections.Generic; +using System.Linq; +using Content.Shared.Lathe; +using Content.Shared.Materials; +using Content.Shared.Prototypes; +using Content.Shared.Whitelist; +using Robust.Shared.GameObjects; +using Robust.Shared.Prototypes; + +namespace Content.IntegrationTests.Tests.Lathe; + +[TestFixture] +public sealed class LatheTest +{ + [Test] + public async Task TestLatheRecipeIngredientsFitLathe() + { + await using var pair = await PoolManager.GetServerClient(); + var server = pair.Server; + + var mapData = await pair.CreateTestMap(); + + var entMan = server.EntMan; + var protoMan = server.ProtoMan; + var compFactory = server.ResolveDependency(); + var materialStorageSystem = server.System(); + var whitelistSystem = server.System(); + + await server.WaitAssertion(() => + { + // Find all the lathes + var latheProtos = protoMan.EnumeratePrototypes() + .Where(p => !p.Abstract) + .Where(p => !pair.IsTestPrototype(p)) + .Where(p => p.HasComponent()); + + // Find every EntityPrototype that can be inserted into a MaterialStorage + var materialEntityProtos = protoMan.EnumeratePrototypes() + .Where(p => !p.Abstract) + .Where(p => !pair.IsTestPrototype(p)) + .Where(p => p.HasComponent()); + + // Spawn all of the above material EntityPrototypes - we need actual entities to do whitelist checks + var materialEntities = new List(materialEntityProtos.Count()); + foreach (var materialEntityProto in materialEntityProtos) + { + materialEntities.Add(entMan.SpawnEntity(materialEntityProto.ID, mapData.GridCoords)); + } + + Assert.Multiple(() => + { + // Check each lathe individually + foreach (var latheProto in latheProtos) + { + if (!latheProto.TryGetComponent(out var latheComp, compFactory)) + continue; + + if (!latheProto.TryGetComponent(out var storageComp, compFactory)) + continue; + + // Test which material-containing entities are accepted by this lathe + var acceptedMaterials = new HashSet>(); + foreach (var materialEntity in materialEntities) + { + Assert.That(entMan.TryGetComponent(materialEntity, out var compositionComponent)); + if (whitelistSystem.IsWhitelistFail(storageComp.Whitelist, materialEntity)) + continue; + + // Mark the lathe as accepting each material in the entity + foreach (var (material, _) in compositionComponent.MaterialComposition) + { + acceptedMaterials.Add(material); + } + } + + // Check each recipe assigned to this lathe + foreach (var recipeId in latheComp.StaticRecipes) + { + Assert.That(protoMan.TryIndex(recipeId, out var recipeProto)); + + // Check each material called for by the recipe + foreach (var (materialId, _) in recipeProto.Materials) + { + Assert.That(protoMan.TryIndex(materialId, out var materialProto)); + // Make sure the material is accepted by the lathe + Assert.That(acceptedMaterials, Does.Contain(materialId), $"Lathe {latheProto.ID} has recipe {recipeId} but does not accept any materials containing {materialId}"); + } + } + } + }); + }); + + await pair.CleanReturnAsync(); + } +} From 121b09e29fcc1e029cc636dc2a454b7abf23ccf6 Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Sat, 14 Dec 2024 21:10:21 -0500 Subject: [PATCH 2/6] Add check for storage limit --- Content.IntegrationTests/Tests/Lathe/LatheTest.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs index 8856be525834..05148d3eaaee 100644 --- a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs +++ b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs @@ -79,11 +79,14 @@ await server.WaitAssertion(() => Assert.That(protoMan.TryIndex(recipeId, out var recipeProto)); // Check each material called for by the recipe - foreach (var (materialId, _) in recipeProto.Materials) + foreach (var (materialId, quantity) in recipeProto.Materials) { Assert.That(protoMan.TryIndex(materialId, out var materialProto)); // Make sure the material is accepted by the lathe Assert.That(acceptedMaterials, Does.Contain(materialId), $"Lathe {latheProto.ID} has recipe {recipeId} but does not accept any materials containing {materialId}"); + // Make sure the recipe doesn't call for more material than the lathe can hold + if (storageComp.StorageLimit != null) + Assert.That(quantity, Is.LessThanOrEqualTo(storageComp.StorageLimit), $"Lathe {latheProto.ID} has recipe {recipeId} which calls for {quantity} units of {materialId} but can only hold {storageComp.StorageLimit}"); } } } From 68699fbff170969e58f4c56d682edef080aebdfe Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Mon, 16 Dec 2024 00:07:30 -0500 Subject: [PATCH 3/6] Track total recipe material volume --- Content.IntegrationTests/Tests/Lathe/LatheTest.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs index 05148d3eaaee..1030e7887ffb 100644 --- a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs +++ b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs @@ -78,16 +78,19 @@ await server.WaitAssertion(() => { Assert.That(protoMan.TryIndex(recipeId, out var recipeProto)); + // Track the total material volume of the recipe + var totalQuantity = 0; // Check each material called for by the recipe foreach (var (materialId, quantity) in recipeProto.Materials) { Assert.That(protoMan.TryIndex(materialId, out var materialProto)); // Make sure the material is accepted by the lathe Assert.That(acceptedMaterials, Does.Contain(materialId), $"Lathe {latheProto.ID} has recipe {recipeId} but does not accept any materials containing {materialId}"); - // Make sure the recipe doesn't call for more material than the lathe can hold - if (storageComp.StorageLimit != null) - Assert.That(quantity, Is.LessThanOrEqualTo(storageComp.StorageLimit), $"Lathe {latheProto.ID} has recipe {recipeId} which calls for {quantity} units of {materialId} but can only hold {storageComp.StorageLimit}"); + totalQuantity += quantity; } + // Make sure the recipe doesn't call for more material than the lathe can hold + if (storageComp.StorageLimit != null) + 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}"); } } }); From 7341ec451c093204304503932140089d7d99de28 Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Mon, 16 Dec 2024 00:28:42 -0500 Subject: [PATCH 4/6] Check dynamic and emag recipes too --- Content.IntegrationTests/Tests/Lathe/LatheTest.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs index 1030e7887ffb..b079bc4d7196 100644 --- a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs +++ b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs @@ -3,6 +3,7 @@ using Content.Shared.Lathe; using Content.Shared.Materials; using Content.Shared.Prototypes; +using Content.Shared.Research.Prototypes; using Content.Shared.Whitelist; using Robust.Shared.GameObjects; using Robust.Shared.Prototypes; @@ -73,8 +74,18 @@ await server.WaitAssertion(() => } } + // Collect all the recipes assigned to this lathe + var recipes = new List>(); + recipes.AddRange(latheComp.StaticRecipes); + recipes.AddRange(latheComp.DynamicRecipes); + if (latheProto.TryGetComponent(out var emagRecipesComp, compFactory)) + { + recipes.AddRange(emagRecipesComp.EmagStaticRecipes); + recipes.AddRange(emagRecipesComp.EmagDynamicRecipes); + } + // Check each recipe assigned to this lathe - foreach (var recipeId in latheComp.StaticRecipes) + foreach (var recipeId in recipes) { Assert.That(protoMan.TryIndex(recipeId, out var recipeProto)); From b42e7e09ad0209d02bd1fa018559b8b4ccce017d Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Mon, 16 Dec 2024 00:31:51 -0500 Subject: [PATCH 5/6] Move AllLatheRecipesValidTest from ResearchTest to LatheTest --- .../Tests/Lathe/LatheTest.cs | 20 +++++++++++++++++++ .../Tests/ResearchTest.cs | 20 ------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs index b079bc4d7196..89a88fc2854f 100644 --- a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs +++ b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs @@ -109,4 +109,24 @@ await server.WaitAssertion(() => await pair.CleanReturnAsync(); } + + [Test] + public async Task AllLatheRecipesValidTest() + { + await using var pair = await PoolManager.GetServerClient(); + + var server = pair.Server; + var proto = server.ResolveDependency(); + + Assert.Multiple(() => + { + foreach (var recipe in proto.EnumeratePrototypes()) + { + if (recipe.Result == null) + Assert.That(recipe.ResultReagents, Is.Not.Null, $"Recipe '{recipe.ID}' has no result or result reagents."); + } + }); + + await pair.CleanReturnAsync(); + } } diff --git a/Content.IntegrationTests/Tests/ResearchTest.cs b/Content.IntegrationTests/Tests/ResearchTest.cs index f50e6111da3f..7ae29a79ffd5 100644 --- a/Content.IntegrationTests/Tests/ResearchTest.cs +++ b/Content.IntegrationTests/Tests/ResearchTest.cs @@ -98,24 +98,4 @@ await server.WaitAssertion(() => await pair.CleanReturnAsync(); } - - [Test] - public async Task AllLatheRecipesValidTest() - { - await using var pair = await PoolManager.GetServerClient(); - - var server = pair.Server; - var proto = server.ResolveDependency(); - - Assert.Multiple(() => - { - foreach (var recipe in proto.EnumeratePrototypes()) - { - if (recipe.Result == null) - Assert.That(recipe.ResultReagents, Is.Not.Null, $"Recipe '{recipe.ID}' has no result or result reagents."); - } - }); - - await pair.CleanReturnAsync(); - } } From 9b0bbbec26c9b22f21e6e9a88aaba2bbe4799b5a Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Mon, 16 Dec 2024 00:33:09 -0500 Subject: [PATCH 6/6] Extremely minor modernization --- Content.IntegrationTests/Tests/Lathe/LatheTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs index 89a88fc2854f..88aece967b75 100644 --- a/Content.IntegrationTests/Tests/Lathe/LatheTest.cs +++ b/Content.IntegrationTests/Tests/Lathe/LatheTest.cs @@ -116,7 +116,7 @@ public async Task AllLatheRecipesValidTest() await using var pair = await PoolManager.GetServerClient(); var server = pair.Server; - var proto = server.ResolveDependency(); + var proto = server.ProtoMan; Assert.Multiple(() => {