Files
HomeAssistant/LightMonitor.Manager.Tests/PresenceServiceTests.cs
T
gitea_admin f919d6b25b Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
2026-12-01 12:33:31 +02:00

133 lines
5.0 KiB
C#

#region
using FluentAssertions;
using LightMonitor.Models;
using LightMonitorManager;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
#endregion
namespace LightMonitor.Manager.Tests;
public class PresenceServiceTests
{
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
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);
}
}
}