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 _haContextMock = new(); private readonly Mock _lightControlServiceMock = new(); private readonly Mock _presenceServiceMock = new(); private readonly Subject _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>(), _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); } }