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 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();
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using System.Text.Json;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager.Models;
|
||||
@@ -5,6 +7,8 @@ using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
@@ -20,12 +24,12 @@ internal static class LeakMonitorTestData
|
||||
|
||||
public static readonly string[] DefaultSensorIds = [AquariumSensorId, BoilerSensorId];
|
||||
|
||||
private static readonly DateTimeOffset DefaultStart = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
private static readonly DateTimeOffset DefaultStart = new DateTimeOffset(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
/// <summary>
|
||||
/// Every casing of the "water detected" state, across both configured sensors.
|
||||
/// </summary>
|
||||
public static TheoryData<string, string?> WaterDetectedCases { get; } = new()
|
||||
public static TheoryData<string, string?> WaterDetectedCases { get; } = new TheoryData<string, string?>
|
||||
{
|
||||
{ AquariumSensorId, LeakMonitorConsts.WaterDetectedState },
|
||||
{ AquariumSensorId, "ON" },
|
||||
@@ -37,7 +41,7 @@ internal static class LeakMonitorTestData
|
||||
/// <summary>
|
||||
/// Every casing of the "water cleared" state, across both configured sensors.
|
||||
/// </summary>
|
||||
public static TheoryData<string, string?> WaterClearedCases { get; } = new()
|
||||
public static TheoryData<string, string?> WaterClearedCases { get; } = new TheoryData<string, string?>
|
||||
{
|
||||
{ AquariumSensorId, LeakMonitorConsts.WaterClearedState },
|
||||
{ AquariumSensorId, "OFF" },
|
||||
@@ -83,7 +87,7 @@ internal static class LeakMonitorTestData
|
||||
/// (sensor id, expected notification id) rows for the id sanitising rules:
|
||||
/// dots become underscores, other characters and blanks are preserved.
|
||||
/// </summary>
|
||||
public static TheoryData<string, string> NotificationIdCases { get; } = new()
|
||||
public static TheoryData<string, string> NotificationIdCases { get; } = new TheoryData<string, string>
|
||||
{
|
||||
{ AquariumSensorId, "leak_monitor_binary_sensor_aquarium_leak_sensor" },
|
||||
{ BoilerSensorId, "leak_monitor_binary_sensor_boiler_moisture" },
|
||||
@@ -100,7 +104,7 @@ internal static class LeakMonitorTestData
|
||||
/// rows straddling the strict <c>elapsed < cooldown</c> boundary, plus zero, negative
|
||||
/// and int-max cooldowns.
|
||||
/// </summary>
|
||||
public static TheoryData<int, int, int> CooldownBoundaryCases { get; } = new()
|
||||
public static TheoryData<int, int, int> CooldownBoundaryCases { get; } = new TheoryData<int, int, int>
|
||||
{
|
||||
{ 5, 0, 1 },
|
||||
{ 5, 299, 1 },
|
||||
@@ -172,7 +176,7 @@ internal static class LeakMonitorTestData
|
||||
string? newState)
|
||||
{
|
||||
return new StateChange(new Entity(haContext, entityId), CreateOptionalState(oldState),
|
||||
CreateOptionalState(newState));
|
||||
CreateOptionalState(newState));
|
||||
}
|
||||
|
||||
public static IReadOnlyList<StateChange> CreateStateChangeSequence(
|
||||
@@ -183,7 +187,7 @@ internal static class LeakMonitorTestData
|
||||
return
|
||||
[
|
||||
.. transitions.Select(transition =>
|
||||
CreateStateChange(haContext, entityId, transition.OldState, transition.NewState))
|
||||
CreateStateChange(haContext, entityId, transition.OldState, transition.NewState))
|
||||
];
|
||||
}
|
||||
|
||||
@@ -196,13 +200,13 @@ internal static class LeakMonitorTestData
|
||||
var calls = new List<ServiceCall>();
|
||||
|
||||
haContextMock
|
||||
.Setup(h => h.CallService(
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()))
|
||||
.Callback<string, string, ServiceTarget?, object?>((domain, service, target, data) =>
|
||||
calls.Add(new ServiceCall(domain, service, target, data)));
|
||||
.Setup(h => h.CallService(
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()))
|
||||
.Callback<string, string, ServiceTarget?, object?>((domain, service, target, data) =>
|
||||
calls.Add(new ServiceCall(domain, service, target, data)));
|
||||
|
||||
return calls;
|
||||
}
|
||||
@@ -258,7 +262,10 @@ internal static class LeakMonitorTestData
|
||||
|
||||
public void Publish(T value)
|
||||
{
|
||||
foreach (var observer in _observers.ToArray()) observer.OnNext(value);
|
||||
foreach (var observer in _observers.ToArray())
|
||||
{
|
||||
observer.OnNext(value);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Subscription(List<IObserver<T>> observers, IObserver<T> observer) : IDisposable
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using FluentAssertions;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager;
|
||||
@@ -8,59 +10,61 @@ using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
public class LeakNotificationServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<LeakNotificationService>> _loggerMock = new();
|
||||
private readonly LeakMonitorTestData.ManualTimeProvider _timeProvider = new();
|
||||
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
|
||||
private readonly Mock<ILogger<LeakNotificationService>> _loggerMock = new Mock<ILogger<LeakNotificationService>>();
|
||||
private readonly LeakMonitorTestData.ManualTimeProvider _timeProvider = new LeakMonitorTestData.ManualTimeProvider();
|
||||
|
||||
private LeakNotificationService CreateSut(LeakMonitorOptions? options = null)
|
||||
{
|
||||
return new LeakNotificationService(
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object,
|
||||
Options.Create(options ?? LeakMonitorTestData.CreateOptions()),
|
||||
_timeProvider);
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object,
|
||||
Options.Create(options ?? LeakMonitorTestData.CreateOptions()),
|
||||
_timeProvider);
|
||||
}
|
||||
|
||||
private void VerifyMobilePush(Times times)
|
||||
{
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
}
|
||||
|
||||
private void VerifyPersistentCreate(Times times)
|
||||
{
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
}
|
||||
|
||||
private void VerifyPersistentDismiss(Times times)
|
||||
{
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
}
|
||||
|
||||
#region SendLeakAlert
|
||||
|
||||
public static TheoryData<string, string> SendAlertServiceCalls { get; } = new()
|
||||
public static TheoryData<string, string> SendAlertServiceCalls { get; } = new TheoryData<string, string>
|
||||
{
|
||||
{ LeakMonitorConsts.NotifyDomain, LeakMonitorConsts.MobileAppService },
|
||||
{ LeakMonitorConsts.PersistentNotificationDomain, LeakMonitorConsts.PersistentNotificationCreateService }
|
||||
@@ -75,8 +79,8 @@ public class LeakNotificationServiceTests
|
||||
sut.SendLeakAlert(LeakMonitorTestData.AquariumSensorId);
|
||||
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(domain, service, It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once());
|
||||
h => h.CallService(domain, service, It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -110,8 +114,11 @@ public class LeakNotificationServiceTests
|
||||
var persistentPayload = LeakMonitorTestData.SerializePayload(persistentCall.Data);
|
||||
persistentPayload.GetProperty("title").GetString().Should().Be(LeakMonitorConsts.NotificationTitle);
|
||||
persistentPayload.GetProperty("message").GetString().Should().Be(expectedMessage);
|
||||
persistentPayload.GetProperty("notification_id").GetString().Should()
|
||||
.Be(LeakNotificationService.BuildNotificationId(sensorId));
|
||||
|
||||
persistentPayload.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(LeakNotificationService.BuildNotificationId(sensorId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -136,17 +143,17 @@ public class LeakNotificationServiceTests
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
var persistentCall = calls.Single(c =>
|
||||
c is
|
||||
{
|
||||
Domain: LeakMonitorConsts.PersistentNotificationDomain,
|
||||
Service: LeakMonitorConsts.PersistentNotificationCreateService
|
||||
});
|
||||
c is
|
||||
{
|
||||
Domain: LeakMonitorConsts.PersistentNotificationDomain,
|
||||
Service: LeakMonitorConsts.PersistentNotificationCreateService
|
||||
});
|
||||
|
||||
LeakMonitorTestData.SerializePayload(persistentCall.Data)
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -156,7 +163,10 @@ public class LeakNotificationServiceTests
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++) sut.SendLeakAlert(sensorId);
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Once());
|
||||
VerifyPersistentCreate(Times.Once());
|
||||
@@ -169,7 +179,10 @@ public class LeakNotificationServiceTests
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(notificationCooldownMinutes: 0));
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++) sut.SendLeakAlert(sensorId);
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Exactly(repeats));
|
||||
VerifyPersistentCreate(Times.Exactly(repeats));
|
||||
@@ -214,7 +227,10 @@ public class LeakNotificationServiceTests
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
foreach (var sensorId in LeakMonitorTestData.DefaultSensorIds) sut.SendLeakAlert(sensorId);
|
||||
foreach (var sensorId in LeakMonitorTestData.DefaultSensorIds)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
VerifyPersistentCreate(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
@@ -236,11 +252,11 @@ public class LeakNotificationServiceTests
|
||||
VerifyMobilePush(Times.Exactly(2));
|
||||
|
||||
var boilerNotifications = calls
|
||||
.Where(c => c.Domain == LeakMonitorConsts.NotifyDomain)
|
||||
.Where(c => LeakMonitorTestData.SerializePayload(c.Data)
|
||||
.GetProperty("message")
|
||||
.GetString()!
|
||||
.Contains(LeakMonitorTestData.BoilerSensorId, StringComparison.Ordinal));
|
||||
.Where(c => c.Domain == LeakMonitorConsts.NotifyDomain)
|
||||
.Where(c => LeakMonitorTestData.SerializePayload(c.Data)
|
||||
.GetProperty("message")
|
||||
.GetString()!
|
||||
.Contains(LeakMonitorTestData.BoilerSensorId, StringComparison.Ordinal));
|
||||
|
||||
boilerNotifications.Should().ContainSingle();
|
||||
}
|
||||
@@ -286,11 +302,12 @@ public class LeakNotificationServiceTests
|
||||
dismissCall.Domain.Should().Be(LeakMonitorConsts.PersistentNotificationDomain);
|
||||
dismissCall.Service.Should().Be(LeakMonitorConsts.PersistentNotificationDismissService);
|
||||
dismissCall.Target.Should().BeNull();
|
||||
|
||||
LeakMonitorTestData.SerializePayload(dismissCall.Data)
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user