24729ee267
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
140 lines
4.5 KiB
C#
140 lines
4.5 KiB
C#
using LightMonitor.Models;
|
|
using LightMonitorManager;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
|
|
namespace LightMonitor.Manager.Tests;
|
|
|
|
public class LightControlServiceTests
|
|
{
|
|
private const string LightEntityId = LightMonitorTestData.LightEntityId;
|
|
private const string SunEntityId = LightMonitorTestData.SunEntityId;
|
|
|
|
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
|
|
|
|
private readonly Mock<IHaContext> _haContextMock = new();
|
|
|
|
private LightControlService CreateService(LightMonitorOptions? options = null)
|
|
{
|
|
return new LightControlService(
|
|
_haContextMock.Object,
|
|
Mock.Of<ILogger<LightControlService>>(),
|
|
Options.Create(options ?? _defaultOptions));
|
|
}
|
|
|
|
private void SetupState(string entityId, string? state)
|
|
{
|
|
_haContextMock.Setup(x => x.GetState(entityId))
|
|
.Returns(LightMonitorTestData.CreateOptionalState(state));
|
|
}
|
|
|
|
private void VerifyTurnOn(string entityId, bool shouldTurnOn)
|
|
{
|
|
if (!shouldTurnOn)
|
|
{
|
|
_haContextMock.Verify(x => x.CallService(
|
|
It.IsAny<string>(), It.IsAny<string>(),
|
|
It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
|
Times.Never);
|
|
|
|
return;
|
|
}
|
|
|
|
_haContextMock.Verify(x => x.CallService("light", "turn_on",
|
|
It.Is<ServiceTarget>(target => target.EntityIds != null && target.EntityIds.Contains(entityId)),
|
|
It.IsAny<object?>()),
|
|
Times.Once);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(LightMonitorTestData.SunStateTurnOnCases), MemberType = typeof(LightMonitorTestData))]
|
|
public void TurnOnLight_ShouldOnlyTurnOnWhenSunIsBelowHorizon(string? sunState, bool shouldTurnOn)
|
|
{
|
|
SetupState(SunEntityId, sunState);
|
|
SetupState(LightEntityId, "off");
|
|
|
|
CreateService().TurnOnLight();
|
|
|
|
VerifyTurnOn(LightEntityId, shouldTurnOn);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(LightMonitorTestData.LightStateTurnOnCases), MemberType = typeof(LightMonitorTestData))]
|
|
public void TurnOnLight_WhenSunIsBelowHorizon_ShouldOnlyTurnOnWhenLightIsNotAlreadyOn(
|
|
string? lightState,
|
|
bool shouldTurnOn)
|
|
{
|
|
SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon);
|
|
SetupState(LightEntityId, lightState);
|
|
|
|
CreateService().TurnOnLight();
|
|
|
|
VerifyTurnOn(LightEntityId, shouldTurnOn);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_ShouldTargetTheConfiguredLightEntity()
|
|
{
|
|
const string customLightEntityId = "light.kitchen_ceiling";
|
|
var options = LightMonitorTestData.CreateOptions(customLightEntityId);
|
|
|
|
SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon);
|
|
SetupState(customLightEntityId, "off");
|
|
|
|
CreateService(options).TurnOnLight();
|
|
|
|
VerifyTurnOn(customLightEntityId, true);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WithDefaultOptions_ShouldTargetTheDefaultLightEntity()
|
|
{
|
|
var options = new LightMonitorOptions();
|
|
|
|
SetupState(LightMonitorConsts.DefaultSunEntityId, LightMonitorConsts.SunBelowHorizon);
|
|
SetupState(LightMonitorConsts.DefaultLightEntityId, null);
|
|
|
|
CreateService(options).TurnOnLight();
|
|
|
|
VerifyTurnOn(LightMonitorConsts.DefaultLightEntityId, true);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_WhenSunIsAboveHorizon_ShouldNotReadTheLightState()
|
|
{
|
|
SetupState(SunEntityId, LightMonitorConsts.SunAboveHorizon);
|
|
|
|
CreateService().TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.GetState(LightEntityId), Times.Never);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
public void TurnOnLight_WhenSunEntityIdIsBlank_ShouldNotTurnOnLight(string sunEntityId)
|
|
{
|
|
var options = LightMonitorTestData.CreateOptions(sunEntityId: sunEntityId);
|
|
SetupState(LightEntityId, "off");
|
|
|
|
CreateService(options).TurnOnLight();
|
|
|
|
VerifyTurnOn(LightEntityId, false);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOnLight_ShouldReadEachStateAtMostOnce()
|
|
{
|
|
SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon);
|
|
SetupState(LightEntityId, LightMonitorConsts.LightOnState);
|
|
|
|
CreateService().TurnOnLight();
|
|
|
|
_haContextMock.Verify(x => x.GetState(SunEntityId), Times.Once);
|
|
_haContextMock.Verify(x => x.GetState(LightEntityId), Times.Once);
|
|
VerifyTurnOn(LightEntityId, false);
|
|
}
|
|
} |