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
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using BoilerMonitor.Models.Enums;
|
||||
using BoilerMonitorManager;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using BoilerMonitorManager;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
@@ -10,84 +10,11 @@ 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)
|
||||
private BoilerControlService CreateSut()
|
||||
{
|
||||
var result = CreateSut().DetermineAction(
|
||||
isNowCheap: true, waterReady: false, currentSwitchState: switchState,
|
||||
budgetExhausted: false, budgetOverride: false);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOn);
|
||||
return new BoilerControlService(_loggerMock.Object);
|
||||
}
|
||||
|
||||
[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]
|
||||
@@ -115,11 +42,87 @@ public class BoilerControlServiceTests
|
||||
|
||||
#endregion
|
||||
|
||||
#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(
|
||||
true, false, switchState,
|
||||
false, 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(
|
||||
true, false, switchState,
|
||||
true, 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(
|
||||
false, false, switchState,
|
||||
false, 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(
|
||||
true, waterReady, "on",
|
||||
budgetExhausted, budgetOverride);
|
||||
|
||||
result.Should().Be(BoilerAction.TurnOff);
|
||||
}
|
||||
|
||||
#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).
|
||||
/// 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 TheoryData<bool, bool, bool, bool, BoilerAction> SwitchOnActionMatrix => new()
|
||||
{
|
||||
@@ -151,14 +154,14 @@ public class BoilerControlServiceTests
|
||||
BoilerAction expectedAction)
|
||||
{
|
||||
var result = CreateSut().DetermineAction(
|
||||
isNowCheap, waterReady, currentSwitchState: "on", budgetExhausted, budgetOverride);
|
||||
isNowCheap, waterReady, "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).
|
||||
/// 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 TheoryData<bool, bool, bool, bool, BoilerAction> SwitchOffActionMatrix => new()
|
||||
{
|
||||
@@ -190,10 +193,10 @@ public class BoilerControlServiceTests
|
||||
BoilerAction expectedAction)
|
||||
{
|
||||
var result = CreateSut().DetermineAction(
|
||||
isNowCheap, waterReady, currentSwitchState: "off", budgetExhausted, budgetOverride);
|
||||
isNowCheap, waterReady, "off", budgetExhausted, budgetOverride);
|
||||
|
||||
result.Should().Be(expectedAction);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,38 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="10.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="11.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.11.0" />
|
||||
<PackageReference Include="Moq" Version="4.21.0" />
|
||||
<PackageReference Include="NetDaemon.Extensions.Mqtt" Version="26.43.1" />
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.43.1" />
|
||||
<PackageReference Include="System.Reactive" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="10.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="11.0.1"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0"/>
|
||||
<PackageReference Include="xunit" Version="2.9.3"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.11.0"/>
|
||||
<PackageReference Include="Moq" Version="4.21.0"/>
|
||||
<PackageReference Include="NetDaemon.Extensions.Mqtt" Version="26.43.1"/>
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.43.1"/>
|
||||
<PackageReference Include="System.Reactive" Version="7.0.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BoilerMonitor.Manager\BoilerMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\BoilerMonitor.Models\BoilerMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BoilerMonitor.Manager\BoilerMonitor.Manager.csproj"/>
|
||||
<ProjectReference Include="..\BoilerMonitor.Models\BoilerMonitor.Models.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -50,10 +50,10 @@ public class BoilerMonitorConfigTests
|
||||
bool expected)
|
||||
{
|
||||
var config = BoilerMonitorTestData.CreateConfig(
|
||||
boilerPowerEntityId: powerEntityId,
|
||||
boilerSwitchEntityId: switchEntityId,
|
||||
pollIntervalSeconds: pollIntervalSeconds,
|
||||
heatingPowerThresholdWatts: heatingPowerThresholdWatts);
|
||||
powerEntityId,
|
||||
switchEntityId,
|
||||
pollIntervalSeconds,
|
||||
heatingPowerThresholdWatts);
|
||||
|
||||
config.IsFullyConfigured.Should().Be(expected, scenario);
|
||||
}
|
||||
@@ -62,9 +62,9 @@ public class BoilerMonitorConfigTests
|
||||
public void IsFullyConfigured_WhenFullyPopulated_ShouldNotDependOnEntityIdCasingOrPadding()
|
||||
{
|
||||
var config = BoilerMonitorTestData.CreateConfig(
|
||||
boilerPowerEntityId: " sensor.sonoff_1002230e32_power ",
|
||||
boilerSwitchEntityId: "SWITCH.SONOFF_1002230E32");
|
||||
" sensor.sonoff_1002230e32_power ",
|
||||
"SWITCH.SONOFF_1002230E32");
|
||||
|
||||
config.IsFullyConfigured.Should().BeTrue("entity ids are only checked for non-whitespace content");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,10 +47,13 @@ public class BoilerMonitorEntityServiceTests
|
||||
.Returns(Task.CompletedTask);
|
||||
}
|
||||
|
||||
private BoilerMonitorEntityService CreateSut() => new(
|
||||
_entityManagerMock.Object,
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object);
|
||||
private BoilerMonitorEntityService CreateSut()
|
||||
{
|
||||
return new BoilerMonitorEntityService(
|
||||
_entityManagerMock.Object,
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object);
|
||||
}
|
||||
|
||||
#region ReadPersistedState
|
||||
|
||||
@@ -61,7 +64,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
TimeSpan? expectedRunTime)
|
||||
{
|
||||
_haContextMock.SetupEntityStates(
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(todayRuntime: runtimeState, sessionCount: "2"));
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(runtimeState, "2"));
|
||||
|
||||
var result = CreateSut().ReadPersistedState();
|
||||
|
||||
@@ -83,7 +86,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
int? expectedSessionCount)
|
||||
{
|
||||
_haContextMock.SetupEntityStates(
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(todayRuntime: "3600", sessionCount: sessionCountState));
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates("3600", sessionCountState));
|
||||
|
||||
var result = CreateSut().ReadPersistedState();
|
||||
|
||||
@@ -102,7 +105,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedState_WithValidState_ShouldReturnPersistedValues()
|
||||
{
|
||||
_haContextMock.SetupEntityStates(BoilerMonitorTestData.CreatePersistedEntityStates(
|
||||
todayRuntime: "7200", sessionCount: "3", skipPriceCap: "ON", budgetOverride: "OFF"));
|
||||
"7200", "3", "ON", "OFF"));
|
||||
|
||||
var result = CreateSut().ReadPersistedState();
|
||||
|
||||
@@ -135,7 +138,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedState_WhenBothRuntimeEntitiesDoNotExist_ShouldReturnNull()
|
||||
{
|
||||
_haContextMock.SetupEntityStates(
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(todayRuntime: null, sessionCount: null));
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(null, null));
|
||||
|
||||
CreateSut().ReadPersistedState().Should().BeNull();
|
||||
}
|
||||
@@ -242,11 +245,11 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task UpdateEntitiesAsync_ShouldWriteAllRuntimeEntitiesWithExpectedFormatting()
|
||||
{
|
||||
var context = BoilerMonitorTestData.CreateUpdateContext(
|
||||
todayRunTime: TimeSpan.FromSeconds(3723.9),
|
||||
remainingTime: TimeSpan.FromSeconds(42.9),
|
||||
budgetPercentage: 150.0,
|
||||
budgetExhausted: true,
|
||||
sessionCount: 4,
|
||||
TimeSpan.FromSeconds(3723.9),
|
||||
TimeSpan.FromSeconds(42.9),
|
||||
150.0,
|
||||
true,
|
||||
4,
|
||||
waterReady: true);
|
||||
|
||||
await CreateSut().UpdateEntitiesAsync(context);
|
||||
@@ -257,7 +260,8 @@ public class BoilerMonitorEntityServiceTests
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(BudgetPercentageEntityId, "100"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(BudgetExhaustedEntityId, "on"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(SessionCountEntityId, "4"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()), Times.Exactly(5));
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()),
|
||||
Times.Exactly(5));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -302,7 +306,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task UpdateEntitiesAsync_ShouldTruncateTodayRunTimeToWholeSeconds(double seconds, string expectedState)
|
||||
{
|
||||
await CreateSut().UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(todayRunTime: TimeSpan.FromSeconds(seconds)));
|
||||
BoilerMonitorTestData.CreateUpdateContext(TimeSpan.FromSeconds(seconds)));
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(TodayRuntimeEntityId, expectedState), Times.Once);
|
||||
}
|
||||
@@ -380,8 +384,8 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task CreateEntitiesAsync_WhenOneEntityFails_ShouldStillAttemptEveryEntity()
|
||||
{
|
||||
_entityManagerMock
|
||||
.Setup(em => em.CreateEntityAsync(It.Is<EntityCreateModel>(
|
||||
entity => entity.EntityId == CleanupButtonEntityId)))
|
||||
.Setup(em =>
|
||||
em.CreateEntityAsync(It.Is<EntityCreateModel>(entity => entity.EntityId == CleanupButtonEntityId)))
|
||||
.ThrowsAsync(new InvalidOperationException("MQTT unavailable"));
|
||||
|
||||
var act = () => CreateSut().CreateEntitiesAsync();
|
||||
@@ -418,7 +422,8 @@ public class BoilerMonitorEntityServiceTests
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(entityId, expectedState), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(10));
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()), Times.Exactly(5));
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()),
|
||||
Times.Exactly(5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -481,9 +486,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
await CreateSut().CleanupEntitiesAsync();
|
||||
|
||||
foreach (var entityId in CleanableEntityIds)
|
||||
{
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity(entityId), Times.Once);
|
||||
}
|
||||
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity(CleanupButtonEntityId), Times.Never);
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity(It.IsAny<string>()), Times.Exactly(CleanableEntityIds.Length));
|
||||
@@ -531,7 +534,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task Operation_WhenCancellationIsAlreadyRequested_ShouldThrowAndNotTouchEntityManager(
|
||||
CancelableOperation operation)
|
||||
{
|
||||
var canceledToken = new CancellationToken(canceled: true);
|
||||
var canceledToken = new CancellationToken(true);
|
||||
var sut = CreateSut();
|
||||
Func<Task> act = operation switch
|
||||
{
|
||||
@@ -548,4 +551,4 @@ public class BoilerMonitorEntityServiceTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using FluentAssertions;
|
||||
using BoilerMonitorManager.Models;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
@@ -32,7 +32,7 @@ public class BoilerMonitorOptionsTests
|
||||
[InlineData(true)]
|
||||
public void Enabled_WhenSet_ShouldReturnConfiguredValue(bool enabled)
|
||||
{
|
||||
var options = BoilerMonitorTestData.CreateOptions(enabled: enabled);
|
||||
var options = BoilerMonitorTestData.CreateOptions(enabled);
|
||||
|
||||
options.Enabled.Should().Be(enabled);
|
||||
}
|
||||
@@ -50,10 +50,10 @@ public class BoilerMonitorOptionsTests
|
||||
[Fact]
|
||||
public void Options_WhenBothFlagsDisabled_ShouldPreserveEachValueIndependently()
|
||||
{
|
||||
var options = BoilerMonitorTestData.CreateOptions(enabled: false, cleanupEntitiesOnStartup: false);
|
||||
var options = BoilerMonitorTestData.CreateOptions(false, false);
|
||||
|
||||
options.Enabled.Should().BeFalse();
|
||||
options.CleanupEntitiesOnStartup.Should().BeFalse();
|
||||
BoilerMonitorOptions.SectionName.Should().Be("BoilerMonitor");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@ using static BoilerMonitor.Models.BoilerMonitorConsts;
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Shared, deterministic fixtures used by the BoilerMonitor tests: config/options builders,
|
||||
/// entity-state sets, parse samples and a fixed clock. Keeping them here avoids copy-pasted
|
||||
/// setup and makes every timestamp/DST assertion independent of the real clock.
|
||||
/// Shared, deterministic fixtures used by the BoilerMonitor tests: config/options builders,
|
||||
/// entity-state sets, parse samples and a fixed clock. Keeping them here avoids copy-pasted
|
||||
/// setup and makes every timestamp/DST assertion independent of the real clock.
|
||||
/// </summary>
|
||||
internal static class BoilerMonitorTestData
|
||||
{
|
||||
@@ -21,30 +21,36 @@ internal static class BoilerMonitorTestData
|
||||
/// <summary>Fixed "now" (UTC) used by every time based test.</summary>
|
||||
public static readonly DateTimeOffset BaseTime = new(2026, 3, 31, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
public static FakeTimeProvider CreateTimeProvider(DateTimeOffset? utcNow = null) =>
|
||||
new(utcNow ?? BaseTime);
|
||||
public static FakeTimeProvider CreateTimeProvider(DateTimeOffset? utcNow = null)
|
||||
{
|
||||
return new FakeTimeProvider(utcNow ?? BaseTime);
|
||||
}
|
||||
|
||||
public static BoilerMonitorConfig CreateConfig(
|
||||
string? boilerPowerEntityId = PowerEntityId,
|
||||
string? boilerSwitchEntityId = SwitchEntityId,
|
||||
int? pollIntervalSeconds = DefaultPollIntervalSeconds,
|
||||
double? heatingPowerThresholdWatts = DefaultHeatingPowerThresholdWatts) =>
|
||||
new()
|
||||
double? heatingPowerThresholdWatts = DefaultHeatingPowerThresholdWatts)
|
||||
{
|
||||
return new BoilerMonitorConfig
|
||||
{
|
||||
BoilerPowerEntityId = boilerPowerEntityId,
|
||||
BoilerSwitchEntityId = boilerSwitchEntityId,
|
||||
PollIntervalSeconds = pollIntervalSeconds,
|
||||
HeatingPowerThresholdWatts = heatingPowerThresholdWatts
|
||||
};
|
||||
}
|
||||
|
||||
public static BoilerMonitorOptions CreateOptions(
|
||||
bool enabled = true,
|
||||
bool cleanupEntitiesOnStartup = true) =>
|
||||
new()
|
||||
bool cleanupEntitiesOnStartup = true)
|
||||
{
|
||||
return new BoilerMonitorOptions
|
||||
{
|
||||
Enabled = enabled,
|
||||
CleanupEntitiesOnStartup = cleanupEntitiesOnStartup
|
||||
};
|
||||
}
|
||||
|
||||
public static BoilerMonitorUpdateContext CreateUpdateContext(
|
||||
TimeSpan? todayRunTime = null,
|
||||
@@ -56,8 +62,9 @@ internal static class BoilerMonitorTestData
|
||||
bool waterReady = false,
|
||||
string? boilerSwitchState = "on",
|
||||
double? boilerPowerWatts = 0.0,
|
||||
DateTimeOffset? cooldownSince = null) =>
|
||||
new()
|
||||
DateTimeOffset? cooldownSince = null)
|
||||
{
|
||||
return new BoilerMonitorUpdateContext
|
||||
{
|
||||
TodayRunTime = todayRunTime ?? TimeSpan.FromHours(1),
|
||||
RemainingTime = remainingTime ?? TimeSpan.FromHours(1),
|
||||
@@ -68,33 +75,43 @@ internal static class BoilerMonitorTestData
|
||||
WaterReady = waterReady,
|
||||
BoilerSwitchState = boilerSwitchState,
|
||||
BoilerPowerWatts = boilerPowerWatts,
|
||||
CooldownSince = cooldownSince,
|
||||
CooldownSince = cooldownSince
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The four entity states <c>ReadPersistedState</c> reads, with valid defaults.
|
||||
/// A null value means the entity does not exist in Home Assistant.
|
||||
/// The four entity states <c>ReadPersistedState</c> reads, with valid defaults.
|
||||
/// A null value means the entity does not exist in Home Assistant.
|
||||
/// </summary>
|
||||
public static Dictionary<string, string?> CreatePersistedEntityStates(
|
||||
string? todayRuntime = "7200",
|
||||
string? sessionCount = "3",
|
||||
string? skipPriceCap = "OFF",
|
||||
string? budgetOverride = "OFF") =>
|
||||
new()
|
||||
string? budgetOverride = "OFF")
|
||||
{
|
||||
return new Dictionary<string, string?>
|
||||
{
|
||||
[TodayRuntimeEntityId] = todayRuntime,
|
||||
[SessionCountEntityId] = sessionCount,
|
||||
[SkipPriceCapEntityId] = skipPriceCap,
|
||||
[BudgetOverrideEntityId] = budgetOverride
|
||||
};
|
||||
}
|
||||
|
||||
public static EntityState CreateEntityState(string state) => new() { State = state };
|
||||
public static EntityState CreateEntityState(string state)
|
||||
{
|
||||
return new EntityState { State = state };
|
||||
}
|
||||
|
||||
public static EntityState CreateEntityStateWithAttributes(IReadOnlyDictionary<string, object?> attributes) =>
|
||||
new() { State = "yes", AttributesJson = JsonSerializer.SerializeToElement(attributes) };
|
||||
public static EntityState CreateEntityStateWithAttributes(IReadOnlyDictionary<string, object?> attributes)
|
||||
{
|
||||
return new EntityState { State = "yes", AttributesJson = JsonSerializer.SerializeToElement(attributes) };
|
||||
}
|
||||
|
||||
public static EntityState CreateWaterReadyEntityStateWithCooldown(object? cooldownSince) =>
|
||||
CreateEntityStateWithAttributes(new Dictionary<string, object?> { ["cooldown_since"] = cooldownSince });
|
||||
public static EntityState CreateWaterReadyEntityStateWithCooldown(object? cooldownSince)
|
||||
{
|
||||
return CreateEntityStateWithAttributes(new Dictionary<string, object?> { ["cooldown_since"] = cooldownSince });
|
||||
}
|
||||
|
||||
extension(Mock<IHaContext> haContextMock)
|
||||
{
|
||||
@@ -107,13 +124,15 @@ internal static class BoilerMonitorTestData
|
||||
}
|
||||
}
|
||||
|
||||
public void SetupEntityState(string entityId, EntityState? entityState) =>
|
||||
public void SetupEntityState(string entityId, EntityState? entityState)
|
||||
{
|
||||
haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entity-state samples for the today-runtime sensor: valid values with the expected
|
||||
/// <see cref="TimeSpan"/>, plus unavailable/negative/overflow values that must be rejected.
|
||||
/// Entity-state samples for the today-runtime sensor: valid values with the expected
|
||||
/// <see cref="TimeSpan" />, plus unavailable/negative/overflow values that must be rejected.
|
||||
/// </summary>
|
||||
public static TheoryData<string?, TimeSpan?> RuntimeStateSamples()
|
||||
{
|
||||
@@ -141,11 +160,10 @@ internal static class BoilerMonitorTestData
|
||||
data.Add("-1", null);
|
||||
data.Add("-0.001", null);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entity-state samples for the session-count sensor, including the int boundaries.
|
||||
/// Entity-state samples for the session-count sensor, including the int boundaries.
|
||||
/// </summary>
|
||||
public static TheoryData<string?, int?> SessionCountStateSamples()
|
||||
{
|
||||
@@ -168,12 +186,11 @@ internal static class BoilerMonitorTestData
|
||||
data.Add("2147483648", null);
|
||||
data.Add("1e3", null);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Water-ready detection samples around the heating threshold, including negative,
|
||||
/// zero, exact-boundary, huge and NaN readings.
|
||||
/// Water-ready detection samples around the heating threshold, including negative,
|
||||
/// zero, exact-boundary, huge and NaN readings.
|
||||
/// </summary>
|
||||
public static TheoryData<string, double, bool> PowerReadingSamples()
|
||||
{
|
||||
@@ -188,11 +205,10 @@ internal static class BoilerMonitorTestData
|
||||
data.Add("maximum", double.MaxValue, false);
|
||||
data.Add("not-a-number", double.NaN, true);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Elapsed-time sweep showing that the water-ready cooldown never expires on its own.
|
||||
/// Elapsed-time sweep showing that the water-ready cooldown never expires on its own.
|
||||
/// </summary>
|
||||
public static TheoryData<TimeSpan> GeneratedElapsedTimes()
|
||||
{
|
||||
@@ -207,12 +223,11 @@ internal static class BoilerMonitorTestData
|
||||
data.Add(TimeSpan.FromHours(48));
|
||||
data.Add(TimeSpan.FromHours(168));
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cooldown restore samples around the UTC-midnight cut-off, including DST start/end
|
||||
/// timestamps carrying non-zero offsets.
|
||||
/// Cooldown restore samples around the UTC-midnight cut-off, including DST start/end
|
||||
/// timestamps carrying non-zero offsets.
|
||||
/// </summary>
|
||||
public static TheoryData<string, DateTimeOffset, DateTimeOffset, bool> CooldownRestoreSamples()
|
||||
{
|
||||
@@ -259,9 +274,10 @@ internal static class BoilerMonitorTestData
|
||||
false
|
||||
);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
private static DateTimeOffset TodayMidnight() =>
|
||||
new(BaseTime.Date, TimeSpan.Zero);
|
||||
}
|
||||
private static DateTimeOffset TodayMidnight()
|
||||
{
|
||||
return new DateTimeOffset(BaseTime.Date, TimeSpan.Zero);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
using BoilerMonitor.Models;
|
||||
using BoilerMonitorManager;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
using Moq;
|
||||
using BoilerMonitorManager;
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
@@ -16,7 +16,10 @@ public class WaterReadyServiceTests
|
||||
private readonly Mock<ILogger<WaterReadyService>> _loggerMock = new();
|
||||
private readonly FakeTimeProvider _timeProvider = BoilerMonitorTestData.CreateTimeProvider();
|
||||
|
||||
private WaterReadyService CreateSut() => new(_loggerMock.Object, _timeProvider);
|
||||
private WaterReadyService CreateSut()
|
||||
{
|
||||
return new WaterReadyService(_loggerMock.Object, _timeProvider);
|
||||
}
|
||||
|
||||
private WaterReadyService CreateSutWithHeatingObserved()
|
||||
{
|
||||
@@ -33,10 +36,7 @@ public class WaterReadyServiceTests
|
||||
double heatingThresholdWatts = Threshold)
|
||||
{
|
||||
var result = false;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
result = sut.IsWaterReady(switchState, powerWatts, heatingThresholdWatts);
|
||||
}
|
||||
for (var i = 0; i < count; i++) result = sut.IsWaterReady(switchState, powerWatts, heatingThresholdWatts);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -282,7 +282,8 @@ public class WaterReadyServiceTests
|
||||
{
|
||||
var sut = CreateSutWithHeatingObserved();
|
||||
|
||||
var result = FeedReadings(sut, RequiredReadings, powerWatts: powerWatts, heatingThresholdWatts: heatingThresholdWatts);
|
||||
var result = FeedReadings(sut, RequiredReadings, powerWatts: powerWatts,
|
||||
heatingThresholdWatts: heatingThresholdWatts);
|
||||
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
@@ -475,4 +476,4 @@ public class WaterReadyServiceTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user