feat: migrate boiler control implementation to BoilerMonitor namespace and add budget handling logic
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
using BoilerMonitor.Models.Enums;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using BoilerMonitorManager;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
public class BoilerControlServiceTests
|
||||
{
|
||||
private readonly BoilerControlService _sut;
|
||||
|
||||
public BoilerControlServiceTests()
|
||||
{
|
||||
var loggerMock = new Mock<ILogger<BoilerControlService>>();
|
||||
_sut = new BoilerControlService(loggerMock.Object);
|
||||
}
|
||||
|
||||
#region TurnOn scenarios
|
||||
|
||||
[Theory]
|
||||
[InlineData("off")]
|
||||
[InlineData("OFF")]
|
||||
[InlineData("Off")]
|
||||
[InlineData(null)]
|
||||
[InlineData("unavailable")]
|
||||
public void DetermineAction_WhenCheapAndWaterNotReadyAndBudgetAvailable_ShouldReturnTurnOn(string? switchState)
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: true, waterReady: false, currentSwitchState: switchState,
|
||||
budgetExhausted: false, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOn);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("off")]
|
||||
[InlineData(null)]
|
||||
public void DetermineAction_WhenBudgetExhaustedButOverrideOn_ShouldReturnTurnOn(string? switchState)
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: true, waterReady: false, currentSwitchState: switchState,
|
||||
budgetExhausted: true, budgetOverride: true);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOn);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region TurnOff scenarios
|
||||
|
||||
[Fact]
|
||||
public void DetermineAction_WhenNotCheapAndSwitchIsOn_ShouldReturnTurnOff()
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: false, waterReady: false, currentSwitchState: "on",
|
||||
budgetExhausted: false, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOff);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DetermineAction_WhenWaterReadyAndSwitchIsOn_ShouldReturnTurnOff()
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: true, waterReady: true, currentSwitchState: "on",
|
||||
budgetExhausted: false, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOff);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DetermineAction_WhenBudgetExhaustedAndNoOverride_ShouldReturnTurnOff()
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: true, waterReady: false, currentSwitchState: "on",
|
||||
budgetExhausted: true, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOff);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("on")]
|
||||
[InlineData("ON")]
|
||||
[InlineData("On")]
|
||||
public void DetermineAction_WhenNotCheapAndSwitchIsOnCaseInsensitive_ShouldReturnTurnOff(string switchState)
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: false, waterReady: false, currentSwitchState: switchState,
|
||||
budgetExhausted: false, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOff);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region NoChange scenarios
|
||||
|
||||
[Fact]
|
||||
public void DetermineAction_WhenCheapAndNotReadyAndAlreadyOn_ShouldReturnNoChange()
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: true, waterReady: false, currentSwitchState: "on",
|
||||
budgetExhausted: false, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.NoChange);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DetermineAction_WhenNotCheapAndAlreadyOff_ShouldReturnNoChange()
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: false, waterReady: false, currentSwitchState: "off",
|
||||
budgetExhausted: false, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.NoChange);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DetermineAction_WhenBudgetExhaustedAndAlreadyOff_ShouldReturnNoChange()
|
||||
{
|
||||
var result = _sut.DetermineAction(
|
||||
isNowCheap: true, waterReady: false, currentSwitchState: "off",
|
||||
budgetExhausted: true, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.NoChange);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comprehensive theory covering all key combinations
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, false, "off", false, false, BoilerAction.TurnOn)]
|
||||
[InlineData(true, false, "on", false, false, BoilerAction.NoChange)]
|
||||
[InlineData(true, true, "on", false, false, BoilerAction.TurnOff)]
|
||||
[InlineData(true, true, "off", false, false, BoilerAction.NoChange)]
|
||||
[InlineData(false, false, "on", false, false, BoilerAction.TurnOff)]
|
||||
[InlineData(false, false, "off", false, false, BoilerAction.NoChange)]
|
||||
[InlineData(true, false, "on", true, false, BoilerAction.TurnOff)]
|
||||
[InlineData(true, false, "off", true, false, BoilerAction.NoChange)]
|
||||
[InlineData(true, false, "off", true, true, BoilerAction.TurnOn)]
|
||||
[InlineData(true, false, "on", true, true, BoilerAction.NoChange)]
|
||||
public void DetermineAction_AllCombinations_ShouldReturnExpectedAction(
|
||||
bool isNowCheap, bool waterReady, string switchState,
|
||||
bool budgetExhausted, bool budgetOverride, BoilerAction expectedAction)
|
||||
{
|
||||
var result = _sut.DetermineAction(isNowCheap, waterReady, switchState, budgetExhausted, budgetOverride);
|
||||
|
||||
result.Should().Be(expectedAction);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="8.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.4.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BoilerMonitor.Manager\BoilerMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\BoilerMonitor.Models\BoilerMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,133 @@
|
||||
using FluentAssertions;
|
||||
using BoilerMonitorManager.Models;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
public class BoilerMonitorConfigTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenAllFieldsSet_ShouldReturnTrue()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = "sensor.power",
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = 30,
|
||||
HeatingPowerThresholdWatts = 5.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenPowerEntityMissing_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = null,
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = 30,
|
||||
HeatingPowerThresholdWatts = 5.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenSwitchEntityMissing_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = "sensor.power",
|
||||
BoilerSwitchEntityId = null,
|
||||
PollIntervalSeconds = 30,
|
||||
HeatingPowerThresholdWatts = 5.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenPollIntervalMissing_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = "sensor.power",
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = null,
|
||||
HeatingPowerThresholdWatts = 5.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenThresholdMissing_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = "sensor.power",
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = 30,
|
||||
HeatingPowerThresholdWatts = null
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenPowerEntityIsWhitespace_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = " ",
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = 30,
|
||||
HeatingPowerThresholdWatts = 5.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenPollIntervalIsZero_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = "sensor.power",
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = 0,
|
||||
HeatingPowerThresholdWatts = 5.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenThresholdIsZero_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = "sensor.power",
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = 30,
|
||||
HeatingPowerThresholdWatts = 0.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsFullyConfigured_WhenPollIntervalIsNegative_ShouldReturnFalse()
|
||||
{
|
||||
var config = new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = "sensor.power",
|
||||
BoilerSwitchEntityId = "switch.boiler",
|
||||
PollIntervalSeconds = -1,
|
||||
HeatingPowerThresholdWatts = 5.0
|
||||
};
|
||||
|
||||
config.IsFullyConfigured.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
using BoilerMonitor.Models;
|
||||
using BoilerMonitorManager;
|
||||
using BoilerMonitorManager.Models;
|
||||
using FluentAssertions;
|
||||
using HomeAssistant.Entities.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using static BoilerMonitor.Models.BoilerMonitorConsts;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
public class BoilerMonitorEntityServiceTests
|
||||
{
|
||||
private readonly Mock<IEntityManager> _entityManagerMock;
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<BoilerMonitorEntityService>> _loggerMock;
|
||||
private readonly BoilerMonitorEntityService _sut;
|
||||
|
||||
public BoilerMonitorEntityServiceTests()
|
||||
{
|
||||
_entityManagerMock = new Mock<IEntityManager>();
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<BoilerMonitorEntityService>>();
|
||||
_sut = new BoilerMonitorEntityService(
|
||||
_entityManagerMock.Object,
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object);
|
||||
}
|
||||
|
||||
private void SetupEntityState(string entityId, string? state)
|
||||
{
|
||||
if (state is null)
|
||||
{
|
||||
_haContextMock.Setup(h => h.GetState(entityId)).Returns((EntityState?)null);
|
||||
}
|
||||
else
|
||||
{
|
||||
_haContextMock.Setup(h => h.GetState(entityId))
|
||||
.Returns(new EntityState { State = state });
|
||||
}
|
||||
}
|
||||
|
||||
#region ReadPersistedState
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WithValidState_ShouldReturnPersistedValues()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "3600");
|
||||
SetupEntityState(SessionCountEntityId, "5");
|
||||
SetupEntityState(SkipPriceCapEntityId, "ON");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(3600));
|
||||
result.SessionCount.Should().Be(5);
|
||||
result.SkipPriceCapState.Should().Be("ON");
|
||||
result.BudgetOverrideState.Should().Be("OFF");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WithZeroRuntime_ShouldReturnZeroValues()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "0");
|
||||
SetupEntityState(SessionCountEntityId, "0");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result!.AccumulatedRunTime.Should().Be(TimeSpan.Zero);
|
||||
result.SessionCount.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenRuntimeIsUnavailable_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "unavailable");
|
||||
SetupEntityState(SessionCountEntityId, "5");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenRuntimeIsUnknown_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "unknown");
|
||||
SetupEntityState(SessionCountEntityId, "3");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenSessionCountIsUnavailable_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "1800");
|
||||
SetupEntityState(SessionCountEntityId, "unavailable");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenRuntimeEntityDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, null);
|
||||
SetupEntityState(SessionCountEntityId, "3");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenSessionCountEntityDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "1800");
|
||||
SetupEntityState(SessionCountEntityId, null);
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenRuntimeIsNonNumeric_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "not_a_number");
|
||||
SetupEntityState(SessionCountEntityId, "3");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenSessionCountIsNonNumeric_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "1800");
|
||||
SetupEntityState(SessionCountEntityId, "abc");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenRuntimeIsNegative_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "-100");
|
||||
SetupEntityState(SessionCountEntityId, "3");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenSessionCountIsNegative_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "1800");
|
||||
SetupEntityState(SessionCountEntityId, "-1");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WhenSwitchEntitiesAreNull_ShouldStillReturnWithNullSwitchStates()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "900");
|
||||
SetupEntityState(SessionCountEntityId, "2");
|
||||
SetupEntityState(SkipPriceCapEntityId, null);
|
||||
SetupEntityState(BudgetOverrideEntityId, null);
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(900));
|
||||
result.SessionCount.Should().Be(2);
|
||||
result.SkipPriceCapState.Should().BeNull();
|
||||
result.BudgetOverrideState.Should().BeNull();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")] // empty string
|
||||
[InlineData(" ")] // whitespace
|
||||
public void ReadPersistedState_WhenRuntimeIsEmptyOrWhitespace_ShouldReturnNull(string state)
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, state);
|
||||
SetupEntityState(SessionCountEntityId, "3");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadPersistedState_WithDecimalRuntime_ShouldParseCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntityState(TodayRuntimeEntityId, "1800.5");
|
||||
SetupEntityState(SessionCountEntityId, "2");
|
||||
SetupEntityState(SkipPriceCapEntityId, "OFF");
|
||||
SetupEntityState(BudgetOverrideEntityId, "OFF");
|
||||
|
||||
// Act
|
||||
var result = _sut.ReadPersistedState();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(1800.5));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ResetTransientEntitiesAsync
|
||||
|
||||
[Fact]
|
||||
public async Task ResetTransientEntitiesAsync_ShouldResetStatusAndWaterReady()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetTransientEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(StatusEntityId, StatusAwaitingConfiguration), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(WaterReadyEntityId, "unknown"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetTransientEntitiesAsync_ShouldNotResetRuntimeEntities()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetTransientEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(TodayRuntimeEntityId, It.IsAny<string>()), Times.Never);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(SessionCountEntityId, It.IsAny<string>()), Times.Never);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(BudgetPercentageEntityId, It.IsAny<string>()), Times.Never);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(BudgetExhaustedEntityId, It.IsAny<string>()), Times.Never);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(RemainingTimeEntityId, It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetTransientEntitiesAsync_ShouldNotResetSwitchEntities()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetTransientEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(SkipPriceCapEntityId, It.IsAny<string>()), Times.Never);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(BudgetOverrideEntityId, It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using FluentAssertions;
|
||||
using BoilerMonitorManager.Models;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
public class BoilerMonitorOptionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Defaults_ShouldHaveEnabledTrue()
|
||||
{
|
||||
var options = new BoilerMonitorOptions();
|
||||
|
||||
options.Enabled.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SectionName_ShouldBeBoilerMonitor()
|
||||
{
|
||||
BoilerMonitorOptions.SectionName.Should().Be("BoilerMonitor");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Enabled_WhenSetToFalse_ShouldReturnFalse()
|
||||
{
|
||||
var options = new BoilerMonitorOptions { Enabled = false };
|
||||
|
||||
options.Enabled.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
using FluentAssertions;
|
||||
using BoilerMonitorManager;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
public class BoilerRunTimeTrackerTests
|
||||
{
|
||||
private readonly BoilerRunTimeTracker _sut = new();
|
||||
|
||||
#region RecordPollResult
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_WhenHeating_ShouldAccumulateRunTime()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(90));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_WhenNotHeating_ShouldNotAccumulate()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
|
||||
_sut.RecordPollResult(false, interval);
|
||||
_sut.RecordPollResult(false, interval);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.Zero);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_WhenHeatingStarts_ShouldIncrementSessionCount()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
|
||||
_sut.RecordPollResult(true, interval);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.SessionCount.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_ContinuousHeating_ShouldCountAsOneSession()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.SessionCount.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_HeatingStopsAndRestarts_ShouldCountAsTwoSessions()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(false, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.SessionCount.Should().Be(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_MultipleSessions_ShouldAccumulateCorrectly()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(false, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(90));
|
||||
snapshot.SessionCount.Should().Be(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_WhenHeating_ShouldSetIsInSessionTrue()
|
||||
{
|
||||
_sut.RecordPollResult(true, TimeSpan.FromSeconds(30));
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.IsInSession.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordPollResult_WhenNotHeating_ShouldSetIsInSessionFalse()
|
||||
{
|
||||
_sut.RecordPollResult(true, TimeSpan.FromSeconds(30));
|
||||
_sut.RecordPollResult(false, TimeSpan.FromSeconds(30));
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.IsInSession.Should().BeFalse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Reset
|
||||
|
||||
[Fact]
|
||||
public void Reset_ShouldClearAllState()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
|
||||
_sut.Reset();
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.Zero);
|
||||
snapshot.SessionCount.Should().Be(0);
|
||||
snapshot.IsInSession.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_AfterReset_ShouldAccumulateFromZero()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(30);
|
||||
_sut.RecordPollResult(true, interval);
|
||||
_sut.Reset();
|
||||
_sut.RecordPollResult(true, interval);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(30));
|
||||
snapshot.SessionCount.Should().Be(1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RestoreFromSnapshot
|
||||
|
||||
[Fact]
|
||||
public void RestoreFromSnapshot_ShouldSetAccumulatedRunTimeAndSessionCount()
|
||||
{
|
||||
// Arrange
|
||||
var runTime = TimeSpan.FromMinutes(45);
|
||||
const int sessionCount = 3;
|
||||
|
||||
// Act
|
||||
_sut.RestoreFromSnapshot(runTime, sessionCount);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(runTime);
|
||||
snapshot.SessionCount.Should().Be(sessionCount);
|
||||
snapshot.IsInSession.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RestoreFromSnapshot_ShouldOverwriteExistingState()
|
||||
{
|
||||
// Arrange
|
||||
_sut.RecordPollResult(true, TimeSpan.FromSeconds(30));
|
||||
_sut.RecordPollResult(true, TimeSpan.FromSeconds(30));
|
||||
|
||||
var restoredRunTime = TimeSpan.FromHours(2);
|
||||
const int restoredSessions = 5;
|
||||
|
||||
// Act
|
||||
_sut.RestoreFromSnapshot(restoredRunTime, restoredSessions);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(restoredRunTime);
|
||||
snapshot.SessionCount.Should().Be(restoredSessions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RestoreFromSnapshot_ShouldAllowFurtherAccumulationFromRestoredState()
|
||||
{
|
||||
// Arrange
|
||||
var restoredRunTime = TimeSpan.FromMinutes(30);
|
||||
const int restoredSessions = 2;
|
||||
var pollInterval = TimeSpan.FromSeconds(30);
|
||||
|
||||
_sut.RestoreFromSnapshot(restoredRunTime, restoredSessions);
|
||||
|
||||
// Act
|
||||
_sut.RecordPollResult(true, pollInterval);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(restoredRunTime + pollInterval);
|
||||
snapshot.SessionCount.Should().Be(restoredSessions + 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RestoreFromSnapshot_WithZeroValues_ShouldSetToZero()
|
||||
{
|
||||
// Arrange
|
||||
_sut.RecordPollResult(true, TimeSpan.FromSeconds(30));
|
||||
|
||||
// Act
|
||||
_sut.RestoreFromSnapshot(TimeSpan.Zero, 0);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.Zero);
|
||||
snapshot.SessionCount.Should().Be(0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Thread safety
|
||||
|
||||
[Fact]
|
||||
public async Task RecordPollResult_ConcurrentCalls_ShouldNotCorruptState()
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(1);
|
||||
const int iterationsPerThread = 1000;
|
||||
const int threadCount = 4;
|
||||
|
||||
var tasks = Enumerable.Range(0, threadCount).Select(_ => Task.Run(() =>
|
||||
{
|
||||
for (var i = 0; i < iterationsPerThread; i++)
|
||||
{
|
||||
_sut.RecordPollResult(true, interval);
|
||||
}
|
||||
})).ToArray();
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.FromSeconds(iterationsPerThread * threadCount));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetSnapshot
|
||||
|
||||
[Fact]
|
||||
public void GetSnapshot_Initial_ShouldReturnZeroState()
|
||||
{
|
||||
var snapshot = _sut.GetSnapshot();
|
||||
|
||||
snapshot.AccumulatedRunTime.Should().Be(TimeSpan.Zero);
|
||||
snapshot.SessionCount.Should().Be(0);
|
||||
snapshot.IsInSession.Should().BeFalse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -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