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:
2026-11-29 21:30:13 +02:00
parent 1152462765
commit 5777e641f6
79 changed files with 7162 additions and 3196 deletions
@@ -11,14 +11,10 @@ namespace LightMonitor.Manager.Tests;
public class LightControlServiceTests
{
private const string LightEntityId = "light.sonoff_1002214d40";
private const string SunEntityId = "sun.sun";
private const string LightEntityId = LightMonitorTestData.LightEntityId;
private const string SunEntityId = LightMonitorTestData.SunEntityId;
private readonly LightMonitorOptions _defaultOptions = new()
{
LightEntityId = LightEntityId,
SunEntityId = SunEntityId
};
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new();
@@ -30,87 +26,116 @@ public class LightControlServiceTests
Options.Create(options ?? _defaultOptions));
}
[Fact]
public void TurnOnLight_WhenDarkAndLightIsOff_ShouldTurnOnLight()
private void SetupState(string entityId, string? state)
{
_haContextMock.Setup(x => x.GetState(SunEntityId))
.Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon });
_haContextMock.Setup(x => x.GetState(LightEntityId))
.Returns(new EntityState { State = "off" });
_haContextMock.Setup(x => x.GetState(entityId))
.Returns(LightMonitorTestData.CreateOptionalState(state));
}
var service = CreateService();
service.TurnOnLight();
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.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Once);
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_WhenDarkAndLightIsAlreadyOn_ShouldNotCallService()
public void TurnOnLight_ShouldTargetTheConfiguredLightEntity()
{
_haContextMock.Setup(x => x.GetState(SunEntityId))
.Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon });
_haContextMock.Setup(x => x.GetState(LightEntityId))
.Returns(new EntityState { State = "on" });
const string customLightEntityId = "light.kitchen_ceiling";
var options = LightMonitorTestData.CreateOptions(lightEntityId: customLightEntityId);
var service = CreateService();
service.TurnOnLight();
SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon);
SetupState(customLightEntityId, "off");
_haContextMock.Verify(x => x.CallService("light", "turn_on",
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
CreateService(options).TurnOnLight();
VerifyTurnOn(customLightEntityId, true);
}
[Fact]
public void TurnOnLight_WhenSunIsAboveHorizon_ShouldNotTurnOnLight()
public void TurnOnLight_WithDefaultOptions_ShouldTargetTheDefaultLightEntity()
{
_haContextMock.Setup(x => x.GetState(SunEntityId))
.Returns(new EntityState { State = LightMonitorConsts.SunAboveHorizon });
var options = new LightMonitorOptions();
var service = CreateService();
service.TurnOnLight();
SetupState(LightMonitorConsts.DefaultSunEntityId, LightMonitorConsts.SunBelowHorizon);
SetupState(LightMonitorConsts.DefaultLightEntityId, null);
_haContextMock.Verify(x => x.CallService("light", "turn_on",
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
CreateService(options).TurnOnLight();
VerifyTurnOn(LightMonitorConsts.DefaultLightEntityId, true);
}
[Fact]
public void TurnOnLight_WhenSunStateIsNull_ShouldNotTurnOnLight()
public void TurnOnLight_WhenSunIsAboveHorizon_ShouldNotReadTheLightState()
{
_haContextMock.Setup(x => x.GetState(SunEntityId))
.Returns((EntityState?)null);
SetupState(SunEntityId, LightMonitorConsts.SunAboveHorizon);
var service = CreateService();
service.TurnOnLight();
CreateService().TurnOnLight();
_haContextMock.Verify(x => x.CallService("light", "turn_on",
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
_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_WhenSunStateIsUnavailable_ShouldNotTurnOnLight()
public void TurnOnLight_ShouldReadEachStateAtMostOnce()
{
_haContextMock.Setup(x => x.GetState(SunEntityId))
.Returns(new EntityState { State = "unavailable" });
SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon);
SetupState(LightEntityId, LightMonitorConsts.LightOnState);
var service = CreateService();
service.TurnOnLight();
CreateService().TurnOnLight();
_haContextMock.Verify(x => x.CallService("light", "turn_on",
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Never);
}
[Fact]
public void TurnOnLight_WhenLightStateIsNull_ShouldTurnOnLight()
{
_haContextMock.Setup(x => x.GetState(SunEntityId))
.Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon });
_haContextMock.Setup(x => x.GetState(LightEntityId))
.Returns((EntityState?)null);
var service = CreateService();
service.TurnOnLight();
_haContextMock.Verify(x => x.CallService("light", "turn_on",
It.IsAny<ServiceTarget>(), It.IsAny<object?>()), Times.Once);
_haContextMock.Verify(x => x.GetState(SunEntityId), Times.Once);
_haContextMock.Verify(x => x.GetState(LightEntityId), Times.Once);
VerifyTurnOn(LightEntityId, false);
}
}