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:
2026-12-01 12:33:31 +02:00
parent 77458383d1
commit f919d6b25b
115 changed files with 3115 additions and 1959 deletions
+107 -91
View File
@@ -1,3 +1,5 @@
#region
using FluentAssertions;
using LeakMonitor.Models;
using LeakMonitorManager;
@@ -9,14 +11,16 @@ using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
#endregion
namespace LeakMonitor.Manager.Tests;
public class LeakMonitorInitTests
{
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<ILogger<LeakMonitorInit>> _loggerMock = new();
private readonly Mock<ILeakNotificationService> _notificationServiceMock = new();
private readonly LeakMonitorTestData.TrackingObservable<StateChange> _stateChanges = new();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
private readonly Mock<ILogger<LeakMonitorInit>> _loggerMock = new Mock<ILogger<LeakMonitorInit>>();
private readonly Mock<ILeakNotificationService> _notificationServiceMock = new Mock<ILeakNotificationService>();
private readonly LeakMonitorTestData.TrackingObservable<StateChange> _stateChanges = new LeakMonitorTestData.TrackingObservable<StateChange>();
public LeakMonitorInitTests()
{
@@ -27,10 +31,10 @@ public class LeakMonitorInitTests
private LeakMonitorInit CreateSut(LeakMonitorOptions? options = null)
{
return new LeakMonitorInit(
_haContextMock.Object,
_notificationServiceMock.Object,
_loggerMock.Object,
Options.Create(options ?? LeakMonitorTestData.CreateOptions()));
_haContextMock.Object,
_notificationServiceMock.Object,
_loggerMock.Object,
Options.Create(options ?? LeakMonitorTestData.CreateOptions()));
}
private void VerifyNotifications(Times expected)
@@ -42,9 +46,9 @@ public class LeakMonitorInitTests
private void VerifyLogged(LogLevel level, Times times)
{
var logCount = _loggerMock.Invocations.Count(invocation =>
invocation.Method.Name == nameof(ILogger.Log) &&
invocation.Arguments[0] is LogLevel loggedLevel &&
loggedLevel == level);
invocation.Method.Name == nameof(ILogger.Log) &&
invocation.Arguments[0] is LogLevel loggedLevel &&
loggedLevel == level);
times.Validate(logCount);
}
@@ -56,11 +60,12 @@ public class LeakMonitorInitTests
public void HandleStateChange_WhenWaterDetected_ShouldSendLeakAlert(string sensorEntityId, string? newState)
{
var sut = CreateSut();
var change = LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
newState);
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
newState);
sut.HandleStateChange(sensorEntityId, change);
@@ -73,11 +78,12 @@ public class LeakMonitorInitTests
public void HandleStateChange_WhenWaterCleared_ShouldDismissLeakAlert(string sensorEntityId, string? newState)
{
var sut = CreateSut();
var change = LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterDetectedState,
newState);
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterDetectedState,
newState);
sut.HandleStateChange(sensorEntityId, change);
@@ -90,11 +96,12 @@ public class LeakMonitorInitTests
public void HandleStateChange_WhenStateIsNotActionable_ShouldNotNotify(string? newState)
{
var sut = CreateSut();
var change = LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
newState);
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
newState);
sut.HandleStateChange(LeakMonitorTestData.AquariumSensorId, change);
@@ -107,18 +114,20 @@ public class LeakMonitorInitTests
// Deduplication is the notification service's job (cooldown); the init handler
// forwards each "on" event it receives.
var sut = CreateSut();
var change = LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
LeakMonitorConsts.WaterDetectedState);
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
LeakMonitorConsts.WaterDetectedState);
sut.HandleStateChange(LeakMonitorTestData.AquariumSensorId, change);
sut.HandleStateChange(LeakMonitorTestData.AquariumSensorId, change);
_notificationServiceMock.Verify(
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Exactly(2));
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Exactly(2));
_notificationServiceMock.Verify(n => n.DismissLeakAlert(It.IsAny<string>()), Times.Never());
}
@@ -127,11 +136,12 @@ public class LeakMonitorInitTests
public void HandleStateChange_WithBlankSensorId_ShouldForwardTheIdVerbatim(string sensorEntityId)
{
var sut = CreateSut();
var change = LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState);
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState);
sut.HandleStateChange(sensorEntityId, change);
@@ -160,9 +170,10 @@ public class LeakMonitorInitTests
CreateSut();
_stateChanges.SubscriptionCount.Should().Be(LeakMonitorTestData.DefaultSensorIds.Length);
_haContextMock.Verify(
h => h.StateAllChanges(),
Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
h => h.StateAllChanges(),
Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
}
[Theory]
@@ -174,10 +185,10 @@ public class LeakMonitorInitTests
_stateChanges.SubscriptionCount.Should().Be(1);
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
_notificationServiceMock.Verify(n => n.SendLeakAlert(sensorEntityId), Times.Once());
}
@@ -186,22 +197,23 @@ public class LeakMonitorInitTests
public void InitializeAsync_WithNullSensorEntry_ShouldStillSubscribeRemainingSensors()
{
CreateSut(LeakMonitorTestData.CreateOptions(
[null!, LeakMonitorTestData.BoilerSensorId]));
[null!, LeakMonitorTestData.BoilerSensorId]));
_stateChanges.SubscriptionCount.Should().Be(2);
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
LeakMonitorTestData.BoilerSensorId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
_haContextMock.Object,
LeakMonitorTestData.BoilerSensorId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
_notificationServiceMock.Verify(
n => n.SendLeakAlert(LeakMonitorTestData.BoilerSensorId),
Times.Once());
n => n.SendLeakAlert(LeakMonitorTestData.BoilerSensorId),
Times.Once());
_notificationServiceMock.Verify(
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Never());
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Never());
}
[Theory]
@@ -213,15 +225,16 @@ public class LeakMonitorInitTests
CreateSut();
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
newState));
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterClearedState,
newState));
_notificationServiceMock.Verify(n => n.SendLeakAlert(sensorEntityId), Times.Once());
_notificationServiceMock.Verify(
n => n.SendLeakAlert(It.Is<string>(id => id != sensorEntityId)),
Times.Never());
n => n.SendLeakAlert(It.Is<string>(id => id != sensorEntityId)),
Times.Never());
}
[Theory]
@@ -233,10 +246,10 @@ public class LeakMonitorInitTests
CreateSut();
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterDetectedState,
newState));
_haContextMock.Object,
sensorEntityId,
LeakMonitorConsts.WaterDetectedState,
newState));
_notificationServiceMock.Verify(n => n.DismissLeakAlert(sensorEntityId), Times.Once());
_notificationServiceMock.Verify(n => n.SendLeakAlert(It.IsAny<string>()), Times.Never());
@@ -249,10 +262,10 @@ public class LeakMonitorInitTests
CreateSut();
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
newState));
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
newState));
VerifyNotifications(Times.Never());
}
@@ -263,10 +276,10 @@ public class LeakMonitorInitTests
CreateSut();
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
LeakMonitorConsts.WaterDetectedState));
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
LeakMonitorConsts.WaterDetectedState));
VerifyNotifications(Times.Never());
}
@@ -277,10 +290,10 @@ public class LeakMonitorInitTests
CreateSut();
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
"binary_sensor.unrelated_motion",
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
_haContextMock.Object,
"binary_sensor.unrelated_motion",
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
VerifyNotifications(Times.Never());
}
@@ -291,55 +304,58 @@ public class LeakMonitorInitTests
CreateSut();
foreach (var change in LeakMonitorTestData.CreateStateChangeSequence(
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorTestData.DetectionSequence))
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorTestData.DetectionSequence))
{
_stateChanges.Publish(change);
}
_notificationServiceMock.Verify(
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Exactly(2));
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Exactly(2));
_notificationServiceMock.Verify(
n => n.DismissLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Exactly(2));
n => n.DismissLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Exactly(2));
}
[Fact]
public void InitializeAsync_WhenNotificationServiceThrows_ShouldLogErrorAndKeepMonitoring()
{
_notificationServiceMock
.Setup(n => n.SendLeakAlert(It.IsAny<string>()))
.Throws(new InvalidOperationException("notification failed"));
.Setup(n => n.SendLeakAlert(It.IsAny<string>()))
.Throws(new InvalidOperationException("notification failed"));
CreateSut();
var act = () => _stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterClearedState,
LeakMonitorConsts.WaterDetectedState));
act.Should().NotThrow();
VerifyLogged(LogLevel.Error, Times.Once());
// The failed event must not tear down the subscription.
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
LeakMonitorConsts.WaterClearedState));
_haContextMock.Object,
LeakMonitorTestData.AquariumSensorId,
LeakMonitorConsts.WaterDetectedState,
LeakMonitorConsts.WaterClearedState));
_notificationServiceMock.Verify(
n => n.DismissLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Once());
n => n.DismissLeakAlert(LeakMonitorTestData.AquariumSensorId),
Times.Once());
}
[Fact]
public void InitializeAsync_WhenSubscriptionFails_ShouldSwallowAndLogError()
{
_haContextMock
.Setup(h => h.StateAllChanges())
.Throws(new InvalidOperationException("home assistant offline"));
.Setup(h => h.StateAllChanges())
.Throws(new InvalidOperationException("home assistant offline"));
var act = () => CreateSut();