Files
HomeAssistant/BoilerMonitor.Manager.Tests/BoilerControlServiceTests.cs
T
gitea_admin 5777e641f6 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
2026-11-29 21:30:13 +02:00

200 lines
7.2 KiB
C#

using BoilerMonitor.Models.Enums;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Moq;
using BoilerMonitorManager;
namespace BoilerMonitor.Manager.Tests;
public class BoilerControlServiceTests
{
private readonly Mock<ILogger<BoilerControlService>> _loggerMock = new();
private BoilerControlService CreateSut() => new(_loggerMock.Object);
#region TurnOn scenarios
[Theory]
[InlineData("off")]
[InlineData("OFF")]
[InlineData("Off")]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("off ")]
[InlineData("unavailable")]
[InlineData("unknown")]
[InlineData("0")]
public void DetermineAction_WhenCheapAndWaterNotReadyAndBudgetAvailable_ShouldReturnTurnOn(string? switchState)
{
var result = CreateSut().DetermineAction(
isNowCheap: true, waterReady: false, currentSwitchState: switchState,
budgetExhausted: false, budgetOverride: false);
result.Should().Be(BoilerAction.TurnOn);
}
[Theory]
[InlineData("off", BoilerAction.TurnOn)]
[InlineData(null, BoilerAction.TurnOn)]
[InlineData("unavailable", BoilerAction.TurnOn)]
[InlineData("on", BoilerAction.NoChange)]
[InlineData("On", BoilerAction.NoChange)]
[InlineData("ON", BoilerAction.NoChange)]
public void DetermineAction_WhenBudgetExhaustedButOverrideOn_ShouldReturnTurnOnOnlyIfNotAlreadyOn(
string? switchState,
BoilerAction expectedAction)
{
var result = CreateSut().DetermineAction(
isNowCheap: true, waterReady: false, currentSwitchState: switchState,
budgetExhausted: true, budgetOverride: true);
result.Should().Be(expectedAction);
}
#endregion
#region TurnOff scenarios
[Theory]
[InlineData("on")]
[InlineData("ON")]
[InlineData("On")]
[InlineData("oN")]
public void DetermineAction_WhenNotCheapAndSwitchIsOn_ShouldReturnTurnOff(string switchState)
{
var result = CreateSut().DetermineAction(
isNowCheap: false, waterReady: false, currentSwitchState: switchState,
budgetExhausted: false, budgetOverride: false);
result.Should().Be(BoilerAction.TurnOff);
}
[Theory]
[InlineData(true, false, false)] // water ready
[InlineData(false, true, false)] // budget exhausted, no override
[InlineData(true, true, true)] // water ready wins over the budget override
public void DetermineAction_WhenCheapButShouldNotHeatAndSwitchIsOn_ShouldReturnTurnOff(
bool waterReady,
bool budgetExhausted,
bool budgetOverride)
{
var result = CreateSut().DetermineAction(
isNowCheap: true, waterReady: waterReady, currentSwitchState: "on",
budgetExhausted: budgetExhausted, budgetOverride: budgetOverride);
result.Should().Be(BoilerAction.TurnOff);
}
#endregion
#region NoChange scenarios
[Theory]
[InlineData(true, false, "on", false, false)]
[InlineData(false, false, "off", false, false)]
[InlineData(false, false, null, false, false)]
[InlineData(false, false, "unavailable", false, false)]
[InlineData(true, true, "off", false, false)]
[InlineData(true, true, null, false, false)]
[InlineData(true, false, "off", true, false)]
[InlineData(true, false, "off ", true, false)]
[InlineData(true, true, "off", true, true)]
public void DetermineAction_WhenDesiredStateMatchesCurrentState_ShouldReturnNoChange(
bool isNowCheap,
bool waterReady,
string? switchState,
bool budgetExhausted,
bool budgetOverride)
{
var result = CreateSut().DetermineAction(
isNowCheap, waterReady, switchState, budgetExhausted, budgetOverride);
result.Should().Be(BoilerAction.NoChange);
}
#endregion
#region Comprehensive truth tables
/// <summary>
/// Complete truth table for a boiler that is currently ON: it may only stay on when
/// it is cheap, water is not ready and the budget is available (or overridden).
/// </summary>
public static IEnumerable<object[]> SwitchOnActionMatrix =>
[
[false, false, false, false, BoilerAction.TurnOff],
[false, false, false, true, BoilerAction.TurnOff],
[false, false, true, false, BoilerAction.TurnOff],
[false, false, true, true, BoilerAction.TurnOff],
[false, true, false, false, BoilerAction.TurnOff],
[false, true, false, true, BoilerAction.TurnOff],
[false, true, true, false, BoilerAction.TurnOff],
[false, true, true, true, BoilerAction.TurnOff],
[true, false, false, false, BoilerAction.NoChange],
[true, false, false, true, BoilerAction.NoChange],
[true, false, true, false, BoilerAction.TurnOff],
[true, false, true, true, BoilerAction.NoChange],
[true, true, false, false, BoilerAction.TurnOff],
[true, true, false, true, BoilerAction.TurnOff],
[true, true, true, false, BoilerAction.TurnOff],
[true, true, true, true, BoilerAction.TurnOff]
];
[Theory]
[MemberData(nameof(SwitchOnActionMatrix))]
public void DetermineAction_AllCombinationsWhileSwitchIsOn_ShouldReturnExpectedAction(
bool isNowCheap,
bool waterReady,
bool budgetExhausted,
bool budgetOverride,
BoilerAction expectedAction)
{
var result = CreateSut().DetermineAction(
isNowCheap, waterReady, currentSwitchState: "on", budgetExhausted, budgetOverride);
result.Should().Be(expectedAction);
}
/// <summary>
/// Complete truth table for a boiler that is currently OFF: it may only turn on when
/// it is cheap, water is not ready and the budget is available (or overridden).
/// </summary>
public static IEnumerable<object[]> SwitchOffActionMatrix =>
[
[false, false, false, false, BoilerAction.NoChange],
[false, false, false, true, BoilerAction.NoChange],
[false, false, true, false, BoilerAction.NoChange],
[false, false, true, true, BoilerAction.NoChange],
[false, true, false, false, BoilerAction.NoChange],
[false, true, false, true, BoilerAction.NoChange],
[false, true, true, false, BoilerAction.NoChange],
[false, true, true, true, BoilerAction.NoChange],
[true, false, false, false, BoilerAction.TurnOn],
[true, false, false, true, BoilerAction.TurnOn],
[true, false, true, false, BoilerAction.NoChange],
[true, false, true, true, BoilerAction.TurnOn],
[true, true, false, false, BoilerAction.NoChange],
[true, true, false, true, BoilerAction.NoChange],
[true, true, true, false, BoilerAction.NoChange],
[true, true, true, true, BoilerAction.NoChange]
];
[Theory]
[MemberData(nameof(SwitchOffActionMatrix))]
public void DetermineAction_AllCombinationsWhileSwitchIsOff_ShouldReturnExpectedAction(
bool isNowCheap,
bool waterReady,
bool budgetExhausted,
bool budgetOverride,
BoilerAction expectedAction)
{
var result = CreateSut().DetermineAction(
isNowCheap, waterReady, currentSwitchState: "off", budgetExhausted, budgetOverride);
result.Should().Be(expectedAction);
}
#endregion
}