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 System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
@@ -5,50 +6,55 @@ using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager;
|
||||
using static BatteryMonitor.Manager.Tests.BatteryMonitorTestData;
|
||||
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
public class BatteryNotificationServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<BatteryNotificationService>> _loggerMock;
|
||||
|
||||
public BatteryNotificationServiceTests()
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<BatteryNotificationService>>();
|
||||
}
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<BatteryNotificationService>> _loggerMock = new();
|
||||
|
||||
private BatteryNotificationService CreateSut()
|
||||
{
|
||||
return new BatteryNotificationService(_haContextMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
private void VerifyCallService(string service, Times times)
|
||||
{
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
service,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), times);
|
||||
}
|
||||
|
||||
#region NotifyLowBattery
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithDevices_ShouldCallPersistentNotificationCreate()
|
||||
[Theory]
|
||||
[MemberData(nameof(NotifyDeviceCounts), MemberType = typeof(BatteryMonitorTestData))]
|
||||
public void NotifyLowBattery_WithDevices_ShouldCallPersistentNotificationCreate(int deviceCount)
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.bathroom_battery", FriendlyName = "Bathroom Battery", BatteryLevel = 3 },
|
||||
new() { EntityId = "sensor.bedroom_battery", FriendlyName = "Bedroom Battery", BatteryLevel = 1 }
|
||||
};
|
||||
var devices = CreateSensorInfos(
|
||||
Enumerable.Range(0, deviceCount)
|
||||
.Select(i => ($"sensor.device_{i:D3}", $"Device {i:D3}", (double)(i % 5)))
|
||||
.ToArray());
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationCreateService, Times.Once());
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationDismissService, Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithEmptyList_ShouldNotCallService()
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void NotifyLowBattery_WithEmptyList_ShouldNotCallService(bool useArray)
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>();
|
||||
IReadOnlyList<BatterySensorInfo> devices = useArray
|
||||
? Array.Empty<BatterySensorInfo>()
|
||||
: new List<BatterySensorInfo>();
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
@@ -58,6 +64,59 @@ public class BatteryNotificationServiceTests
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Never);
|
||||
_loggerMock.Invocations.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_ShouldSendTitleNotificationIdAndDeviceSummary()
|
||||
{
|
||||
object? payload = null;
|
||||
_haContextMock
|
||||
.Setup(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()))
|
||||
.Callback<string, string, ServiceTarget?, object?>((_, _, _, data) => payload = data);
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(CreateSensorInfos(
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1)));
|
||||
|
||||
payload.Should().NotBeNull();
|
||||
|
||||
using var document = JsonDocument.Parse(JsonSerializer.Serialize(payload));
|
||||
var root = document.RootElement;
|
||||
root.GetProperty("title").GetString().Should().Be(BatteryMonitorConsts.NotificationTitle);
|
||||
root.GetProperty("notification_id").GetString().Should().Be(BatteryMonitorConsts.NotificationId);
|
||||
|
||||
var message = root.GetProperty("message").GetString();
|
||||
message.Should().Contain("Device A").And.Contain("Device B");
|
||||
message.Should().Contain("1%").And.Contain("4%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithDevices_ShouldLogDeviceCount()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.a", "Device A", 2),
|
||||
("sensor.b", "Device B", 3));
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_loggerMock.Verify(
|
||||
l => l.Log(
|
||||
LogLevel.Information,
|
||||
It.IsAny<EventId>(),
|
||||
It.Is<It.IsAnyType>((state, _) => string.Equals(
|
||||
state!.ToString(),
|
||||
"Sent low battery notification for 2 device(s)",
|
||||
StringComparison.Ordinal)),
|
||||
It.IsAny<Exception?>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -70,11 +129,25 @@ public class BatteryNotificationServiceTests
|
||||
var sut = CreateSut();
|
||||
sut.DismissNotification();
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationDismissService, Times.Once());
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationCreateService, Times.Never());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(2)]
|
||||
[InlineData(5)]
|
||||
public void DismissNotification_WhenCalledRepeatedly_ShouldDismissEachTime(int calls)
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
for (var i = 0; i < calls; i++)
|
||||
{
|
||||
sut.DismissNotification();
|
||||
}
|
||||
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationDismissService, Times.Exactly(calls));
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationCreateService, Times.Never());
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -84,41 +157,88 @@ public class BatteryNotificationServiceTests
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldOrderByBatteryLevel()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Device A", BatteryLevel = 4 },
|
||||
new() { EntityId = "sensor.b", FriendlyName = "Device B", BatteryLevel = 1 },
|
||||
new() { EntityId = "sensor.c", FriendlyName = "Device C", BatteryLevel = 3 }
|
||||
};
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1),
|
||||
("sensor.c", "Device C", 3));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(4);
|
||||
lines[1].Should().Contain("Device B");
|
||||
lines[2].Should().Contain("Device C");
|
||||
lines[3].Should().Contain("Device A");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldKeepInsertionOrderForEqualLevels()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.first", "First Device", 4),
|
||||
("sensor.second", "Second Device", 4),
|
||||
("sensor.third", "Third Device", 1));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(4);
|
||||
lines[1].Should().Contain("Third Device");
|
||||
lines[2].Should().Contain("First Device");
|
||||
lines[3].Should().Contain("Second Device");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithGeneratedDevices_ShouldOrderByAscendingLevel()
|
||||
{
|
||||
var devices = CreateGeneratedSensorInfos(50);
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var expectedLines = devices
|
||||
.OrderBy(d => d.BatteryLevel)
|
||||
.Select(d => $"- **{d.FriendlyName}**: {d.BatteryLevel:F0}%");
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(devices.Count + 1);
|
||||
lines.Skip(1).Should().Equal(expectedLines);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainTitle()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Device A", BatteryLevel = 2 }
|
||||
};
|
||||
var devices = CreateSensorInfos(("sensor.a", "Device A", 2));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("low battery");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Bathroom Battery", 3, "- **Bathroom Battery**: 3%")]
|
||||
[InlineData("Test Device", 1, "- **Test Device**: 1%")]
|
||||
[InlineData("Empty Battery", 0, "- **Empty Battery**: 0%")]
|
||||
[InlineData("Negative Battery", -7, "- **Negative Battery**: -7%")]
|
||||
[InlineData("Rounded Down", 12.4, "- **Rounded Down**: 12%")]
|
||||
[InlineData("Rounded Up", 12.6, "- **Rounded Up**: 13%")]
|
||||
[InlineData("Full Battery", 100, "- **Full Battery**: 100%")]
|
||||
[InlineData("", 5, "- ****: 5%")]
|
||||
[InlineData(" ", 4, "- ** **: 4%")]
|
||||
public void BuildNotificationMessage_ShouldFormatDeviceLine(string friendlyName, double batteryLevel, string expectedLine)
|
||||
{
|
||||
var devices = CreateSensorInfos(("sensor.a", friendlyName, batteryLevel));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(2);
|
||||
lines[1].Should().Be(expectedLine);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainDeviceNameAndLevel()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Bathroom Battery", BatteryLevel = 3 }
|
||||
};
|
||||
var devices = CreateSensorInfos(("sensor.a", "Bathroom Battery", 3));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
@@ -129,15 +249,36 @@ public class BatteryNotificationServiceTests
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithSingleDevice_ShouldFormatCorrectly()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Test Device", BatteryLevel = 1 }
|
||||
};
|
||||
var devices = CreateSensorInfos(("sensor.a", "Test Device", 1));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("- **Test Device**: 1%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithDuplicateDevices_ShouldListEveryDevice()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2),
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(3);
|
||||
lines[1].Should().Be("- **Duplicate Battery**: 2%");
|
||||
lines[2].Should().Be("- **Duplicate Battery**: 2%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithNoDevices_ShouldContainOnlyHeader()
|
||||
{
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(Array.Empty<BatterySensorInfo>());
|
||||
|
||||
message.Should().Be("The following devices have low battery:");
|
||||
message.Should().NotContain("- **");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user