This commit is contained in:
2026-06-28 13:58:35 +03:00
parent 8f45028715
commit e6bf8258bb
114 changed files with 4365 additions and 874 deletions
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LeakMonitor.Manager\LeakMonitor.Manager.csproj" />
<ProjectReference Include="..\LeakMonitor.Models\LeakMonitor.Models.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,112 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using LeakMonitor.Models;
using LeakMonitorManager;
using LeakMonitorManager.Interface;
using LeakMonitorManager.Models;
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;
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"]
};
}
private LeakMonitorInit CreateSut()
{
return new LeakMonitorInit(
_haContextMock.Object,
_notificationServiceMock.Object,
_loggerMock.Object,
Options.Create(_options));
}
#region HandleStateChange
[Fact]
public void HandleStateChange_WaterDetected_ShouldSendLeakAlert()
{
var sut = CreateSut();
var change = CreateStateChange("off", LeakMonitorConsts.WaterDetectedState);
sut.HandleStateChange("binary_sensor.aquarium_leak_sensor", change);
_notificationServiceMock.Verify(
n => n.SendLeakAlert("binary_sensor.aquarium_leak_sensor"),
Times.Once);
}
[Fact]
public void HandleStateChange_WaterCleared_ShouldDismissLeakAlert()
{
var sut = CreateSut();
var change = CreateStateChange("on", LeakMonitorConsts.WaterClearedState);
sut.HandleStateChange("binary_sensor.aquarium_leak_sensor", change);
_notificationServiceMock.Verify(
n => n.DismissLeakAlert("binary_sensor.aquarium_leak_sensor"),
Times.Once);
}
[Fact]
public void HandleStateChange_UnknownState_ShouldNotNotify()
{
var sut = CreateSut();
var change = CreateStateChange("off", "unavailable");
sut.HandleStateChange("binary_sensor.aquarium_leak_sensor", 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);
}
#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;
return new StateChange(entity, oldEntityState, newEntityState);
}
}
@@ -0,0 +1,155 @@
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using LeakMonitor.Models;
using LeakMonitorManager;
using LeakMonitorManager.Models;
namespace LeakMonitor.Manager.Tests;
public class LeakNotificationServiceTests
{
private readonly Mock<IHaContext> _haContextMock;
private readonly Mock<ILogger<LeakNotificationService>> _loggerMock;
private readonly LeakMonitorOptions _options;
public LeakNotificationServiceTests()
{
_haContextMock = new Mock<IHaContext>();
_loggerMock = new Mock<ILogger<LeakNotificationService>>();
_options = new LeakMonitorOptions
{
Enabled = true,
NotificationCooldownMinutes = LeakMonitorConsts.DefaultNotificationCooldownMinutes
};
}
private LeakNotificationService CreateSut()
{
return new LeakNotificationService(
_haContextMock.Object,
_loggerMock.Object,
Options.Create(_options));
}
#region SendLeakAlert
[Fact]
public void SendLeakAlert_ShouldCallMobileNotifyService()
{
var sut = CreateSut();
sut.SendLeakAlert("binary_sensor.aquarium_leak_sensor");
_haContextMock.Verify(h => h.CallService(
LeakMonitorConsts.NotifyDomain,
LeakMonitorConsts.MobileAppService,
It.IsAny<ServiceTarget?>(),
It.IsAny<object?>()), Times.Once);
}
[Fact]
public void SendLeakAlert_ShouldCallPersistentNotificationCreate()
{
var sut = CreateSut();
sut.SendLeakAlert("binary_sensor.aquarium_leak_sensor");
_haContextMock.Verify(h => h.CallService(
LeakMonitorConsts.PersistentNotificationDomain,
LeakMonitorConsts.PersistentNotificationCreateService,
It.IsAny<ServiceTarget?>(),
It.IsAny<object?>()), Times.Once);
}
[Fact]
public void SendLeakAlert_CalledTwiceWithinCooldown_ShouldOnlyNotifyOnce()
{
var sut = CreateSut();
const string sensorId = "binary_sensor.aquarium_leak_sensor";
sut.SendLeakAlert(sensorId);
sut.SendLeakAlert(sensorId);
_haContextMock.Verify(h => h.CallService(
LeakMonitorConsts.NotifyDomain,
LeakMonitorConsts.MobileAppService,
It.IsAny<ServiceTarget?>(),
It.IsAny<object?>()), Times.Once);
}
[Fact]
public void SendLeakAlert_DifferentSensors_ShouldNotifyForEach()
{
var sut = CreateSut();
sut.SendLeakAlert("binary_sensor.aquarium_leak_sensor");
sut.SendLeakAlert("binary_sensor.boiler_moisture");
_haContextMock.Verify(h => h.CallService(
LeakMonitorConsts.NotifyDomain,
LeakMonitorConsts.MobileAppService,
It.IsAny<ServiceTarget?>(),
It.IsAny<object?>()), Times.Exactly(2));
}
[Fact]
public void SendLeakAlert_WithZeroCooldown_ShouldAlwaysNotify()
{
_options.NotificationCooldownMinutes = 0;
var sut = CreateSut();
const string sensorId = "binary_sensor.aquarium_leak_sensor";
sut.SendLeakAlert(sensorId);
sut.SendLeakAlert(sensorId);
_haContextMock.Verify(h => h.CallService(
LeakMonitorConsts.NotifyDomain,
LeakMonitorConsts.MobileAppService,
It.IsAny<ServiceTarget?>(),
It.IsAny<object?>()), Times.Exactly(2));
}
#endregion
#region DismissLeakAlert
[Fact]
public void DismissLeakAlert_ShouldCallPersistentNotificationDismiss()
{
var sut = CreateSut();
sut.DismissLeakAlert("binary_sensor.aquarium_leak_sensor");
_haContextMock.Verify(h => h.CallService(
LeakMonitorConsts.PersistentNotificationDomain,
LeakMonitorConsts.PersistentNotificationDismissService,
It.IsAny<ServiceTarget?>(),
It.IsAny<object?>()), Times.Once);
}
#endregion
#region BuildNotificationId
[Fact]
public void BuildNotificationId_ShouldReplaceDotsWithUnderscores()
{
var result = LeakNotificationService.BuildNotificationId("binary_sensor.aquarium_leak_sensor");
result.Should().Be("leak_monitor_binary_sensor_aquarium_leak_sensor");
}
[Fact]
public void BuildNotificationId_ShouldStartWithPrefix()
{
var result = LeakNotificationService.BuildNotificationId("binary_sensor.boiler_moisture");
result.Should().StartWith(LeakMonitorConsts.NotificationIdPrefix);
}
#endregion
}