AbsorbentTest.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using Content.Server.Fluids.EntitySystems;
  2. using Content.Shared.Chemistry.Components;
  3. using Content.Shared.Chemistry.EntitySystems;
  4. using Content.Shared.FixedPoint;
  5. using Content.Shared.Fluids;
  6. using Robust.Shared.GameObjects;
  7. using Robust.Shared.Prototypes;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace Content.IntegrationTests.Tests.Fluids;
  11. [TestFixture]
  12. [TestOf(typeof(AbsorbentComponent))]
  13. public sealed class AbsorbentTest
  14. {
  15. private const string UserDummyId = "UserDummy";
  16. private const string AbsorbentDummyId = "AbsorbentDummy";
  17. private const string RefillableDummyId = "RefillableDummy";
  18. private const string SmallRefillableDummyId = "SmallRefillableDummy";
  19. private const string EvaporablePrototypeId = "Water";
  20. private const string NonEvaporablePrototypeId = "Cola";
  21. [TestPrototypes]
  22. private const string Prototypes = $@"
  23. - type: entity
  24. name: {UserDummyId}
  25. id: {UserDummyId}
  26. - type: entity
  27. name: {AbsorbentDummyId}
  28. id: {AbsorbentDummyId}
  29. components:
  30. - type: Absorbent
  31. - type: SolutionContainerManager
  32. solutions:
  33. absorbed:
  34. maxVol: 100
  35. - type: entity
  36. name: {RefillableDummyId}
  37. id: {RefillableDummyId}
  38. components:
  39. - type: SolutionContainerManager
  40. solutions:
  41. refillable:
  42. maxVol: 200
  43. - type: RefillableSolution
  44. solution: refillable
  45. - type: entity
  46. name: {SmallRefillableDummyId}
  47. id: {SmallRefillableDummyId}
  48. components:
  49. - type: SolutionContainerManager
  50. solutions:
  51. refillable:
  52. maxVol: 20
  53. - type: RefillableSolution
  54. solution: refillable
  55. ";
  56. public sealed record TestSolutionReagents(FixedPoint2 VolumeOfEvaporable, FixedPoint2 VolumeOfNonEvaporable);
  57. public record TestSolutionCase(
  58. string Case, // Only for clarity purposes
  59. TestSolutionReagents InitialAbsorbentSolution,
  60. TestSolutionReagents InitialRefillableSolution,
  61. TestSolutionReagents ExpectedAbsorbentSolution,
  62. TestSolutionReagents ExpectedRefillableSolution);
  63. [TestCaseSource(nameof(TestCasesToRun))]
  64. public async Task AbsorbentOnRefillableTest(TestSolutionCase testCase)
  65. {
  66. await using var pair = await PoolManager.GetServerClient();
  67. var server = pair.Server;
  68. var testMap = await pair.CreateTestMap();
  69. var coordinates = testMap.GridCoords;
  70. var entityManager = server.ResolveDependency<IEntityManager>();
  71. var absorbentSystem = entityManager.System<AbsorbentSystem>();
  72. var solutionContainerSystem = entityManager.System<SharedSolutionContainerSystem>();
  73. var prototypeManager = server.ResolveDependency<IPrototypeManager>();
  74. EntityUid user = default;
  75. EntityUid absorbent = default;
  76. EntityUid refillable = default;
  77. AbsorbentComponent component = null;
  78. await server.WaitAssertion(() =>
  79. {
  80. user = entityManager.SpawnEntity(UserDummyId, coordinates);
  81. absorbent = entityManager.SpawnEntity(AbsorbentDummyId, coordinates);
  82. refillable = entityManager.SpawnEntity(RefillableDummyId, coordinates);
  83. entityManager.TryGetComponent(absorbent, out component);
  84. solutionContainerSystem.TryGetSolution(absorbent, AbsorbentComponent.SolutionName, out var absorbentSoln, out var absorbentSolution);
  85. solutionContainerSystem.TryGetRefillableSolution(refillable, out var refillableSoln, out var refillableSolution);
  86. // Arrange
  87. if (testCase.InitialAbsorbentSolution.VolumeOfEvaporable > FixedPoint2.Zero)
  88. solutionContainerSystem.AddSolution(absorbentSoln.Value, new Solution(EvaporablePrototypeId, testCase.InitialAbsorbentSolution.VolumeOfEvaporable));
  89. if (testCase.InitialAbsorbentSolution.VolumeOfNonEvaporable > FixedPoint2.Zero)
  90. solutionContainerSystem.AddSolution(absorbentSoln.Value, new Solution(NonEvaporablePrototypeId, testCase.InitialAbsorbentSolution.VolumeOfNonEvaporable));
  91. if (testCase.InitialRefillableSolution.VolumeOfEvaporable > FixedPoint2.Zero)
  92. solutionContainerSystem.AddSolution(refillableSoln.Value, new Solution(EvaporablePrototypeId, testCase.InitialRefillableSolution.VolumeOfEvaporable));
  93. if (testCase.InitialRefillableSolution.VolumeOfNonEvaporable > FixedPoint2.Zero)
  94. solutionContainerSystem.AddSolution(refillableSoln.Value, new Solution(NonEvaporablePrototypeId, testCase.InitialRefillableSolution.VolumeOfNonEvaporable));
  95. // Act
  96. absorbentSystem.Mop(user, refillable, absorbent, component);
  97. // Assert
  98. var absorbentComposition = absorbentSolution.GetReagentPrototypes(prototypeManager).ToDictionary(r => r.Key.ID, r => r.Value);
  99. var refillableComposition = refillableSolution.GetReagentPrototypes(prototypeManager).ToDictionary(r => r.Key.ID, r => r.Value);
  100. Assert.Multiple(() =>
  101. {
  102. Assert.That(VolumeOfPrototypeInComposition(absorbentComposition, EvaporablePrototypeId), Is.EqualTo(testCase.ExpectedAbsorbentSolution.VolumeOfEvaporable));
  103. Assert.That(VolumeOfPrototypeInComposition(absorbentComposition, NonEvaporablePrototypeId), Is.EqualTo(testCase.ExpectedAbsorbentSolution.VolumeOfNonEvaporable));
  104. Assert.That(VolumeOfPrototypeInComposition(refillableComposition, EvaporablePrototypeId), Is.EqualTo(testCase.ExpectedRefillableSolution.VolumeOfEvaporable));
  105. Assert.That(VolumeOfPrototypeInComposition(refillableComposition, NonEvaporablePrototypeId), Is.EqualTo(testCase.ExpectedRefillableSolution.VolumeOfNonEvaporable));
  106. });
  107. });
  108. await pair.RunTicksSync(5);
  109. await pair.CleanReturnAsync();
  110. }
  111. [TestCaseSource(nameof(TestCasesToRunOnSmallRefillable))]
  112. public async Task AbsorbentOnSmallRefillableTest(TestSolutionCase testCase)
  113. {
  114. await using var pair = await PoolManager.GetServerClient();
  115. var server = pair.Server;
  116. var testMap = await pair.CreateTestMap();
  117. var coordinates = testMap.GridCoords;
  118. var entityManager = server.ResolveDependency<IEntityManager>();
  119. var absorbentSystem = entityManager.System<AbsorbentSystem>();
  120. var solutionContainerSystem = entityManager.System<SharedSolutionContainerSystem>();
  121. var prototypeManager = server.ResolveDependency<IPrototypeManager>();
  122. EntityUid user = default;
  123. EntityUid absorbent = default;
  124. EntityUid refillable = default;
  125. AbsorbentComponent component = null;
  126. await server.WaitAssertion(() =>
  127. {
  128. user = entityManager.SpawnEntity(UserDummyId, coordinates);
  129. absorbent = entityManager.SpawnEntity(AbsorbentDummyId, coordinates);
  130. refillable = entityManager.SpawnEntity(SmallRefillableDummyId, coordinates);
  131. entityManager.TryGetComponent(absorbent, out component);
  132. solutionContainerSystem.TryGetSolution(absorbent, AbsorbentComponent.SolutionName, out var absorbentSoln, out var absorbentSolution);
  133. solutionContainerSystem.TryGetRefillableSolution(refillable, out var refillableSoln, out var refillableSolution);
  134. // Arrange
  135. solutionContainerSystem.AddSolution(absorbentSoln.Value, new Solution(EvaporablePrototypeId, testCase.InitialAbsorbentSolution.VolumeOfEvaporable));
  136. if (testCase.InitialAbsorbentSolution.VolumeOfNonEvaporable > FixedPoint2.Zero)
  137. solutionContainerSystem.AddSolution(absorbentSoln.Value, new Solution(NonEvaporablePrototypeId, testCase.InitialAbsorbentSolution.VolumeOfNonEvaporable));
  138. if (testCase.InitialRefillableSolution.VolumeOfEvaporable > FixedPoint2.Zero)
  139. solutionContainerSystem.AddSolution(refillableSoln.Value, new Solution(EvaporablePrototypeId, testCase.InitialRefillableSolution.VolumeOfEvaporable));
  140. if (testCase.InitialRefillableSolution.VolumeOfNonEvaporable > FixedPoint2.Zero)
  141. solutionContainerSystem.AddSolution(refillableSoln.Value, new Solution(NonEvaporablePrototypeId, testCase.InitialRefillableSolution.VolumeOfNonEvaporable));
  142. // Act
  143. absorbentSystem.Mop(user, refillable, absorbent, component);
  144. // Assert
  145. var absorbentComposition = absorbentSolution.GetReagentPrototypes(prototypeManager).ToDictionary(r => r.Key.ID, r => r.Value);
  146. var refillableComposition = refillableSolution.GetReagentPrototypes(prototypeManager).ToDictionary(r => r.Key.ID, r => r.Value);
  147. Assert.Multiple(() =>
  148. {
  149. Assert.That(VolumeOfPrototypeInComposition(absorbentComposition, EvaporablePrototypeId), Is.EqualTo(testCase.ExpectedAbsorbentSolution.VolumeOfEvaporable));
  150. Assert.That(VolumeOfPrototypeInComposition(absorbentComposition, NonEvaporablePrototypeId), Is.EqualTo(testCase.ExpectedAbsorbentSolution.VolumeOfNonEvaporable));
  151. Assert.That(VolumeOfPrototypeInComposition(refillableComposition, EvaporablePrototypeId), Is.EqualTo(testCase.ExpectedRefillableSolution.VolumeOfEvaporable));
  152. Assert.That(VolumeOfPrototypeInComposition(refillableComposition, NonEvaporablePrototypeId), Is.EqualTo(testCase.ExpectedRefillableSolution.VolumeOfNonEvaporable));
  153. });
  154. });
  155. await pair.RunTicksSync(5);
  156. await pair.CleanReturnAsync();
  157. }
  158. private static FixedPoint2 VolumeOfPrototypeInComposition(Dictionary<string, FixedPoint2> composition, string prototypeId)
  159. {
  160. return composition.TryGetValue(prototypeId, out var value) ? value : FixedPoint2.Zero;
  161. }
  162. public static readonly TestSolutionCase[] TestCasesToRun = new TestSolutionCase[]
  163. {
  164. // Both empty case
  165. new(
  166. "Both empty - no transfer",
  167. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  168. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  169. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  170. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero)
  171. ),
  172. // Just water cases
  173. new(
  174. "Transfer water to empty refillable",
  175. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  176. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  177. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  178. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero)
  179. ),
  180. new(
  181. "Transfer water to empty absorbent",
  182. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  183. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  184. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  185. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero)
  186. ),
  187. new(
  188. "Both partially filled with water while everything fits in absorbent",
  189. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  190. new TestSolutionReagents(FixedPoint2.New(40), FixedPoint2.Zero),
  191. new TestSolutionReagents(FixedPoint2.New(90), FixedPoint2.Zero),
  192. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero)
  193. ),
  194. new(
  195. "Both partially filled with water while not everything fits in absorbent",
  196. new TestSolutionReagents(FixedPoint2.New(70), FixedPoint2.Zero),
  197. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  198. new TestSolutionReagents(FixedPoint2.New(100), FixedPoint2.Zero),
  199. new TestSolutionReagents(FixedPoint2.New(20), FixedPoint2.Zero)
  200. ),
  201. // Just contaminants cases
  202. new(
  203. "Transfer contaminants to empty refillable",
  204. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(50)),
  205. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  206. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  207. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(50))
  208. ),
  209. new(
  210. "Do not transfer contaminants back to empty absorbent",
  211. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  212. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(50)),
  213. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  214. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(50))
  215. ),
  216. new(
  217. "Add contaminants to preexisting while everything fits in refillable",
  218. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(50)),
  219. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(130)),
  220. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  221. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(180))
  222. ),
  223. new(
  224. "Add contaminants to preexisting while not everything fits in refillable",
  225. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(90)),
  226. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(130)),
  227. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(20)),
  228. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(200))
  229. ),
  230. // Mixed: water and contaminants cases
  231. new(
  232. "Transfer just contaminants into empty refillable",
  233. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(50)),
  234. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  235. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  236. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(50))
  237. ),
  238. new(
  239. "Transfer just contaminants into non-empty refillable while everything fits",
  240. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(50)),
  241. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(60)),
  242. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  243. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(110))
  244. ),
  245. new(
  246. "Transfer just contaminants into non-empty refillable while not everything fits",
  247. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(50)),
  248. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(170)),
  249. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(20)),
  250. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(200))
  251. ),
  252. new(
  253. "Transfer just contaminants and absorb water from water refillable",
  254. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(50)),
  255. new TestSolutionReagents(FixedPoint2.New(70), FixedPoint2.Zero),
  256. new TestSolutionReagents(FixedPoint2.New(100), FixedPoint2.Zero),
  257. new TestSolutionReagents(FixedPoint2.New(20), FixedPoint2.New(50))
  258. ),
  259. new(
  260. "Transfer just contaminants and absorb water from a full water refillable",
  261. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(50)),
  262. new TestSolutionReagents(FixedPoint2.New(200), FixedPoint2.Zero),
  263. new TestSolutionReagents(FixedPoint2.New(100), FixedPoint2.Zero),
  264. new TestSolutionReagents(FixedPoint2.New(150), FixedPoint2.New(50))
  265. ),
  266. new(
  267. "Transfer just contaminants and absorb water from a full mixed refillable",
  268. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(50)),
  269. new TestSolutionReagents(FixedPoint2.New(100), FixedPoint2.New(100)),
  270. new TestSolutionReagents(FixedPoint2.New(100), FixedPoint2.Zero),
  271. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(150))
  272. ),
  273. new(
  274. "Transfer just contaminants and absorb water from a low-water mixed refillable",
  275. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.New(50)),
  276. new TestSolutionReagents(FixedPoint2.New(10), FixedPoint2.New(100)),
  277. new TestSolutionReagents(FixedPoint2.New(60), FixedPoint2.Zero),
  278. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(150))
  279. ),
  280. new(
  281. "Contaminants for water exchange",
  282. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(100)),
  283. new TestSolutionReagents(FixedPoint2.New(200), FixedPoint2.Zero),
  284. new TestSolutionReagents(FixedPoint2.New(100), FixedPoint2.Zero),
  285. new TestSolutionReagents(FixedPoint2.New(100), FixedPoint2.New(100))
  286. )
  287. };
  288. public static readonly TestSolutionCase[] TestCasesToRunOnSmallRefillable = new TestSolutionCase[]
  289. {
  290. // Only testing cases where small refillable AvailableVolume makes a difference
  291. new(
  292. "Transfer water to empty refillable",
  293. new TestSolutionReagents(FixedPoint2.New(50), FixedPoint2.Zero),
  294. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  295. new TestSolutionReagents(FixedPoint2.New(30), FixedPoint2.Zero),
  296. new TestSolutionReagents(FixedPoint2.New(20), FixedPoint2.Zero)
  297. ),
  298. new(
  299. "Transfer contaminants to empty refillable",
  300. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(50)),
  301. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.Zero),
  302. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(30)),
  303. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(20))
  304. ),
  305. new(
  306. "Mixed transfer in limited space",
  307. new TestSolutionReagents(FixedPoint2.New(20), FixedPoint2.New(25)),
  308. new TestSolutionReagents(FixedPoint2.New(10), FixedPoint2.New(5)),
  309. new TestSolutionReagents(FixedPoint2.New(30), FixedPoint2.New(10)),
  310. new TestSolutionReagents(FixedPoint2.Zero, FixedPoint2.New(20))
  311. )
  312. };
  313. }