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 FluentAssertions;
using LightMonitor.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
@@ -13,10 +14,7 @@ public class PresenceServiceTests
{
private readonly Mock<IHaContext> _haContextMock = new();
private readonly LightMonitorOptions _defaultOptions = new()
{
PersonEntityIds = ["person.marius", "person.erika", "person.home_phone"]
};
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private PresenceService CreateService(LightMonitorOptions? options = null)
{
@@ -26,120 +24,106 @@ public class PresenceServiceTests
Options.Create(options ?? _defaultOptions));
}
[Fact]
public void IsAnyoneHome_WhenOnePersonIsHome_ShouldReturnTrue()
private void SetupStates(IReadOnlyList<string?> personStates)
{
_haContextMock.Setup(x => x.GetState("person.marius"))
.Returns(new EntityState { State = "home" });
_haContextMock.Setup(x => x.GetState("person.erika"))
.Returns(new EntityState { State = "not_home" });
_haContextMock.Setup(x => x.GetState("person.home_phone"))
.Returns(new EntityState { State = "not_home" });
var service = CreateService();
service.IsAnyoneHome().Should().BeTrue();
}
[Fact]
public void IsAnyoneHome_WhenAllPersonsAreHome_ShouldReturnTrue()
{
_haContextMock.Setup(x => x.GetState("person.marius"))
.Returns(new EntityState { State = "home" });
_haContextMock.Setup(x => x.GetState("person.erika"))
.Returns(new EntityState { State = "home" });
_haContextMock.Setup(x => x.GetState("person.home_phone"))
.Returns(new EntityState { State = "home" });
var service = CreateService();
service.IsAnyoneHome().Should().BeTrue();
}
[Fact]
public void IsAnyoneHome_WhenNoPersonIsHome_ShouldReturnFalse()
{
_haContextMock.Setup(x => x.GetState("person.marius"))
.Returns(new EntityState { State = "not_home" });
_haContextMock.Setup(x => x.GetState("person.erika"))
.Returns(new EntityState { State = "not_home" });
_haContextMock.Setup(x => x.GetState("person.home_phone"))
.Returns(new EntityState { State = "not_home" });
var service = CreateService();
service.IsAnyoneHome().Should().BeFalse();
}
[Fact]
public void IsAnyoneHome_WhenStateIsUnavailable_ShouldSkipAndReturnFalse()
{
_haContextMock.Setup(x => x.GetState("person.marius"))
.Returns(new EntityState { State = "unavailable" });
_haContextMock.Setup(x => x.GetState("person.erika"))
.Returns(new EntityState { State = "unknown" });
_haContextMock.Setup(x => x.GetState("person.home_phone"))
.Returns((EntityState?)null);
var service = CreateService();
service.IsAnyoneHome().Should().BeFalse();
}
[Fact]
public void IsAnyoneHome_WhenStateIsNull_ShouldSkipAndReturnFalse()
{
_haContextMock.Setup(x => x.GetState("person.marius"))
.Returns((EntityState?)null);
_haContextMock.Setup(x => x.GetState("person.erika"))
.Returns((EntityState?)null);
_haContextMock.Setup(x => x.GetState("person.home_phone"))
.Returns((EntityState?)null);
var service = CreateService();
service.IsAnyoneHome().Should().BeFalse();
}
[Fact]
public void IsAnyoneHome_WhenMixedStatesWithOneHome_ShouldReturnTrue()
{
_haContextMock.Setup(x => x.GetState("person.marius"))
.Returns(new EntityState { State = "unavailable" });
_haContextMock.Setup(x => x.GetState("person.erika"))
.Returns(new EntityState { State = "home" });
_haContextMock.Setup(x => x.GetState("person.home_phone"))
.Returns((EntityState?)null);
var service = CreateService();
service.IsAnyoneHome().Should().BeTrue();
for (var index = 0; index < personStates.Count; index++)
{
_haContextMock.Setup(x => x.GetState(LightMonitorTestData.PersonEntityIds[index]))
.Returns(LightMonitorTestData.CreateOptionalState(personStates[index]));
}
}
[Theory]
[InlineData("HOME")]
[InlineData("Home")]
[InlineData("home")]
public void IsAnyoneHome_CaseInsensitiveHomeState_ShouldReturnTrue(string homeState)
[MemberData(nameof(LightMonitorTestData.PresenceStateMatrix), MemberType = typeof(LightMonitorTestData))]
public void IsAnyoneHome_ForEveryPresenceCombination_ShouldReturnTrueOnlyWhenSomeoneIsHome(string?[] personStates)
{
_haContextMock.Setup(x => x.GetState("person.marius"))
.Returns(new EntityState { State = homeState });
_haContextMock.Setup(x => x.GetState("person.erika"))
.Returns(new EntityState { State = "not_home" });
_haContextMock.Setup(x => x.GetState("person.home_phone"))
.Returns(new EntityState { State = "not_home" });
SetupStates(personStates);
var service = CreateService();
var expected = personStates.Any(state =>
string.Equals(state, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase));
service.IsAnyoneHome().Should().BeTrue();
CreateService().IsAnyoneHome().Should().Be(expected);
}
[Theory]
[MemberData(nameof(LightMonitorTestData.PersonNewStateCases), MemberType = typeof(LightMonitorTestData))]
public void IsAnyoneHome_ShouldOnlyTreatHomeStateAsPresent(string? state, bool expected)
{
_haContextMock.Setup(x => x.GetState(LightMonitorTestData.PrimaryPersonEntityId))
.Returns(LightMonitorTestData.CreateOptionalState(state));
var options = LightMonitorTestData.CreateOptions(
personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]);
CreateService(options).IsAnyoneHome().Should().Be(expected);
}
[Theory]
[MemberData(nameof(LightMonitorTestData.NoPersonEntityIdSets), MemberType = typeof(LightMonitorTestData))]
public void IsAnyoneHome_WithNoUsablePersonEntityIds_ShouldReturnFalse(string[] personEntityIds)
{
var options = LightMonitorTestData.CreateOptions(personEntityIds: personEntityIds);
CreateService(options).IsAnyoneHome().Should().BeFalse();
}
[Fact]
public void IsAnyoneHome_WithEmptyPersonList_ShouldReturnFalse()
public void IsAnyoneHome_WithEmptyPersonList_ShouldNotQueryAnyEntity()
{
var options = new LightMonitorOptions { PersonEntityIds = [] };
var service = CreateService(options);
var options = LightMonitorTestData.CreateOptions(personEntityIds: []);
CreateService(options).IsAnyoneHome().Should().BeFalse();
_haContextMock.Verify(x => x.GetState(It.IsAny<string>()), Times.Never);
}
[Fact]
public void IsAnyoneHome_ShouldStopCheckingAfterTheFirstHomePerson()
{
_haContextMock.Setup(x => x.GetState(LightMonitorTestData.PrimaryPersonEntityId))
.Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState));
CreateService().IsAnyoneHome().Should().BeTrue();
_haContextMock.Verify(x => x.GetState(LightMonitorTestData.SecondaryPersonEntityId), Times.Never);
_haContextMock.Verify(x => x.GetState(LightMonitorTestData.PhonePersonEntityId), Times.Never);
}
[Fact]
public void IsAnyoneHome_ShouldCheckEveryConfiguredPersonWhenNobodyIsHome()
{
CreateService().IsAnyoneHome().Should().BeFalse();
foreach (var personEntityId in LightMonitorTestData.PersonEntityIds)
{
_haContextMock.Verify(x => x.GetState(personEntityId), Times.Once);
}
}
[Fact]
public void IsAnyoneHome_ShouldIgnorePersonsThatAreNotConfigured()
{
_haContextMock.Setup(x => x.GetState(LightMonitorTestData.SecondaryPersonEntityId))
.Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState));
var options = LightMonitorTestData.CreateOptions(
personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]);
CreateService(options).IsAnyoneHome().Should().BeFalse();
_haContextMock.Verify(x => x.GetState(LightMonitorTestData.SecondaryPersonEntityId), Times.Never);
}
[Fact]
public void IsAnyoneHome_WithDefaultOptions_ShouldQueryAllDefaultPersonEntities()
{
var service = CreateService(new LightMonitorOptions());
service.IsAnyoneHome().Should().BeFalse();
foreach (var personEntityId in LightMonitorConsts.DefaultPersonEntityIds)
{
_haContextMock.Verify(x => x.GetState(personEntityId), Times.Once);
}
}
}