Files
HomeAssistant/LightMonitor.Manager.Tests/PresenceServiceTests.cs
T
gitea_admin 17774b40b8 Convert test data sources to typed TheoryData
Replace untyped MemberData sources with TheoryData<...>, use collection
expressions for array and list literals, drop redundant casts and null
suppressions, and give log-assertion helpers an IsEnabled setup so guarded
log calls stay verifiable.
2026-11-29 21:42:41 +02:00

131 lines
4.7 KiB
C#

using FluentAssertions;
using LightMonitor.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using LightMonitorManager;
namespace LightMonitor.Manager.Tests;
public class PresenceServiceTests
{
private readonly Mock<IHaContext> _haContextMock = new();
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private PresenceService CreateService(LightMonitorOptions? options = null)
{
return new PresenceService(
_haContextMock.Object,
Mock.Of<ILogger<PresenceService>>(),
Options.Create(options ?? _defaultOptions));
}
private void SetupStates(string?[] personStates)
{
for (var index = 0; index < personStates.Length; index++)
{
var entityId = LightMonitorTestData.PersonEntityIds[index];
var state = LightMonitorTestData.CreateOptionalState(personStates[index]);
_haContextMock.Setup(x => x.GetState(entityId))
.Returns(state);
}
}
[Theory]
[MemberData(nameof(LightMonitorTestData.PresenceStateMatrix), MemberType = typeof(LightMonitorTestData))]
public void IsAnyoneHome_ForEveryPresenceCombination_ShouldReturnTrueOnlyWhenSomeoneIsHome(string?[] personStates)
{
SetupStates(personStates);
var expected = personStates.Any(state =>
string.Equals(state, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase));
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_ShouldNotQueryAnyEntity()
{
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);
}
}
}