feat: add BatteryMonitor implementation with discovery, notification, and scheduling services
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager;
|
||||
|
||||
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 BatteryNotificationService CreateSut()
|
||||
{
|
||||
return new BatteryNotificationService(_haContextMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
#region NotifyLowBattery
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithDevices_ShouldCallPersistentNotificationCreate()
|
||||
{
|
||||
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 sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotifyLowBattery_WithEmptyList_ShouldNotCallService()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>();
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Never);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DismissNotification
|
||||
|
||||
[Fact]
|
||||
public void DismissNotification_ShouldCallPersistentNotificationDismiss()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
sut.DismissNotification();
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region BuildNotificationMessage
|
||||
|
||||
[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 message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines[1].Should().Contain("Device B");
|
||||
lines[2].Should().Contain("Device C");
|
||||
lines[3].Should().Contain("Device A");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainTitle()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Device A", BatteryLevel = 2 }
|
||||
};
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("low battery");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_ShouldContainDeviceNameAndLevel()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Bathroom Battery", BatteryLevel = 3 }
|
||||
};
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("Bathroom Battery");
|
||||
message.Should().Contain("3%");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildNotificationMessage_WithSingleDevice_ShouldFormatCorrectly()
|
||||
{
|
||||
var devices = new List<BatterySensorInfo>
|
||||
{
|
||||
new() { EntityId = "sensor.a", FriendlyName = "Test Device", BatteryLevel = 1 }
|
||||
};
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
message.Should().Contain("- **Test Device**: 1%");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user