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:
@@ -12,74 +12,204 @@ namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
public class LeakNotificationServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<LeakNotificationService>> _loggerMock;
|
||||
private readonly LeakMonitorOptions _options;
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<LeakNotificationService>> _loggerMock = new();
|
||||
private readonly LeakMonitorTestData.ManualTimeProvider _timeProvider = new();
|
||||
|
||||
public LeakNotificationServiceTests()
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<LeakNotificationService>>();
|
||||
_options = new LeakMonitorOptions
|
||||
{
|
||||
Enabled = true,
|
||||
NotificationCooldownMinutes = LeakMonitorConsts.DefaultNotificationCooldownMinutes
|
||||
};
|
||||
}
|
||||
|
||||
private LeakNotificationService CreateSut()
|
||||
private LeakNotificationService CreateSut(LeakMonitorOptions? options = null)
|
||||
{
|
||||
return new LeakNotificationService(
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object,
|
||||
Options.Create(_options),
|
||||
TimeProvider.System);
|
||||
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);
|
||||
}
|
||||
|
||||
private void VerifyPersistentCreate(Times times)
|
||||
{
|
||||
_haContextMock.Verify(
|
||||
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);
|
||||
}
|
||||
|
||||
#region SendLeakAlert
|
||||
|
||||
[Fact]
|
||||
public void SendLeakAlert_ShouldCallMobileNotifyService()
|
||||
public static TheoryData<string, string> SendAlertServiceCalls { get; } = new()
|
||||
{
|
||||
{ LeakMonitorConsts.NotifyDomain, LeakMonitorConsts.MobileAppService },
|
||||
{ LeakMonitorConsts.PersistentNotificationDomain, LeakMonitorConsts.PersistentNotificationCreateService }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(SendAlertServiceCalls))]
|
||||
public void SendLeakAlert_ShouldCallExpectedService(string domain, string service)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.SendLeakAlert("binary_sensor.aquarium_leak_sensor");
|
||||
sut.SendLeakAlert(LeakMonitorTestData.AquariumSensorId);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(domain, service, It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendLeakAlert_ShouldCallPersistentNotificationCreate()
|
||||
public void SendLeakAlert_ShouldIncludeSensorDetailsInPayloads()
|
||||
{
|
||||
var calls = LeakMonitorTestData.CaptureServiceCalls(_haContextMock);
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
var expectedMessage = $"Water detected by {sensorId}! Check immediately.";
|
||||
|
||||
sut.SendLeakAlert("binary_sensor.aquarium_leak_sensor");
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
calls.Should().HaveCount(2);
|
||||
|
||||
var mobileCall = calls.Single(c => c.Service == LeakMonitorConsts.MobileAppService);
|
||||
mobileCall.Domain.Should().Be(LeakMonitorConsts.NotifyDomain);
|
||||
mobileCall.Target.Should().BeNull();
|
||||
|
||||
var mobilePayload = LeakMonitorTestData.SerializePayload(mobileCall.Data);
|
||||
mobilePayload.GetProperty("title").GetString().Should().Be(LeakMonitorConsts.NotificationTitle);
|
||||
mobilePayload.GetProperty("message").GetString().Should().Be(expectedMessage);
|
||||
|
||||
var sound = mobilePayload.GetProperty("data").GetProperty("push").GetProperty("sound");
|
||||
sound.GetProperty("critical").GetInt32().Should().Be(1);
|
||||
sound.GetProperty("volume").GetDouble().Should().Be(1.0);
|
||||
|
||||
var persistentCall = calls.Single(c => c.Service == LeakMonitorConsts.PersistentNotificationCreateService);
|
||||
persistentCall.Domain.Should().Be(LeakMonitorConsts.PersistentNotificationDomain);
|
||||
persistentCall.Target.Should().BeNull();
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendLeakAlert_CalledTwiceWithinCooldown_ShouldOnlyNotifyOnce()
|
||||
public void SendLeakAlert_ShouldNotDismissNotification()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
const string sensorId = "binary_sensor.aquarium_leak_sensor";
|
||||
|
||||
sut.SendLeakAlert(LeakMonitorTestData.AquariumSensorId);
|
||||
|
||||
VerifyMobilePush(Times.Once());
|
||||
VerifyPersistentCreate(Times.Once());
|
||||
VerifyPersistentDismiss(Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.NotificationIdCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void SendLeakAlert_ShouldUseExpectedNotificationId(string sensorId, string expectedNotificationId)
|
||||
{
|
||||
var calls = LeakMonitorTestData.CaptureServiceCalls(_haContextMock);
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
var persistentCall = calls.Single(c =>
|
||||
c.Domain == LeakMonitorConsts.PersistentNotificationDomain &&
|
||||
c.Service == LeakMonitorConsts.PersistentNotificationCreateService);
|
||||
|
||||
LeakMonitorTestData.SerializePayload(persistentCall.Data)
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.RepeatedNotificationCounts), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void SendLeakAlert_RepeatedWithinCooldown_ShouldOnlyNotifyOnce(int repeats)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Once());
|
||||
VerifyPersistentCreate(Times.Once());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.RepeatedNotificationCounts), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void SendLeakAlert_RepeatedWithZeroCooldown_ShouldAlwaysNotify(int repeats)
|
||||
{
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(notificationCooldownMinutes: 0));
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Exactly(repeats));
|
||||
VerifyPersistentCreate(Times.Exactly(repeats));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.CooldownBoundaryCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void SendLeakAlert_ShouldRespectCooldownBoundaries(
|
||||
int cooldownMinutes,
|
||||
int elapsedSeconds,
|
||||
int expectedNotificationCount)
|
||||
{
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(notificationCooldownMinutes: cooldownMinutes));
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
sut.SendLeakAlert(sensorId);
|
||||
_timeProvider.AdvanceSeconds(elapsedSeconds);
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
VerifyMobilePush(Times.Exactly(expectedNotificationCount));
|
||||
VerifyPersistentCreate(Times.Exactly(expectedNotificationCount));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendLeakAlert_AfterEachCooldownExpires_ShouldNotifyEveryTime()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
sut.SendLeakAlert(sensorId);
|
||||
_timeProvider.Advance(TimeSpan.FromMinutes(LeakMonitorConsts.DefaultNotificationCooldownMinutes));
|
||||
sut.SendLeakAlert(sensorId);
|
||||
_timeProvider.Advance(TimeSpan.FromMinutes(LeakMonitorConsts.DefaultNotificationCooldownMinutes));
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
VerifyMobilePush(Times.Exactly(3));
|
||||
VerifyPersistentCreate(Times.Exactly(3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -87,31 +217,50 @@ public class LeakNotificationServiceTests
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.SendLeakAlert("binary_sensor.aquarium_leak_sensor");
|
||||
sut.SendLeakAlert("binary_sensor.boiler_moisture");
|
||||
foreach (var sensorId in LeakMonitorTestData.DefaultSensorIds)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Exactly(2));
|
||||
VerifyMobilePush(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
VerifyPersistentCreate(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendLeakAlert_WithZeroCooldown_ShouldAlwaysNotify()
|
||||
public void SendLeakAlert_CooldownShouldBeTrackedPerSensor()
|
||||
{
|
||||
_options.NotificationCooldownMinutes = 0;
|
||||
var calls = LeakMonitorTestData.CaptureServiceCalls(_haContextMock);
|
||||
var sut = CreateSut();
|
||||
const string sensorId = "binary_sensor.aquarium_leak_sensor";
|
||||
|
||||
sut.SendLeakAlert(sensorId);
|
||||
sut.SendLeakAlert(sensorId);
|
||||
sut.SendLeakAlert(LeakMonitorTestData.AquariumSensorId);
|
||||
_timeProvider.AdvanceSeconds(60);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Exactly(2));
|
||||
// A first-ever alert for a different sensor is not suppressed by the first sensor's cooldown.
|
||||
sut.SendLeakAlert(LeakMonitorTestData.BoilerSensorId);
|
||||
sut.SendLeakAlert(LeakMonitorTestData.AquariumSensorId);
|
||||
|
||||
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));
|
||||
|
||||
boilerNotifications.Should().ContainSingle();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendLeakAlert_WithNullSensorId_ShouldThrowAndNotNotify()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
var act = () => sut.SendLeakAlert(null!);
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
VerifyMobilePush(Times.Never());
|
||||
VerifyPersistentCreate(Times.Never());
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -123,33 +272,82 @@ public class LeakNotificationServiceTests
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.DismissLeakAlert("binary_sensor.aquarium_leak_sensor");
|
||||
sut.DismissLeakAlert(LeakMonitorTestData.AquariumSensorId);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
VerifyPersistentDismiss(Times.Once());
|
||||
VerifyMobilePush(Times.Never());
|
||||
VerifyPersistentCreate(Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.NotificationIdCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void DismissLeakAlert_ShouldUseExpectedNotificationId(string sensorId, string expectedNotificationId)
|
||||
{
|
||||
var calls = LeakMonitorTestData.CaptureServiceCalls(_haContextMock);
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.DismissLeakAlert(sensorId);
|
||||
|
||||
var dismissCall = calls.Should().ContainSingle().Which;
|
||||
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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DismissLeakAlert_CalledRepeatedly_ShouldDismissEachTime()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
sut.DismissLeakAlert(sensorId);
|
||||
sut.DismissLeakAlert(sensorId);
|
||||
|
||||
VerifyPersistentDismiss(Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DismissLeakAlert_ShouldNotResetCooldown()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
sut.SendLeakAlert(sensorId);
|
||||
sut.DismissLeakAlert(sensorId);
|
||||
_timeProvider.AdvanceSeconds(60);
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
VerifyMobilePush(Times.Once());
|
||||
VerifyPersistentCreate(Times.Once());
|
||||
VerifyPersistentDismiss(Times.Once());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region BuildNotificationId
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationId_ShouldReplaceDotsWithUnderscores()
|
||||
[Theory]
|
||||
[MemberData(nameof(LeakMonitorTestData.NotificationIdCases), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void BuildNotificationId_ShouldSanitizeSensorId(string sensorEntityId, string expected)
|
||||
{
|
||||
var result = LeakNotificationService.BuildNotificationId("binary_sensor.aquarium_leak_sensor");
|
||||
var result = LeakNotificationService.BuildNotificationId(sensorEntityId);
|
||||
|
||||
result.Should().Be("leak_monitor_binary_sensor_aquarium_leak_sensor");
|
||||
result.Should().Be(expected);
|
||||
result.Should().StartWith(LeakMonitorConsts.NotificationIdPrefix);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationId_ShouldStartWithPrefix()
|
||||
public void BuildNotificationId_WithNullSensorId_ShouldThrow()
|
||||
{
|
||||
var result = LeakNotificationService.BuildNotificationId("binary_sensor.boiler_moisture");
|
||||
// Production has no null guard; the string.Replace call fails before any prefix is built.
|
||||
Action act = () => LeakNotificationService.BuildNotificationId(null!);
|
||||
|
||||
result.Should().StartWith(LeakMonitorConsts.NotificationIdPrefix);
|
||||
act.Should().Throw<NullReferenceException>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user