feat: migrate boiler control implementation to BoilerMonitor namespace and add budget handling logic
This commit is contained in:
@@ -0,0 +1,430 @@
|
||||
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 readonly Mock<ILogger<WaterReadyService>> _loggerMock;
|
||||
private readonly FakeTimeProvider _timeProvider;
|
||||
|
||||
public WaterReadyServiceTests()
|
||||
{
|
||||
_loggerMock = new Mock<ILogger<WaterReadyService>>();
|
||||
_timeProvider = new FakeTimeProvider(DateTimeOffset.UtcNow);
|
||||
}
|
||||
|
||||
private WaterReadyService CreateSut()
|
||||
{
|
||||
return new WaterReadyService(_loggerMock.Object, _timeProvider);
|
||||
}
|
||||
|
||||
#region IsWaterReady — basic behavior (no grace period active)
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_SwitchOnAndLowPower_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_SwitchOnAndPowerBelowThreshold_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", 4.9, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_SwitchOnAndPowerAtThreshold_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", 5.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_SwitchOnAndHighPower_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", 2000.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_SwitchOff_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("off", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_NullSwitchState_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady(null, 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_NullPowerWatts_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", null, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_BothNull_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady(null, null, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ON")]
|
||||
[InlineData("On")]
|
||||
[InlineData("on")]
|
||||
public void IsWaterReady_SwitchOnCaseInsensitive_ShouldReturnTrue(string switchState)
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady(switchState, 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
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)
|
||||
{
|
||||
// Arrange — fresh instance per case to avoid cooldown side-effects
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", powerWatts, threshold);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Warm-up grace period
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_DuringGracePeriod_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.NotifyTurnedOn();
|
||||
|
||||
// Act — still within grace period, low power should be suppressed
|
||||
var result = sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_AfterGracePeriodExpires_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.NotifyTurnedOn();
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WarmUpGracePeriod);
|
||||
|
||||
// Act — grace period has elapsed, low power means water is ready
|
||||
var result = sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_DuringGracePeriodWithHighPower_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.NotifyTurnedOn();
|
||||
|
||||
// Act — high power during grace period is still "not ready" (heating)
|
||||
var result = sut.IsWaterReady("on", 2000.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_JustBeforeGracePeriodExpires_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.NotifyTurnedOn();
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WarmUpGracePeriod - TimeSpan.FromSeconds(1));
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_WithoutNotifyTurnedOn_ShouldNotApplyGracePeriod()
|
||||
{
|
||||
// Arrange — no NotifyTurnedOn call, so no grace period should be active
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_MultipleTurnOns_ShouldResetGracePeriod()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.NotifyTurnedOn();
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WarmUpGracePeriod - TimeSpan.FromSeconds(10));
|
||||
|
||||
// Act — second turn-on resets the grace period
|
||||
sut.NotifyTurnedOn();
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(10));
|
||||
|
||||
var result = sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert — still within the new grace period
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_SwitchOffDuringGracePeriod_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.NotifyTurnedOn();
|
||||
|
||||
// Act — switch is off, so not ready regardless of grace period
|
||||
var result = sut.IsWaterReady("off", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Water ready cooldown period
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_DuringCooldown_ShouldReturnTrueRegardlessOfSwitchState()
|
||||
{
|
||||
// Arrange — trigger water ready detection, then turn switch off
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Act — switch is now off, but cooldown should keep reporting ready
|
||||
var result = sut.IsWaterReady("off", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_DuringCooldown_ShouldReturnTrueEvenWithNullState()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady(null, null, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_AfterCooldownExpires_ShouldResumeNormalDetection()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WaterReadyCooldownPeriod);
|
||||
|
||||
// Act — cooldown expired, switch is off → not ready
|
||||
var result = sut.IsWaterReady("off", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_JustBeforeCooldownExpires_ShouldStillReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WaterReadyCooldownPeriod - TimeSpan.FromSeconds(1));
|
||||
|
||||
// Act
|
||||
var result = sut.IsWaterReady("off", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsInCooldown_WhenCooldownActive_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Act & Assert
|
||||
sut.IsInCooldown.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsInCooldown_WhenNoCooldownTriggered_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act & Assert
|
||||
sut.IsInCooldown.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsInCooldown_AfterCooldownExpires_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WaterReadyCooldownPeriod);
|
||||
|
||||
// Act & Assert
|
||||
sut.IsInCooldown.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResetCooldown_ShouldClearActiveCooldown()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
sut.IsInCooldown.Should().BeTrue();
|
||||
|
||||
// Act
|
||||
sut.ResetCooldown();
|
||||
|
||||
// Assert
|
||||
sut.IsInCooldown.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResetCooldown_ShouldAllowBoilerToTurnOnAgain()
|
||||
{
|
||||
// Arrange — water becomes ready, then cooldown is reset (midnight)
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
sut.ResetCooldown();
|
||||
|
||||
// Act — switch is off, no power → not ready (normal detection resumes)
|
||||
var result = sut.IsWaterReady("off", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_CooldownOnlySetOnFirstDetection_ShouldNotResetTimestamp()
|
||||
{
|
||||
// Arrange — first detection sets _waterReadySince
|
||||
var sut = CreateSut();
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Advance halfway through cooldown
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WaterReadyCooldownPeriod / 2);
|
||||
|
||||
// Act — second detection during cooldown should NOT reset the timestamp
|
||||
sut.IsWaterReady("on", 0.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Advance remaining half
|
||||
_timeProvider.Advance(BoilerMonitorConsts.WaterReadyCooldownPeriod / 2);
|
||||
|
||||
// Assert — cooldown should have expired based on original timestamp
|
||||
sut.IsInCooldown.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWaterReady_HighPowerDoesNotTriggerCooldown_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act — water is not ready (high power), no cooldown should be triggered
|
||||
sut.IsWaterReady("on", 2000.0, BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts);
|
||||
|
||||
// Assert
|
||||
sut.IsInCooldown.Should().BeFalse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user