Files
HomeAssistant/LightMonitor.Manager.Tests/LightControlServiceTests.cs
T
gitea_admin ada3c77778 Fix culture-sensitive parsing, log swallowing and closure capture
- DateTimeTypeConverter parses with the invariant culture instead of the
  ambient one (NordPool timestamps arrive as ISO-8601)
- Shower last-shower-time state is formatted with the invariant culture
- PresenceServiceTests no longer captures the loop variable in a Moq setup
- CheapestIntervalsFinderTests compare prices with a tolerance instead of
  floating-point equality
- MqttRecreateApp logs the unit_of_measurement read failure instead of
  silently swallowing every exception
- LightMonitorOptions moves to LightMonitor.Models so the namespace matches
  the project and file location
2026-11-29 21:31:45 +02:00

141 lines
4.5 KiB
C#

using LightMonitor.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using LightMonitorManager;
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(lightEntityId: 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);
}
}