Update
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user