Add shared test data builders and Qodana configuration

- Extract per-monitor *TestData helper classes and rework tests to use them
- Add boiler run-time tracker abstraction (implementation pending)
- Drop unused NordPool init/update-result types and stale PLAN.md
- Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
2026-11-29 21:30:13 +02:00
parent 1152462765
commit 5777e641f6
79 changed files with 7162 additions and 3196 deletions
@@ -11,15 +11,10 @@ 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;
private readonly FakeTimeProvider _timeProvider;
public WaterReadyServiceTests()
{
_loggerMock = new Mock<ILogger<WaterReadyService>>();
_timeProvider = new FakeTimeProvider(DateTimeOffset.UtcNow);
}
private readonly Mock<ILogger<WaterReadyService>> _loggerMock = new();
private readonly FakeTimeProvider _timeProvider = BoilerMonitorTestData.CreateTimeProvider();
private WaterReadyService CreateSut() => new(_loggerMock.Object, _timeProvider);
@@ -30,26 +25,36 @@ public class WaterReadyServiceTests
return sut;
}
private static bool TriggerWaterReady(WaterReadyService sut, double powerWatts = 0.0)
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 < BoilerMonitorConsts.ConsecutiveReadingsRequired; i++)
for (var i = 0; i < count; i++)
{
result = sut.IsWaterReady("on", powerWatts, Threshold);
result = sut.IsWaterReady(switchState, powerWatts, heatingThresholdWatts);
}
return result;
}
#region Heating observation prerequisite
[Fact]
public void IsWaterReady_SwitchOnLowPowerButNeverObservedHeating_ShouldReturnFalse()
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(10)]
public void IsWaterReady_WhenHeatingWasNeverObserved_ShouldAlwaysReturnFalse(int readingCount)
{
var sut = CreateSut();
var result = sut.IsWaterReady("on", 0.0, Threshold);
var result = FeedReadings(sut, readingCount);
result.Should().BeFalse();
sut.IsInCooldown.Should().BeFalse();
}
[Fact]
@@ -57,7 +62,7 @@ public class WaterReadyServiceTests
{
var sut = CreateSutWithHeatingObserved();
var result = TriggerWaterReady(sut);
var result = FeedReadings(sut, RequiredReadings);
result.Should().BeTrue();
}
@@ -66,11 +71,21 @@ public class WaterReadyServiceTests
public void RecordHeatingObserved_ShouldEnableDetection()
{
var sut = CreateSut();
sut.RecordHeatingObserved();
var result = TriggerWaterReady(sut);
FeedReadings(sut, RequiredReadings).Should().BeTrue();
}
result.Should().BeTrue();
[Fact]
public void RecordHeatingObserved_WhenCalledRepeatedly_ShouldStayEnabled()
{
var sut = CreateSut();
sut.RecordHeatingObserved();
sut.RecordHeatingObserved();
FeedReadings(sut, RequiredReadings).Should().BeTrue();
}
[Fact]
@@ -79,7 +94,7 @@ public class WaterReadyServiceTests
var sut = CreateSutWithHeatingObserved();
sut.NotifyTurnedOn();
var result = sut.IsWaterReady("on", 0.0, Threshold);
var result = FeedReadings(sut, 10);
result.Should().BeFalse("heating observation was reset by NotifyTurnedOn");
}
@@ -91,20 +106,32 @@ public class WaterReadyServiceTests
sut.NotifyTurnedOn();
sut.RecordHeatingObserved();
var result = TriggerWaterReady(sut);
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();
TriggerWaterReady(sut);
FeedReadings(sut, RequiredReadings);
sut.ResetCooldown();
sut.RecordHeatingObserved();
var result = TriggerWaterReady(sut);
var result = FeedReadings(sut, RequiredReadings);
result.Should().BeTrue("detection should work again after reset + new heating observation");
}
@@ -113,112 +140,99 @@ public class WaterReadyServiceTests
#region Debouncing (consecutive readings)
[Fact]
public void IsWaterReady_SingleBelowThresholdReading_ShouldReturnFalse()
[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 = sut.IsWaterReady("on", 0.0, Threshold);
var result = FeedReadings(sut, readingCount);
result.Should().BeFalse("only 1 consecutive reading, need {0}",
BoilerMonitorConsts.ConsecutiveReadingsRequired);
result.Should().Be(expected);
}
[Fact]
public void IsWaterReady_ConsecutiveBelowThresholdReadings_ShouldReturnTrue()
[Theory]
[InlineData(HighPowerWatts)]
[InlineData(Threshold)]
[InlineData(double.MaxValue)]
public void IsWaterReady_WhenReadingIsAtOrAboveThreshold_ShouldResetConsecutiveCount(double powerWatts)
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, RequiredReadings - 1).Should().BeFalse();
var result = TriggerWaterReady(sut);
sut.IsWaterReady("on", powerWatts, Threshold).Should().BeFalse("power is not below the threshold");
result.Should().BeTrue();
FeedReadings(sut, 1).Should().BeFalse("the counter was reset by the above-threshold reading");
FeedReadings(sut, RequiredReadings - 1).Should().BeTrue();
}
[Fact]
public void IsWaterReady_BelowThresholdResetByAboveThreshold_ShouldRequireConsecutiveAgain()
[Theory]
[InlineData(0)]
[InlineData(1)]
public void IsWaterReady_NotifyTurnedOn_ShouldResetConsecutiveCount(int belowThresholdBeforeTurnOn)
{
var sut = CreateSutWithHeatingObserved();
FeedReadings(sut, belowThresholdBeforeTurnOn);
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");
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
[Fact]
public void IsWaterReady_SwitchOnAndPowerAtThreshold_ShouldReturnFalse()
[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 = sut.IsWaterReady("on", Threshold, Threshold);
var result = FeedReadings(sut, 10, switchState);
result.Should().BeFalse();
}
[Fact]
public void IsWaterReady_SwitchOnAndHighPower_ShouldReturnFalse()
[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 = 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);
var result = FeedReadings(sut, 10, switchState, powerWatts);
result.Should().BeFalse();
}
@@ -226,33 +240,49 @@ public class WaterReadyServiceTests
[Theory]
[InlineData("ON")]
[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);
}
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)]
public void IsWaterReady_CustomThreshold_ShouldUseProvidedValue(double powerWatts, double threshold, bool expected)
[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 = false;
for (var i = 0; i < BoilerMonitorConsts.ConsecutiveReadingsRequired; i++)
{
result = sut.IsWaterReady("on", powerWatts, threshold);
}
var result = FeedReadings(sut, RequiredReadings, powerWatts: powerWatts, heatingThresholdWatts: heatingThresholdWatts);
result.Should().Be(expected);
}
@@ -261,60 +291,37 @@ public class WaterReadyServiceTests
#region Cooldown period
[Fact]
public void IsWaterReady_DuringCooldown_ShouldReturnTrueRegardlessOfSwitchState()
[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();
TriggerWaterReady(sut);
FeedReadings(sut, RequiredReadings).Should().BeTrue();
var result = sut.IsWaterReady("off", 0.0, Threshold);
var result = sut.IsWaterReady(switchState, powerWatts, Threshold);
result.Should().BeTrue();
}
[Fact]
public void IsWaterReady_DuringCooldown_ShouldReturnTrueEvenWithNullState()
[Theory]
[MemberData(nameof(BoilerMonitorTestData.GeneratedElapsedTimes), MemberType = typeof(BoilerMonitorTestData))]
public void IsWaterReady_CooldownDoesNotExpireByTime_ShouldStayActive(TimeSpan elapsed)
{
var sut = CreateSutWithHeatingObserved();
TriggerWaterReady(sut);
FeedReadings(sut, RequiredReadings);
var triggeredAt = sut.WaterReadySince;
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);
_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]
@@ -326,67 +333,65 @@ public class WaterReadyServiceTests
}
[Fact]
public void IsInCooldown_DoesNotExpireByTime_ShouldRemainTrueAfterHours()
public void ResetCooldown_ShouldClearActiveCooldownAndAllowDetectionToResume()
{
var sut = CreateSutWithHeatingObserved();
TriggerWaterReady(sut);
_timeProvider.Advance(TimeSpan.FromHours(8));
sut.IsInCooldown.Should().BeTrue();
}
[Fact]
public void ResetCooldown_ShouldClearActiveCooldown()
{
var sut = CreateSutWithHeatingObserved();
TriggerWaterReady(sut);
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_ShouldAllowNormalDetectionToResume()
public void ResetCooldown_WhenNoCooldownIsActive_ShouldBeANoOp()
{
var sut = CreateSutWithHeatingObserved();
TriggerWaterReady(sut);
var sut = CreateSut();
sut.ResetCooldown();
var result = sut.IsWaterReady("off", 0.0, Threshold);
result.Should().BeFalse();
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();
TriggerWaterReady(sut);
FeedReadings(sut, RequiredReadings);
var originalTimestamp = sut.WaterReadySince;
_timeProvider.Advance(TimeSpan.FromHours(2));
sut.IsWaterReady("on", 0.0, Threshold);
sut.IsWaterReady("on", 0.0, Threshold).Should().BeTrue();
sut.WaterReadySince.Should().Be(originalTimestamp);
}
[Fact]
public void IsWaterReady_HighPowerDoesNotTriggerCooldown_ShouldReturnFalse()
[Theory]
[InlineData(Threshold)]
[InlineData(HighPowerWatts)]
[InlineData(double.MaxValue)]
public void IsWaterReady_PowerAtOrAboveThreshold_ShouldNotStartCooldown(double powerWatts)
{
var sut = CreateSutWithHeatingObserved();
sut.IsWaterReady("on", HighPowerWatts, Threshold);
FeedReadings(sut, RequiredReadings, powerWatts: powerWatts).Should().BeFalse();
sut.IsInCooldown.Should().BeFalse();
sut.WaterReadySince.Should().BeNull();
}
[Fact]
public void NotifyTurnedOn_DuringActiveCooldown_ShouldNotClearCooldown()
{
var sut = CreateSutWithHeatingObserved();
TriggerWaterReady(sut);
FeedReadings(sut, RequiredReadings);
sut.NotifyTurnedOn();
@@ -397,60 +402,22 @@ public class WaterReadyServiceTests
#region Cooldown persistence
[Fact]
public void RestoreCooldownState_ShouldResumeActiveCooldown()
[Theory]
[MemberData(nameof(BoilerMonitorTestData.CooldownRestoreSamples), MemberType = typeof(BoilerMonitorTestData))]
public void RestoreCooldownState_ShouldRestoreOnlyTimestampsFromTodayUtc(
string scenario,
DateTimeOffset now,
DateTimeOffset cooldownSince,
bool expectedRestored)
{
var sut = CreateSut();
var cooldownStart = _timeProvider.GetUtcNow() - TimeSpan.FromMinutes(30);
var timeProvider = BoilerMonitorTestData.CreateTimeProvider(now);
var sut = new WaterReadyService(_loggerMock.Object, timeProvider);
sut.RestoreCooldownState(cooldownStart);
sut.RestoreCooldownState(cooldownSince);
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);
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]
@@ -459,7 +426,7 @@ public class WaterReadyServiceTests
var sut = CreateSutWithHeatingObserved();
var beforeTrigger = _timeProvider.GetUtcNow();
TriggerWaterReady(sut);
FeedReadings(sut, RequiredReadings).Should().BeTrue();
sut.WaterReadySince.Should().Be(beforeTrigger);
}