AlarmThresholdTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Content.Shared.Atmos.Monitor;
  2. using Robust.Shared.Prototypes;
  3. namespace Content.IntegrationTests.Tests.Atmos
  4. {
  5. [TestFixture]
  6. [TestOf(typeof(AtmosAlarmThreshold))]
  7. public sealed class AlarmThresholdTest
  8. {
  9. [TestPrototypes]
  10. private const string Prototypes = @"
  11. - type: alarmThreshold
  12. id: AlarmThresholdTestDummy
  13. upperBound: !type:AlarmThresholdSetting
  14. threshold: 5
  15. lowerBound: !type:AlarmThresholdSetting
  16. threshold: 1
  17. upperWarnAround: !type:AlarmThresholdSetting
  18. threshold: 0.5
  19. lowerWarnAround: !type:AlarmThresholdSetting
  20. threshold: 1.5
  21. ";
  22. [Test]
  23. public async Task TestAlarmThreshold()
  24. {
  25. await using var pair = await PoolManager.GetServerClient();
  26. var server = pair.Server;
  27. var prototypeManager = server.ResolveDependency<IPrototypeManager>();
  28. AtmosAlarmThreshold threshold = default!;
  29. var proto = prototypeManager.Index<AtmosAlarmThresholdPrototype>("AlarmThresholdTestDummy");
  30. threshold = new(proto);
  31. await server.WaitAssertion(() =>
  32. {
  33. // ensure upper/lower bounds are calculated
  34. Assert.Multiple(() =>
  35. {
  36. Assert.That(threshold.UpperWarningBound.Value, Is.EqualTo(5f * 0.5f));
  37. Assert.That(threshold.LowerWarningBound.Value, Is.EqualTo(1f * 1.5f));
  38. });
  39. // ensure that setting bounds to zero/
  40. // negative numbers is an invalid set
  41. {
  42. threshold.SetLimit(AtmosMonitorLimitType.UpperDanger, 0f);
  43. Assert.That(threshold.UpperBound.Value, Is.EqualTo(5f));
  44. threshold.SetLimit(AtmosMonitorLimitType.UpperDanger, -1f);
  45. Assert.That(threshold.UpperBound.Value, Is.EqualTo(5f));
  46. threshold.SetLimit(AtmosMonitorLimitType.LowerDanger, 0f);
  47. Assert.That(threshold.LowerBound.Value, Is.EqualTo(1f));
  48. threshold.SetLimit(AtmosMonitorLimitType.LowerDanger, -1f);
  49. Assert.That(threshold.LowerBound.Value, Is.EqualTo(1f));
  50. }
  51. // test if making the lower bound higher
  52. // than upper will adjust the upper value
  53. {
  54. threshold.SetLimit(AtmosMonitorLimitType.UpperDanger, 5f);
  55. threshold.SetLimit(AtmosMonitorLimitType.LowerDanger, 6f);
  56. Assert.That(threshold.LowerBound.Value, Is.LessThanOrEqualTo(threshold.UpperBound.Value));
  57. }
  58. // same as above, sets it lower
  59. {
  60. threshold.SetLimit(AtmosMonitorLimitType.UpperDanger, 5f);
  61. threshold.SetLimit(AtmosMonitorLimitType.LowerDanger, 6f);
  62. threshold.SetLimit(AtmosMonitorLimitType.UpperDanger, 1f);
  63. Assert.That(threshold.LowerBound.Value, Is.LessThanOrEqualTo(threshold.UpperBound.Value));
  64. }
  65. // Check that the warning percentage is calculated correcly
  66. {
  67. threshold.SetLimit(AtmosMonitorLimitType.UpperWarning, threshold.UpperBound.Value * 0.5f);
  68. Assert.That(threshold.UpperWarningPercentage.Value, Is.EqualTo(0.5f));
  69. threshold.SetLimit(AtmosMonitorLimitType.LowerWarning, threshold.LowerBound.Value * 1.5f);
  70. Assert.That(threshold.LowerWarningPercentage.Value, Is.EqualTo(1.5f));
  71. threshold.SetLimit(AtmosMonitorLimitType.UpperWarning, threshold.UpperBound.Value * 0.5f);
  72. Assert.That(threshold.UpperWarningPercentage.Value, Is.EqualTo(0.5f));
  73. threshold.SetLimit(AtmosMonitorLimitType.LowerWarning, threshold.LowerBound.Value * 1.5f);
  74. Assert.That(threshold.LowerWarningPercentage.Value, Is.EqualTo(1.5f));
  75. }
  76. // Check that the threshold reporting works correctly:
  77. {
  78. // Set threshold to some known state
  79. threshold.SetLimit(AtmosMonitorLimitType.UpperDanger, 5f);
  80. threshold.SetEnabled(AtmosMonitorLimitType.UpperDanger, true);
  81. threshold.SetLimit(AtmosMonitorLimitType.LowerDanger, 1f);
  82. threshold.SetEnabled(AtmosMonitorLimitType.LowerDanger, true);
  83. threshold.SetLimit(AtmosMonitorLimitType.UpperWarning, 4f);
  84. threshold.SetEnabled(AtmosMonitorLimitType.UpperWarning, true);
  85. threshold.SetLimit(AtmosMonitorLimitType.LowerWarning, 2f);
  86. threshold.SetEnabled(AtmosMonitorLimitType.LowerWarning, true);
  87. // Check a value that's in between each upper/lower warning/panic:
  88. threshold.CheckThreshold(3f, out var alarmType);
  89. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Normal));
  90. threshold.CheckThreshold(1.5f, out alarmType);
  91. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Warning));
  92. threshold.CheckThreshold(4.5f, out alarmType);
  93. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Warning));
  94. threshold.CheckThreshold(5.5f, out alarmType);
  95. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Danger));
  96. threshold.CheckThreshold(0.5f, out alarmType);
  97. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Danger));
  98. // Check that enable/disable is respected:
  99. threshold.CheckThreshold(123.4f, out alarmType);
  100. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Danger));
  101. threshold.SetEnabled(AtmosMonitorLimitType.UpperDanger, false);
  102. threshold.CheckThreshold(123.4f, out alarmType);
  103. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Warning));
  104. threshold.SetEnabled(AtmosMonitorLimitType.UpperWarning, false);
  105. threshold.CheckThreshold(123.4f, out alarmType);
  106. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Normal));
  107. // And for lower thresholds:
  108. threshold.CheckThreshold(0.01f, out alarmType);
  109. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Danger));
  110. threshold.SetEnabled(AtmosMonitorLimitType.LowerDanger, false);
  111. threshold.CheckThreshold(0.01f, out alarmType);
  112. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Warning));
  113. threshold.SetEnabled(AtmosMonitorLimitType.LowerWarning, false);
  114. threshold.CheckThreshold(0.01f, out alarmType);
  115. Assert.That(alarmType, Is.EqualTo(AtmosAlarmType.Normal));
  116. }
  117. });
  118. await pair.CleanReturnAsync();
  119. }
  120. }
  121. }