Files
HomeAssistant/LightMonitor.Manager.Tests/LightMonitorInitTests.cs
T
gitea_admin 24729ee267 Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files
- Collapse single-statement guard blocks and tidy using and member order
- Normalize XML doc indentation and file endings
- Reorder BatteryMonitor test-data helpers and move the exclusion tests into
  their own region
- Carry the same formatting through the boiler config, control, entity and
  test files
2026-11-30 11:52:06 +02:00

258 lines
8.5 KiB
C#

using System.Reactive.Subjects;
using FluentAssertions;
using LightMonitor.Models;
using LightMonitorManager;
using LightMonitorManager.Interface;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
namespace LightMonitor.Manager.Tests;
public class LightMonitorInitTests
{
private const string SunEntityId = LightMonitorTestData.SunEntityId;
private const string PersonEntityId = LightMonitorTestData.PrimaryPersonEntityId;
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new();
private readonly Mock<ILightControlService> _lightControlServiceMock = new();
private readonly Mock<IPresenceService> _presenceServiceMock = new();
private readonly Subject<StateChange> _stateChangesSubject = new();
private LightMonitorInit CreateSut(LightMonitorOptions? options = null, Exception? subscriptionException = null)
{
if (subscriptionException is null)
_haContextMock.Setup(x => x.StateAllChanges())
.Returns(_stateChangesSubject);
else
_haContextMock.Setup(x => x.StateAllChanges())
.Throws(subscriptionException);
return new LightMonitorInit(
_haContextMock.Object,
Mock.Of<ILogger<LightMonitorInit>>(),
_presenceServiceMock.Object,
_lightControlServiceMock.Object,
Options.Create(options ?? _defaultOptions));
}
private void EmitStateChange(string entityId, string? oldState, string? newState)
{
var entity = new Entity(_haContextMock.Object, entityId);
_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_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);
VerifyTurnOn(true);
_presenceServiceMock.Verify(x => x.IsAnyoneHome(), Times.Never);
}
[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);
VerifyTurnOn(anyoneHome);
_presenceServiceMock.Verify(x => x.IsAnyoneHome(), Times.Once);
}
[Fact]
public void SunStateChange_WithNoConfiguredPersons_ShouldStillReactToTheSun()
{
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
CreateSut(LightMonitorTestData.CreateOptions(personEntityIds: []));
EmitStateChange(SunEntityId, LightMonitorConsts.SunAboveHorizon, LightMonitorConsts.SunBelowHorizon);
VerifyTurnOn(true);
}
[Fact]
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.SunBelowHorizon, LightMonitorConsts.SunBelowHorizon);
VerifyTurnOn(false);
}
[Fact]
public void PersonStateChange_WhenStateDoesNotActuallyChange_ShouldNotTurnOnLight()
{
CreateSut();
EmitStateChange(PersonEntityId, LightMonitorConsts.PersonHomeState, LightMonitorConsts.PersonHomeState);
VerifyTurnOn(false);
}
}