Files
HomeAssistant/LightMonitor.Manager.Tests/LightControlServiceTests.cs
T
gitea_admin 5777e641f6 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
2026-11-29 21:30:13 +02:00

142 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;
using LightMonitorManager.Models;
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);
}
}