512 lines
14 KiB
C#
512 lines
14 KiB
C#
using BoilerMonitor.Models;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Time.Testing;
|
|
using Moq;
|
|
using BoilerMonitorManager;
|
|
|
|
namespace BoilerMonitor.Manager.Tests;
|
|
|
|
public class WaterReadyServiceTests
|
|
{
|
|
private const double Threshold = BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts;
|
|
private const double HighPowerWatts = 2150.0;
|
|
|
|
private readonly Mock<ILogger<WaterReadyService>> _loggerMock;
|
|
private readonly FakeTimeProvider _timeProvider;
|
|
|
|
public WaterReadyServiceTests()
|
|
{
|
|
_loggerMock = new Mock<ILogger<WaterReadyService>>();
|
|
_timeProvider = new FakeTimeProvider(DateTimeOffset.UtcNow);
|
|
}
|
|
|
|
private WaterReadyService CreateSut() => new(_loggerMock.Object, _timeProvider);
|
|
|
|
private WaterReadyService CreateSutWithHeatingObserved()
|
|
{
|
|
var sut = CreateSut();
|
|
sut.RecordHeatingObserved();
|
|
return sut;
|
|
}
|
|
|
|
private static bool TriggerWaterReady(WaterReadyService sut, double powerWatts = 0.0)
|
|
{
|
|
var result = false;
|
|
for (var i = 0; i < BoilerMonitorConsts.ConsecutiveReadingsRequired; i++)
|
|
{
|
|
result = sut.IsWaterReady("on", powerWatts, Threshold);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#region Heating observation prerequisite
|
|
|
|
[Fact]
|
|
public void IsWaterReady_SwitchOnLowPowerButNeverObservedHeating_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSut();
|
|
|
|
var result = sut.IsWaterReady("on", 0.0, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_AfterObservedHeatingAndConsecutiveLowPower_ShouldReturnTrue()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = TriggerWaterReady(sut);
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void RecordHeatingObserved_ShouldEnableDetection()
|
|
{
|
|
var sut = CreateSut();
|
|
sut.RecordHeatingObserved();
|
|
|
|
var result = TriggerWaterReady(sut);
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void NotifyTurnedOn_ShouldResetHasObservedHeating()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
sut.NotifyTurnedOn();
|
|
|
|
var result = sut.IsWaterReady("on", 0.0, Threshold);
|
|
|
|
result.Should().BeFalse("heating observation was reset by NotifyTurnedOn");
|
|
}
|
|
|
|
[Fact]
|
|
public void NotifyTurnedOn_ThenObserveHeating_ShouldReEnableDetection()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
sut.NotifyTurnedOn();
|
|
sut.RecordHeatingObserved();
|
|
|
|
var result = TriggerWaterReady(sut);
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_AfterResetAndReObservedHeating_ShouldTriggerAgain()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
sut.ResetCooldown();
|
|
|
|
sut.RecordHeatingObserved();
|
|
var result = TriggerWaterReady(sut);
|
|
|
|
result.Should().BeTrue("detection should work again after reset + new heating observation");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Debouncing (consecutive readings)
|
|
|
|
[Fact]
|
|
public void IsWaterReady_SingleBelowThresholdReading_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = sut.IsWaterReady("on", 0.0, Threshold);
|
|
|
|
result.Should().BeFalse("only 1 consecutive reading, need {0}",
|
|
BoilerMonitorConsts.ConsecutiveReadingsRequired);
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_ConsecutiveBelowThresholdReadings_ShouldReturnTrue()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = TriggerWaterReady(sut);
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_BelowThresholdResetByAboveThreshold_ShouldRequireConsecutiveAgain()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
sut.IsWaterReady("on", 0.0, Threshold);
|
|
sut.IsWaterReady("on", HighPowerWatts, Threshold);
|
|
var result = sut.IsWaterReady("on", 0.0, Threshold);
|
|
|
|
result.Should().BeFalse("counter was reset by above-threshold reading");
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_NotifyTurnedOn_ShouldResetConsecutiveCount()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
sut.IsWaterReady("on", 0.0, Threshold);
|
|
sut.NotifyTurnedOn();
|
|
sut.RecordHeatingObserved();
|
|
var result = sut.IsWaterReady("on", 0.0, Threshold);
|
|
|
|
result.Should().BeFalse("NotifyTurnedOn reset the consecutive counter");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Basic state checks
|
|
|
|
[Fact]
|
|
public void IsWaterReady_SwitchOnAndPowerAtThreshold_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = sut.IsWaterReady("on", Threshold, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_SwitchOnAndHighPower_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = sut.IsWaterReady("on", HighPowerWatts, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_SwitchOff_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = sut.IsWaterReady("off", 0.0, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_NullSwitchState_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = sut.IsWaterReady(null, 0.0, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_NullPowerWatts_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = sut.IsWaterReady("on", null, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_BothNull_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSut();
|
|
|
|
var result = sut.IsWaterReady(null, null, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("ON")]
|
|
[InlineData("On")]
|
|
[InlineData("on")]
|
|
public void IsWaterReady_SwitchOnCaseInsensitive_ShouldReturnTrue(string switchState)
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = false;
|
|
for (var i = 0; i < BoilerMonitorConsts.ConsecutiveReadingsRequired; i++)
|
|
{
|
|
result = sut.IsWaterReady(switchState, 0.0, Threshold);
|
|
}
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(9.9, 10.0, true)]
|
|
[InlineData(10.0, 10.0, false)]
|
|
[InlineData(10.1, 10.0, false)]
|
|
public void IsWaterReady_CustomThreshold_ShouldUseProvidedValue(double powerWatts, double threshold, bool expected)
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
var result = false;
|
|
for (var i = 0; i < BoilerMonitorConsts.ConsecutiveReadingsRequired; i++)
|
|
{
|
|
result = sut.IsWaterReady("on", powerWatts, threshold);
|
|
}
|
|
|
|
result.Should().Be(expected);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Cooldown period
|
|
|
|
[Fact]
|
|
public void IsWaterReady_DuringCooldown_ShouldReturnTrueRegardlessOfSwitchState()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
|
|
var result = sut.IsWaterReady("off", 0.0, Threshold);
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_DuringCooldown_ShouldReturnTrueEvenWithNullState()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
|
|
var result = sut.IsWaterReady(null, null, Threshold);
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_CooldownDoesNotExpireByTime_ShouldStayActiveAfterHours()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
_timeProvider.Advance(TimeSpan.FromHours(12));
|
|
|
|
var result = sut.IsWaterReady("off", 0.0, Threshold);
|
|
|
|
result.Should().BeTrue("cooldown only clears via explicit ResetCooldown, not by elapsed time");
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_CooldownClearedOnlyByExplicitReset_ShouldResumeAfterReset()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
_timeProvider.Advance(TimeSpan.FromHours(5));
|
|
sut.IsInCooldown.Should().BeTrue();
|
|
|
|
sut.ResetCooldown();
|
|
|
|
sut.IsWaterReady("off", 0.0, Threshold).Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsInCooldown_WhenCooldownActive_ShouldReturnTrue()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
|
|
sut.IsInCooldown.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsInCooldown_WhenNoCooldownTriggered_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSut();
|
|
|
|
sut.IsInCooldown.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsInCooldown_DoesNotExpireByTime_ShouldRemainTrueAfterHours()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
_timeProvider.Advance(TimeSpan.FromHours(8));
|
|
|
|
sut.IsInCooldown.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void ResetCooldown_ShouldClearActiveCooldown()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
sut.IsInCooldown.Should().BeTrue();
|
|
|
|
sut.ResetCooldown();
|
|
|
|
sut.IsInCooldown.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void ResetCooldown_ShouldAllowNormalDetectionToResume()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
sut.ResetCooldown();
|
|
|
|
var result = sut.IsWaterReady("off", 0.0, Threshold);
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_CooldownOnlySetOnFirstDetection_ShouldNotResetTimestamp()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
var originalTimestamp = sut.WaterReadySince;
|
|
|
|
_timeProvider.Advance(TimeSpan.FromHours(2));
|
|
sut.IsWaterReady("on", 0.0, Threshold);
|
|
|
|
sut.WaterReadySince.Should().Be(originalTimestamp);
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_HighPowerDoesNotTriggerCooldown_ShouldReturnFalse()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
|
|
sut.IsWaterReady("on", HighPowerWatts, Threshold);
|
|
|
|
sut.IsInCooldown.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void NotifyTurnedOn_DuringActiveCooldown_ShouldNotClearCooldown()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
TriggerWaterReady(sut);
|
|
|
|
sut.NotifyTurnedOn();
|
|
|
|
sut.IsInCooldown.Should().BeTrue("NotifyTurnedOn must not clear the cooldown");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Cooldown persistence
|
|
|
|
[Fact]
|
|
public void RestoreCooldownState_ShouldResumeActiveCooldown()
|
|
{
|
|
var sut = CreateSut();
|
|
var cooldownStart = _timeProvider.GetUtcNow() - TimeSpan.FromMinutes(30);
|
|
|
|
sut.RestoreCooldownState(cooldownStart);
|
|
|
|
sut.IsInCooldown.Should().BeTrue();
|
|
sut.IsWaterReady("off", 0.0, Threshold).Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void RestoreCooldownState_WithYesterdayTimestamp_ShouldNotActivateCooldown()
|
|
{
|
|
var sut = CreateSut();
|
|
var yesterday = _timeProvider.GetUtcNow().AddDays(-1);
|
|
|
|
sut.RestoreCooldownState(yesterday);
|
|
|
|
sut.IsInCooldown.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void RestoreCooldownState_WithTodayTimestamp_ShouldRestoreCooldown()
|
|
{
|
|
var sut = CreateSut();
|
|
var earlierToday = _timeProvider.GetUtcNow() - TimeSpan.FromHours(5);
|
|
|
|
sut.RestoreCooldownState(earlierToday);
|
|
|
|
sut.IsInCooldown.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void RestoreCooldownState_WithExactlyMidnightTimestamp_ShouldRestoreCooldown()
|
|
{
|
|
var sut = CreateSut();
|
|
var todayMidnight = new DateTimeOffset(_timeProvider.GetUtcNow().Date, TimeSpan.Zero);
|
|
|
|
sut.RestoreCooldownState(todayMidnight);
|
|
|
|
sut.IsInCooldown.Should().BeTrue("timestamp at today's midnight is not before today");
|
|
}
|
|
|
|
[Fact]
|
|
public void RestoreCooldownState_ShouldPreserveOriginalTimestamp()
|
|
{
|
|
var sut = CreateSut();
|
|
var cooldownStart = _timeProvider.GetUtcNow() - TimeSpan.FromMinutes(30);
|
|
|
|
sut.RestoreCooldownState(cooldownStart);
|
|
|
|
sut.WaterReadySince.Should().Be(cooldownStart);
|
|
}
|
|
|
|
[Fact]
|
|
public void WaterReadySince_ShouldReturnTimestampWhenCooldownActive()
|
|
{
|
|
var sut = CreateSutWithHeatingObserved();
|
|
var beforeTrigger = _timeProvider.GetUtcNow();
|
|
|
|
TriggerWaterReady(sut);
|
|
|
|
sut.WaterReadySince.Should().Be(beforeTrigger);
|
|
}
|
|
|
|
[Fact]
|
|
public void WaterReadySince_ShouldReturnNullWhenNoCooldown()
|
|
{
|
|
var sut = CreateSut();
|
|
|
|
sut.WaterReadySince.Should().BeNull();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region False positive scenario (the bug this fix addresses)
|
|
|
|
[Fact]
|
|
public void IsWaterReady_BoilerJustTurnedOnPowerStillZero_ShouldNotTriggerFalsePositive()
|
|
{
|
|
var sut = CreateSut();
|
|
sut.NotifyTurnedOn();
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
var result = sut.IsWaterReady("on", 0.0, Threshold);
|
|
result.Should().BeFalse("boiler was never observed heating — this is a ramp-up delay, not 'water ready'");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void IsWaterReady_FullHeatingCycle_ShouldDetectWaterReadyCorrectly()
|
|
{
|
|
var sut = CreateSut();
|
|
sut.NotifyTurnedOn();
|
|
|
|
sut.IsWaterReady("on", 0.0, Threshold).Should().BeFalse("ramp-up, no heating observed yet");
|
|
|
|
sut.RecordHeatingObserved();
|
|
sut.IsWaterReady("on", HighPowerWatts, Threshold).Should().BeFalse("actively heating");
|
|
sut.IsWaterReady("on", HighPowerWatts - 50, Threshold).Should().BeFalse("still heating");
|
|
|
|
sut.IsWaterReady("on", 0.0, Threshold).Should().BeFalse("first below-threshold reading, needs confirmation");
|
|
sut.IsWaterReady("on", 0.0, Threshold).Should().BeTrue("second consecutive below-threshold → water ready");
|
|
|
|
sut.IsInCooldown.Should().BeTrue();
|
|
}
|
|
|
|
#endregion
|
|
}
|