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
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
@@ -12,101 +13,344 @@ namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
public class LeakMonitorInitTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILeakNotificationService> _notificationServiceMock;
|
||||
private readonly Mock<ILogger<LeakMonitorInit>> _loggerMock;
|
||||
private readonly LeakMonitorOptions _options;
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILeakNotificationService> _notificationServiceMock = new();
|
||||
private readonly Mock<ILogger<LeakMonitorInit>> _loggerMock = new();
|
||||
private readonly LeakMonitorTestData.TrackingObservable<StateChange> _stateChanges = new();
|
||||
|
||||
public LeakMonitorInitTests()
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_notificationServiceMock = new Mock<ILeakNotificationService>();
|
||||
_loggerMock = new Mock<ILogger<LeakMonitorInit>>();
|
||||
_options = new LeakMonitorOptions
|
||||
{
|
||||
Enabled = true,
|
||||
SensorEntityIds = ["binary_sensor.aquarium_leak_sensor", "binary_sensor.boiler_moisture"]
|
||||
};
|
||||
_haContextMock.Setup(h => h.StateAllChanges()).Returns(_stateChanges);
|
||||
}
|
||||
|
||||
private LeakMonitorInit CreateSut()
|
||||
private LeakMonitorInit CreateSut(LeakMonitorOptions? options = null)
|
||||
{
|
||||
return new LeakMonitorInit(
|
||||
_haContextMock.Object,
|
||||
_notificationServiceMock.Object,
|
||||
_loggerMock.Object,
|
||||
Options.Create(_options));
|
||||
Options.Create(options ?? LeakMonitorTestData.CreateOptions()));
|
||||
}
|
||||
|
||||
private void VerifyNotifications(Times expected)
|
||||
{
|
||||
_notificationServiceMock.Verify(n => n.SendLeakAlert(It.IsAny<string>()), expected);
|
||||
_notificationServiceMock.Verify(n => n.DismissLeakAlert(It.IsAny<string>()), expected);
|
||||
}
|
||||
|
||||
private void VerifyLogged(LogLevel level, Times times)
|
||||
{
|
||||
_loggerMock.Verify(
|
||||
l => l.Log(
|
||||
level,
|
||||
It.IsAny<EventId>(),
|
||||
It.IsAny<It.IsAnyType>(),
|
||||
It.IsAny<Exception?>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
|
||||
times);
|
||||
}
|
||||
|
||||
#region HandleStateChange
|
||||
|
||||
[Fact]
|
||||
public void HandleStateChange_WaterDetected_ShouldSendLeakAlert()
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.WaterDetectedCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void HandleStateChange_WhenWaterDetected_ShouldSendLeakAlert(string sensorEntityId, string? newState)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
var change = CreateStateChange("off", LeakMonitorConsts.WaterDetectedState);
|
||||
var change = LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
sensorEntityId,
|
||||
LeakMonitorConsts.WaterClearedState,
|
||||
newState);
|
||||
|
||||
sut.HandleStateChange("binary_sensor.aquarium_leak_sensor", change);
|
||||
sut.HandleStateChange(sensorEntityId, change);
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.SendLeakAlert("binary_sensor.aquarium_leak_sensor"),
|
||||
Times.Once);
|
||||
_notificationServiceMock.Verify(n => n.SendLeakAlert(sensorEntityId), Times.Once());
|
||||
_notificationServiceMock.Verify(n => n.DismissLeakAlert(It.IsAny<string>()), Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.WaterClearedCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void HandleStateChange_WhenWaterCleared_ShouldDismissLeakAlert(string sensorEntityId, string? newState)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
var change = LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
sensorEntityId,
|
||||
LeakMonitorConsts.WaterDetectedState,
|
||||
newState);
|
||||
|
||||
sut.HandleStateChange(sensorEntityId, change);
|
||||
|
||||
_notificationServiceMock.Verify(n => n.DismissLeakAlert(sensorEntityId), Times.Once());
|
||||
_notificationServiceMock.Verify(n => n.SendLeakAlert(It.IsAny<string>()), Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.NonActionableStateCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void HandleStateChange_WhenStateIsNotActionable_ShouldNotNotify(string? newState)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
var change = LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
LeakMonitorTestData.AquariumSensorId,
|
||||
LeakMonitorConsts.WaterDetectedState,
|
||||
newState);
|
||||
|
||||
sut.HandleStateChange(LeakMonitorTestData.AquariumSensorId, change);
|
||||
|
||||
VerifyNotifications(Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandleStateChange_WaterCleared_ShouldDismissLeakAlert()
|
||||
public void HandleStateChange_WhenDetectionRepeats_ShouldForwardEveryDetection()
|
||||
{
|
||||
// Deduplication is the notification service's job (cooldown); the init handler
|
||||
// forwards each "on" event it receives.
|
||||
var sut = CreateSut();
|
||||
var change = CreateStateChange("on", LeakMonitorConsts.WaterClearedState);
|
||||
var change = LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
LeakMonitorTestData.AquariumSensorId,
|
||||
LeakMonitorConsts.WaterDetectedState,
|
||||
LeakMonitorConsts.WaterDetectedState);
|
||||
|
||||
sut.HandleStateChange("binary_sensor.aquarium_leak_sensor", change);
|
||||
sut.HandleStateChange(LeakMonitorTestData.AquariumSensorId, change);
|
||||
sut.HandleStateChange(LeakMonitorTestData.AquariumSensorId, change);
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.DismissLeakAlert("binary_sensor.aquarium_leak_sensor"),
|
||||
Times.Once);
|
||||
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
|
||||
Times.Exactly(2));
|
||||
_notificationServiceMock.Verify(n => n.DismissLeakAlert(It.IsAny<string>()), Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandleStateChange_UnknownState_ShouldNotNotify()
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.BlankSensorIds), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void HandleStateChange_WithBlankSensorId_ShouldForwardTheIdVerbatim(string sensorEntityId)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
var change = CreateStateChange("off", "unavailable");
|
||||
var change = LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
sensorEntityId,
|
||||
LeakMonitorConsts.WaterClearedState,
|
||||
LeakMonitorConsts.WaterDetectedState);
|
||||
|
||||
sut.HandleStateChange("binary_sensor.aquarium_leak_sensor", change);
|
||||
sut.HandleStateChange(sensorEntityId, change);
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.SendLeakAlert(It.IsAny<string>()),
|
||||
Times.Never);
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.DismissLeakAlert(It.IsAny<string>()),
|
||||
Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandleStateChange_CaseInsensitive_ShouldDetectWater()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
var change = CreateStateChange("off", "ON");
|
||||
|
||||
sut.HandleStateChange("binary_sensor.boiler_moisture", change);
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.SendLeakAlert("binary_sensor.boiler_moisture"),
|
||||
Times.Once);
|
||||
_notificationServiceMock.Verify(n => n.SendLeakAlert(sensorEntityId), Times.Once());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private StateChange CreateStateChange(string? oldState, string? newState)
|
||||
{
|
||||
var entity = new Entity(_haContextMock.Object, "binary_sensor.test");
|
||||
var oldEntityState = oldState is not null
|
||||
? new EntityState { State = oldState }
|
||||
: null;
|
||||
var newEntityState = newState is not null
|
||||
? new EntityState { State = newState }
|
||||
: null;
|
||||
#region InitializeAsync
|
||||
|
||||
return new StateChange(entity, oldEntityState, newEntityState);
|
||||
[Fact]
|
||||
public void InitializeAsync_WhenNoSensorsConfigured_ShouldNotSubscribeAndLogWarning()
|
||||
{
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(sensorEntityIds: []));
|
||||
|
||||
sut.Should().NotBeNull();
|
||||
_stateChanges.SubscriptionCount.Should().Be(0);
|
||||
_haContextMock.Verify(h => h.StateAllChanges(), Times.Never());
|
||||
VerifyNotifications(Times.Never());
|
||||
VerifyLogged(LogLevel.Warning, Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeAsync_WhenSensorsConfigured_ShouldSubscribeToEachSensor()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
_stateChanges.SubscriptionCount.Should().Be(LeakMonitorTestData.DefaultSensorIds.Length);
|
||||
_haContextMock.Verify(
|
||||
h => h.StateAllChanges(),
|
||||
Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.BlankSensorIds), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void InitializeAsync_WithBlankSensorId_ShouldStillSubscribeAndForward(string sensorEntityId)
|
||||
{
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(sensorEntityIds: [sensorEntityId]));
|
||||
|
||||
_stateChanges.SubscriptionCount.Should().Be(1);
|
||||
|
||||
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
sensorEntityId,
|
||||
LeakMonitorConsts.WaterClearedState,
|
||||
LeakMonitorConsts.WaterDetectedState));
|
||||
|
||||
_notificationServiceMock.Verify(n => n.SendLeakAlert(sensorEntityId), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeAsync_WithNullSensorEntry_ShouldStillSubscribeRemainingSensors()
|
||||
{
|
||||
CreateSut(LeakMonitorTestData.CreateOptions(
|
||||
sensorEntityIds: [null!, LeakMonitorTestData.BoilerSensorId]));
|
||||
|
||||
_stateChanges.SubscriptionCount.Should().Be(2);
|
||||
|
||||
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
LeakMonitorTestData.BoilerSensorId,
|
||||
LeakMonitorConsts.WaterClearedState,
|
||||
LeakMonitorConsts.WaterDetectedState));
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.SendLeakAlert(LeakMonitorTestData.BoilerSensorId),
|
||||
Times.Once());
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
|
||||
Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.WaterDetectedCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void InitializeAsync_WhenSubscribedSensorDetectsWater_ShouldSendLeakAlert(
|
||||
string sensorEntityId,
|
||||
string? newState)
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_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());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.WaterClearedCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void InitializeAsync_WhenSubscribedSensorClearsWater_ShouldDismissLeakAlert(
|
||||
string sensorEntityId,
|
||||
string? newState)
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
sensorEntityId,
|
||||
LeakMonitorConsts.WaterDetectedState,
|
||||
newState));
|
||||
|
||||
_notificationServiceMock.Verify(n => n.DismissLeakAlert(sensorEntityId), Times.Once());
|
||||
_notificationServiceMock.Verify(n => n.SendLeakAlert(It.IsAny<string>()), Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.NonActionableStateCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void InitializeAsync_WhenSubscribedSensorReportsNonActionableState_ShouldNotNotify(string? newState)
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
LeakMonitorTestData.AquariumSensorId,
|
||||
LeakMonitorConsts.WaterDetectedState,
|
||||
newState));
|
||||
|
||||
VerifyNotifications(Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeAsync_WhenStateIsUnchanged_ShouldNotNotify()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
LeakMonitorTestData.AquariumSensorId,
|
||||
LeakMonitorConsts.WaterDetectedState,
|
||||
LeakMonitorConsts.WaterDetectedState));
|
||||
|
||||
VerifyNotifications(Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeAsync_WhenUnrelatedEntityChanges_ShouldNotNotify()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
_stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_haContextMock.Object,
|
||||
"binary_sensor.unrelated_motion",
|
||||
LeakMonitorConsts.WaterClearedState,
|
||||
LeakMonitorConsts.WaterDetectedState));
|
||||
|
||||
VerifyNotifications(Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeAsync_WhenDetectionSequenceArrives_ShouldNotifyPerTransition()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
foreach (var change in LeakMonitorTestData.CreateStateChangeSequence(
|
||||
_haContextMock.Object,
|
||||
LeakMonitorTestData.AquariumSensorId,
|
||||
LeakMonitorTestData.DetectionSequence))
|
||||
{
|
||||
_stateChanges.Publish(change);
|
||||
}
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
|
||||
Times.Exactly(2));
|
||||
_notificationServiceMock.Verify(
|
||||
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"));
|
||||
|
||||
CreateSut();
|
||||
|
||||
var act = () => _stateChanges.Publish(LeakMonitorTestData.CreateStateChange(
|
||||
_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));
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.DismissLeakAlert(LeakMonitorTestData.AquariumSensorId),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeAsync_WhenSubscriptionFails_ShouldSwallowAndLogError()
|
||||
{
|
||||
_haContextMock
|
||||
.Setup(h => h.StateAllChanges())
|
||||
.Throws(new InvalidOperationException("home assistant offline"));
|
||||
|
||||
var act = () => CreateSut();
|
||||
|
||||
act.Should().NotThrow();
|
||||
_stateChanges.SubscriptionCount.Should().Be(0);
|
||||
VerifyNotifications(Times.Never());
|
||||
VerifyLogged(LogLevel.Error, Times.Once());
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user