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
@@ -1,4 +1,5 @@
using System.Reactive.Subjects;
using FluentAssertions;
using LightMonitor.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@@ -13,25 +14,28 @@ namespace LightMonitor.Manager.Tests;
public class LightMonitorInitTests
{
private const string SunEntityId = "sun.sun";
private const string PersonEntityId = "person.marius";
private const string SunEntityId = LightMonitorTestData.SunEntityId;
private const string PersonEntityId = LightMonitorTestData.PrimaryPersonEntityId;
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<IPresenceService> _presenceServiceMock = new();
private readonly Mock<ILightControlService> _lightControlServiceMock = new();
private readonly Subject<StateChange> _stateChangesSubject = new();
private readonly LightMonitorOptions _defaultOptions = new()
{
Enabled = true,
SunEntityId = SunEntityId,
PersonEntityIds = [PersonEntityId]
};
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private LightMonitorInit CreateSut(LightMonitorOptions? options = null)
private LightMonitorInit CreateSut(LightMonitorOptions? options = null, Exception? subscriptionException = null)
{
_haContextMock.Setup(x => x.StateAllChanges())
.Returns(_stateChangesSubject);
if (subscriptionException is null)
{
_haContextMock.Setup(x => x.StateAllChanges())
.Returns(_stateChangesSubject);
}
else
{
_haContextMock.Setup(x => x.StateAllChanges())
.Throws(subscriptionException);
}
return new LightMonitorInit(
_haContextMock.Object,
@@ -44,124 +48,219 @@ public class LightMonitorInitTests
private void EmitStateChange(string entityId, string? oldState, string? newState)
{
var entity = new Entity(_haContextMock.Object, entityId);
var old = oldState is not null ? new EntityState { State = oldState } : null;
var @new = newState is not null ? new EntityState { State = newState } : null;
_stateChangesSubject.OnNext(new StateChange(entity, old, @new));
_stateChangesSubject.OnNext(new StateChange(
entity,
LightMonitorTestData.CreateOptionalState(oldState),
LightMonitorTestData.CreateOptionalState(newState)));
}
private void VerifyTurnOn(bool shouldTurnOn)
{
if (shouldTurnOn)
{
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Once);
}
else
{
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
}
[Theory]
[MemberData(nameof(LightMonitorTestData.PersonNewStateCases), MemberType = typeof(LightMonitorTestData))]
public void PersonStateChange_ShouldTurnOnLightOnlyWhenNewStateIsHome(string? newState, bool shouldTurnOn)
{
CreateSut();
EmitStateChange(PersonEntityId, "not_home", newState);
VerifyTurnOn(shouldTurnOn);
}
[Theory]
[MemberData(nameof(LightMonitorTestData.PersonArrivalTransitions), MemberType = typeof(LightMonitorTestData))]
public void PersonStateChange_ToHome_ShouldIgnoreOnlyRecoveryTransitions(string? oldState, bool shouldTurnOn)
{
CreateSut();
EmitStateChange(PersonEntityId, oldState, LightMonitorConsts.PersonHomeState);
VerifyTurnOn(shouldTurnOn);
}
[Theory]
[InlineData(LightMonitorTestData.PrimaryPersonEntityId)]
[InlineData(LightMonitorTestData.SecondaryPersonEntityId)]
[InlineData(LightMonitorTestData.PhonePersonEntityId)]
public void PersonStateChange_ShouldTurnOnLightForEveryConfiguredPerson(string personEntityId)
{
CreateSut();
EmitStateChange(personEntityId, "not_home", LightMonitorConsts.PersonHomeState);
VerifyTurnOn(true);
}
[Fact]
public void PersonStateChange_FromNotHomeToHome_ShouldCallTurnOnLight()
public void PersonStateChange_ForUnconfiguredPerson_ShouldNotTurnOnLight()
{
CreateSut(LightMonitorTestData.CreateOptions(personEntityIds: [PersonEntityId]));
EmitStateChange(
LightMonitorTestData.SecondaryPersonEntityId,
"not_home",
LightMonitorConsts.PersonHomeState);
VerifyTurnOn(false);
}
[Fact]
public void PersonStateChange_ShouldNotConsultThePresenceService()
{
CreateSut();
EmitStateChange(PersonEntityId, "not_home", LightMonitorConsts.PersonHomeState);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Once);
VerifyTurnOn(true);
_presenceServiceMock.Verify(x => x.IsAnyoneHome(), Times.Never);
}
[Fact]
public void PersonStateChange_FromUnavailableToHome_ShouldNotCallTurnOnLight()
{
CreateSut();
EmitStateChange(PersonEntityId, LightMonitorConsts.UnavailableState, LightMonitorConsts.PersonHomeState);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
[Fact]
public void PersonStateChange_FromUnknownToHome_ShouldNotCallTurnOnLight()
{
CreateSut();
EmitStateChange(PersonEntityId, LightMonitorConsts.UnknownState, LightMonitorConsts.PersonHomeState);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
[Fact]
public void PersonStateChange_FromNullToHome_ShouldNotCallTurnOnLight()
{
CreateSut();
EmitStateChange(PersonEntityId, null, LightMonitorConsts.PersonHomeState);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
[Fact]
public void PersonStateChange_FromNotHomeToNotHome_ShouldNotCallTurnOnLight()
{
CreateSut();
EmitStateChange(PersonEntityId, "not_home", "not_home");
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
[Fact]
public void SunStateChange_FromAboveHorizonToBelowHorizon_WhenSomeoneIsHome_ShouldCallTurnOnLight()
[Theory]
[MemberData(nameof(LightMonitorTestData.SunStateTurnOnCases), MemberType = typeof(LightMonitorTestData))]
public void SunStateChange_ShouldTurnOnLightOnlyWhenNewStateIsBelowHorizon(string? newState, bool shouldTurnOn)
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
CreateSut();
EmitStateChange(SunEntityId, LightMonitorConsts.SunAboveHorizon, newState);
VerifyTurnOn(shouldTurnOn);
}
[Theory]
[MemberData(nameof(LightMonitorTestData.SunBelowHorizonTransitions), MemberType = typeof(LightMonitorTestData))]
public void SunStateChange_ToBelowHorizon_ShouldIgnoreOnlyRecoveryTransitions(
string? oldState,
bool shouldTurnOn)
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
CreateSut();
EmitStateChange(SunEntityId, oldState, LightMonitorConsts.SunBelowHorizon);
VerifyTurnOn(shouldTurnOn);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void SunStateChange_ToBelowHorizon_ShouldTurnOnLightOnlyWhenSomeoneIsHome(bool anyoneHome)
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(anyoneHome);
CreateSut();
EmitStateChange(SunEntityId, LightMonitorConsts.SunAboveHorizon, LightMonitorConsts.SunBelowHorizon);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Once);
VerifyTurnOn(anyoneHome);
_presenceServiceMock.Verify(x => x.IsAnyoneHome(), Times.Once);
}
[Fact]
public void SunStateChange_FromAboveHorizonToBelowHorizon_WhenNoOneIsHome_ShouldNotCallTurnOnLight()
public void SunStateChange_WithNoConfiguredPersons_ShouldStillReactToTheSun()
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(false);
CreateSut();
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
CreateSut(LightMonitorTestData.CreateOptions(personEntityIds: []));
EmitStateChange(SunEntityId, LightMonitorConsts.SunAboveHorizon, LightMonitorConsts.SunBelowHorizon);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
VerifyTurnOn(true);
}
[Fact]
public void SunStateChange_FromUnavailableToBelowHorizon_ShouldNotCallTurnOnLight()
public void SunStateChange_WhenPresenceCheckThrows_ShouldKeepListening()
{
_presenceServiceMock.SetupSequence(x => x.IsAnyoneHome())
.Throws(new InvalidOperationException("presence failed"))
.Returns(true);
CreateSut();
var act = () => EmitStateChange(
SunEntityId,
LightMonitorConsts.SunAboveHorizon,
LightMonitorConsts.SunBelowHorizon);
act.Should().NotThrow();
VerifyTurnOn(false);
EmitStateChange(SunEntityId, LightMonitorConsts.SunAboveHorizon, LightMonitorConsts.SunBelowHorizon);
VerifyTurnOn(true);
}
[Fact]
public void PersonStateChange_WhenTurnOnLightThrows_ShouldKeepListening()
{
_lightControlServiceMock.SetupSequence(x => x.TurnOnLight())
.Throws(new InvalidOperationException("light failed"))
.Pass();
CreateSut();
var act = () => EmitStateChange(PersonEntityId, "not_home", LightMonitorConsts.PersonHomeState);
act.Should().NotThrow();
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Once);
EmitStateChange(PersonEntityId, "not_home", LightMonitorConsts.PersonHomeState);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Exactly(2));
}
[Fact]
public void Constructor_WhenStateSubscriptionThrows_ShouldNotThrow()
{
var act = () => CreateSut(subscriptionException: new InvalidOperationException("state stream failed"));
act.Should().NotThrow();
VerifyTurnOn(false);
}
[Fact]
public async Task InitializeAsync_ShouldCompleteWithoutThrowing()
{
var sut = CreateSut();
var act = () => sut.InitializeAsync();
await act.Should().NotThrowAsync();
}
[Fact]
public void StateChange_ForUnrelatedEntity_ShouldNotTurnOnLight()
{
CreateSut();
EmitStateChange("sensor.sun_next_dawn", LightMonitorConsts.UnavailableState, LightMonitorConsts.SunBelowHorizon);
VerifyTurnOn(false);
}
[Fact]
public void SunStateChange_WhenStateDoesNotActuallyChange_ShouldNotTurnOnLight()
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
CreateSut();
EmitStateChange(SunEntityId, LightMonitorConsts.UnavailableState, LightMonitorConsts.SunBelowHorizon);
EmitStateChange(SunEntityId, LightMonitorConsts.SunBelowHorizon, LightMonitorConsts.SunBelowHorizon);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
VerifyTurnOn(false);
}
[Fact]
public void SunStateChange_FromUnknownToBelowHorizon_ShouldNotCallTurnOnLight()
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
CreateSut();
EmitStateChange(SunEntityId, LightMonitorConsts.UnknownState, LightMonitorConsts.SunBelowHorizon);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
[Fact]
public void SunStateChange_FromNullToBelowHorizon_ShouldNotCallTurnOnLight()
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
CreateSut();
EmitStateChange(SunEntityId, null, LightMonitorConsts.SunBelowHorizon);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
}
[Fact]
public void SunStateChange_FromBelowHorizonToAboveHorizon_ShouldNotCallTurnOnLight()
public void PersonStateChange_WhenStateDoesNotActuallyChange_ShouldNotTurnOnLight()
{
CreateSut();
EmitStateChange(SunEntityId, LightMonitorConsts.SunBelowHorizon, LightMonitorConsts.SunAboveHorizon);
EmitStateChange(PersonEntityId, LightMonitorConsts.PersonHomeState, LightMonitorConsts.PersonHomeState);
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
VerifyTurnOn(false);
}
}