Files
HomeAssistant/BoilerMonitor.Manager.Tests/WaterReadyServiceTests.cs
T
gitea_admin 24729ee267 Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files
- Collapse single-statement guard blocks and tidy using and member order
- Normalize XML doc indentation and file endings
- Reorder BatteryMonitor test-data helpers and move the exclusion tests into
  their own region
- Carry the same formatting through the boiler config, control, entity and
  test files
2026-11-30 11:52:06 +02:00

479 lines
14 KiB
C#

using BoilerMonitor.Models;
using BoilerMonitorManager;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Time.Testing;
using Moq;
namespace BoilerMonitor.Manager.Tests;
public class WaterReadyServiceTests
{
private const double Threshold = BoilerMonitorConsts.DefaultHeatingPowerThresholdWatts;
private const double HighPowerWatts = 2150.0;
private const int RequiredReadings = BoilerMonitorConsts.ConsecutiveReadingsRequired;
private readonly Mock<ILogger<WaterReadyService>> _loggerMock = new();
private readonly FakeTimeProvider _timeProvider = BoilerMonitorTestData.CreateTimeProvider();
private WaterReadyService CreateSut()
{
return new WaterReadyService(_loggerMock.Object, _timeProvider);
}
private WaterReadyService CreateSutWithHeatingObserved()
{
var sut = CreateSut();
sut.RecordHeatingObserved();
return sut;
}
private static bool FeedReadings(
WaterReadyService sut,
int count,
string? switchState = "on",
double? powerWatts = 0.0,
double heatingThresholdWatts = Threshold)
{
var result = false;
for (var i = 0; i < count; i++) result = sut.IsWaterReady(switchState, powerWatts, heatingThresholdWatts);
return result;
}
#region Heating observation prerequisite
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(10)]
public void IsWaterReady_WhenHeatingWasNeverObserved_ShouldAlwaysReturnFalse(int readingCount)
{
var sut = CreateSut();
var result = FeedReadings(sut, readingCount);
result.Should().BeFalse();
sut.IsInCooldown.Should().BeFalse();
}
[Fact]
public void IsWaterReady_AfterObservedHeatingAndConsecutiveLowPower_ShouldReturnTrue()
{
var sut = CreateSutWithHeatingObserved();
var result = FeedReadings(sut, RequiredReadings);
result.Should().BeTrue();
}
[Fact]
public void RecordHeatingObserved_ShouldEnableDetection()
{
var sut = CreateSut();
sut.RecordHeatingObserved();
FeedReadings(sut, RequiredReadings).Should().BeTrue();
}
[Fact]
public void RecordHeatingObserved_WhenCalledRepeatedly_ShouldStayEnabled()
{
var sut = CreateSut();
sut.RecordHeatingObserved();
sut.RecordHeatingObserved();
FeedReadings(sut, RequiredReadings).Should().BeTrue();
}
[Fact]
public void NotifyTurnedOn_ShouldResetHasObservedHeating()
{
var sut = CreateSutWithHeatingObserved();
sut.NotifyTurnedOn();
var result = FeedReadings(sut, 10);
result.Should().BeFalse("heating observation was reset by NotifyTurnedOn");
}
[Fact]
public void NotifyTurnedOn_ThenObserveHeating_ShouldReEnableDetection()
{
var sut = CreateSutWithHeatingObserved();
sut.NotifyTurnedOn();
sut.RecordHeatingObserved();
var result = FeedReadings(sut, RequiredReadings);
result.Should().BeTrue();
}
[Fact]
public void ResetCooldown_ShouldKeepHeatingObservationEnabled()
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings).Should().BeTrue();
sut.ResetCooldown();
FeedReadings(sut, RequiredReadings).Should().BeTrue(
"ResetCooldown clears the cooldown and counter, not the heating observation");
}
[Fact]
public void IsWaterReady_AfterResetAndReObservedHeating_ShouldTriggerAgain()
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings);
sut.ResetCooldown();
sut.RecordHeatingObserved();
var result = FeedReadings(sut, RequiredReadings);
result.Should().BeTrue("detection should work again after reset + new heating observation");
}
#endregion
#region Debouncing (consecutive readings)
[Theory]
[InlineData(1, false)]
[InlineData(2, true)]
[InlineData(3, true)]
[InlineData(10, true)]
public void IsWaterReady_ShouldRequireConsecutiveBelowThresholdReadings(int readingCount, bool expected)
{
var sut = CreateSutWithHeatingObserved();
var result = FeedReadings(sut, readingCount);
result.Should().Be(expected);
}
[Theory]
[InlineData(HighPowerWatts)]
[InlineData(Threshold)]
[InlineData(double.MaxValue)]
public void IsWaterReady_WhenReadingIsAtOrAboveThreshold_ShouldResetConsecutiveCount(double powerWatts)
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings - 1).Should().BeFalse();
sut.IsWaterReady("on", powerWatts, Threshold).Should().BeFalse("power is not below the threshold");
FeedReadings(sut, 1).Should().BeFalse("the counter was reset by the above-threshold reading");
FeedReadings(sut, RequiredReadings - 1).Should().BeTrue();
}
[Theory]
[InlineData(0)]
[InlineData(1)]
public void IsWaterReady_NotifyTurnedOn_ShouldResetConsecutiveCount(int belowThresholdBeforeTurnOn)
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, belowThresholdBeforeTurnOn);
sut.NotifyTurnedOn();
sut.RecordHeatingObserved();
FeedReadings(sut, RequiredReadings - 1).Should().BeFalse("NotifyTurnedOn reset the consecutive counter");
FeedReadings(sut, 1).Should().BeTrue();
}
[Fact]
public void IsWaterReady_InterleavedReadings_ShouldOnlyTriggerAfterLastConsecutiveLowReading()
{
var sut = CreateSutWithHeatingObserved();
var results = new[]
{
sut.IsWaterReady("on", 0.0, Threshold),
sut.IsWaterReady("on", HighPowerWatts, Threshold),
sut.IsWaterReady("on", 0.0, Threshold),
sut.IsWaterReady("on", 0.0, Threshold)
};
results.Should().Equal(false, false, false, true);
}
#endregion
#region Basic state checks
[Theory]
[InlineData("off")]
[InlineData("OFF")]
[InlineData(" off")]
[InlineData("on ")]
[InlineData("unknown")]
[InlineData("unavailable")]
[InlineData("yes")]
[InlineData("1")]
public void IsWaterReady_WhenSwitchIsNotOn_ShouldReturnFalse(string switchState)
{
var sut = CreateSutWithHeatingObserved();
var result = FeedReadings(sut, 10, switchState);
result.Should().BeFalse();
}
[Theory]
[InlineData("on", null)]
[InlineData(null, 0.0)]
[InlineData(null, null)]
[InlineData("", null)]
[InlineData("", 0.0)]
public void IsWaterReady_WhenSwitchStateOrPowerIsNull_ShouldReturnFalse(string? switchState, double? powerWatts)
{
var sut = CreateSutWithHeatingObserved();
var result = FeedReadings(sut, 10, switchState, powerWatts);
result.Should().BeFalse();
}
[Theory]
[InlineData("ON")]
[InlineData("On")]
[InlineData("oN")]
public void IsWaterReady_SwitchOnCaseInsensitive_ShouldReturnTrue(string switchState)
{
var sut = CreateSutWithHeatingObserved();
var result = FeedReadings(sut, RequiredReadings, switchState);
result.Should().BeTrue();
}
[Theory]
[MemberData(nameof(BoilerMonitorTestData.PowerReadingSamples), MemberType = typeof(BoilerMonitorTestData))]
public void IsWaterReady_PowerAroundThreshold_ShouldFollowThresholdRule(
string scenario,
double powerWatts,
bool expectedBelowThreshold)
{
var sut = CreateSutWithHeatingObserved();
var result = FeedReadings(sut, RequiredReadings, powerWatts: powerWatts);
result.Should().Be(
expectedBelowThreshold,
$"{scenario}: readings below {Threshold}W are counted as 'not heating'");
}
[Theory]
[InlineData(9.9, 10.0, true)]
[InlineData(10.0, 10.0, false)]
[InlineData(10.1, 10.0, false)]
[InlineData(0.0, 0.0, false)]
[InlineData(-10.0, -5.0, true)]
[InlineData(-0.001, 0.0, true)]
[InlineData(0.0, -5.0, false)]
[InlineData(10.0, 0.0, false)]
public void IsWaterReady_CustomThreshold_ShouldUseProvidedValue(
double powerWatts,
double heatingThresholdWatts,
bool expected)
{
var sut = CreateSutWithHeatingObserved();
var result = FeedReadings(sut, RequiredReadings, powerWatts: powerWatts,
heatingThresholdWatts: heatingThresholdWatts);
result.Should().Be(expected);
}
#endregion
#region Cooldown period
[Theory]
[InlineData("off", 0.0)]
[InlineData("off", null)]
[InlineData(null, 0.0)]
[InlineData(null, null)]
[InlineData("on", HighPowerWatts)]
[InlineData("unknown", 12.0)]
public void IsWaterReady_DuringCooldown_ShouldReturnTrueRegardlessOfInputs(string? switchState, double? powerWatts)
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings).Should().BeTrue();
var result = sut.IsWaterReady(switchState, powerWatts, Threshold);
result.Should().BeTrue();
}
[Theory]
[MemberData(nameof(BoilerMonitorTestData.GeneratedElapsedTimes), MemberType = typeof(BoilerMonitorTestData))]
public void IsWaterReady_CooldownDoesNotExpireByTime_ShouldStayActive(TimeSpan elapsed)
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings);
var triggeredAt = sut.WaterReadySince;
_timeProvider.Advance(elapsed);
sut.IsInCooldown.Should().BeTrue();
sut.IsWaterReady("off", 0.0, Threshold).Should().BeTrue(
"cooldown only clears via explicit ResetCooldown, not by elapsed time");
sut.WaterReadySince.Should().Be(triggeredAt);
}
[Fact]
public void IsInCooldown_WhenNoCooldownTriggered_ShouldReturnFalse()
{
var sut = CreateSut();
sut.IsInCooldown.Should().BeFalse();
}
[Fact]
public void ResetCooldown_ShouldClearActiveCooldownAndAllowDetectionToResume()
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings);
sut.IsInCooldown.Should().BeTrue();
sut.ResetCooldown();
sut.IsInCooldown.Should().BeFalse();
sut.WaterReadySince.Should().BeNull();
sut.IsWaterReady("off", 0.0, Threshold).Should().BeFalse();
FeedReadings(sut, RequiredReadings - 1).Should().BeFalse();
FeedReadings(sut, 1).Should().BeTrue();
}
[Fact]
public void ResetCooldown_WhenNoCooldownIsActive_ShouldBeANoOp()
{
var sut = CreateSut();
sut.ResetCooldown();
sut.IsInCooldown.Should().BeFalse();
sut.WaterReadySince.Should().BeNull();
FeedReadings(sut, 10).Should().BeFalse("heating was never observed");
}
[Fact]
public void IsWaterReady_CooldownOnlySetOnFirstDetection_ShouldNotResetTimestamp()
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings);
var originalTimestamp = sut.WaterReadySince;
_timeProvider.Advance(TimeSpan.FromHours(2));
sut.IsWaterReady("on", 0.0, Threshold).Should().BeTrue();
sut.WaterReadySince.Should().Be(originalTimestamp);
}
[Theory]
[InlineData(Threshold)]
[InlineData(HighPowerWatts)]
[InlineData(double.MaxValue)]
public void IsWaterReady_PowerAtOrAboveThreshold_ShouldNotStartCooldown(double powerWatts)
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings, powerWatts: powerWatts).Should().BeFalse();
sut.IsInCooldown.Should().BeFalse();
sut.WaterReadySince.Should().BeNull();
}
[Fact]
public void NotifyTurnedOn_DuringActiveCooldown_ShouldNotClearCooldown()
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings);
sut.NotifyTurnedOn();
sut.IsInCooldown.Should().BeTrue("NotifyTurnedOn must not clear the cooldown");
}
#endregion
#region Cooldown persistence
[Theory]
[MemberData(nameof(BoilerMonitorTestData.CooldownRestoreSamples), MemberType = typeof(BoilerMonitorTestData))]
public void RestoreCooldownState_ShouldRestoreOnlyTimestampsFromTodayUtc(
string scenario,
DateTimeOffset now,
DateTimeOffset cooldownSince,
bool expectedRestored)
{
var timeProvider = BoilerMonitorTestData.CreateTimeProvider(now);
var sut = new WaterReadyService(_loggerMock.Object, timeProvider);
sut.RestoreCooldownState(cooldownSince);
sut.IsInCooldown.Should().Be(expectedRestored, scenario);
sut.WaterReadySince.Should().Be(expectedRestored ? cooldownSince : null, scenario);
sut.IsWaterReady("off", 0.0, Threshold).Should().Be(expectedRestored, scenario);
}
[Fact]
public void WaterReadySince_ShouldReturnTimestampWhenCooldownActive()
{
var sut = CreateSutWithHeatingObserved();
var beforeTrigger = _timeProvider.GetUtcNow();
FeedReadings(sut, RequiredReadings).Should().BeTrue();
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
}