Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using System.Text.Json;
|
||||
using BoilerMonitorManager;
|
||||
using FluentAssertions;
|
||||
@@ -8,6 +10,8 @@ using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using static BoilerMonitor.Models.BoilerMonitorConsts;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BoilerMonitor.Manager.Tests;
|
||||
|
||||
public class BoilerMonitorEntityServiceTests
|
||||
@@ -31,28 +35,31 @@ public class BoilerMonitorEntityServiceTests
|
||||
AutoManagementEntityId
|
||||
];
|
||||
|
||||
private readonly Mock<IEntityManager> _entityManagerMock = new();
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<BoilerMonitorEntityService>> _loggerMock = new();
|
||||
private readonly Mock<IEntityManager> _entityManagerMock = new Mock<IEntityManager>();
|
||||
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
|
||||
private readonly Mock<ILogger<BoilerMonitorEntityService>> _loggerMock = new Mock<ILogger<BoilerMonitorEntityService>>();
|
||||
|
||||
public BoilerMonitorEntityServiceTests()
|
||||
{
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<EntityCreateModel>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
}
|
||||
|
||||
private BoilerMonitorEntityService CreateSut()
|
||||
{
|
||||
return new BoilerMonitorEntityService(
|
||||
_entityManagerMock.Object,
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object);
|
||||
_entityManagerMock.Object,
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object);
|
||||
}
|
||||
|
||||
#region ReadPersistedState
|
||||
@@ -64,13 +71,14 @@ public class BoilerMonitorEntityServiceTests
|
||||
TimeSpan? expectedRunTime)
|
||||
{
|
||||
_haContextMock.SetupEntityStates(
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(runtimeState, "2"));
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(runtimeState, "2"));
|
||||
|
||||
var result = CreateSut().ReadPersistedState();
|
||||
|
||||
if (expectedRunTime is null)
|
||||
{
|
||||
result.Should().BeNull();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -86,13 +94,14 @@ public class BoilerMonitorEntityServiceTests
|
||||
int? expectedSessionCount)
|
||||
{
|
||||
_haContextMock.SetupEntityStates(
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates("3600", sessionCountState));
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates("3600", sessionCountState));
|
||||
|
||||
var result = CreateSut().ReadPersistedState();
|
||||
|
||||
if (expectedSessionCount is null)
|
||||
{
|
||||
result.Should().BeNull();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -105,7 +114,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedState_WithValidState_ShouldReturnPersistedValues()
|
||||
{
|
||||
_haContextMock.SetupEntityStates(BoilerMonitorTestData.CreatePersistedEntityStates(
|
||||
"7200", "3", "ON"));
|
||||
"7200", "3", "ON"));
|
||||
|
||||
var result = CreateSut().ReadPersistedState();
|
||||
|
||||
@@ -125,7 +134,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedState_ShouldReturnRawSwitchStates(string? switchState)
|
||||
{
|
||||
_haContextMock.SetupEntityStates(BoilerMonitorTestData.CreatePersistedEntityStates(
|
||||
skipPriceCap: switchState, budgetOverride: switchState));
|
||||
skipPriceCap: switchState, budgetOverride: switchState));
|
||||
|
||||
var result = CreateSut().ReadPersistedState();
|
||||
|
||||
@@ -138,7 +147,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedState_WhenBothRuntimeEntitiesDoNotExist_ShouldReturnNull()
|
||||
{
|
||||
_haContextMock.SetupEntityStates(
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(null, null));
|
||||
BoilerMonitorTestData.CreatePersistedEntityStates(null, null));
|
||||
|
||||
CreateSut().ReadPersistedState().Should().BeNull();
|
||||
}
|
||||
@@ -151,9 +160,10 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedCooldownTimestamp_WithValidIsoString_ShouldReturnTimestamp()
|
||||
{
|
||||
var cooldownSince = BoilerMonitorTestData.BaseTime - TimeSpan.FromMinutes(30);
|
||||
|
||||
_haContextMock.SetupEntityState(
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown(cooldownSince.ToString("O")));
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown(cooldownSince.ToString("O")));
|
||||
|
||||
var result = CreateSut().ReadPersistedCooldownTimestamp();
|
||||
|
||||
@@ -164,9 +174,10 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedCooldownTimestamp_WithUtcDesignator_ShouldReturnTimestamp()
|
||||
{
|
||||
var cooldownSince = new DateTimeOffset(2026, 3, 31, 9, 15, 0, TimeSpan.Zero);
|
||||
|
||||
_haContextMock.SetupEntityState(
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown("2026-03-31T09:15:00Z"));
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown("2026-03-31T09:15:00Z"));
|
||||
|
||||
var result = CreateSut().ReadPersistedCooldownTimestamp();
|
||||
|
||||
@@ -193,9 +204,9 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedCooldownTimestamp_WhenAttributeIsMissing_ShouldReturnNull()
|
||||
{
|
||||
_haContextMock.SetupEntityState(
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateEntityStateWithAttributes(
|
||||
new Dictionary<string, object?> { ["friendly_name"] = "Boiler water ready" }));
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateEntityStateWithAttributes(
|
||||
new Dictionary<string, object?> { ["friendly_name"] = "Boiler water ready" }));
|
||||
|
||||
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
||||
}
|
||||
@@ -212,8 +223,8 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedCooldownTimestamp_WhenAttributeIsNotAValidTimestamp_ShouldReturnNull(object? cooldownSince)
|
||||
{
|
||||
_haContextMock.SetupEntityState(
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown(cooldownSince));
|
||||
WaterReadyEntityId,
|
||||
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown(cooldownSince));
|
||||
|
||||
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
||||
}
|
||||
@@ -222,7 +233,7 @@ public class BoilerMonitorEntityServiceTests
|
||||
public void ReadPersistedCooldownTimestamp_WhenHaContextThrows_ShouldReturnNull()
|
||||
{
|
||||
_haContextMock.Setup(h => h.GetState(WaterReadyEntityId))
|
||||
.Throws(new InvalidOperationException("Home Assistant unavailable"));
|
||||
.Throws(new InvalidOperationException("Home Assistant unavailable"));
|
||||
|
||||
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
||||
}
|
||||
@@ -245,12 +256,12 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task UpdateEntitiesAsync_ShouldWriteAllRuntimeEntitiesWithExpectedFormatting()
|
||||
{
|
||||
var context = BoilerMonitorTestData.CreateUpdateContext(
|
||||
TimeSpan.FromSeconds(3723.9),
|
||||
TimeSpan.FromSeconds(42.9),
|
||||
150.0,
|
||||
true,
|
||||
4,
|
||||
waterReady: true);
|
||||
TimeSpan.FromSeconds(3723.9),
|
||||
TimeSpan.FromSeconds(42.9),
|
||||
150.0,
|
||||
true,
|
||||
4,
|
||||
waterReady: true);
|
||||
|
||||
await CreateSut().UpdateEntitiesAsync(context);
|
||||
|
||||
@@ -260,8 +271,9 @@ 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));
|
||||
Times.Exactly(5));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -276,11 +288,12 @@ public class BoilerMonitorEntityServiceTests
|
||||
double budgetPercentage,
|
||||
string expectedState)
|
||||
{
|
||||
await CreateSut().UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(budgetPercentage: budgetPercentage));
|
||||
await CreateSut()
|
||||
.UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(budgetPercentage: budgetPercentage));
|
||||
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState(BudgetPercentageEntityId, expectedState), Times.Once);
|
||||
em => em.SetEntityState(BudgetPercentageEntityId, expectedState), Times.Once);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -291,8 +304,9 @@ public class BoilerMonitorEntityServiceTests
|
||||
[InlineData(3600, "3600")]
|
||||
public async Task UpdateEntitiesAsync_ShouldClampAndTruncateRemainingTime(int seconds, string expectedState)
|
||||
{
|
||||
await CreateSut().UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(remainingTime: TimeSpan.FromSeconds(seconds)));
|
||||
await CreateSut()
|
||||
.UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(remainingTime: TimeSpan.FromSeconds(seconds)));
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(RemainingTimeEntityId, expectedState), Times.Once);
|
||||
}
|
||||
@@ -305,8 +319,9 @@ public class BoilerMonitorEntityServiceTests
|
||||
[InlineData(86_400.0, "86400")]
|
||||
public async Task UpdateEntitiesAsync_ShouldTruncateTodayRunTimeToWholeSeconds(double seconds, string expectedState)
|
||||
{
|
||||
await CreateSut().UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(TimeSpan.FromSeconds(seconds)));
|
||||
await CreateSut()
|
||||
.UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(TimeSpan.FromSeconds(seconds)));
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(TodayRuntimeEntityId, expectedState), Times.Once);
|
||||
}
|
||||
@@ -315,15 +330,17 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task UpdateEntitiesAsync_WhenSwitchStateIsNull_ShouldWriteUnknownWaterReadyAttributes()
|
||||
{
|
||||
object? capturedAttributes = null;
|
||||
_entityManagerMock
|
||||
.Setup(em => em.SetEntityAttributes(WaterReadyEntityId, It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attributes) => capturedAttributes = attributes)
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
await CreateSut().UpdateEntitiesAsync(BoilerMonitorTestData.CreateUpdateContext(
|
||||
waterReady: false,
|
||||
boilerSwitchState: null,
|
||||
boilerPowerWatts: 1234.5));
|
||||
_entityManagerMock
|
||||
.Setup(em => em.SetEntityAttributes(WaterReadyEntityId, It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attributes) => capturedAttributes = attributes)
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
await CreateSut()
|
||||
.UpdateEntitiesAsync(BoilerMonitorTestData.CreateUpdateContext(
|
||||
waterReady: false,
|
||||
boilerSwitchState: null,
|
||||
boilerPowerWatts: 1234.5));
|
||||
|
||||
capturedAttributes.Should().NotBeNull();
|
||||
var attributes = JsonSerializer.SerializeToElement(capturedAttributes!);
|
||||
@@ -338,13 +355,15 @@ public class BoilerMonitorEntityServiceTests
|
||||
{
|
||||
var cooldownSince = BoilerMonitorTestData.BaseTime - TimeSpan.FromMinutes(15);
|
||||
object? capturedAttributes = null;
|
||||
_entityManagerMock
|
||||
.Setup(em => em.SetEntityAttributes(WaterReadyEntityId, It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attributes) => capturedAttributes = attributes)
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
await CreateSut().UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(cooldownSince: cooldownSince));
|
||||
_entityManagerMock
|
||||
.Setup(em => em.SetEntityAttributes(WaterReadyEntityId, It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attributes) => capturedAttributes = attributes)
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
await CreateSut()
|
||||
.UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(cooldownSince: cooldownSince));
|
||||
|
||||
var attributes = JsonSerializer.SerializeToElement(capturedAttributes!);
|
||||
attributes.GetProperty("cooldown_since").GetString().Should().Be(cooldownSince.ToString("O"));
|
||||
@@ -358,49 +377,54 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task CreateEntitiesAsync_ShouldCreateEveryBoilerMonitorEntityOnce()
|
||||
{
|
||||
var createdEntityIds = new List<string?>();
|
||||
|
||||
_entityManagerMock
|
||||
.Setup(em => em.CreateEntityAsync(It.IsAny<EntityCreateModel>()))
|
||||
.Callback<EntityCreateModel>(entity => createdEntityIds.Add(entity.EntityId))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Setup(em => em.CreateEntityAsync(It.IsAny<EntityCreateModel>()))
|
||||
.Callback<EntityCreateModel>(entity => createdEntityIds.Add(entity.EntityId))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
await CreateSut().CreateEntitiesAsync();
|
||||
|
||||
createdEntityIds.Should().HaveCount(15).And.OnlyHaveUniqueItems();
|
||||
createdEntityIds.Should().Contain(
|
||||
[
|
||||
ConfigPowerEntityId,
|
||||
ConfigSwitchEntityId,
|
||||
ConfigPollIntervalEntityId,
|
||||
ConfigHeatingThresholdEntityId,
|
||||
CleanupButtonEntityId,
|
||||
WaterReadyEntityId,
|
||||
TodayRuntimeEntityId,
|
||||
BudgetExhaustedEntityId,
|
||||
AutoManagementEntityId
|
||||
]);
|
||||
|
||||
createdEntityIds.Should()
|
||||
.Contain(
|
||||
[
|
||||
ConfigPowerEntityId,
|
||||
ConfigSwitchEntityId,
|
||||
ConfigPollIntervalEntityId,
|
||||
ConfigHeatingThresholdEntityId,
|
||||
CleanupButtonEntityId,
|
||||
WaterReadyEntityId,
|
||||
TodayRuntimeEntityId,
|
||||
BudgetExhaustedEntityId,
|
||||
AutoManagementEntityId
|
||||
]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateEntitiesAsync_WhenOneEntityFails_ShouldStillAttemptEveryEntity()
|
||||
{
|
||||
_entityManagerMock
|
||||
.Setup(em =>
|
||||
em.CreateEntityAsync(It.Is<EntityCreateModel>(entity => entity.EntityId == CleanupButtonEntityId)))
|
||||
.ThrowsAsync(new InvalidOperationException("MQTT unavailable"));
|
||||
.Setup(em =>
|
||||
em.CreateEntityAsync(It.Is<EntityCreateModel>(entity => entity.EntityId == CleanupButtonEntityId)))
|
||||
.ThrowsAsync(new InvalidOperationException("MQTT unavailable"));
|
||||
|
||||
var act = () => CreateSut().CreateEntitiesAsync();
|
||||
|
||||
await act.Should().NotThrowAsync();
|
||||
|
||||
_entityManagerMock.Invocations
|
||||
.Count(invocation => invocation.Method.Name == nameof(IEntityManager.CreateEntityAsync))
|
||||
.Should().Be(15);
|
||||
.Count(invocation => invocation.Method.Name == nameof(IEntityManager.CreateEntityAsync))
|
||||
.Should()
|
||||
.Be(15);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ResetEntitiesAsync
|
||||
|
||||
public static TheoryData<string, string> ResetEntityStates => new()
|
||||
public static TheoryData<string, string> ResetEntityStates => new TheoryData<string, string>
|
||||
{
|
||||
{ StatusEntityId, StatusAwaitingConfiguration },
|
||||
{ WaterReadyEntityId, "unknown" },
|
||||
@@ -422,16 +446,17 @@ 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));
|
||||
Times.Exactly(5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntitiesAsync_WhenEntityManagerThrows_ShouldNotThrow()
|
||||
{
|
||||
_entityManagerMock
|
||||
.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("MQTT unavailable"));
|
||||
.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("MQTT unavailable"));
|
||||
|
||||
var act = () => CreateSut().ResetEntitiesAsync();
|
||||
|
||||
@@ -486,7 +511,9 @@ 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));
|
||||
@@ -496,8 +523,8 @@ public class BoilerMonitorEntityServiceTests
|
||||
public async Task CleanupEntitiesAsync_WhenRemoveFails_ShouldRethrow()
|
||||
{
|
||||
_entityManagerMock
|
||||
.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.Throws(new InvalidOperationException("MQTT unavailable"));
|
||||
.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.Throws(new InvalidOperationException("MQTT unavailable"));
|
||||
|
||||
var act = () => CreateSut().CleanupEntitiesAsync();
|
||||
|
||||
@@ -536,12 +563,13 @@ public class BoilerMonitorEntityServiceTests
|
||||
{
|
||||
var canceledToken = new CancellationToken(true);
|
||||
var sut = CreateSut();
|
||||
|
||||
Func<Task> act = operation switch
|
||||
{
|
||||
CancelableOperation.CreateEntities => () => sut.CreateEntitiesAsync(canceledToken),
|
||||
CancelableOperation.ResetEntities => () => sut.ResetEntitiesAsync(canceledToken),
|
||||
CancelableOperation.UpdateEntities => () => sut.UpdateEntitiesAsync(
|
||||
BoilerMonitorTestData.CreateUpdateContext(), canceledToken),
|
||||
BoilerMonitorTestData.CreateUpdateContext(), canceledToken),
|
||||
CancelableOperation.ResetTransientEntities => () => sut.ResetTransientEntitiesAsync(canceledToken),
|
||||
_ => () => sut.CleanupEntitiesAsync(canceledToken)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user